tesseract  4.00.00dev
imagedata.cpp
Go to the documentation of this file.
1 // File: imagedata.h
3 // Description: Class to hold information about a single multi-page tiff
4 // training file and its corresponding boxes or text file.
5 // Author: Ray Smith
6 // Created: Tue May 28 08:56:06 PST 2013
7 //
8 // (C) Copyright 2013, Google Inc.
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 
20 // Include automatically generated configuration file if running autoconf.
21 #ifdef HAVE_CONFIG_H
22 #include "config_auto.h"
23 #endif
24 
25 #include "imagedata.h"
26 
27 #if defined(__MINGW32__)
28 #include <unistd.h>
29 #else
30 #include <thread>
31 #endif
32 
33 #include "allheaders.h"
34 #include "boxread.h"
35 #include "callcpp.h"
36 #include "helpers.h"
37 #include "tprintf.h"
38 
39 // Number of documents to read ahead while training. Doesn't need to be very
40 // large.
41 const int kMaxReadAhead = 8;
42 
43 namespace tesseract {
44 
45 WordFeature::WordFeature() : x_(0), y_(0), dir_(0) {
46 }
47 
49  : x_(IntCastRounded(fcoord.x())),
50  y_(ClipToRange(IntCastRounded(fcoord.y()), 0, MAX_UINT8)),
51  dir_(dir) {
52 }
53 
54 // Computes the maximum x and y value in the features.
56  int* max_x, int* max_y) {
57  *max_x = 0;
58  *max_y = 0;
59  for (int f = 0; f < features.size(); ++f) {
60  if (features[f].x_ > *max_x) *max_x = features[f].x_;
61  if (features[f].y_ > *max_y) *max_y = features[f].y_;
62  }
63 }
64 
65 // Draws the features in the given window.
67  ScrollView* window) {
68 #ifndef GRAPHICS_DISABLED
69  for (int f = 0; f < features.size(); ++f) {
70  FCOORD pos(features[f].x_, features[f].y_);
71  FCOORD dir;
72  dir.from_direction(features[f].dir_);
73  dir *= 8.0f;
74  window->SetCursor(IntCastRounded(pos.x() - dir.x()),
75  IntCastRounded(pos.y() - dir.y()));
76  window->DrawTo(IntCastRounded(pos.x() + dir.x()),
77  IntCastRounded(pos.y() + dir.y()));
78  }
79 #endif
80 }
81 
82 // Writes to the given file. Returns false in case of error.
83 bool WordFeature::Serialize(FILE* fp) const {
84  if (fwrite(&x_, sizeof(x_), 1, fp) != 1) return false;
85  if (fwrite(&y_, sizeof(y_), 1, fp) != 1) return false;
86  if (fwrite(&dir_, sizeof(dir_), 1, fp) != 1) return false;
87  return true;
88 }
89 // Reads from the given file. Returns false in case of error.
90 // If swap is true, assumes a big/little-endian swap is needed.
91 bool WordFeature::DeSerialize(bool swap, FILE* fp) {
92  if (fread(&x_, sizeof(x_), 1, fp) != 1) return false;
93  if (swap) ReverseN(&x_, sizeof(x_));
94  if (fread(&y_, sizeof(y_), 1, fp) != 1) return false;
95  if (fread(&dir_, sizeof(dir_), 1, fp) != 1) return false;
96  return true;
97 }
98 
100  const GenericVector<WordFeature>& word_features,
101  GenericVector<FloatWordFeature>* float_features) {
102  for (int i = 0; i < word_features.size(); ++i) {
104  f.x = word_features[i].x();
105  f.y = word_features[i].y();
106  f.dir = word_features[i].dir();
107  f.x_bucket = 0; // Will set it later.
108  float_features->push_back(f);
109  }
110 }
111 
112 // Sort function to sort first by x-bucket, then by y.
113 /* static */
114 int FloatWordFeature::SortByXBucket(const void* v1, const void* v2) {
115  const FloatWordFeature* f1 = static_cast<const FloatWordFeature*>(v1);
116  const FloatWordFeature* f2 = static_cast<const FloatWordFeature*>(v2);
117  int x_diff = f1->x_bucket - f2->x_bucket;
118  if (x_diff == 0) return f1->y - f2->y;
119  return x_diff;
120 }
121 
122 ImageData::ImageData() : page_number_(-1), vertical_text_(false) {
123 }
124 // Takes ownership of the pix and destroys it.
125 ImageData::ImageData(bool vertical, Pix* pix)
126  : page_number_(0), vertical_text_(vertical) {
127  SetPix(pix);
128 }
130 }
131 
132 // Builds and returns an ImageData from the basic data. Note that imagedata,
133 // truth_text, and box_text are all the actual file data, NOT filenames.
134 ImageData* ImageData::Build(const char* name, int page_number, const char* lang,
135  const char* imagedata, int imagedatasize,
136  const char* truth_text, const char* box_text) {
137  ImageData* image_data = new ImageData();
138  image_data->imagefilename_ = name;
139  image_data->page_number_ = page_number;
140  image_data->language_ = lang;
141  // Save the imagedata.
142  image_data->image_data_.resize_no_init(imagedatasize);
143  memcpy(&image_data->image_data_[0], imagedata, imagedatasize);
144  if (!image_data->AddBoxes(box_text)) {
145  if (truth_text == NULL || truth_text[0] == '\0') {
146  tprintf("Error: No text corresponding to page %d from image %s!\n",
147  page_number, name);
148  delete image_data;
149  return NULL;
150  }
151  image_data->transcription_ = truth_text;
152  // If we have no boxes, the transcription is in the 0th box_texts_.
153  image_data->box_texts_.push_back(truth_text);
154  // We will create a box for the whole image on PreScale, to save unpacking
155  // the image now.
156  } else if (truth_text != NULL && truth_text[0] != '\0' &&
157  image_data->transcription_ != truth_text) {
158  // Save the truth text as it is present and disagrees with the box text.
159  image_data->transcription_ = truth_text;
160  }
161  return image_data;
162 }
163 
164 // Writes to the given file. Returns false in case of error.
165 bool ImageData::Serialize(TFile* fp) const {
166  if (!imagefilename_.Serialize(fp)) return false;
167  if (fp->FWrite(&page_number_, sizeof(page_number_), 1) != 1) return false;
168  if (!image_data_.Serialize(fp)) return false;
169  if (!language_.Serialize(fp)) return false;
170  if (!transcription_.Serialize(fp)) return false;
171  // WARNING: Will not work across different endian machines.
172  if (!boxes_.Serialize(fp)) return false;
173  if (!box_texts_.SerializeClasses(fp)) return false;
174  inT8 vertical = vertical_text_;
175  if (fp->FWrite(&vertical, sizeof(vertical), 1) != 1) return false;
176  return true;
177 }
178 
179 // Reads from the given file. Returns false in case of error.
180 // If swap is true, assumes a big/little-endian swap is needed.
182  if (!imagefilename_.DeSerialize(fp)) return false;
183  if (fp->FReadEndian(&page_number_, sizeof(page_number_), 1) != 1)
184  return false;
185  if (!image_data_.DeSerialize(fp)) return false;
186  if (!language_.DeSerialize(fp)) return false;
187  if (!transcription_.DeSerialize(fp)) return false;
188  // WARNING: Will not work across different endian machines.
189  if (!boxes_.DeSerialize(fp)) return false;
190  if (!box_texts_.DeSerializeClasses(fp)) return false;
191  inT8 vertical = 0;
192  if (fp->FRead(&vertical, sizeof(vertical), 1) != 1) return false;
193  vertical_text_ = vertical != 0;
194  return true;
195 }
196 
197 // As DeSerialize, but only seeks past the data - hence a static method.
199  if (!STRING::SkipDeSerialize(fp)) return false;
201  if (fp->FRead(&page_number, sizeof(page_number), 1) != 1) return false;
202  if (!GenericVector<char>::SkipDeSerialize(fp)) return false;
203  if (!STRING::SkipDeSerialize(fp)) return false;
204  if (!STRING::SkipDeSerialize(fp)) return false;
205  if (!GenericVector<TBOX>::SkipDeSerialize(fp)) return false;
206  if (!GenericVector<STRING>::SkipDeSerializeClasses(fp)) return false;
207  inT8 vertical = 0;
208  return fp->FRead(&vertical, sizeof(vertical), 1) == 1;
209 }
210 
211 // Saves the given Pix as a PNG-encoded string and destroys it.
212 void ImageData::SetPix(Pix* pix) {
213  SetPixInternal(pix, &image_data_);
214 }
215 
216 // Returns the Pix image for *this. Must be pixDestroyed after use.
217 Pix* ImageData::GetPix() const {
218  return GetPixInternal(image_data_);
219 }
220 
221 // Gets anything and everything with a non-NULL pointer, prescaled to a
222 // given target_height (if 0, then the original image height), and aligned.
223 // Also returns (if not NULL) the width and height of the scaled image.
224 // The return value is the scaled Pix, which must be pixDestroyed after use,
225 // and scale_factor (if not NULL) is set to the scale factor that was applied
226 // to the image to achieve the target_height.
227 Pix* ImageData::PreScale(int target_height, int max_height, float* scale_factor,
228  int* scaled_width, int* scaled_height,
229  GenericVector<TBOX>* boxes) const {
230  int input_width = 0;
231  int input_height = 0;
232  Pix* src_pix = GetPix();
233  ASSERT_HOST(src_pix != NULL);
234  input_width = pixGetWidth(src_pix);
235  input_height = pixGetHeight(src_pix);
236  if (target_height == 0) {
237  target_height = MIN(input_height, max_height);
238  }
239  float im_factor = static_cast<float>(target_height) / input_height;
240  if (scaled_width != NULL)
241  *scaled_width = IntCastRounded(im_factor * input_width);
242  if (scaled_height != NULL)
243  *scaled_height = target_height;
244  // Get the scaled image.
245  Pix* pix = pixScale(src_pix, im_factor, im_factor);
246  if (pix == NULL) {
247  tprintf("Scaling pix of size %d, %d by factor %g made null pix!!\n",
248  input_width, input_height, im_factor);
249  }
250  if (scaled_width != NULL) *scaled_width = pixGetWidth(pix);
251  if (scaled_height != NULL) *scaled_height = pixGetHeight(pix);
252  pixDestroy(&src_pix);
253  if (boxes != NULL) {
254  // Get the boxes.
255  boxes->truncate(0);
256  for (int b = 0; b < boxes_.size(); ++b) {
257  TBOX box = boxes_[b];
258  box.scale(im_factor);
259  boxes->push_back(box);
260  }
261  if (boxes->empty()) {
262  // Make a single box for the whole image.
263  TBOX box(0, 0, im_factor * input_width, target_height);
264  boxes->push_back(box);
265  }
266  }
267  if (scale_factor != NULL) *scale_factor = im_factor;
268  return pix;
269 }
270 
272  return image_data_.size();
273 }
274 
275 // Draws the data in a new window.
276 void ImageData::Display() const {
277 #ifndef GRAPHICS_DISABLED
278  const int kTextSize = 64;
279  // Draw the image.
280  Pix* pix = GetPix();
281  if (pix == NULL) return;
282  int width = pixGetWidth(pix);
283  int height = pixGetHeight(pix);
284  ScrollView* win = new ScrollView("Imagedata", 100, 100,
285  2 * (width + 2 * kTextSize),
286  2 * (height + 4 * kTextSize),
287  width + 10, height + 3 * kTextSize, true);
288  win->Image(pix, 0, height - 1);
289  pixDestroy(&pix);
290  // Draw the boxes.
291  win->Pen(ScrollView::RED);
292  win->Brush(ScrollView::NONE);
293  int text_size = kTextSize;
294  if (!boxes_.empty() && boxes_[0].height() * 2 < text_size)
295  text_size = boxes_[0].height() * 2;
296  win->TextAttributes("Arial", text_size, false, false, false);
297  if (!boxes_.empty()) {
298  for (int b = 0; b < boxes_.size(); ++b) {
299  boxes_[b].plot(win);
300  win->Text(boxes_[b].left(), height + kTextSize, box_texts_[b].string());
301  }
302  } else {
303  // The full transcription.
304  win->Pen(ScrollView::CYAN);
305  win->Text(0, height + kTextSize * 2, transcription_.string());
306  }
307  win->Update();
308  window_wait(win);
309 #endif
310 }
311 
312 // Adds the supplied boxes and transcriptions that correspond to the correct
313 // page number.
315  const GenericVector<STRING>& texts,
316  const GenericVector<int>& box_pages) {
317  // Copy the boxes and make the transcription.
318  for (int i = 0; i < box_pages.size(); ++i) {
319  if (page_number_ >= 0 && box_pages[i] != page_number_) continue;
320  transcription_ += texts[i];
321  boxes_.push_back(boxes[i]);
322  box_texts_.push_back(texts[i]);
323  }
324 }
325 
326 // Saves the given Pix as a PNG-encoded string and destroys it.
327 void ImageData::SetPixInternal(Pix* pix, GenericVector<char>* image_data) {
328  l_uint8* data;
329  size_t size;
330  pixWriteMem(&data, &size, pix, IFF_PNG);
331  pixDestroy(&pix);
332  image_data->resize_no_init(size);
333  memcpy(&(*image_data)[0], data, size);
334  free(data);
335 }
336 
337 // Returns the Pix image for the image_data. Must be pixDestroyed after use.
338 Pix* ImageData::GetPixInternal(const GenericVector<char>& image_data) {
339  Pix* pix = NULL;
340  if (!image_data.empty()) {
341  // Convert the array to an image.
342  const unsigned char* u_data =
343  reinterpret_cast<const unsigned char*>(&image_data[0]);
344  pix = pixReadMem(u_data, image_data.size());
345  }
346  return pix;
347 }
348 
349 // Parses the text string as a box file and adds any discovered boxes that
350 // match the page number. Returns false on error.
351 bool ImageData::AddBoxes(const char* box_text) {
352  if (box_text != NULL && box_text[0] != '\0') {
354  GenericVector<STRING> texts;
355  GenericVector<int> box_pages;
356  if (ReadMemBoxes(page_number_, false, box_text, &boxes,
357  &texts, NULL, &box_pages)) {
358  AddBoxes(boxes, texts, box_pages);
359  return true;
360  } else {
361  tprintf("Error: No boxes for page %d from image %s!\n",
362  page_number_, imagefilename_.string());
363  }
364  }
365  return false;
366 }
367 
368 // Thread function to call ReCachePages.
369 void* ReCachePagesFunc(void* data) {
370  DocumentData* document_data = static_cast<DocumentData*>(data);
371  document_data->ReCachePages();
372  return NULL;
373 }
374 
376  : document_name_(name),
377  pages_offset_(-1),
378  total_pages_(-1),
379  memory_used_(0),
380  max_memory_(0),
381  reader_(NULL) {}
382 
384  SVAutoLock lock_p(&pages_mutex_);
385  SVAutoLock lock_g(&general_mutex_);
386 }
387 
388 // Reads all the pages in the given lstmf filename to the cache. The reader
389 // is used to read the file.
390 bool DocumentData::LoadDocument(const char* filename, int start_page,
391  inT64 max_memory, FileReader reader) {
392  SetDocument(filename, max_memory, reader);
393  pages_offset_ = start_page;
394  return ReCachePages();
395 }
396 
397 // Sets up the document, without actually loading it.
398 void DocumentData::SetDocument(const char* filename, inT64 max_memory,
399  FileReader reader) {
400  SVAutoLock lock_p(&pages_mutex_);
401  SVAutoLock lock(&general_mutex_);
402  document_name_ = filename;
403  pages_offset_ = -1;
404  max_memory_ = max_memory;
405  reader_ = reader;
406 }
407 
408 // Writes all the pages to the given filename. Returns false on error.
409 bool DocumentData::SaveDocument(const char* filename, FileWriter writer) {
410  SVAutoLock lock(&pages_mutex_);
411  TFile fp;
412  fp.OpenWrite(NULL);
413  if (!pages_.Serialize(&fp) || !fp.CloseWrite(filename, writer)) {
414  tprintf("Serialize failed: %s\n", filename);
415  return false;
416  }
417  return true;
418 }
420  SVAutoLock lock(&pages_mutex_);
421  TFile fp;
422  fp.OpenWrite(buffer);
423  return pages_.Serialize(&fp);
424 }
425 
426 // Adds the given page data to this document, counting up memory.
428  SVAutoLock lock(&pages_mutex_);
429  pages_.push_back(page);
430  set_memory_used(memory_used() + page->MemoryUsed());
431 }
432 
433 // If the given index is not currently loaded, loads it using a separate
434 // thread.
436  ImageData* page = NULL;
437  if (IsPageAvailable(index, &page)) return;
438  SVAutoLock lock(&pages_mutex_);
439  if (pages_offset_ == index) return;
440  pages_offset_ = index;
441  pages_.clear();
443 }
444 
445 // Returns a pointer to the page with the given index, modulo the total
446 // number of pages. Blocks until the background load is completed.
447 const ImageData* DocumentData::GetPage(int index) {
448  ImageData* page = NULL;
449  while (!IsPageAvailable(index, &page)) {
450  // If there is no background load scheduled, schedule one now.
451  pages_mutex_.Lock();
452  bool needs_loading = pages_offset_ != index;
453  pages_mutex_.Unlock();
454  if (needs_loading) LoadPageInBackground(index);
455  // We can't directly load the page, or the background load will delete it
456  // while the caller is using it, so give it a chance to work.
457 #if defined(__MINGW32__)
458  sleep(1);
459 #else
460  std::this_thread::sleep_for(std::chrono::seconds(1));
461 #endif
462  }
463  return page;
464 }
465 
466 // Returns true if the requested page is available, and provides a pointer,
467 // which may be NULL if the document is empty. May block, even though it
468 // doesn't guarantee to return true.
469 bool DocumentData::IsPageAvailable(int index, ImageData** page) {
470  SVAutoLock lock(&pages_mutex_);
471  int num_pages = NumPages();
472  if (num_pages == 0 || index < 0) {
473  *page = NULL; // Empty Document.
474  return true;
475  }
476  if (num_pages > 0) {
477  index = Modulo(index, num_pages);
478  if (pages_offset_ <= index && index < pages_offset_ + pages_.size()) {
479  *page = pages_[index - pages_offset_]; // Page is available already.
480  return true;
481  }
482  }
483  return false;
484 }
485 
486 // Removes all pages from memory and frees the memory, but does not forget
487 // the document metadata.
489  SVAutoLock lock(&pages_mutex_);
490  inT64 memory_saved = memory_used();
491  pages_.clear();
492  pages_offset_ = -1;
493  set_total_pages(-1);
494  set_memory_used(0);
495  tprintf("Unloaded document %s, saving %" PRId64 " memory\n",
496  document_name_.string(), memory_saved);
497  return memory_saved;
498 }
499 
500 // Shuffles all the pages in the document.
502  TRand random;
503  // Different documents get shuffled differently, but the same for the same
504  // name.
505  random.set_seed(document_name_.string());
506  int num_pages = pages_.size();
507  // Execute one random swap for each page in the document.
508  for (int i = 0; i < num_pages; ++i) {
509  int src = random.IntRand() % num_pages;
510  int dest = random.IntRand() % num_pages;
511  std::swap(pages_[src], pages_[dest]);
512  }
513 }
514 
515 // Locks the pages_mutex_ and Loads as many pages can fit in max_memory_
516 // starting at index pages_offset_.
517 bool DocumentData::ReCachePages() {
518  SVAutoLock lock(&pages_mutex_);
519  // Read the file.
520  set_total_pages(0);
521  set_memory_used(0);
522  int loaded_pages = 0;
523  pages_.truncate(0);
524  TFile fp;
525  if (!fp.Open(document_name_, reader_) ||
526  !PointerVector<ImageData>::DeSerializeSize(&fp, &loaded_pages) ||
527  loaded_pages <= 0) {
528  tprintf("Deserialize header failed: %s\n", document_name_.string());
529  return false;
530  }
531  pages_offset_ %= loaded_pages;
532  // Skip pages before the first one we want, and load the rest until max
533  // memory and skip the rest after that.
534  int page;
535  for (page = 0; page < loaded_pages; ++page) {
536  if (page < pages_offset_ ||
537  (max_memory_ > 0 && memory_used() > max_memory_)) {
539  tprintf("Deserializeskip failed\n");
540  break;
541  }
542  } else {
543  if (!pages_.DeSerializeElement(&fp)) break;
544  ImageData* image_data = pages_.back();
545  if (image_data->imagefilename().length() == 0) {
546  image_data->set_imagefilename(document_name_);
547  image_data->set_page_number(page);
548  }
549  set_memory_used(memory_used() + image_data->MemoryUsed());
550  }
551  }
552  if (page < loaded_pages) {
553  tprintf("Deserialize failed: %s read %d/%d pages\n",
554  document_name_.string(), page, loaded_pages);
555  pages_.truncate(0);
556  } else {
557  tprintf("Loaded %d/%d pages (%d-%d) of document %s\n", pages_.size(),
558  loaded_pages, pages_offset_ + 1, pages_offset_ + pages_.size(),
559  document_name_.string());
560  }
561  set_total_pages(loaded_pages);
562  return !pages_.empty();
563 }
564 
565 // A collection of DocumentData that knows roughly how much memory it is using.
567  : num_pages_per_doc_(0), max_memory_(max_memory) {}
569 
570 // Adds all the documents in the list of filenames, counting memory.
571 // The reader is used to read the files.
573  CachingStrategy cache_strategy,
574  FileReader reader) {
575  cache_strategy_ = cache_strategy;
576  inT64 fair_share_memory = 0;
577  // In the round-robin case, each DocumentData handles restricting its content
578  // to its fair share of memory. In the sequential case, DocumentCache
579  // determines which DocumentDatas are held entirely in memory.
580  if (cache_strategy_ == CS_ROUND_ROBIN)
581  fair_share_memory = max_memory_ / filenames.size();
582  for (int arg = 0; arg < filenames.size(); ++arg) {
583  STRING filename = filenames[arg];
584  DocumentData* document = new DocumentData(filename);
585  document->SetDocument(filename.string(), fair_share_memory, reader);
586  AddToCache(document);
587  }
588  if (!documents_.empty()) {
589  // Try to get the first page now to verify the list of filenames.
590  if (GetPageBySerial(0) != NULL) return true;
591  tprintf("Load of page 0 failed!\n");
592  }
593  return false;
594 }
595 
596 // Adds document to the cache.
598  documents_.push_back(data);
599  return true;
600 }
601 
602 // Finds and returns a document by name.
603 DocumentData* DocumentCache::FindDocument(const STRING& document_name) const {
604  for (int i = 0; i < documents_.size(); ++i) {
605  if (documents_[i]->document_name() == document_name)
606  return documents_[i];
607  }
608  return NULL;
609 }
610 
611 // Returns the total number of pages in an epoch. For CS_ROUND_ROBIN cache
612 // strategy, could take a long time.
614  if (cache_strategy_ == CS_SEQUENTIAL) {
615  // In sequential mode, we assume each doc has the same number of pages
616  // whether it is true or not.
617  if (num_pages_per_doc_ == 0) GetPageSequential(0);
618  return num_pages_per_doc_ * documents_.size();
619  }
620  int total_pages = 0;
621  int num_docs = documents_.size();
622  for (int d = 0; d < num_docs; ++d) {
623  // We have to load a page to make NumPages() valid.
624  documents_[d]->GetPage(0);
625  total_pages += documents_[d]->NumPages();
626  }
627  return total_pages;
628 }
629 
630 // Returns a page by serial number, selecting them in a round-robin fashion
631 // from all the documents. Highly disk-intensive, but doesn't need samples
632 // to be shuffled between files to begin with.
633 const ImageData* DocumentCache::GetPageRoundRobin(int serial) {
634  int num_docs = documents_.size();
635  int doc_index = serial % num_docs;
636  const ImageData* doc = documents_[doc_index]->GetPage(serial / num_docs);
637  for (int offset = 1; offset <= kMaxReadAhead && offset < num_docs; ++offset) {
638  doc_index = (serial + offset) % num_docs;
639  int page = (serial + offset) / num_docs;
640  documents_[doc_index]->LoadPageInBackground(page);
641  }
642  return doc;
643 }
644 
645 // Returns a page by serial number, selecting them in sequence from each file.
646 // Requires the samples to be shuffled between the files to give a random or
647 // uniform distribution of data. Less disk-intensive than GetPageRoundRobin.
648 const ImageData* DocumentCache::GetPageSequential(int serial) {
649  int num_docs = documents_.size();
650  ASSERT_HOST(num_docs > 0);
651  if (num_pages_per_doc_ == 0) {
652  // Use the pages in the first doc as the number of pages in each doc.
653  documents_[0]->GetPage(0);
654  num_pages_per_doc_ = documents_[0]->NumPages();
655  if (num_pages_per_doc_ == 0) {
656  tprintf("First document cannot be empty!!\n");
657  ASSERT_HOST(num_pages_per_doc_ > 0);
658  }
659  // Get rid of zero now if we don't need it.
660  if (serial / num_pages_per_doc_ % num_docs > 0) documents_[0]->UnCache();
661  }
662  int doc_index = serial / num_pages_per_doc_ % num_docs;
663  const ImageData* doc =
664  documents_[doc_index]->GetPage(serial % num_pages_per_doc_);
665  // Count up total memory. Background loading makes it more complicated to
666  // keep a running count.
667  inT64 total_memory = 0;
668  for (int d = 0; d < num_docs; ++d) {
669  total_memory += documents_[d]->memory_used();
670  }
671  if (total_memory >= max_memory_) {
672  // Find something to un-cache.
673  // If there are more than 3 in front, then serial is from the back reader
674  // of a pair of readers. If we un-cache from in-front-2 to 2-ahead, then
675  // we create a hole between them and then un-caching the backmost occupied
676  // will work for both.
677  int num_in_front = CountNeighbourDocs(doc_index, 1);
678  for (int offset = num_in_front - 2;
679  offset > 1 && total_memory >= max_memory_; --offset) {
680  int next_index = (doc_index + offset) % num_docs;
681  total_memory -= documents_[next_index]->UnCache();
682  }
683  // If that didn't work, the best solution is to un-cache from the back. If
684  // we take away the document that a 2nd reader is using, it will put it
685  // back and make a hole between.
686  int num_behind = CountNeighbourDocs(doc_index, -1);
687  for (int offset = num_behind; offset < 0 && total_memory >= max_memory_;
688  ++offset) {
689  int next_index = (doc_index + offset + num_docs) % num_docs;
690  total_memory -= documents_[next_index]->UnCache();
691  }
692  }
693  int next_index = (doc_index + 1) % num_docs;
694  if (!documents_[next_index]->IsCached() && total_memory < max_memory_) {
695  documents_[next_index]->LoadPageInBackground(0);
696  }
697  return doc;
698 }
699 
700 // Helper counts the number of adjacent cached neighbours of index looking in
701 // direction dir, ie index+dir, index+2*dir etc.
702 int DocumentCache::CountNeighbourDocs(int index, int dir) {
703  int num_docs = documents_.size();
704  for (int offset = dir; abs(offset) < num_docs; offset += dir) {
705  int offset_index = (index + offset + num_docs) % num_docs;
706  if (!documents_[offset_index]->IsCached()) return offset - dir;
707  }
708  return num_docs;
709 }
710 
711 } // namespace tesseract.
bool DeSerialize(bool swap, FILE *fp)
Definition: imagedata.cpp:91
bool DeSerialize(bool swap, FILE *fp)
Pix * PreScale(int target_height, int max_height, float *scale_factor, int *scaled_width, int *scaled_height, GenericVector< TBOX > *boxes) const
Definition: imagedata.cpp:227
const ImageData * GetPageBySerial(int serial)
Definition: imagedata.h:335
static void FromWordFeatures(const GenericVector< WordFeature > &word_features, GenericVector< FloatWordFeature > *float_features)
Definition: imagedata.cpp:99
void AddBoxes(const GenericVector< TBOX > &boxes, const GenericVector< STRING > &texts, const GenericVector< int > &box_pages)
Definition: imagedata.cpp:314
void Display() const
Definition: imagedata.cpp:276
static int SortByXBucket(const void *, const void *)
Definition: imagedata.cpp:114
Definition: points.h:189
int64_t inT64
Definition: host.h:40
const GenericVector< char > & image_data() const
Definition: imagedata.h:136
int32_t inT32
Definition: host.h:38
bool Serialize(TFile *fp) const
Definition: imagedata.cpp:165
inT32 IntRand()
Definition: helpers.h:55
int page_number() const
Definition: imagedata.h:130
bool(* FileReader)(const STRING &filename, GenericVector< char > *data)
void Brush(Color color)
Definition: scrollview.cpp:732
void AddPageToDocument(ImageData *page)
Definition: imagedata.cpp:427
#define MAX_UINT8
Definition: host.h:63
bool DeSerialize(bool swap, FILE *fp)
Definition: strngs.cpp:163
voidpf void uLong size
Definition: ioapi.h:39
void SetDocument(const char *filename, inT64 max_memory, FileReader reader)
Definition: imagedata.cpp:398
bool CloseWrite(const STRING &filename, FileWriter writer)
Definition: serialis.cpp:140
int push_back(T object)
#define tprintf(...)
Definition: tprintf.h:31
void SetPix(Pix *pix)
Definition: imagedata.cpp:212
const char * string() const
Definition: strngs.cpp:198
void resize_no_init(int size)
Definition: genericvector.h:66
void Unlock()
Unlocks on a mutex.
Definition: svutil.cpp:78
static void ComputeSize(const GenericVector< WordFeature > &features, int *max_x, int *max_y)
Definition: imagedata.cpp:55
void from_direction(uinT8 direction)
Definition: points.cpp:115
voidpf uLong offset
Definition: ioapi.h:42
int FReadEndian(void *buffer, int size, int count)
Definition: serialis.cpp:97
bool empty() const
Definition: genericvector.h:90
void truncate(int size)
DocumentCache(inT64 max_memory)
Definition: imagedata.cpp:566
bool SaveToBuffer(GenericVector< char > *buffer)
Definition: imagedata.cpp:419
inT32 length() const
Definition: strngs.cpp:193
int IntCastRounded(double x)
Definition: helpers.h:179
int size() const
Definition: genericvector.h:72
static bool DeSerializeSize(TFile *fp, inT32 *size)
void SetCursor(int x, int y)
Definition: scrollview.cpp:525
void OpenWrite(GenericVector< char > *data)
Definition: serialis.cpp:125
#define ASSERT_HOST(x)
Definition: errcode.h:84
int MemoryUsed() const
Definition: imagedata.cpp:271
void scale(const float f)
Definition: rect.h:171
const int kMaxReadAhead
Definition: imagedata.cpp:41
const STRING & box_text(int index) const
Definition: imagedata.h:154
Pix * GetPix() const
Definition: imagedata.cpp:217
DocumentData * FindDocument(const STRING &document_name) const
Definition: imagedata.cpp:603
Definition: strngs.h:45
static void StartThread(void *(*func)(void *), void *arg)
Create new thread.
Definition: svutil.cpp:87
bool AddToCache(DocumentData *data)
Definition: imagedata.cpp:597
static void Update()
Definition: scrollview.cpp:715
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:122
char window_wait(ScrollView *win)
Definition: callcpp.cpp:111
bool Serialize(FILE *fp) const
Definition: strngs.cpp:148
bool LoadDocument(const char *filename, int start_page, inT64 max_memory, FileReader reader)
Definition: imagedata.cpp:390
const ImageData * GetPage(int index)
Definition: imagedata.cpp:447
static bool SkipDeSerialize(tesseract::TFile *fp)
Definition: imagedata.cpp:198
int FWrite(const void *buffer, int size, int count)
Definition: serialis.cpp:148
bool LoadDocuments(const GenericVector< STRING > &filenames, CachingStrategy cache_strategy, FileReader reader)
Definition: imagedata.cpp:572
bool IsPageAvailable(int index, ImageData **page)
Definition: imagedata.cpp:469
bool SaveDocument(const char *filename, FileWriter writer)
Definition: imagedata.cpp:409
void LoadPageInBackground(int index)
Definition: imagedata.cpp:435
Definition: rect.h:30
const GenericVector< TBOX > & boxes() const
Definition: imagedata.h:148
const STRING & imagefilename() const
Definition: imagedata.h:124
int8_t inT8
Definition: host.h:34
static void Draw(const GenericVector< WordFeature > &features, ScrollView *window)
Definition: imagedata.cpp:66
inT64 memory_used() const
Definition: imagedata.h:231
#define MIN(x, y)
Definition: ndminx.h:28
bool(* FileWriter)(const GenericVector< char > &data, const STRING &filename)
CachingStrategy
Definition: imagedata.h:40
const char * filename
Definition: ioapi.h:38
float y() const
Definition: points.h:212
static ImageData * Build(const char *name, int page_number, const char *lang, const char *imagedata, int imagedatasize, const char *truth_text, const char *box_text)
Definition: imagedata.cpp:134
uint8_t uinT8
Definition: host.h:35
bool Open(const STRING &filename, FileReader reader)
Definition: serialis.cpp:38
bool Serialize(FILE *fp) const
void set_page_number(int num)
Definition: imagedata.h:133
void * ReCachePagesFunc(void *data)
Definition: imagedata.cpp:369
void Text(int x, int y, const char *mystring)
Definition: scrollview.cpp:658
static bool SkipDeSerialize(tesseract::TFile *fp)
Definition: strngs.cpp:183
void set_imagefilename(const STRING &name)
Definition: imagedata.h:127
void Image(struct Pix *image, int x_pos, int y_pos)
Definition: scrollview.cpp:773
void set_seed(uinT64 seed)
Definition: helpers.h:45
DocumentData(const STRING &name)
Definition: imagedata.cpp:375
void Lock()
Locks on a mutex.
Definition: svutil.cpp:70
bool DeSerialize(TFile *fp)
Definition: imagedata.cpp:181
friend void * ReCachePagesFunc(void *data)
Definition: imagedata.cpp:369
bool Serialize(FILE *fp) const
Definition: imagedata.cpp:83
float x() const
Definition: points.h:209
const char features[]
Definition: feature_tests.c:2
bool DeSerializeClasses(bool swap, FILE *fp)
bool SerializeClasses(FILE *fp) const
void Pen(Color color)
Definition: scrollview.cpp:726
int Modulo(int a, int b)
Definition: helpers.h:164
bool ReadMemBoxes(int target_page, bool skip_blanks, const char *box_data, GenericVector< TBOX > *boxes, GenericVector< STRING > *texts, GenericVector< STRING > *box_texts, GenericVector< int > *pages)
Definition: boxread.cpp:65
void DrawTo(int x, int y)
Definition: scrollview.cpp:531
void TextAttributes(const char *font, int pixel_size, bool bold, bool italic, bool underlined)
Definition: scrollview.cpp:641
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:184
int FRead(void *buffer, int size, int count)
Definition: serialis.cpp:108
int NumPages() const
Definition: imagedata.h:227