tesseract  3.05.00
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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

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) {}
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

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
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  }
const ImageData * tesseract::DocumentData::GetPage ( int  index)

Definition at line 446 of file imagedata.cpp.

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

Definition at line 470 of file imagedata.cpp.

470  {
471  SVAutoLock lock(&pages_mutex_);
472  int num_pages = NumPages();
473  if (num_pages == 0 || index < 0) {
474  *page = NULL; // Empty Document.
475  return true;
476  }
477  if (num_pages > 0) {
478  index = Modulo(index, num_pages);
479  if (pages_offset_ <= index && index < pages_offset_ + pages_.size()) {
480  *page = pages_[index - pages_offset_]; // Page is available already.
481  return true;
482  }
483  }
484  return false;
485 }
int Modulo(int a, int b)
Definition: helpers.h:157
int NumPages() const
Definition: imagedata.h:229
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
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();
442 }
static void StartThread(void *(*func)(void *), void *arg)
Create new thread.
Definition: svutil.cpp:192
bool IsPageAvailable(int index, ImageData **page)
Definition: imagedata.cpp:470
friend void * ReCachePagesFunc(void *data)
Definition: imagedata.cpp:366
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  }
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  }
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
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 }
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 }
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  }
inT64 tesseract::DocumentData::UnCache ( )

Definition at line 489 of file imagedata.cpp.

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

Friends And Related Function Documentation

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: