ccstruct/blobs.cpp

Go to the documentation of this file.
00001 
00021 /*----------------------------------------------------------------------
00022               I n c l u d e s
00023 ----------------------------------------------------------------------*/
00024 #include "mfcpch.h"
00025 #include "blobs.h"
00026 #include "cutil.h"
00027 #include "emalloc.h"
00028 #include "structures.h"
00029 
00030 /*----------------------------------------------------------------------
00031               F u n c t i o n s
00032 ----------------------------------------------------------------------*/
00037 void blob_origin(TBLOB *blob,       /*blob to compute on */
00038                  TPOINT *origin) {  /*return value */
00039   TPOINT topleft;                /*bounding box */
00040   TPOINT botright;
00041 
00042                                  /*find bounding box */
00043   blob_bounding_box(blob, &topleft, &botright); 
00044                                  /*centre of box */
00045   origin->x = (topleft.x + botright.x) / 2;
00046   origin->y = (topleft.y + botright.y) / 2;
00047 }
00048 
00049 
00055 void blob_bounding_box(TBLOB *blob,               /*blob to compute on */
00056                        register TPOINT *topleft,  /*bounding box */
00057                        register TPOINT *botright) {
00058   register TESSLINE *outline;    /*current outline */
00059 
00060   if (blob == NULL || blob->outlines == NULL) {
00061     topleft->x = topleft->y = 0;
00062     *botright = *topleft;        /*default value */
00063   }
00064   else {
00065     outline = blob->outlines;
00066     *topleft = outline->topleft;
00067     *botright = outline->botright;
00068     for (outline = outline->next; outline != NULL; outline = outline->next) {
00069       if (outline->topleft.x < topleft->x)
00070                                  /*find extremes */
00071         topleft->x = outline->topleft.x;
00072       if (outline->botright.x > botright->x)
00073                                  /*find extremes */
00074         botright->x = outline->botright.x;
00075       if (outline->topleft.y > topleft->y)
00076                                  /*find extremes */
00077         topleft->y = outline->topleft.y;
00078       if (outline->botright.y < botright->y)
00079                                  /*find extremes */
00080         botright->y = outline->botright.y;
00081     }
00082   }
00083 }
00084 
00085 
00089 void blobs_bounding_box(TBLOB *blobs, TPOINT *topleft, TPOINT *botright) { 
00090   TPOINT tl;
00091   TPOINT br;
00092   TBLOB *blob;
00093   /* Start with first blob */
00094   blob_bounding_box(blobs, topleft, botright); 
00095 
00096   iterate_blobs(blob, blobs) { 
00097     blob_bounding_box(blob, &tl, &br); 
00098 
00099     if (tl.x < topleft->x)
00100       topleft->x = tl.x;
00101     if (tl.y > topleft->y)
00102       topleft->y = tl.y;
00103     if (br.x > botright->x)
00104       botright->x = br.x;
00105     if (br.y < botright->y)
00106       botright->y = br.y;
00107   }
00108 }
00109 
00110 
00115 void blobs_origin(TBLOB *blobs,      /*blob to compute on */
00116                   TPOINT *origin) {  /*return value */
00117   TPOINT topleft;                /*bounding box */
00118   TPOINT botright;
00119 
00120                                  /*find bounding box */
00121   blobs_bounding_box(blobs, &topleft, &botright); 
00122                                  /*center of box */
00123   origin->x = (topleft.x + botright.x) / 2;
00124   origin->y = (topleft.y + botright.y) / 2;
00125 }
00126 
00127 
00133 WIDTH_RECORD *blobs_widths(TBLOB *blobs) {  /*blob to compute on */
00134   WIDTH_RECORD *width_record;
00135   TPOINT topleft;                /*bounding box */
00136   TPOINT botright;
00137   TBLOB *blob;                   /*blob to compute on */
00138   int i = 0;
00139   int blob_end;
00140   int num_blobs = count_blobs (blobs);
00141 
00142   /* Get memory */
00143   width_record = (WIDTH_RECORD *) memalloc (sizeof (int) * num_blobs * 2);
00144   width_record->num_chars = num_blobs;
00145 
00146   blob_bounding_box(blobs, &topleft, &botright); 
00147   width_record->widths[i++] = botright.x - topleft.x;
00148   /* First width */
00149   blob_end = botright.x;
00150 
00151   iterate_blobs (blob, blobs->next) {
00152     blob_bounding_box(blob, &topleft, &botright); 
00153     width_record->widths[i++] = topleft.x - blob_end;
00154     width_record->widths[i++] = botright.x - topleft.x;
00155     blob_end = botright.x;
00156   }
00157   return (width_record);
00158 }
00159 
00160 
00164 int count_blobs(TBLOB *blobs) { 
00165   TBLOB *b;
00166   int x = 0;
00167 
00168   iterate_blobs (b, blobs) x++;
00169   return (x);
00170 }
00171 
00172 
00177 void delete_word(TWERD *word) { 
00178   TBLOB *blob;
00179   TBLOB *nextblob;
00180   TESSLINE *outline;
00181   TESSLINE *nextoutline;
00182   TESSLINE *child;
00183   TESSLINE *nextchild;
00184 
00185   for (blob = word->blobs; blob; blob = nextblob) {
00186     nextblob = blob->next;
00187 
00188     for (outline = blob->outlines; outline; outline = nextoutline) {
00189       nextoutline = outline->next;
00190 
00191       delete_edgepts (outline->loop);
00192 
00193       for (child = outline->child; child; child = nextchild) {
00194         nextchild = child->next;
00195 
00196         delete_edgepts (child->loop);
00197 
00198         oldoutline(child); 
00199       }
00200       oldoutline(outline); 
00201     }
00202     oldblob(blob); 
00203   }
00204   if (word->correct != NULL)
00205     strfree (word->correct);     /* Reclaim memory */
00206   oldword(word); 
00207 }
00208 
00209 
00213 void delete_edgepts(register EDGEPT *edgepts) { 
00214   register EDGEPT *this_edge;
00215   register EDGEPT *next_edge;
00216 
00217   if (edgepts == NULL)
00218     return;
00219 
00220   this_edge = edgepts;
00221   do {
00222     next_edge = this_edge->next;
00223     oldedgept(this_edge); 
00224     this_edge = next_edge;
00225   }
00226   while (this_edge != edgepts);
00227 }

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