tesseract 3.04.01

api/renderer.h

Go to the documentation of this file.
00001 
00002 // File:        renderer.h
00003 // Description: Rendering interface to inject into TessBaseAPI
00004 //
00005 // (C) Copyright 2011, Google Inc.
00006 // Licensed under the Apache License, Version 2.0 (the "License");
00007 // you may not use this file except in compliance with the License.
00008 // You may obtain a copy of the License at
00009 // http://www.apache.org/licenses/LICENSE-2.0
00010 // Unless required by applicable law or agreed to in writing, software
00011 // distributed under the License is distributed on an "AS IS" BASIS,
00012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 // See the License for the specific language governing permissions and
00014 // limitations under the License.
00015 //
00017 
00018 #ifndef TESSERACT_API_RENDERER_H__
00019 #define TESSERACT_API_RENDERER_H__
00020 
00021 // To avoid collision with other typenames include the ABSOLUTE MINIMUM
00022 // complexity of includes here. Use forward declarations wherever possible
00023 // and hide includes of complex types in baseapi.cpp.
00024 #include "genericvector.h"
00025 #include "platform.h"
00026 #include "publictypes.h"
00027 
00028 namespace tesseract {
00029 
00030 class TessBaseAPI;
00031 
00045 class TESS_API TessResultRenderer {
00046   public:
00047     virtual ~TessResultRenderer();
00048 
00049     // Takes ownership of pointer so must be new'd instance.
00050     // Renderers aren't ordered, but appends the sequences of next parameter
00051     // and existing next(). The renderers should be unique across both lists.
00052     void insert(TessResultRenderer* next);
00053 
00054     // Returns the next renderer or NULL.
00055     TessResultRenderer* next() { return next_; }
00056 
00061     bool BeginDocument(const char* title);
00062 
00071     bool AddImage(TessBaseAPI* api);
00072 
00077     bool EndDocument();
00078 
00079     const char* file_extension() const { return file_extension_; }
00080     const char* title() const { return title_; }
00081 
00091     int imagenum() const { return imagenum_; }
00092 
00093   protected:
00104     TessResultRenderer(const char *outputbase,
00105                        const char* extension);
00106 
00107     // Hook for specialized handling in BeginDocument()
00108     virtual bool BeginDocumentHandler();
00109 
00110     // This must be overriden to render the OCR'd results
00111     virtual bool AddImageHandler(TessBaseAPI* api) = 0;
00112 
00113     // Hook for specialized handling in EndDocument()
00114     virtual bool EndDocumentHandler();
00115 
00116     // Renderers can call this to append '\0' terminated strings into
00117     // the output string returned by GetOutput.
00118     // This method will grow the output buffer if needed.
00119     void AppendString(const char* s);
00120 
00121     // Renderers can call this to append binary byte sequences into
00122     // the output string returned by GetOutput. Note that s is not necessarily
00123     // '\0' terminated (and can contain '\0' within it).
00124     // This method will grow the output buffer if needed.
00125     void AppendData(const char* s, int len);
00126 
00127   private:
00128     const char* file_extension_;  // standard extension for generated output
00129     const char* title_;           // title of document being renderered
00130     int imagenum_;                // index of last image added
00131 
00132     FILE* fout_;                  // output file pointer
00133     TessResultRenderer* next_;    // Can link multiple renderers together
00134     bool happy_;                  // I get grumpy when the disk fills up, etc.
00135 };
00136 
00140 class TESS_API TessTextRenderer : public TessResultRenderer {
00141  public:
00142   explicit TessTextRenderer(const char *outputbase);
00143 
00144  protected:
00145   virtual bool AddImageHandler(TessBaseAPI* api);
00146 };
00147 
00151 class TESS_API TessHOcrRenderer : public TessResultRenderer {
00152  public:
00153   explicit TessHOcrRenderer(const char *outputbase, bool font_info);
00154   explicit TessHOcrRenderer(const char *outputbase);
00155 
00156 protected:
00157   virtual bool BeginDocumentHandler();
00158   virtual bool AddImageHandler(TessBaseAPI* api);
00159   virtual bool EndDocumentHandler();
00160 
00161 private:
00162   bool font_info_;              // whether to print font information
00163 };
00164 
00168 class TESS_API TessPDFRenderer : public TessResultRenderer {
00169  public:
00170   // datadir is the location of the TESSDATA. We need it because
00171   // we load a custom PDF font from this location.
00172   TessPDFRenderer(const char *outputbase, const char *datadir);
00173 
00174 protected:
00175   virtual bool BeginDocumentHandler();
00176   virtual bool AddImageHandler(TessBaseAPI* api);
00177   virtual bool EndDocumentHandler();
00178 
00179 private:
00180   // We don't want to have every image in memory at once,
00181   // so we store some metadata as we go along producing
00182   // PDFs one page at a time. At the end that metadata is
00183   // used to make everything that isn't easily handled in a
00184   // streaming fashion.
00185   long int obj_;                     // counter for PDF objects
00186   GenericVector<long int> offsets_;  // offset of every PDF object in bytes
00187   GenericVector<long int> pages_;    // object number for every /Page object
00188   const char *datadir_;              // where to find the custom font
00189   // Bookkeeping only. DIY = Do It Yourself.
00190   void AppendPDFObjectDIY(size_t objectsize);
00191   // Bookkeeping + emit data.
00192   void AppendPDFObject(const char *data);
00193   // Create the /Contents object for an entire page.
00194   static char* GetPDFTextObjects(TessBaseAPI* api,
00195                                  double width, double height);
00196   // Turn an image into a PDF object. Only transcode if we have to.
00197   static bool imageToPDFObj(Pix *pix, char *filename, long int objnum,
00198                           char **pdf_object, long int *pdf_object_size);
00199 };
00200 
00201 
00205 class TESS_API TessUnlvRenderer : public TessResultRenderer {
00206  public:
00207   explicit TessUnlvRenderer(const char *outputbase);
00208 
00209  protected:
00210   virtual bool AddImageHandler(TessBaseAPI* api);
00211 };
00212 
00216 class TESS_API TessBoxTextRenderer : public TessResultRenderer {
00217  public:
00218   explicit TessBoxTextRenderer(const char *outputbase);
00219 
00220  protected:
00221   virtual bool AddImageHandler(TessBaseAPI* api);
00222 };
00223 
00227 class TESS_API TessOsdRenderer : public TessResultRenderer {
00228  public:
00229   explicit TessOsdRenderer(const char* outputbase);
00230 
00231  protected:
00232   virtual bool AddImageHandler(TessBaseAPI* api);
00233 };
00234 
00235 }  // namespace tesseract.
00236 
00237 #endif  // TESSERACT_API_RENDERER_H__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines