tesseract  3.03
/usr/local/google/home/jbreiden/tesseract-ocr-read-only/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 arent 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 
00076     bool AddError(TessBaseAPI* api);
00077 
00082     bool EndDocument();
00083 
00084     const char* full_typename() const { return full_typename_; }
00085     const char* file_extension() const { return file_extension_; }
00086     const char* title() const { return title_; }
00087 
00097     int imagenum() const { return imagenum_; }
00098 
00106     virtual bool GetOutput(const char** data, int* data_len) const;
00107 
00108   protected:
00112     TessResultRenderer(const char* type, const char* extension);
00113 
00114     // Hook for specialized handling in BeginDocument()
00115     virtual bool BeginDocumentHandler();
00116 
00117     // This must be overriden to render the OCR'd results
00118     virtual bool AddImageHandler(TessBaseAPI* api) = 0;
00119 
00120     // The default handler ignores the error and just returns true
00121     virtual bool AddErrorHandler(TessBaseAPI* api);
00122 
00123     // Hook for specialized handling in EndDocument()
00124     virtual bool EndDocumentHandler();
00125 
00126     // Clear output data.
00127     void ResetData();
00128 
00129     // Renderers can call this method to allocate data storage in advance,
00130     // which can cut down on allocations and copying. This isnt required,
00131     // and if used can still request less than will ultimately be used without
00132     // worrying about data corruption. It's purely performance.
00133     // Note that relative_len is in addition to what is already being used.
00134     void ReserveAdditionalData(int relative_len);
00135 
00136     // Renderers can call this to append '\0' terminated strings into
00137     // the output string returned by GetOutput.
00138     // This method will grow the output buffer if needed.
00139     void AppendString(const char* s);
00140 
00141     // Renderers can call this to append binary byte sequences into
00142     // the output string returned by GetOutput. Note that s is not necessarily
00143     // '\0' terminated (and can contain '\0' within it).
00144     // This method will grow the output buffer if needed.
00145     void AppendData(const char* s, int len);
00146 
00147   private:
00148     const char* full_typename_;   // name of renderer
00149     const char* file_extension_;  // standard extension for generated output
00150     const char* title_;           // title of document being renderered
00151     int imagenum_;                // index of last image added
00152 
00153     char* output_data_;           // output bytes
00154     int   output_alloc_;          // bytes allocated
00155     int   output_len_;            // bytes actually used
00156     TessResultRenderer* next_;    // Can link multiple renderers together.
00157 };
00158 
00162 class TESS_API TessTextRenderer : public TessResultRenderer {
00163  public:
00164   TessTextRenderer();
00165 
00166  protected:
00167   virtual bool AddImageHandler(TessBaseAPI* api);
00168 };
00169 
00173 class TESS_API TessHOcrRenderer : public TessResultRenderer {
00174  public:
00175   TessHOcrRenderer();
00176 
00177 protected:
00178   virtual bool BeginDocumentHandler();
00179   virtual bool AddImageHandler(TessBaseAPI* api);
00180   virtual bool EndDocumentHandler();
00181 };
00182 
00186 class TESS_API TessPDFRenderer : public TessResultRenderer {
00187  public:
00188   TessPDFRenderer(const char *datadir);
00189 
00190 protected:
00191   virtual bool BeginDocumentHandler();
00192   virtual bool AddImageHandler(TessBaseAPI* api);
00193   virtual bool EndDocumentHandler();
00194 
00195 private:
00196   // We don't want to have every image in memory at once,
00197   // so we store some metadata as we go along producing
00198   // PDFs one page at a time. At the end that metadata is
00199   // used to make everything that isn't easily handled in a
00200   // streaming fashion.
00201   long int obj_;                     // counter for PDF objects
00202   GenericVector<long int> offsets_;  // offset of every PDF object in bytes
00203   GenericVector<long int> pages_;    // object number for every /Page object
00204   const char *datadir_;              // where to find the custom font
00205   // Bookkeeping only. DIY = Do It Yourself.
00206   void AppendPDFObjectDIY(size_t objectsize);
00207   // Bookkeeping + emit data.
00208   void AppendPDFObject(const char *data);
00209   // Create the /Contents object for an entire page.
00210   static char* GetPDFTextObjects(TessBaseAPI* api,
00211                                  double width, double height,
00212                                  int page_number);
00213   // Attempt to create PFD object from an image without transcoding.
00214   static bool fileToPDFObj(char *filename, long int objnum,
00215                            char **pdf_object, long int *pdf_object_size);
00216   // Turn a Pix into a the very best PDF object that we can.
00217   static bool pixToPDFObj(Pix *pix, long int objnum,
00218                           char **pdf_object, long int *pdf_object_size);
00219 };
00220 
00221 
00225 class TESS_API TessUnlvRenderer : public TessResultRenderer {
00226  public:
00227   TessUnlvRenderer();
00228 
00229  protected:
00230   virtual bool AddImageHandler(TessBaseAPI* api);
00231 };
00232 
00236 class TESS_API TessBoxTextRenderer : public TessResultRenderer {
00237  public:
00238   TessBoxTextRenderer();
00239 
00240  protected:
00241   virtual bool AddImageHandler(TessBaseAPI* api);
00242 };
00243 
00244 }  // namespace tesseract.
00245 
00246 #endif  // TESSERACT_API_RENDERER_H__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines