27 #include "allheaders.h" 33 #if defined(__MINGW32__) 35 #elif __cplusplus > 199711L // in C++11 56 int* max_x,
int* max_y) {
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_;
68 #ifndef GRAPHICS_DISABLED 69 for (
int f = 0; f < features.
size(); ++f) {
70 FCOORD pos(features[f].x_, features[f].y_);
72 dir.from_direction(features[f].dir_);
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;
92 if (fread(&x_,
sizeof(x_), 1, fp) != 1)
return false;
94 if (fread(&y_,
sizeof(y_), 1, fp) != 1)
return false;
95 if (fread(&dir_,
sizeof(dir_), 1, fp) != 1)
return false;
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();
118 if (x_diff == 0)
return f1->
y - f2->
y;
126 : page_number_(0), vertical_text_(vertical) {
135 const char* imagedata,
int imagedatasize,
136 const char* truth_text,
const char* box_text) {
143 memcpy(&
image_data->image_data_[0], imagedata, imagedatasize);
145 if (truth_text == NULL || truth_text[0] ==
'\0') {
146 tprintf(
"Error: No text corresponding to page %d from image %s!\n",
156 }
else if (truth_text != NULL && truth_text[0] !=
'\0' &&
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 (!transcription_.
Serialize(fp))
return false;
173 inT8 vertical = vertical_text_;
174 if (fp->
FWrite(&vertical,
sizeof(vertical), 1) != 1)
return false;
181 if (!imagefilename_.
DeSerialize(swap, fp))
return false;
182 if (fp->
FRead(&page_number_,
sizeof(page_number_), 1) != 1)
return false;
183 if (swap)
ReverseN(&page_number_,
sizeof(page_number_));
184 if (!image_data_.
DeSerialize(swap, fp))
return false;
185 if (!transcription_.
DeSerialize(swap, fp))
return false;
190 if (fp->
FRead(&vertical,
sizeof(vertical), 1) != 1)
return false;
191 vertical_text_ = vertical != 0;
205 return fp->
FRead(&vertical,
sizeof(vertical), 1) == 1;
210 SetPixInternal(pix, &image_data_);
215 return GetPixInternal(image_data_);
225 int* scaled_width,
int* scaled_height,
228 int input_height = 0;
231 input_width = pixGetWidth(src_pix);
232 input_height = pixGetHeight(src_pix);
233 if (target_height == 0) {
234 target_height =
MIN(input_height, max_height);
236 float im_factor =
static_cast<float>(target_height) / input_height;
237 if (scaled_width != NULL)
239 if (scaled_height != NULL)
240 *scaled_height = target_height;
242 Pix* pix = pixScale(src_pix, im_factor, im_factor);
244 tprintf(
"Scaling pix of size %d, %d by factor %g made null pix!!\n",
245 input_width, input_height, im_factor);
247 if (scaled_width != NULL) *scaled_width = pixGetWidth(pix);
248 if (scaled_height != NULL) *scaled_height = pixGetHeight(pix);
249 pixDestroy(&src_pix);
253 for (
int b = 0; b < boxes_.
size(); ++b) {
254 TBOX box = boxes_[b];
255 box.
scale(im_factor);
260 TBOX box(0, 0, im_factor * input_width, target_height);
264 if (scale_factor != NULL) *scale_factor = im_factor;
269 return image_data_.
size();
274 #ifndef GRAPHICS_DISABLED 275 const int kTextSize = 64;
278 if (pix == NULL)
return;
279 int width = pixGetWidth(pix);
280 int height = pixGetHeight(pix);
282 2 * (width + 2 * kTextSize),
283 2 * (height + 4 * kTextSize),
284 width + 10, height + 3 * kTextSize,
true);
285 win->
Image(pix, 0, height - 1);
290 int text_size = kTextSize;
291 if (!boxes_.
empty() && boxes_[0].height() * 2 < text_size)
292 text_size = boxes_[0].height() * 2;
294 if (!boxes_.
empty()) {
295 for (
int b = 0; b < boxes_.
size(); ++b) {
297 win->
Text(boxes_[b].left(), height + kTextSize, box_texts_[b].
string());
302 win->
Text(0, height + kTextSize * 2, transcription_.
string());
315 for (
int i = 0; i < box_pages.
size(); ++i) {
316 if (page_number_ >= 0 && box_pages[i] != page_number_)
continue;
317 transcription_ += texts[i];
327 pixWriteMem(&data, &size, pix, IFF_PNG);
339 const unsigned char* u_data =
340 reinterpret_cast<const unsigned char*
>(&
image_data[0]);
354 &texts, NULL, &box_pages)) {
358 tprintf(
"Error: No boxes for page %d from image %s!\n",
359 page_number_, imagefilename_.
string());
368 document_data->ReCachePages();
373 : document_name_(name),
388 int start_page,
inT64 max_memory,
391 pages_offset_ = start_page;
392 return ReCachePages();
403 max_memory_ = max_memory;
422 return pages_.Serialize(&fp);
428 pages_.push_back(page);
438 if (pages_offset_ == index)
return;
439 pages_offset_ = index;
441 #ifndef GRAPHICS_DISABLED 443 #endif // GRAPHICS_DISABLED 453 bool needs_loading = pages_offset_ != index;
458 #if __cplusplus > 199711L && !defined(__MINGW32__) 459 std::this_thread::sleep_for(std::chrono::seconds(1));
475 if (num_pages == 0 || index < 0) {
480 index =
Modulo(index, num_pages);
481 if (pages_offset_ <= index && index < pages_offset_ + pages_.size()) {
482 *page = pages_[index - pages_offset_];
498 tprintf(
"Unloaded document %s, saving %d memory\n", document_name_.
string(),
505 bool DocumentData::ReCachePages() {
510 int loaded_pages = 0;
513 if (!fp.
Open(document_name_, reader_) ||
516 tprintf(
"Deserialize header failed: %s\n", document_name_.
string());
519 pages_offset_ %= loaded_pages;
523 for (page = 0; page < loaded_pages; ++page) {
524 if (page < pages_offset_ ||
525 (max_memory_ > 0 &&
memory_used() > max_memory_)) {
528 if (!pages_.DeSerializeElement(
false, &fp))
break;
529 ImageData* image_data = pages_.back();
530 if (image_data->imagefilename().length() == 0) {
531 image_data->set_imagefilename(document_name_);
532 image_data->set_page_number(page);
534 image_data->set_language(lang_);
535 set_memory_used(
memory_used() + image_data->MemoryUsed());
538 if (page < loaded_pages) {
539 tprintf(
"Deserialize failed: %s read %d/%d pages\n",
540 document_name_.
string(), page, loaded_pages);
543 tprintf(
"Loaded %d/%d pages (%d-%d) of document %s\n", pages_.size(),
544 loaded_pages, pages_offset_, pages_offset_ + pages_.size(),
547 set_total_pages(loaded_pages);
548 return !pages_.empty();
553 : num_pages_per_doc_(0), max_memory_(max_memory) {}
562 cache_strategy_ = cache_strategy;
563 inT64 fair_share_memory = 0;
568 fair_share_memory = max_memory_ / filenames.
size();
569 for (
int arg = 0; arg < filenames.
size(); ++arg) {
575 if (!documents_.empty()) {
578 tprintf(
"Load of page 0 failed!\n");
586 documents_.push_back(data);
592 for (
int i = 0; i < documents_.size(); ++i) {
593 if (documents_[i]->document_name() == document_name)
594 return documents_[i];
605 if (num_pages_per_doc_ == 0) GetPageSequential(0);
606 return num_pages_per_doc_ * documents_.size();
609 int num_docs = documents_.size();
610 for (
int d = 0; d < num_docs; ++d) {
612 documents_[d]->GetPage(0);
613 total_pages += documents_[d]->NumPages();
621 const ImageData* DocumentCache::GetPageRoundRobin(
int serial) {
622 int num_docs = documents_.size();
623 int doc_index = serial % num_docs;
624 const ImageData* doc = documents_[doc_index]->GetPage(serial / num_docs);
625 for (
int offset = 1; offset <=
kMaxReadAhead && offset < num_docs; ++offset) {
626 doc_index = (serial + offset) % num_docs;
627 int page = (serial + offset) / num_docs;
628 documents_[doc_index]->LoadPageInBackground(page);
636 const ImageData* DocumentCache::GetPageSequential(
int serial) {
637 int num_docs = documents_.size();
639 if (num_pages_per_doc_ == 0) {
641 documents_[0]->GetPage(0);
642 num_pages_per_doc_ = documents_[0]->NumPages();
643 if (num_pages_per_doc_ == 0) {
644 tprintf(
"First document cannot be empty!!\n");
648 if (serial / num_pages_per_doc_ % num_docs > 0) documents_[0]->UnCache();
650 int doc_index = serial / num_pages_per_doc_ % num_docs;
651 const ImageData* doc =
652 documents_[doc_index]->GetPage(serial % num_pages_per_doc_);
655 inT64 total_memory = 0;
656 for (
int d = 0; d < num_docs; ++d) {
657 total_memory += documents_[d]->memory_used();
659 if (total_memory >= max_memory_) {
665 int num_in_front = CountNeighbourDocs(doc_index, 1);
666 for (
int offset = num_in_front - 2;
667 offset > 1 && total_memory >= max_memory_; --offset) {
668 int next_index = (doc_index + offset) % num_docs;
669 total_memory -= documents_[next_index]->UnCache();
674 int num_behind = CountNeighbourDocs(doc_index, -1);
675 for (
int offset = num_behind; offset < 0 && total_memory >= max_memory_;
677 int next_index = (doc_index + offset + num_docs) % num_docs;
678 total_memory -= documents_[next_index]->UnCache();
681 int next_index = (doc_index + 1) % num_docs;
682 if (!documents_[next_index]->IsCached() && total_memory < max_memory_) {
683 documents_[next_index]->LoadPageInBackground(0);
690 int DocumentCache::CountNeighbourDocs(
int index,
int dir) {
691 int num_docs = documents_.size();
692 for (
int offset = dir; abs(offset) < num_docs; offset += dir) {
693 int offset_index = (index + offset + num_docs) % num_docs;
694 if (!documents_[offset_index]->IsCached())
return offset - dir;
const ImageData * GetPageBySerial(int serial)
void DrawTo(int x, int y)
void Image(struct Pix *image, int x_pos, int y_pos)
static void ComputeSize(const GenericVector< WordFeature > &features, int *max_x, int *max_y)
bool LoadDocument(const char *filename, const char *lang, int start_page, inT64 max_memory, FileReader reader)
void scale(const float f)
int IntCastRounded(double x)
bool SaveDocument(const char *filename, FileWriter writer)
void resize_no_init(int size)
static bool DeSerializeSkip(bool swap, TFile *fp)
bool SaveToBuffer(GenericVector< char > *buffer)
static void Draw(const GenericVector< WordFeature > &features, ScrollView *window)
int FWrite(const void *buffer, int size, int count)
void SetCursor(int x, int y)
friend void * ReCachePagesFunc(void *data)
void SetDocument(const char *filename, const char *lang, inT64 max_memory, FileReader reader)
bool Open(const STRING &filename, FileReader reader)
const GenericVector< TBOX > & boxes() const
static bool SkipDeSerialize(bool swap, tesseract::TFile *fp)
bool DeSerialize(bool swap, FILE *fp)
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
void TextAttributes(const char *font, int pixel_size, bool bold, bool italic, bool underlined)
bool DeSerializeClasses(bool swap, FILE *fp)
bool DeSerialize(bool swap, FILE *fp)
const GenericVector< char > & image_data() const
bool Serialize(FILE *fp) const
bool Serialize(TFile *fp) const
void * ReCachePagesFunc(void *data)
void Text(int x, int y, const char *mystring)
const ImageData * GetPage(int index)
bool LoadDocuments(const GenericVector< STRING > &filenames, const char *lang, CachingStrategy cache_strategy, FileReader reader)
const char * string() const
bool CloseWrite(const STRING &filename, FileWriter writer)
DocumentCache(inT64 max_memory)
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)
bool AddToCache(DocumentData *data)
void LoadPageInBackground(int index)
int FRead(void *buffer, int size, int count)
bool IsPageAvailable(int index, ImageData **page)
void Lock()
Locks on a mutex.
bool Serialize(FILE *fp) const
DocumentData(const STRING &name)
static int SortByXBucket(const void *, const void *)
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)
const STRING & box_text(int index) const
bool DeSerialize(bool swap, TFile *fp)
bool Serialize(FILE *fp) const
void OpenWrite(GenericVector< char > *data)
Pix * PreScale(int target_height, int max_height, float *scale_factor, int *scaled_width, int *scaled_height, GenericVector< TBOX > *boxes) const
void AddBoxes(const GenericVector< TBOX > &boxes, const GenericVector< STRING > &texts, const GenericVector< int > &box_pages)
inT64 memory_used() const
static bool DeSerializeSize(bool swap, TFile *fp, inT32 *size)
bool SerializeClasses(FILE *fp) const
void Unlock()
Unlocks on a mutex.
void AddPageToDocument(ImageData *page)
static void FromWordFeatures(const GenericVector< WordFeature > &word_features, GenericVector< FloatWordFeature > *float_features)
bool(* FileReader)(const STRING &filename, GenericVector< char > *data)
char window_wait(ScrollView *win)
void ReverseN(void *ptr, int num_bytes)
static bool SkipDeSerialize(bool swap, tesseract::TFile *fp)
DocumentData * FindDocument(const STRING &document_name) const
static void StartThread(void *(*func)(void *), void *arg)
Create new thread.
bool(* FileWriter)(const GenericVector< char > &data, const STRING &filename)
bool DeSerialize(bool swap, FILE *fp)