Tesseract  3.02
tesseract-ocr/ccstruct/ocrblock.h
Go to the documentation of this file.
00001 /**********************************************************************
00002  * File:        ocrblock.h  (Formerly block.h)
00003  * Description: Page block class definition.
00004  * Author:              Ray Smith
00005  * Created:             Thu Mar 14 17:32:01 GMT 1991
00006  *
00007  * (C) Copyright 1991, Hewlett-Packard Ltd.
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  *
00018  **********************************************************************/
00019 
00020 #ifndef           OCRBLOCK_H
00021 #define           OCRBLOCK_H
00022 
00023 #include          "img.h"
00024 #include          "ocrpara.h"
00025 #include          "ocrrow.h"
00026 #include          "pdblock.h"
00027 
00028 class BLOCK;                     //forward decl
00029 
00030 ELISTIZEH (BLOCK)
00031 class BLOCK:public ELIST_LINK, public PDBLK
00032 //page block
00033 {
00034   friend class BLOCK_RECT_IT;    //block iterator
00035 
00036  public:
00037   BLOCK()
00038     : re_rotation_(1.0f, 0.0f),
00039       classify_rotation_(1.0f, 0.0f),
00040       skew_(1.0f, 0.0f) {
00041     right_to_left_ = false;
00042     hand_poly = NULL;
00043   }
00044   BLOCK(const char *name,  //< filename
00045         BOOL8 prop,        //< proportional
00046         inT16 kern,        //< kerning
00047         inT16 space,       //< spacing
00048         inT16 xmin,        //< bottom left
00049         inT16 ymin,
00050         inT16 xmax,        //< top right
00051         inT16 ymax);
00052 
00053   ~BLOCK () {
00054   }
00055 
00063   void set_stats(BOOL8 prop,
00064                  inT16 kern,
00065                  inT16 space,
00066                  inT16 ch_pitch) {
00067     proportional = prop;
00068     kerning = (inT8) kern;
00069     spacing = space;
00070     pitch = ch_pitch;
00071   }
00073   void set_xheight(inT32 height) {
00074     xheight = height;
00075   }
00077   void set_font_class(inT16 font) {
00078     font_class = font;
00079   }
00081   BOOL8 prop() const {
00082     return proportional;
00083   }
00084   bool right_to_left() const {
00085     return right_to_left_;
00086   }
00087   void set_right_to_left(bool value) {
00088     right_to_left_ = value;
00089   }
00091   inT32 fixed_pitch() const {
00092     return pitch;
00093   }
00095   inT16 kern() const {
00096     return kerning;
00097   }
00099   inT16 font() const {
00100     return font_class;
00101   }
00103   inT16 space() const {
00104     return spacing;
00105   }
00107   const char *name() const {
00108     return filename.string ();
00109   }
00111   inT32 x_height() const {
00112     return xheight;
00113   }
00114   float cell_over_xheight() const {
00115     return cell_over_xheight_;
00116   }
00117   void set_cell_over_xheight(float ratio) {
00118     cell_over_xheight_ = ratio;
00119   }
00121   ROW_LIST *row_list() {
00122     return &rows;
00123   }
00124   // Compute the margins between the edges of each row and this block's
00125   // polyblock, and store the results in the rows.
00126   void compute_row_margins();
00127 
00128   // get paragraphs
00129   PARA_LIST *para_list() {
00130     return &paras_;
00131   }
00133   C_BLOB_LIST *blob_list() {
00134     return &c_blobs;
00135   }
00136   C_BLOB_LIST *reject_blobs() {
00137     return &rej_blobs;
00138   }
00139   FCOORD re_rotation() const {
00140     return re_rotation_;         // How to transform coords back to image.
00141   }
00142   void set_re_rotation(const FCOORD& rotation) {
00143     re_rotation_ = rotation;
00144   }
00145   FCOORD classify_rotation() const {
00146     return classify_rotation_;   // Apply this before classifying.
00147   }
00148   void set_classify_rotation(const FCOORD& rotation) {
00149     classify_rotation_ = rotation;
00150   }
00151   FCOORD skew() const {
00152     return skew_;                // Direction of true horizontal.
00153   }
00154   void set_skew(const FCOORD& skew) {
00155     skew_ = skew;
00156   }
00157   const ICOORD& median_size() const {
00158     return median_size_;
00159   }
00160   void set_median_size(int x, int y) {
00161     median_size_.set_x(x);
00162     median_size_.set_y(y);
00163   }
00164 
00165   Pix* render_mask() {
00166     return PDBLK::render_mask(re_rotation_);
00167   }
00168 
00169   // Reflects the polygon in the y-axis and recomputes the bounding_box.
00170   // Does nothing to any contained rows/words/blobs etc.
00171   void reflect_polygon_in_y_axis();
00172 
00173   void rotate(const FCOORD& rotation);
00174 
00176   void sort_rows();
00177 
00179   void compress();
00180 
00182   void check_pitch();
00183 
00185   void compress(const ICOORD vec);
00186 
00188   void print(FILE *fp, BOOL8 dump);
00189 
00190   BLOCK& operator=(const BLOCK & source);
00191 
00192  private:
00193   BOOL8 proportional;          //< proportional
00194   bool right_to_left_;         //< major script is right to left.
00195   inT8 kerning;                //< inter blob gap
00196   inT16 spacing;               //< inter word gap
00197   inT16 pitch;                 //< pitch of non-props
00198   inT16 font_class;            //< correct font class
00199   inT32 xheight;               //< height of chars
00200   float cell_over_xheight_;    //< Ratio of cell height to xheight.
00201   STRING filename;             //< name of block
00202   ROW_LIST rows;               //< rows in block
00203   PARA_LIST paras_;            //< paragraphs of block
00204   C_BLOB_LIST c_blobs;         //< before textord
00205   C_BLOB_LIST rej_blobs;       //< duff stuff
00206   FCOORD re_rotation_;         //< How to transform coords back to image.
00207   FCOORD classify_rotation_;   //< Apply this before classifying.
00208   FCOORD skew_;                //< Direction of true horizontal.
00209   ICOORD median_size_;         //< Median size of blobs.
00210 };
00211 
00212 int decreasing_top_order(const void *row1, const void *row2);
00213 
00214 // A function to print segmentation stats for the given block list.
00215 void PrintSegmentationStats(BLOCK_LIST* block_list);
00216 
00217 // Extracts blobs fromo the given block list and adds them to the output list.
00218 // The block list must have been created by performing a page segmentation.
00219 void ExtractBlobsFromSegmentation(BLOCK_LIST* blocks,
00220                                   C_BLOB_LIST* output_blob_list);
00221 
00222 // Refreshes the words in the block_list by using blobs in the
00223 // new_blobs list.
00224 // Block list must have word segmentation in it.
00225 // It consumes the blobs provided in the new_blobs list. The blobs leftover in
00226 // the new_blobs list after the call weren't matched to any blobs of the words
00227 // in block list.
00228 // The output not_found_blobs is a list of blobs from the original segmentation
00229 // in the block_list for which no corresponding new blobs were found.
00230 void RefreshWordBlobsFromNewBlobs(BLOCK_LIST* block_list,
00231                                   C_BLOB_LIST* new_blobs,
00232                                   C_BLOB_LIST* not_found_blobs);
00233 
00234 #endif