ccstruct/ocrblock.cpp

Go to the documentation of this file.
00001 
00020 #include "mfcpch.h"
00021 #include          <stdlib.h>
00022 #include          "blckerr.h"
00023 #include          "ocrblock.h"
00024 #include          "tprintf.h"
00025 
00026 #define BLOCK_LABEL_HEIGHT  150  //char height of block id
00027 
00031 ELISTIZE_S (BLOCK)
00032 BLOCK::BLOCK (                   //rectangular block
00033 const char *name,                //filename
00034 BOOL8 prop,                      //proportional
00035 INT16 kern,                      //kerning
00036 INT16 space,                     //spacing
00037 INT16 xmin,                      //bottom left
00038 INT16 ymin, INT16 xmax,          //top right
00039 INT16 ymax):
00040 PDBLK (xmin, ymin, xmax, ymax),
00041 filename(name) {  //box(ICOORD(xmin,ymin),ICOORD(xmax,ymax))
00042                                  //boundaries
00043   ICOORDELT_IT left_it = &leftside;
00044   ICOORDELT_IT right_it = &rightside;
00045 
00046   proportional = prop;
00047   kerning = kern;
00048   spacing = space;
00049   font_class = -1;               //not assigned
00050   hand_block = NULL;
00051   hand_poly = NULL;
00052   left_it.set_to_list (&leftside);
00053   right_it.set_to_list (&rightside);
00054                                  //make default box
00055   left_it.add_to_end (new ICOORDELT (xmin, ymin));
00056   left_it.add_to_end (new ICOORDELT (xmin, ymax));
00057   right_it.add_to_end (new ICOORDELT (xmax, ymin));
00058   right_it.add_to_end (new ICOORDELT (xmax, ymax));
00059 }
00060 
00061 
00062 /* ==================
00063  * BLOCK::set_sides
00064  *
00065  * Sets left and right vertex lists
00066  ==================== */
00067 
00068 //void                                                          BLOCK::set_sides(                       //set vertex lists
00069 //ICOORDELT_LIST             *left,                        //left vertices
00070 //ICOORDELT_LIST             *right                        //right vertices
00071 //)
00072 //{
00073 //      ICOORDELT_IT                            left_it= &leftside;                             //boundaries
00074 //      ICOORDELT_IT                            right_it= &rightside;
00075 
00076 //      leftside.clear();
00077 //      left_it.move_to_first();
00078 //      left_it.add_list_before(left);
00079 //      rightside.clear();
00080 //      right_it.move_to_first();
00081 //      right_it.add_list_before(right);
00082 //}
00083 
00084 /* ==================
00085  * BLOCK::contains
00086  *
00087  * Return TRUE if the given point is within the block.
00088  ==================== */
00089 
00090 //BOOL8                                                         BLOCK::contains(                                        //test containment
00091 //ICOORD                                                        pt                                                                              //point to test
00092 //)
00093 //{
00094 //      BLOCK_RECT_IT                           it=this;                                                                //rectangle iterator
00095 //      ICOORD                                          bleft,tright;                                           //corners of rectangle
00096 
00097 //      for (it.start_block();!it.cycled_rects();it.forward())
00098 //      {
00099 //              it.bounding_box(bleft,tright);                                                  //get rectangle
00100 //              if (pt.x()>=bleft.x() && pt.x()<=tright.x()                     //inside rect
00101 //              && pt.y()>=bleft.y() && pt.y()<=tright.y())
00102 //                      return TRUE;                                                                                            //is inside
00103 //      }
00104 //      return FALSE;                                                                                                           //not inside
00105 //}
00106 
00107 /* ==================
00108  * BLOCK::move
00109  *
00110  * Reposition block
00111  ==================== */
00112 
00113 //void                                                     BLOCK::move(                                         // reposition block
00114 //const ICOORD                                  vec                                                                     // by vector
00115 //)
00116 //{
00117 //   ROW_IT                                             row_it( &rows );
00118 //      ICOORDELT_IT                            it( &leftside );
00119 
00120 //      for( row_it.mark_cycle_pt(); !row_it.cycled_list(); row_it.forward() )
00121 //         row_it.data()->move( vec );
00122 
00123 //      for( it.mark_cycle_pt(); !it.cycled_list(); it.forward() )
00124 //         *(it.data()) += vec;
00125 
00126 //   it.set_to_list( &rightside );
00127 
00128 //   for( it.mark_cycle_pt(); !it.cycled_list(); it.forward() )
00129 //         *(it.data()) += vec;
00130 
00131 //   box.move( vec );
00132 //}
00133 
00137 int decreasing_top_order(  //
00138                          const void *row1,
00139                          const void *row2) {
00140   return (*(ROW **) row2)->bounding_box ().top () -
00141     (*(ROW **) row1)->bounding_box ().top ();
00142 }
00143 
00144 
00150 void BLOCK::sort_rows() {  // order on "top"
00151   ROW_IT row_it(&rows); 
00152 
00153   row_it.sort (decreasing_top_order);
00154 }
00155 
00156 
00163 void BLOCK::compress() {  // squash it up
00164   #define           ROW_SPACING 5
00165 
00166   ROW_IT row_it(&rows); 
00167   ROW *row;
00168   ICOORD row_spacing (0, ROW_SPACING);
00169 
00170   ICOORDELT_IT icoordelt_it;
00171 
00172   sort_rows(); 
00173 
00174   box = BOX (box.topleft (), box.topleft ());
00175   box.move_bottom_edge (ROW_SPACING);
00176   for (row_it.mark_cycle_pt (); !row_it.cycled_list (); row_it.forward ()) {
00177     row = row_it.data ();
00178     row->move (box.botleft () - row_spacing -
00179       row->bounding_box ().topleft ());
00180     box += row->bounding_box ();
00181   }
00182 
00183   leftside.clear ();
00184   icoordelt_it.set_to_list (&leftside);
00185   icoordelt_it.add_to_end (new ICOORDELT (box.left (), box.bottom ()));
00186   icoordelt_it.add_to_end (new ICOORDELT (box.left (), box.top ()));
00187   rightside.clear ();
00188   icoordelt_it.set_to_list (&rightside);
00189   icoordelt_it.add_to_end (new ICOORDELT (box.right (), box.bottom ()));
00190   icoordelt_it.add_to_end (new ICOORDELT (box.right (), box.top ()));
00191 }
00192 
00193 
00200 void BLOCK::check_pitch() {  // check prop
00201   //      tprintf("Missing FFT fixed pitch stuff!\n");
00202   pitch = -1;
00203 }
00204 
00205 
00211 void BLOCK::compress(                  // squash it up
00212                      const ICOORD vec  // and move
00213                     ) {
00214   box.move (vec);
00215   compress(); 
00216 }
00217 
00218 
00222 void BLOCK::print(            //print list of sides
00223                   FILE *,     //file to print on
00224                   BOOL8 dump  //print full detail
00225                  ) {
00226   ICOORDELT_IT it = &leftside;   //iterator
00227 
00228   box.print ();
00229   tprintf ("Proportional= %s\n", proportional ? "TRUE" : "FALSE");
00230   tprintf ("Kerning= %d\n", kerning);
00231   tprintf ("Spacing= %d\n", spacing);
00232   tprintf ("Fixed_pitch=%d\n", pitch);
00233   tprintf ("Filename= %s\n", filename.string ());
00234 
00235   if (dump) {
00236     tprintf ("Left side coords are:\n");
00237     for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ())
00238       tprintf ("(%d,%d) ", it.data ()->x (), it.data ()->y ());
00239     tprintf ("\n");
00240     tprintf ("Right side coords are:\n");
00241     it.set_to_list (&rightside);
00242     for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ())
00243       tprintf ("(%d,%d) ", it.data ()->x (), it.data ()->y ());
00244     tprintf ("\n");
00245   }
00246 }
00247 
00248 
00249 /* ==================
00250  * BLOCK::plot
00251  *
00252  * Plot the outline of a block in the given colour.
00253  ==================== */
00254 
00255 //void        BLOCK::plot(                      //draw outline
00256 //WINDOW      window,                           //window to draw in
00257 //INT32       serial,                           //serial number
00258 //COLOUR      colour                            //colour to draw in
00259 //)
00260 //{
00261 //      ICOORD                 startpt;         //start of outline
00262 //      ICOORD                 endpt;           //end of outline
00263 //      ICOORD                 prevpt;          //previous point
00264 //      ICOORDELT_IT        it= &leftside;      //iterator
00265 //      char                  number[32];       //block id
00266 //
00267 //      line_color_index(window,colour);        //set the colour
00268 //      text_color_index(window,colour);
00269 //      character_height(window,(float)BLOCK_LABEL_HEIGHT);
00270 //      text_font_index(window,6);
00271 //
00272 //      if (!leftside.empty())
00273 //      {
00274 //         startpt= *(it.data());         //bottom left corner
00277 //         sprintf(number,"%d",serial);
00278 //         text2d(window,startpt.x(),startpt.y(),number,0,FALSE);
00279 //
00280 //         move2d(window,startpt.x(),startpt.y());
00281 //         do
00282 //         {
00283 //            prevpt= *(it.data());    //previous point
00284 //            it.forward();            //move to next point
00285 //            draw2d(window,prevpt.x(),it.data()->y());    //draw round corner
00286 //            draw2d(window,it.data()->x(),it.data()->y());
00287 //         }
00288 //         while (!it.at_last());        //until end of list
00289 //         endpt= *(it.data());                                                                                    //end point
00290 
00291 //         move2d(window,startpt.x(),startpt.y());     //other side of boundary
00292 //         it.set_to_list(&rightside);
00293 //         prevpt=startpt;
00294 //         for (it.mark_cycle_pt();!it.cycled_list();it.forward())
00295 //         {
00296 //            draw2d(window,prevpt.x(),it.data()->y());  //draw round corner
00297 //            draw2d(window,it.data()->x(),it.data()->y());
00298 //            prevpt= *(it.data());                      //previous point
00299 //         }
00300 //         draw2d(window,endpt.x(),endpt.y());           //close boundary
00301 //         if (hand_block!=NULL)
00302 //            hand_block->plot(window,colour,serial);
00303 //      }
00304 //}
00305 
00306 /* ==================
00307  * BLOCK::show
00308  *
00309  * Show the image corresponding to a block as its set of rectangles.
00310  ==================== */
00311 
00312 //void        BLOCK::show(    //show image block
00313 //IMAGE       *image,                 //image to show
00314 //WINDOW      window                  //window to show in
00315 //)
00316 //{
00317 //      BLOCK_RECT_IT       it=this;              //rectangle iterator
00318 //      ICOORD              bleft,tright;         //corners of rectangle
00319 
00320 //      for (it.start_block();!it.cycled_rects();it.forward())
00321 //      {
00322 //              it.bounding_box(bleft,tright);    //get rectangle
00325 //              show_sub_image(image,bleft.x(),bleft.y(),
00326 //                      tright.x()-bleft.x(),tright.y()-bleft.y(),
00327 //                      window,bleft.x(),bleft.y());  //show it
00328 //      }
00329 //}
00330 
00336 BLOCK & BLOCK::operator= (       //assignment
00337 const BLOCK & source             //from this
00338 ) {
00339   this->ELIST_LINK::operator= (source);
00340   this->PDBLK::operator= (source);
00341   proportional = source.proportional;
00342   kerning = source.kerning;
00343   spacing = source.spacing;
00344   filename = source.filename;    //STRINGs assign ok
00345   if (!rows.empty ())
00346     rows.clear ();
00347   //   if ( !leftside.empty() )
00348   //              leftside.clear();
00349   // if ( !rightside.empty() )
00350   //              rightside.clear();
00351   //   leftside.deep_copy( &source.leftside );
00352   //   rightside.deep_copy( &source.rightside );
00353   //      box=source.box;
00354   return *this;
00355 }

Generated on Wed Feb 28 19:49:08 2007 for Tesseract by  doxygen 1.5.1