2013-09-21 03:39:59 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
// File: renderer.h
|
|
|
|
// Description: Rendering interface to inject into TessBaseAPI
|
|
|
|
//
|
|
|
|
// (C) Copyright 2011, Google Inc.
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-12-04 21:45:26 +08:00
|
|
|
#ifndef TESSERACT_API_RENDERER_H_
|
|
|
|
#define TESSERACT_API_RENDERER_H_
|
2013-09-21 03:39:59 +08:00
|
|
|
|
|
|
|
// To avoid collision with other typenames include the ABSOLUTE MINIMUM
|
|
|
|
// complexity of includes here. Use forward declarations wherever possible
|
|
|
|
// and hide includes of complex types in baseapi.cpp.
|
2014-01-10 01:58:55 +08:00
|
|
|
#include "genericvector.h"
|
2013-09-21 03:39:59 +08:00
|
|
|
#include "platform.h"
|
|
|
|
#include "publictypes.h"
|
|
|
|
|
|
|
|
namespace tesseract {
|
|
|
|
|
|
|
|
class TessBaseAPI;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
class TESS_API TessResultRenderer {
|
|
|
|
public:
|
|
|
|
virtual ~TessResultRenderer();
|
|
|
|
|
|
|
|
// Takes ownership of pointer so must be new'd instance.
|
2014-05-21 23:46:21 +08:00
|
|
|
// Renderers aren't ordered, but appends the sequences of next parameter
|
2013-09-21 03:39:59 +08:00
|
|
|
// and existing next(). The renderers should be unique across both lists.
|
|
|
|
void insert(TessResultRenderer* next);
|
|
|
|
|
|
|
|
// Returns the next renderer or NULL.
|
|
|
|
TessResultRenderer* next() { return next_; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts a new document with the given title.
|
|
|
|
* This clears the contents of the output data.
|
2017-04-03 01:01:16 +08:00
|
|
|
* Title should use UTF-8 encoding.
|
2013-09-21 03:39:59 +08:00
|
|
|
*/
|
|
|
|
bool BeginDocument(const char* title);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
bool AddImage(TessBaseAPI* api);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finishes the document and finalizes the output data
|
|
|
|
* Invalid if BeginDocument not yet called.
|
|
|
|
*/
|
|
|
|
bool EndDocument();
|
|
|
|
|
|
|
|
const char* file_extension() const { return file_extension_; }
|
2016-11-08 07:38:07 +08:00
|
|
|
const char* title() const { return title_.c_str(); }
|
2013-09-21 03:39:59 +08:00
|
|
|
|
|
|
|
/**
|
2014-05-21 23:46:21 +08:00
|
|
|
* Returns the index of the last image given to AddImage
|
2013-09-21 03:39:59 +08:00
|
|
|
* (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.
|
|
|
|
*/
|
|
|
|
int imagenum() const { return imagenum_; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/**
|
2014-05-21 23:46:21 +08:00
|
|
|
* 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.
|
2013-09-21 03:39:59 +08:00
|
|
|
*/
|
2014-05-21 23:46:21 +08:00
|
|
|
TessResultRenderer(const char *outputbase,
|
|
|
|
const char* extension);
|
2013-09-21 03:39:59 +08:00
|
|
|
|
|
|
|
// Hook for specialized handling in BeginDocument()
|
|
|
|
virtual bool BeginDocumentHandler();
|
|
|
|
|
|
|
|
// This must be overriden to render the OCR'd results
|
|
|
|
virtual bool AddImageHandler(TessBaseAPI* api) = 0;
|
|
|
|
|
|
|
|
// Hook for specialized handling in EndDocument()
|
|
|
|
virtual bool EndDocumentHandler();
|
|
|
|
|
|
|
|
// Renderers can call this to append '\0' terminated strings into
|
|
|
|
// the output string returned by GetOutput.
|
|
|
|
// This method will grow the output buffer if needed.
|
|
|
|
void AppendString(const char* s);
|
|
|
|
|
|
|
|
// Renderers can call this to append binary byte sequences into
|
|
|
|
// the output string returned by GetOutput. Note that s is not necessarily
|
|
|
|
// '\0' terminated (and can contain '\0' within it).
|
|
|
|
// This method will grow the output buffer if needed.
|
|
|
|
void AppendData(const char* s, int len);
|
|
|
|
|
|
|
|
private:
|
|
|
|
const char* file_extension_; // standard extension for generated output
|
2016-11-08 07:38:07 +08:00
|
|
|
STRING title_; // title of document being renderered
|
2013-09-21 03:39:59 +08:00
|
|
|
int imagenum_; // index of last image added
|
|
|
|
|
2014-05-21 23:46:21 +08:00
|
|
|
FILE* fout_; // output file pointer
|
|
|
|
TessResultRenderer* next_; // Can link multiple renderers together
|
|
|
|
bool happy_; // I get grumpy when the disk fills up, etc.
|
2013-09-21 03:39:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders tesseract output into a plain UTF-8 text string
|
|
|
|
*/
|
|
|
|
class TESS_API TessTextRenderer : public TessResultRenderer {
|
|
|
|
public:
|
2014-05-21 23:46:21 +08:00
|
|
|
explicit TessTextRenderer(const char *outputbase);
|
2013-09-21 03:39:59 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual bool AddImageHandler(TessBaseAPI* api);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders tesseract output into an hocr text string
|
|
|
|
*/
|
|
|
|
class TESS_API TessHOcrRenderer : public TessResultRenderer {
|
|
|
|
public:
|
2014-08-04 00:22:12 +08:00
|
|
|
explicit TessHOcrRenderer(const char *outputbase, bool font_info);
|
2014-05-21 23:46:21 +08:00
|
|
|
explicit TessHOcrRenderer(const char *outputbase);
|
2013-09-21 03:39:59 +08:00
|
|
|
|
2016-11-08 02:46:33 +08:00
|
|
|
protected:
|
2013-09-21 03:39:59 +08:00
|
|
|
virtual bool BeginDocumentHandler();
|
|
|
|
virtual bool AddImageHandler(TessBaseAPI* api);
|
|
|
|
virtual bool EndDocumentHandler();
|
2014-08-04 00:22:12 +08:00
|
|
|
|
2016-11-08 02:46:33 +08:00
|
|
|
private:
|
|
|
|
bool font_info_; // whether to print font information
|
2013-09-21 03:39:59 +08:00
|
|
|
};
|
|
|
|
|
2014-08-19 13:59:26 +08:00
|
|
|
/**
|
2016-03-02 02:41:19 +08:00
|
|
|
* Renders Tesseract output into a TSV string
|
2014-08-19 13:59:26 +08:00
|
|
|
*/
|
2016-03-02 02:41:19 +08:00
|
|
|
class TESS_API TessTsvRenderer : public TessResultRenderer {
|
2014-08-19 13:59:26 +08:00
|
|
|
public:
|
2016-11-08 02:46:33 +08:00
|
|
|
explicit TessTsvRenderer(const char* outputbase, bool font_info);
|
|
|
|
explicit TessTsvRenderer(const char* outputbase);
|
2014-08-19 13:59:26 +08:00
|
|
|
|
2016-11-08 02:46:33 +08:00
|
|
|
protected:
|
2014-08-19 13:59:26 +08:00
|
|
|
virtual bool BeginDocumentHandler();
|
|
|
|
virtual bool AddImageHandler(TessBaseAPI* api);
|
|
|
|
virtual bool EndDocumentHandler();
|
|
|
|
|
2016-11-08 02:46:33 +08:00
|
|
|
private:
|
2014-08-19 13:59:26 +08:00
|
|
|
bool font_info_; // whether to print font information
|
|
|
|
};
|
|
|
|
|
2014-01-10 01:58:55 +08:00
|
|
|
/**
|
|
|
|
* Renders tesseract output into searchable PDF
|
|
|
|
*/
|
|
|
|
class TESS_API TessPDFRenderer : public TessResultRenderer {
|
|
|
|
public:
|
2014-05-21 23:46:21 +08:00
|
|
|
// datadir is the location of the TESSDATA. We need it because
|
|
|
|
// we load a custom PDF font from this location.
|
2018-01-11 16:11:30 +08:00
|
|
|
TessPDFRenderer(const char* outputbase, const char* datadir, bool textonly, int jpg_quality);
|
2014-01-10 01:58:55 +08:00
|
|
|
|
2016-11-08 02:46:33 +08:00
|
|
|
protected:
|
2014-01-10 01:58:55 +08:00
|
|
|
virtual bool BeginDocumentHandler();
|
|
|
|
virtual bool AddImageHandler(TessBaseAPI* api);
|
|
|
|
virtual bool EndDocumentHandler();
|
|
|
|
|
2016-11-08 02:46:33 +08:00
|
|
|
private:
|
2014-01-10 01:58:55 +08:00
|
|
|
// We don't want to have every image in memory at once,
|
|
|
|
// so we store some metadata as we go along producing
|
2017-01-21 04:26:34 +08:00
|
|
|
// PDFs one page at a time. At the end, that metadata is
|
2014-01-10 01:58:55 +08:00
|
|
|
// used to make everything that isn't easily handled in a
|
|
|
|
// streaming fashion.
|
|
|
|
long int obj_; // counter for PDF objects
|
|
|
|
GenericVector<long int> offsets_; // offset of every PDF object in bytes
|
|
|
|
GenericVector<long int> pages_; // object number for every /Page object
|
|
|
|
const char *datadir_; // where to find the custom font
|
2017-01-21 04:26:34 +08:00
|
|
|
bool textonly_; // skip images if set
|
2018-01-11 16:11:30 +08:00
|
|
|
int jpg_quality_; // set JPEG quality
|
2014-01-10 01:58:55 +08:00
|
|
|
// Bookkeeping only. DIY = Do It Yourself.
|
|
|
|
void AppendPDFObjectDIY(size_t objectsize);
|
|
|
|
// Bookkeeping + emit data.
|
|
|
|
void AppendPDFObject(const char *data);
|
|
|
|
// Create the /Contents object for an entire page.
|
2017-01-21 04:26:34 +08:00
|
|
|
char* GetPDFTextObjects(TessBaseAPI* api, double width, double height);
|
2014-08-12 07:15:25 +08:00
|
|
|
// Turn an image into a PDF object. Only transcode if we have to.
|
2014-10-08 05:37:34 +08:00
|
|
|
static bool imageToPDFObj(Pix *pix, char *filename, long int objnum,
|
2018-01-11 16:11:30 +08:00
|
|
|
char **pdf_object, long int *pdf_object_size, int jpg_quality);
|
2014-01-10 01:58:55 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-09-21 03:39:59 +08:00
|
|
|
/**
|
|
|
|
* Renders tesseract output into a plain UTF-8 text string
|
|
|
|
*/
|
|
|
|
class TESS_API TessUnlvRenderer : public TessResultRenderer {
|
|
|
|
public:
|
2014-05-21 23:46:21 +08:00
|
|
|
explicit TessUnlvRenderer(const char *outputbase);
|
2013-09-21 03:39:59 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual bool AddImageHandler(TessBaseAPI* api);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders tesseract output into a plain UTF-8 text string
|
|
|
|
*/
|
|
|
|
class TESS_API TessBoxTextRenderer : public TessResultRenderer {
|
|
|
|
public:
|
2014-05-21 23:46:21 +08:00
|
|
|
explicit TessBoxTextRenderer(const char *outputbase);
|
2013-09-21 03:39:59 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual bool AddImageHandler(TessBaseAPI* api);
|
|
|
|
};
|
|
|
|
|
2015-10-31 02:09:00 +08:00
|
|
|
/**
|
|
|
|
* Renders tesseract output into an osd text string
|
|
|
|
*/
|
|
|
|
class TESS_API TessOsdRenderer : public TessResultRenderer {
|
|
|
|
public:
|
|
|
|
explicit TessOsdRenderer(const char* outputbase);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual bool AddImageHandler(TessBaseAPI* api);
|
|
|
|
};
|
|
|
|
|
2013-09-21 03:39:59 +08:00
|
|
|
} // namespace tesseract.
|
|
|
|
|
2016-12-04 21:45:26 +08:00
|
|
|
#endif // TESSERACT_API_RENDERER_H_
|