All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
tesseract::TessResultRenderer Class Referenceabstract

#include <renderer.h>

Inheritance diagram for tesseract::TessResultRenderer:
tesseract::TessBoxTextRenderer tesseract::TessHOcrRenderer tesseract::TessPDFRenderer tesseract::TessTextRenderer tesseract::TessUnlvRenderer

Public Member Functions

virtual ~TessResultRenderer ()
 
void insert (TessResultRenderer *next)
 
TessResultRenderernext ()
 
bool BeginDocument (const char *title)
 
bool AddImage (TessBaseAPI *api)
 
bool EndDocument ()
 
const char * file_extension () const
 
const char * title () const
 
int imagenum () const
 

Protected Member Functions

 TessResultRenderer (const char *outputbase, const char *extension)
 
virtual bool BeginDocumentHandler ()
 
virtual bool AddImageHandler (TessBaseAPI *api)=0
 
virtual bool EndDocumentHandler ()
 
void AppendString (const char *s)
 
void AppendData (const char *s, int len)
 

Detailed Description

Interface for rendering tesseract results into a document, such as text, HOCR or pdf. This class is abstract. Specific classes handle individual formats. This interface is then used to inject the renderer class into tesseract when processing images.

For simplicity implementing this with tesesract version 3.01, the renderer contains document state that is cleared from document to document just as the TessBaseAPI is. This way the base API can just delegate its rendering functionality to injected renderers, and the renderers can manage the associated state needed for the specific formats in addition to the heuristics for producing it.

Definition at line 45 of file renderer.h.

Constructor & Destructor Documentation

tesseract::TessResultRenderer::~TessResultRenderer ( )
virtual

Definition at line 32 of file renderer.cpp.

32  {
33  if (fout_ != stdout)
34  fclose(fout_);
35  else
36  clearerr(fout_);
37  delete next_;
38 }
tesseract::TessResultRenderer::TessResultRenderer ( const char *  outputbase,
const char *  extension 
)
protected

Called by concrete classes.

outputbase is the name of the output file excluding extension. For example, "/path/to/chocolate-chip-cookie-recipe"

extension indicates the file extension to be used for output files. For example "pdf" will produce a .pdf file, and "hocr" will produce .hocr files.

Definition at line 16 of file renderer.cpp.

18  : file_extension_(extension),
19  title_(""), imagenum_(-1),
20  fout_(stdout),
21  next_(NULL),
22  happy_(true) {
23  if (strcmp(outputbase, "-") && strcmp(outputbase, "stdout")) {
24  STRING outfile = STRING(outputbase) + STRING(".") + STRING(file_extension_);
25  fout_ = fopen(outfile.string(), "wb");
26  if (fout_ == NULL) {
27  happy_ = false;
28  }
29  }
30 }
Definition: strngs.h:44
#define NULL
Definition: host.h:144
const char * string() const
Definition: strngs.cpp:193

Member Function Documentation

bool tesseract::TessResultRenderer::AddImage ( TessBaseAPI api)

Adds the recognized text from the source image to the current document. Invalid if BeginDocument not yet called.

Note that this API is a bit weird but is designed to fit into the current TessBaseAPI implementation where the api has lots of state information that we might want to add in.

Definition at line 64 of file renderer.cpp.

64  {
65  if (!happy_) return false;
66  ++imagenum_;
67  bool ok = AddImageHandler(api);
68  if (next_) {
69  ok = next_->AddImage(api) && ok;
70  }
71  return ok;
72 }
bool AddImage(TessBaseAPI *api)
Definition: renderer.cpp:64
virtual bool AddImageHandler(TessBaseAPI *api)=0
virtual bool tesseract::TessResultRenderer::AddImageHandler ( TessBaseAPI api)
protectedpure virtual
void tesseract::TessResultRenderer::AppendData ( const char *  s,
int  len 
)
protected

Definition at line 87 of file renderer.cpp.

87  {
88  int n = fwrite(s, 1, len, fout_);
89  if (n != len) happy_ = false;
90 }
void tesseract::TessResultRenderer::AppendString ( const char *  s)
protected

Definition at line 83 of file renderer.cpp.

83  {
84  AppendData(s, strlen(s));
85 }
void AppendData(const char *s, int len)
Definition: renderer.cpp:87
bool tesseract::TessResultRenderer::BeginDocument ( const char *  title)

Starts a new document with the given title. This clears the contents of the output data.

Definition at line 53 of file renderer.cpp.

53  {
54  if (!happy_) return false;
55  title_ = title;
56  imagenum_ = -1;
57  bool ok = BeginDocumentHandler();
58  if (next_) {
59  ok = next_->BeginDocument(title) && ok;
60  }
61  return ok;
62 }
const char * title() const
Definition: renderer.h:80
virtual bool BeginDocumentHandler()
Definition: renderer.cpp:92
bool BeginDocument(const char *title)
Definition: renderer.cpp:53
bool tesseract::TessResultRenderer::BeginDocumentHandler ( )
protectedvirtual

Reimplemented in tesseract::TessPDFRenderer, and tesseract::TessHOcrRenderer.

Definition at line 92 of file renderer.cpp.

92  {
93  return happy_;
94 }
bool tesseract::TessResultRenderer::EndDocument ( )

Finishes the document and finalizes the output data Invalid if BeginDocument not yet called.

Definition at line 74 of file renderer.cpp.

74  {
75  if (!happy_) return false;
76  bool ok = EndDocumentHandler();
77  if (next_) {
78  ok = next_->EndDocument() && ok;
79  }
80  return ok;
81 }
virtual bool EndDocumentHandler()
Definition: renderer.cpp:96
bool tesseract::TessResultRenderer::EndDocumentHandler ( )
protectedvirtual

Reimplemented in tesseract::TessPDFRenderer, and tesseract::TessHOcrRenderer.

Definition at line 96 of file renderer.cpp.

96  {
97  return happy_;
98 }
const char* tesseract::TessResultRenderer::file_extension ( ) const
inline

Definition at line 79 of file renderer.h.

79 { return file_extension_; }
int tesseract::TessResultRenderer::imagenum ( ) const
inline

Returns the index of the last image given to AddImage (i.e. images are incremented whether the image succeeded or not)

This is always defined. It means either the number of the current image, the last image ended, or in the completed document depending on when in the document lifecycle you are looking at it. Will return -1 if a document was never started.

Definition at line 91 of file renderer.h.

91 { return imagenum_; }
void tesseract::TessResultRenderer::insert ( TessResultRenderer next)

Definition at line 40 of file renderer.cpp.

40  {
41  if (next == NULL) return;
42 
43  TessResultRenderer* remainder = next_;
44  next_ = next;
45  if (remainder) {
46  while (next->next_ != NULL) {
47  next = next->next_;
48  }
49  next->next_ = remainder;
50  }
51 }
struct TessResultRenderer TessResultRenderer
Definition: capi.h:61
TessResultRenderer * next()
Definition: renderer.h:55
#define NULL
Definition: host.h:144
TessResultRenderer* tesseract::TessResultRenderer::next ( )
inline

Definition at line 55 of file renderer.h.

55 { return next_; }
const char* tesseract::TessResultRenderer::title ( ) const
inline

Definition at line 80 of file renderer.h.

80 { return title_; }

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