22 #include "config_auto.h" 27 #if defined(__MINGW32__) 33 #include "allheaders.h" 59 int* max_x,
int* max_y) {
62 for (
int f = 0; f < features.
size(); ++f) {
63 if (features[f].x_ > *max_x) *max_x = features[f].x_;
64 if (features[f].y_ > *max_y) *max_y = features[f].y_;
71 #ifndef GRAPHICS_DISABLED 72 for (
int f = 0; f < features.
size(); ++f) {
73 FCOORD pos(features[f].x_, features[f].y_);
75 dir.from_direction(features[f].dir_);
103 for (
int i = 0; i < word_features.
size(); ++i) {
105 f.
x = word_features[i].x();
106 f.
y = word_features[i].y();
107 f.
dir = word_features[i].dir();
119 if (x_diff == 0)
return f1->
y - f2->
y;
127 : page_number_(0), vertical_text_(vertical) {
136 const char* imagedata,
int imagedatasize,
137 const char* truth_text,
const char* box_text) {
144 memcpy(&
image_data->image_data_[0], imagedata, imagedatasize);
146 if (truth_text ==
nullptr || truth_text[0] ==
'\0') {
147 tprintf(
"Error: No text corresponding to page %d from image %s!\n",
157 }
else if (truth_text !=
nullptr && truth_text[0] !=
'\0' &&
167 if (!imagefilename_.
Serialize(fp))
return false;
168 if (!fp->
Serialize(&page_number_))
return false;
169 if (!image_data_.
Serialize(fp))
return false;
170 if (!language_.
Serialize(fp))
return false;
171 if (!transcription_.
Serialize(fp))
return false;
175 int8_t vertical = vertical_text_;
192 vertical_text_ = vertical != 0;
212 SetPixInternal(pix, &image_data_);
217 return GetPixInternal(image_data_);
227 int* scaled_width,
int* scaled_height,
230 int input_height = 0;
233 input_width = pixGetWidth(src_pix);
234 input_height = pixGetHeight(src_pix);
235 if (target_height == 0) {
236 target_height = std::min(input_height, max_height);
238 float im_factor =
static_cast<float>(target_height) / input_height;
239 if (scaled_width !=
nullptr)
241 if (scaled_height !=
nullptr)
242 *scaled_height = target_height;
244 Pix* pix = pixScale(src_pix, im_factor, im_factor);
245 if (pix ==
nullptr) {
246 tprintf(
"Scaling pix of size %d, %d by factor %g made null pix!!\n",
247 input_width, input_height, im_factor);
249 if (scaled_width !=
nullptr) *scaled_width = pixGetWidth(pix);
250 if (scaled_height !=
nullptr) *scaled_height = pixGetHeight(pix);
251 pixDestroy(&src_pix);
252 if (
boxes !=
nullptr) {
255 for (
int b = 0; b < boxes_.
size(); ++b) {
256 TBOX box = boxes_[b];
257 box.
scale(im_factor);
262 TBOX box(0, 0, im_factor * input_width, target_height);
266 if (scale_factor !=
nullptr) *scale_factor = im_factor;
271 return image_data_.
size();
276 #ifndef GRAPHICS_DISABLED 277 const int kTextSize = 64;
280 if (pix ==
nullptr)
return;
281 int width = pixGetWidth(pix);
282 int height = pixGetHeight(pix);
284 2 * (width + 2 * kTextSize),
285 2 * (height + 4 * kTextSize),
286 width + 10, height + 3 * kTextSize,
true);
287 win->
Image(pix, 0, height - 1);
292 int text_size = kTextSize;
293 if (!boxes_.
empty() && boxes_[0].height() * 2 < text_size)
294 text_size = boxes_[0].height() * 2;
296 if (!boxes_.
empty()) {
297 for (
int b = 0; b < boxes_.
size(); ++b) {
299 win->
Text(boxes_[b].left(), height + kTextSize, box_texts_[b].
string());
304 win->
Text(0, height + kTextSize * 2, transcription_.
string());
317 for (
int i = 0; i < box_pages.
size(); ++i) {
318 if (page_number_ >= 0 && box_pages[i] != page_number_)
continue;
319 transcription_ += texts[i];
329 pixWriteMem(&data, &size, pix, IFF_PNG);
341 const unsigned char* u_data =
342 reinterpret_cast<const unsigned char*
>(&
image_data[0]);
356 true, &
boxes, &texts,
nullptr,
361 tprintf(
"Error: No boxes for page %d from image %s!\n",
362 page_number_, imagefilename_.
string());
371 document_data->ReCachePages();
376 : document_name_(name),
393 pages_offset_ = start_page;
394 return ReCachePages();
402 document_name_ = filename;
404 max_memory_ = max_memory;
413 if (!pages_.Serialize(&fp) || !fp.
CloseWrite(filename, writer)) {
414 tprintf(
"Serialize failed: %s\n", filename);
423 return pages_.Serialize(&fp);
429 pages_.push_back(page);
439 if (pages_offset_ == index)
return;
440 pages_offset_ = index;
452 bool needs_loading = pages_offset_ != index;
457 #if defined(__MINGW32__) 460 std::this_thread::sleep_for(std::chrono::seconds(1));
472 if (num_pages == 0 || index < 0) {
477 index =
Modulo(index, num_pages);
478 if (pages_offset_ <= index && index < pages_offset_ + pages_.size()) {
479 *page = pages_[index - pages_offset_];
495 tprintf(
"Unloaded document %s, saving %" PRId64
" memory\n",
496 document_name_.
string(), memory_saved);
506 int num_pages = pages_.size();
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]);
517 bool DocumentData::ReCachePages() {
522 int loaded_pages = 0;
525 if (!fp.
Open(document_name_, reader_) ||
528 tprintf(
"Deserialize header failed: %s\n", document_name_.
string());
531 pages_offset_ %= loaded_pages;
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");
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);
549 set_memory_used(
memory_used() + image_data->MemoryUsed());
552 if (page < loaded_pages) {
553 tprintf(
"Deserialize failed: %s read %d/%d pages\n",
554 document_name_.
string(), page, loaded_pages);
557 tprintf(
"Loaded %d/%d pages (%d-%d) of document %s\n", pages_.size(),
558 loaded_pages, pages_offset_ + 1, pages_offset_ + pages_.size(),
561 set_total_pages(loaded_pages);
562 return !pages_.empty();
567 : num_pages_per_doc_(0), max_memory_(max_memory) {}
575 cache_strategy_ = cache_strategy;
576 int64_t fair_share_memory = 0;
581 fair_share_memory = max_memory_ / filenames.
size();
582 for (
int arg = 0; arg < filenames.
size(); ++arg) {
583 STRING filename = filenames[arg];
588 if (!documents_.empty()) {
591 tprintf(
"Load of page 0 failed!\n");
598 documents_.push_back(data);
604 for (
int i = 0; i < documents_.size(); ++i) {
605 if (documents_[i]->document_name() == document_name)
606 return documents_[i];
617 if (num_pages_per_doc_ == 0) GetPageSequential(0);
618 return num_pages_per_doc_ * documents_.size();
621 int num_docs = documents_.size();
622 for (
int d = 0; d < num_docs; ++d) {
624 documents_[d]->GetPage(0);
625 total_pages += documents_[d]->NumPages();
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);
648 const ImageData* DocumentCache::GetPageSequential(
int serial) {
649 int num_docs = documents_.size();
651 if (num_pages_per_doc_ == 0) {
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");
660 if (serial / num_pages_per_doc_ % num_docs > 0) documents_[0]->UnCache();
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_);
667 int64_t total_memory = 0;
668 for (
int d = 0; d < num_docs; ++d) {
669 total_memory += documents_[d]->memory_used();
671 if (total_memory >= max_memory_) {
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();
686 int num_behind = CountNeighbourDocs(doc_index, -1);
687 for (
int offset = num_behind; offset < 0 && total_memory >= max_memory_;
689 int next_index = (doc_index + offset + num_docs) % num_docs;
690 total_memory -= documents_[next_index]->UnCache();
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);
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;
bool Serialize(FILE *fp) const
void AddPageToDocument(ImageData *page)
static bool DeSerializeSkip(TFile *fp)
bool Serialize(FILE *fp) const
void DrawTo(int x, int y)
void Unlock()
Unlocks on a mutex.
const GenericVector< TBOX > & boxes() const
friend void * ReCachePagesFunc(void *data)
void TextAttributes(const char *font, int pixel_size, bool bold, bool italic, bool underlined)
void resize_no_init(int size)
const ImageData * GetPage(int index)
void OpenWrite(GenericVector< char > *data)
static bool DeSerializeSize(TFile *fp, int32_t *size)
static void StartThread(void *(*func)(void *), void *arg)
Create new thread.
void Lock()
Locks on a mutex.
void LoadPageInBackground(int index)
bool DeSerialize(bool swap, FILE *fp)
const char * string() const
bool DeSerialize(char *data, size_t count=1)
bool Serialize(TFile *fp) const
bool SerializeClasses(FILE *fp) const
bool SaveToBuffer(GenericVector< char > *buffer)
const ImageData * GetPageBySerial(int serial)
bool DeSerialize(bool swap, FILE *fp)
bool Serialize(FILE *fp, const char *data, size_t n)
DocumentCache(int64_t max_memory)
DocumentData(const STRING &name)
void SetCursor(int x, int y)
void scale(const float f)
void * ReCachePagesFunc(void *data)
bool Serialize(FILE *fp) const
static bool SkipDeSerialize(tesseract::TFile *fp)
void SetDocument(const char *filename, int64_t max_memory, FileReader reader)
int64_t memory_used() const
void ReverseN(void *ptr, int num_bytes)
Pix * PreScale(int target_height, int max_height, float *scale_factor, int *scaled_width, int *scaled_height, GenericVector< TBOX > *boxes) const
bool DeSerialize(TFile *fp)
void Text(int x, int y, const char *mystring)
const GenericVector< char > & image_data() const
int IntCastRounded(double x)
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 Image(struct Pix *image, int x_pos, int y_pos)
bool Serialize(const char *data, size_t count=1)
DLLSYM void tprintf(const char *format,...)
bool(* FileReader)(const STRING &filename, GenericVector< char > *data)
const STRING & box_text(int index) const
bool IsPageAvailable(int index, ImageData **page)
bool Open(const STRING &filename, FileReader reader)
void AddBoxes(const GenericVector< TBOX > &boxes, const GenericVector< STRING > &texts, const GenericVector< int > &box_pages)
DocumentData * FindDocument(const STRING &document_name) const
static void FromWordFeatures(const GenericVector< WordFeature > &word_features, GenericVector< FloatWordFeature > *float_features)
static void Draw(const GenericVector< WordFeature > &features, ScrollView *window)
bool LoadDocuments(const GenericVector< STRING > &filenames, CachingStrategy cache_strategy, FileReader reader)
bool LoadDocument(const char *filename, int start_page, int64_t max_memory, FileReader reader)
bool DeSerializeClasses(bool swap, FILE *fp)
bool DeSerialize(bool swap, FILE *fp)
bool SaveDocument(const char *filename, FileWriter writer)
bool CloseWrite(const STRING &filename, FileWriter writer)
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
char window_wait(ScrollView *win)
static int SortByXBucket(const void *, const void *)
bool DeSerialize(FILE *fp, char *data, size_t n)
static bool SkipDeSerialize(TFile *fp)
bool(* FileWriter)(const GenericVector< char > &data, const STRING &filename)
static void ComputeSize(const GenericVector< WordFeature > &features, int *max_x, int *max_y)
void set_seed(uint64_t seed)
bool ReadMemBoxes(int target_page, bool skip_blanks, const char *box_data, bool continue_on_failure, GenericVector< TBOX > *boxes, GenericVector< STRING > *texts, GenericVector< STRING > *box_texts, GenericVector< int > *pages)