tesseract  3.05.02
tesseract::DocumentData Class Reference

#include <imagedata.h>

Public Member Functions

 DocumentData (const STRING &name)
 
 ~DocumentData ()
 
bool LoadDocument (const char *filename, const char *lang, int start_page, inT64 max_memory, FileReader reader)
 
void SetDocument (const char *filename, const char *lang, inT64 max_memory, FileReader reader)
 
bool SaveDocument (const char *filename, FileWriter writer)
 
bool SaveToBuffer (GenericVector< char > *buffer)
 
void AddPageToDocument (ImageData *page)
 
const STRINGdocument_name () const
 
int NumPages () const
 
inT64 memory_used () const
 
void LoadPageInBackground (int index)
 
const ImageDataGetPage (int index)
 
bool IsPageAvailable (int index, ImageData **page)
 
ImageDataTakePage (int index)
 
bool IsCached () const
 
inT64 UnCache ()
 

Friends

void * ReCachePagesFunc (void *data)
 

Detailed Description

Definition at line 204 of file imagedata.h.

Constructor & Destructor Documentation

◆ DocumentData()

tesseract::DocumentData::DocumentData ( const STRING name)
explicit

Definition at line 372 of file imagedata.cpp.

373  : document_name_(name),
374  pages_offset_(-1),
375  total_pages_(-1),
376  memory_used_(0),
377  max_memory_(0),
378  reader_(NULL) {}

◆ ~DocumentData()

tesseract::DocumentData::~DocumentData ( )

Definition at line 380 of file imagedata.cpp.

380  {
381  SVAutoLock lock_p(&pages_mutex_);
382  SVAutoLock lock_g(&general_mutex_);
383 }

Member Function Documentation

◆ AddPageToDocument()

void tesseract::DocumentData::AddPageToDocument ( ImageData page)

Definition at line 426 of file imagedata.cpp.

426  {
427  SVAutoLock lock(&pages_mutex_);
428  pages_.push_back(page);
429  set_memory_used(memory_used() + page->MemoryUsed());
430 }
inT64 memory_used() const
Definition: imagedata.h:233

◆ document_name()

const STRING& tesseract::DocumentData::document_name ( ) const
inline

Definition at line 225 of file imagedata.h.

225  {
226  SVAutoLock lock(&general_mutex_);
227  return document_name_;
228  }

◆ GetPage()

const ImageData * tesseract::DocumentData::GetPage ( int  index)

Definition at line 448 of file imagedata.cpp.

448  {
449  ImageData* page = NULL;
450  while (!IsPageAvailable(index, &page)) {
451  // If there is no background load scheduled, schedule one now.
452  pages_mutex_.Lock();
453  bool needs_loading = pages_offset_ != index;
454  pages_mutex_.Unlock();
455  if (needs_loading) LoadPageInBackground(index);
456  // We can't directly load the page, or the background load will delete it
457  // while the caller is using it, so give it a chance to work.
458 #if __cplusplus > 199711L && !defined(__MINGW32__)
459  std::this_thread::sleep_for(std::chrono::seconds(1));
460 #elif _WIN32 // MSVS
461  Sleep(1000);
462 #else
463  sleep(1);
464 #endif
465  }
466  return page;
467 }
void LoadPageInBackground(int index)
Definition: imagedata.cpp:434
bool IsPageAvailable(int index, ImageData **page)
Definition: imagedata.cpp:472
void Lock()
Locks on a mutex.
Definition: svutil.cpp:70
void Unlock()
Unlocks on a mutex.
Definition: svutil.cpp:78

◆ IsCached()

bool tesseract::DocumentData::IsCached ( ) const
inline

Definition at line 265 of file imagedata.h.

265 { return NumPages() >= 0; }
int NumPages() const
Definition: imagedata.h:229

◆ IsPageAvailable()

bool tesseract::DocumentData::IsPageAvailable ( int  index,
ImageData **  page 
)

Definition at line 472 of file imagedata.cpp.

472  {
473  SVAutoLock lock(&pages_mutex_);
474  int num_pages = NumPages();
475  if (num_pages == 0 || index < 0) {
476  *page = NULL; // Empty Document.
477  return true;
478  }
479  if (num_pages > 0) {
480  index = Modulo(index, num_pages);
481  if (pages_offset_ <= index && index < pages_offset_ + pages_.size()) {
482  *page = pages_[index - pages_offset_]; // Page is available already.
483  return true;
484  }
485  }
486  return false;
487 }
int NumPages() const
Definition: imagedata.h:229
int Modulo(int a, int b)
Definition: helpers.h:157

◆ LoadDocument()

bool tesseract::DocumentData::LoadDocument ( const char *  filename,
const char *  lang,
int  start_page,
inT64  max_memory,
FileReader  reader 
)

Definition at line 387 of file imagedata.cpp.

389  {
390  SetDocument(filename, lang, max_memory, reader);
391  pages_offset_ = start_page;
392  return ReCachePages();
393 }
void SetDocument(const char *filename, const char *lang, inT64 max_memory, FileReader reader)
Definition: imagedata.cpp:396

◆ LoadPageInBackground()

void tesseract::DocumentData::LoadPageInBackground ( int  index)

Definition at line 434 of file imagedata.cpp.

434  {
435  ImageData* page = NULL;
436  if (IsPageAvailable(index, &page)) return;
437  SVAutoLock lock(&pages_mutex_);
438  if (pages_offset_ == index) return;
439  pages_offset_ = index;
440  pages_.clear();
441  #ifndef GRAPHICS_DISABLED
443  #endif // GRAPHICS_DISABLED
444 }
friend void * ReCachePagesFunc(void *data)
Definition: imagedata.cpp:366
bool IsPageAvailable(int index, ImageData **page)
Definition: imagedata.cpp:472
static void StartThread(void *(*func)(void *), void *arg)
Create new thread.
Definition: svutil.cpp:192

◆ memory_used()

inT64 tesseract::DocumentData::memory_used ( ) const
inline

Definition at line 233 of file imagedata.h.

233  {
234  SVAutoLock lock(&general_mutex_);
235  return memory_used_;
236  }

◆ NumPages()

int tesseract::DocumentData::NumPages ( ) const
inline

Definition at line 229 of file imagedata.h.

229  {
230  SVAutoLock lock(&general_mutex_);
231  return total_pages_;
232  }

◆ SaveDocument()

bool tesseract::DocumentData::SaveDocument ( const char *  filename,
FileWriter  writer 
)

Definition at line 408 of file imagedata.cpp.

408  {
409  SVAutoLock lock(&pages_mutex_);
410  TFile fp;
411  fp.OpenWrite(NULL);
412  if (!pages_.Serialize(&fp) || !fp.CloseWrite(filename, writer)) {
413  tprintf("Serialize failed: %s\n", filename);
414  return false;
415  }
416  return true;
417 }
#define tprintf(...)
Definition: tprintf.h:31

◆ SaveToBuffer()

bool tesseract::DocumentData::SaveToBuffer ( GenericVector< char > *  buffer)

Definition at line 418 of file imagedata.cpp.

418  {
419  SVAutoLock lock(&pages_mutex_);
420  TFile fp;
421  fp.OpenWrite(buffer);
422  return pages_.Serialize(&fp);
423 }

◆ SetDocument()

void tesseract::DocumentData::SetDocument ( const char *  filename,
const char *  lang,
inT64  max_memory,
FileReader  reader 
)

Definition at line 396 of file imagedata.cpp.

397  {
398  SVAutoLock lock_p(&pages_mutex_);
399  SVAutoLock lock(&general_mutex_);
400  document_name_ = filename;
401  lang_ = lang;
402  pages_offset_ = -1;
403  max_memory_ = max_memory;
404  reader_ = reader;
405 }

◆ TakePage()

ImageData* tesseract::DocumentData::TakePage ( int  index)
inline

Definition at line 257 of file imagedata.h.

257  {
258  SVAutoLock lock(&pages_mutex_);
259  ImageData* page = pages_[index];
260  pages_[index] = NULL;
261  return page;
262  }

◆ UnCache()

inT64 tesseract::DocumentData::UnCache ( )

Definition at line 491 of file imagedata.cpp.

491  {
492  SVAutoLock lock(&pages_mutex_);
493  inT64 memory_saved = memory_used();
494  pages_.clear();
495  pages_offset_ = -1;
496  set_total_pages(-1);
497  set_memory_used(0);
498  tprintf("Unloaded document %s, saving %d memory\n", document_name_.string(),
499  memory_saved);
500  return memory_saved;
501 }
const char * string() const
Definition: strngs.cpp:201
long long int inT64
Definition: host.h:41
#define tprintf(...)
Definition: tprintf.h:31
inT64 memory_used() const
Definition: imagedata.h:233

Friends And Related Function Documentation

◆ ReCachePagesFunc

void* ReCachePagesFunc ( void *  data)
friend

Definition at line 366 of file imagedata.cpp.

366  {
367  DocumentData* document_data = reinterpret_cast<DocumentData*>(data);
368  document_data->ReCachePages();
369  return NULL;
370 }
DocumentData(const STRING &name)
Definition: imagedata.cpp:372

The documentation for this class was generated from the following files: