Tesseract  3.02
tesseract-ocr/ccstruct/boxword.h
Go to the documentation of this file.
00001 
00002 // File:        boxword.h
00003 // Description: Class to represent the bounding boxes of the output.
00004 // Author:      Ray Smith
00005 // Created:     Tue May 25 14:18:14 PDT 2010
00006 //
00007 // (C) Copyright 2010, Google Inc.
00008 // Licensed under the Apache License, Version 2.0 (the "License");
00009 // you may not use this file except in compliance with the License.
00010 // You may obtain a copy of the License at
00011 // http://www.apache.org/licenses/LICENSE-2.0
00012 // Unless required by applicable law or agreed to in writing, software
00013 // distributed under the License is distributed on an "AS IS" BASIS,
00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 // See the License for the specific language governing permissions and
00016 // limitations under the License.
00017 //
00019 
00020 #ifndef TESSERACT_CSTRUCT_BOXWORD_H__
00021 #define TESSERACT_CSTRUCT_BOXWORD_H__
00022 
00023 #include "genericvector.h"
00024 #include "rect.h"
00025 
00026 class BLOCK;
00027 class DENORM;
00028 class PBLOB_LIST;
00029 struct TWERD;
00030 class UNICHARSET;
00031 class WERD;
00032 class WERD_CHOICE;
00033 class WERD_RES;
00034 
00035 namespace tesseract {
00036 
00037 // ScriptPos tells whether a character is subscript, superscript or normal.
00038 enum ScriptPos {
00039   SP_NORMAL,
00040   SP_SUBSCRIPT,
00041   SP_SUPERSCRIPT,
00042   SP_DROPCAP
00043 };
00044 
00045 // Class to hold an array of bounding boxes for an output word and
00046 // the bounding box of the whole word.
00047 class BoxWord {
00048  public:
00049   BoxWord();
00050   explicit BoxWord(const BoxWord& src);
00051   ~BoxWord();
00052 
00053   BoxWord& operator=(const BoxWord& src);
00054 
00055   void CopyFrom(const BoxWord& src);
00056 
00057   // Factory to build a BoxWord from a TWERD and the DENORM to switch
00058   // back to original image coordinates.
00059   // If the denorm is not NULL, then the output is denormalized and rotated
00060   // back to the original image coordinates.
00061   static BoxWord* CopyFromNormalized(const DENORM* denorm,
00062                                      TWERD* tessword);
00063 
00064   // Sets up the script_pos_ member using the tessword to get the bln
00065   // bounding boxes, the best_choice to get the unichars, and the unicharset
00066   // to get the target positions. If small_caps is true, sub/super are not
00067   // considered, but dropcaps are.
00068   void SetScriptPositions(const UNICHARSET& unicharset, bool small_caps,
00069                           TWERD* tessword, WERD_CHOICE* best_choice);
00070 
00071   // Clean up the bounding boxes from the polygonal approximation by
00072   // expanding slightly, then clipping to the blobs from the original_word
00073   // that overlap. If not null, the block provides the inverse rotation.
00074   void ClipToOriginalWord(const BLOCK* block, WERD* original_word);
00075 
00076   // Merges the boxes from start to end, not including end, and deletes
00077   // the boxes between start and end.
00078   void MergeBoxes(int start, int end);
00079 
00080   // Inserts a new box before the given index.
00081   // Recomputes the bounding box.
00082   void InsertBox(int index, const TBOX& box);
00083 
00084   // Deletes the box with the given index, and shuffles up the rest.
00085   // Recomputes the bounding box.
00086   void DeleteBox(int index);
00087 
00088   // Deletes all the boxes stored in BoxWord.
00089   void DeleteAllBoxes();
00090 
00091   // This and other putatively are the same, so call the (permanent) callback
00092   // for each blob index where the bounding boxes match.
00093   // The callback is deleted on completion.
00094   void ProcessMatchedBlobs(const TWERD& other, TessCallback1<int>* cb) const;
00095 
00096   const TBOX& bounding_box() const {
00097     return bbox_;
00098   }
00099   const int length() const {
00100     return length_;
00101   }
00102   const TBOX& BlobBox(int index) const {
00103     return boxes_[index];
00104   }
00105   ScriptPos BlobPosition(int index) const {
00106     if (index < 0 || index >= script_pos_.size())
00107       return SP_NORMAL;
00108     return script_pos_[index];
00109   }
00110 
00111  private:
00112   void ComputeBoundingBox();
00113 
00114   TBOX bbox_;
00115   int length_;
00116   GenericVector<TBOX> boxes_;
00117   GenericVector<ScriptPos> script_pos_;
00118 };
00119 
00120 }  // namespace tesseract.
00121 
00122 
00123 #endif  // TESSERACT_CSTRUCT_BOXWORD_H__