27 #include "allheaders.h"
46 int* max_x,
int* max_y) {
49 for (
int f = 0; f < features.
size(); ++f) {
50 if (features[f].x_ > *max_x) *max_x = features[f].x_;
51 if (features[f].y_ > *max_y) *max_y = features[f].y_;
58 #ifndef GRAPHICS_DISABLED
59 for (
int f = 0; f < features.
size(); ++f) {
60 FCOORD pos(features[f].x_, features[f].y_);
74 if (fwrite(&x_,
sizeof(x_), 1, fp) != 1)
return false;
75 if (fwrite(&y_,
sizeof(y_), 1, fp) != 1)
return false;
76 if (fwrite(&dir_,
sizeof(dir_), 1, fp) != 1)
return false;
82 if (fread(&x_,
sizeof(x_), 1, fp) != 1)
return false;
84 if (fread(&y_,
sizeof(y_), 1, fp) != 1)
return false;
85 if (fread(&dir_,
sizeof(dir_), 1, fp) != 1)
return false;
92 for (
int i = 0; i < word_features.
size(); ++i) {
94 f.
x = word_features[i].x();
95 f.
y = word_features[i].y();
96 f.
dir = word_features[i].dir();
108 if (x_diff == 0)
return f1->
y - f2->
y;
116 : page_number_(0), vertical_text_(vertical) {
125 const char* imagedata,
int imagedatasize,
126 const char* truth_text,
const char* box_text) {
128 image_data->imagefilename_ =
name;
130 image_data->language_ =
lang;
133 memcpy(&image_data->image_data_[0], imagedata, imagedatasize);
134 if (!image_data->
AddBoxes(box_text)) {
135 if (truth_text == NULL || truth_text[0] ==
'\0') {
136 tprintf(
"Error: No text corresponding to page %d from image %s!\n",
141 image_data->transcription_ = truth_text;
143 image_data->box_texts_.
push_back(truth_text);
146 }
else if (truth_text != NULL && truth_text[0] !=
'\0' &&
147 image_data->transcription_ != truth_text) {
149 image_data->transcription_ = truth_text;
156 if (!imagefilename_.
Serialize(fp))
return false;
157 if (fp->
FWrite(&page_number_,
sizeof(page_number_), 1) != 1)
return false;
158 if (!image_data_.
Serialize(fp))
return false;
159 if (!transcription_.
Serialize(fp))
return false;
163 inT8 vertical = vertical_text_;
164 if (fp->
FWrite(&vertical,
sizeof(vertical), 1) != 1)
return false;
171 if (!imagefilename_.
DeSerialize(swap, fp))
return false;
172 if (fp->
FRead(&page_number_,
sizeof(page_number_), 1) != 1)
return false;
173 if (swap)
ReverseN(&page_number_,
sizeof(page_number_));
174 if (!image_data_.
DeSerialize(swap, fp))
return false;
175 if (!transcription_.
DeSerialize(swap, fp))
return false;
180 if (fp->
FRead(&vertical,
sizeof(vertical), 1) != 1)
return false;
181 vertical_text_ = vertical != 0;
187 SetPixInternal(pix, &image_data_);
192 return GetPixInternal(image_data_);
201 int* scaled_width,
int* scaled_height,
204 int input_height = 0;
207 input_width = pixGetWidth(src_pix);
208 input_height = pixGetHeight(src_pix);
209 if (target_height == 0)
210 target_height = input_height;
211 float im_factor =
static_cast<float>(target_height) / input_height;
212 if (scaled_width != NULL)
214 if (scaled_height != NULL)
215 *scaled_height = target_height;
219 *pix = pixScale(src_pix, im_factor, im_factor);
221 tprintf(
"Scaling pix of size %d, %d by factor %g made null pix!!\n",
222 input_width, input_height, im_factor);
224 if (scaled_width != NULL)
225 *scaled_width = pixGetWidth(*pix);
226 if (scaled_height != NULL)
227 *scaled_height = pixGetHeight(*pix);
229 pixDestroy(&src_pix);
233 for (
int b = 0; b < boxes_.
size(); ++b) {
234 TBOX box = boxes_[b];
235 box.
scale(im_factor);
238 if (boxes->
empty()) {
240 TBOX box(0, 0, im_factor * input_width, target_height);
248 return image_data_.
size();
253 #ifndef GRAPHICS_DISABLED
254 const int kTextSize = 64;
257 if (pix == NULL)
return;
258 int width = pixGetWidth(pix);
259 int height = pixGetHeight(pix);
261 2 * (width + 2 * kTextSize),
262 2 * (height + 4 * kTextSize),
263 width + 10, height + 3 * kTextSize,
true);
264 win->
Image(pix, 0, height - 1);
270 for (
int b = 0; b < boxes_.
size(); ++b) {
272 win->
Text(boxes_[b].left(), height + kTextSize, box_texts_[b].
string());
273 TBOX scaled(boxes_[b]);
274 scaled.
scale(256.0 / height);
279 win->
Text(0, height + kTextSize * 2, transcription_.
string());
293 for (
int i = 0; i < box_pages.
size(); ++i) {
294 if (page_number_ >= 0 && box_pages[i] != page_number_)
continue;
295 transcription_ += texts[i];
305 pixWriteMem(&data, &size, pix, IFF_PNG);
308 memcpy(&(*image_data)[0], data, size);
315 if (!image_data.
empty()) {
317 const unsigned char* u_data =
318 reinterpret_cast<const unsigned char*
>(&image_data[0]);
319 pix = pixReadMem(u_data, image_data.
size());
327 if (box_text != NULL && box_text[0] !=
'\0') {
332 &texts, NULL, &box_pages)) {
336 tprintf(
"Error: No boxes for page %d from image %s!\n",
337 page_number_, imagefilename_.
string());
344 : document_name_(name), pages_offset_(0), total_pages_(0),
345 memory_used_(0), max_memory_(0), reader_(NULL) {}
352 int start_page,
inT64 max_memory,
356 pages_offset_ = start_page;
357 max_memory_ = max_memory;
359 return ReCachePages();
366 if (!pages_.Serialize(&fp) || !fp.
CloseWrite(filename, writer)) {
367 tprintf(
"Serialize failed: %s\n", filename);
375 return pages_.Serialize(&fp);
381 index =
Modulo(index, total_pages_);
382 if (index < pages_offset_ || index >= pages_offset_ + pages_.size()) {
383 pages_offset_ = index;
384 if (!ReCachePages())
return NULL;
386 return pages_[index - pages_offset_];
390 bool DocumentData::ReCachePages() {
393 if (!fp.
Open(document_name_, reader_))
return false;
395 if (!pages_.DeSerialize(
false, &fp)) {
396 tprintf(
"Deserialize failed: %s\n", document_name_.
string());
400 total_pages_ = pages_.size();
401 pages_offset_ %= total_pages_;
404 for (page = 0; page < pages_.size(); ++page) {
405 if (page < pages_offset_) {
409 ImageData* image_data = pages_[page];
410 if (max_memory_ > 0 && page > pages_offset_ &&
411 memory_used_ + image_data->MemoryUsed() > max_memory_)
413 if (image_data->imagefilename().length() == 0) {
414 image_data->set_imagefilename(document_name_);
415 image_data->set_page_number(page);
417 image_data->set_language(lang_);
418 memory_used_ += image_data->MemoryUsed();
419 if (pages_offset_ != 0) {
420 pages_[page - pages_offset_] = image_data;
425 pages_.truncate(page - pages_offset_);
426 tprintf(
"Loaded %d/%d pages (%d-%d) of document %s\n",
427 pages_.size(), total_pages_, pages_offset_,
428 pages_offset_ + pages_.size(), document_name_.
string());
429 return !pages_.empty();
434 pages_.push_back(page);
440 : total_pages_(0), memory_used_(0), max_memory_(max_memory) {}
447 inT64 fair_share_memory = max_memory_ / filenames.
size();
448 for (
int arg = 0; arg < filenames.
size(); ++arg) {
452 fair_share_memory, reader)) {
459 tprintf(
"Loaded %d pages, total %gMB\n",
460 total_pages_, memory_used_ / 1048576.0);
461 return total_pages_ > 0;
467 memory_used_ += new_memory;
468 documents_.push_back(data);
472 while (memory_used_ >= max_memory_ && max_memory_ > 0) {
473 tprintf(
"Memory used=%lld vs max=%lld, discarding doc of size %lld\n",
474 memory_used_ , max_memory_, documents_[0]->memory_used());
475 memory_used_ -= documents_[0]->memory_used();
476 total_pages_ -= documents_[0]->NumPages();
477 documents_.remove(0);
484 for (
int i = 0; i < documents_.size(); ++i) {
485 if (documents_[i]->document_name() == document_name)
486 return documents_[i];
494 int document_index = serial % documents_.size();
495 return documents_[document_index]->GetPage(serial / documents_.size());
bool DeSerialize(bool swap, FILE *fp)
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 DeSerializeClasses(bool swap, FILE *fp)
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)
bool(* FileReader)(const STRING &filename, GenericVector< char > *data)
bool DeSerialize(bool swap, TFile *fp)
int IntCastRounded(double x)
void SetCursor(int x, int y)
bool Serialize(FILE *fp) const
void ReverseN(void *ptr, int num_bytes)
float PreScale(int target_height, Pix **pix, int *scaled_width, int *scaled_height, GenericVector< TBOX > *boxes) const
bool Serialize(FILE *fp) const
const ImageData * GetPageBySerial(int serial)
void AddBoxes(const GenericVector< TBOX > &boxes, const GenericVector< STRING > &texts, const GenericVector< int > &box_pages)
const ImageData * GetPage(int index)
char window_wait(ScrollView *win)
bool SerializeClasses(FILE *fp) const
static void Draw(const GenericVector< WordFeature > &features, ScrollView *window)
bool SaveDocument(const char *filename, FileWriter writer)
void Image(struct Pix *image, int x_pos, int y_pos)
bool Serialize(FILE *fp) const
void init_to_size(int size, T t)
bool DeSerialize(bool swap, FILE *fp)
DocumentCache(inT64 max_memory)
void OpenWrite(GenericVector< char > *data)
void Text(int x, int y, const char *mystring)
void DrawTo(int x, int y)
bool DeSerialize(bool swap, FILE *fp)
bool LoadDocuments(const GenericVector< STRING > &filenames, const char *lang, FileReader reader)
void TextAttributes(const char *font, int pixel_size, bool bold, bool italic, bool underlined)
DocumentData * FindDocument(const STRING &document_name) const
bool CloseWrite(const STRING &filename, FileWriter writer)
void AddPageToDocument(ImageData *page)
void from_direction(uinT8 direction)
static int SortByXBucket(const void *, const void *)
const GenericVector< TBOX > & boxes() const
bool AddToCache(DocumentData *data)
DocumentData(const STRING &name)
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
int FWrite(const void *buffer, int size, int count)
inT64 memory_used() const
const GenericVector< char > & image_data() const
bool(* FileWriter)(const GenericVector< char > &data, const STRING &filename)
const char * string() const
static void ComputeSize(const GenericVector< WordFeature > &features, int *max_x, int *max_y)
bool SaveToBuffer(GenericVector< char > *buffer)
bool LoadDocument(const char *filename, const char *lang, int start_page, inT64 max_memory, FileReader reader)
bool Serialize(TFile *fp) const
bool Open(const STRING &filename, FileReader reader)
void scale(const float f)
int FRead(void *buffer, int size, int count)
static void FromWordFeatures(const GenericVector< WordFeature > &word_features, GenericVector< FloatWordFeature > *float_features)
void plot(ScrollView *fd) const