chiark / gitweb /
antialiasing text conversion: seems to handle it in the C code so far ....
[ypp-sc-tools.db-test.git] / pctb / structure.c
index a3cca0b4408fc3da115bd91f069589956c0fb0f7..da80cb9fda86297b74e57f8e6a37ec7a887244bd 100644 (file)
@@ -320,6 +320,8 @@ void find_structure(CanonImage *im, int *max_relevant_y_r) {
   SET_ONCE(text_h, comminty - 1);
   if (max_relevant_y_r)
     SET_ONCE(*max_relevant_y_r, mainr.br.y + 10);
+
+  MUST( text_h <= OCR_MAX_H, MI(text_h) );
 }                  
 
 void check_correct_commodities(void) {
@@ -496,11 +498,12 @@ static void find_table_entry(Rect commod, int colno, Rect *cell) {
        ((double)here_chan - (double)bg_chan) /
        ((double)fg_chan   - (double)bg_chan);
       alpha[i]= alpha_chan;
-      alpha_mean += alpha_chan / 3;
+      alpha_mean += alpha_chan * (1/3.0);
     }
 
-    double alpha_min = alpha_mean - 0.13;
-    double alpha_max = alpha_mean + 0.13;
+    double thresh= 1.0/AAMAXVAL;
+    double alpha_min= alpha_mean - thresh;
+    double alpha_max= alpha_mean + thresh;
     for (i=0; i<3; i++)
       MUST( alpha_min <= alpha[i] && alpha[i] <= alpha_max,
            MI(i);MRGB(here);MRGB(background);MRGB(foreground);
@@ -509,12 +512,12 @@ static void find_table_entry(Rect commod, int colno, Rect *cell) {
     MUST( 0 <= alpha_mean && alpha_mean <= 1,
          MRGB(here);MRGB(background);MRGB(foreground);
          MF(alpha_mean) );
-    int here_int= 7*alpha_mean;
-    if (!(here_int==0 || here_int==7)) monochrome=0;
+    int here_int= AAMAXVAL*alpha_mean;
+    if (!(here_int==0 || here_int==AAMAXVAL)) monochrome=0;
     cim->d[p.y * cim->w + p.x]= '0' + here_int;
   }
 
-  debug_rect("cell07", colno, *cell);
+  debug_rect("cell0M", colno, *cell);
 
   require_rectangle_r(*cell, "0123456789", __LINE__);
 }
@@ -526,22 +529,17 @@ static void ocr_rectangle(Rect r, const OcrCellType ct, FILE *tsv_output) {
   Pixcol cols[w+1];
   int x,y;
   for (x=0; x<w; x++) {
-    Pixcol cx, rv;
-    for (y=0, cx=0, rv=1; y<text_h; y++, rv<<=1) {
+    FILLZERO(cols[x]);
+    for (y=0; y<text_h; y++) {
       Point here= { x+r.tl.x, y+r.tl.y };
       int pixel= get_p(here);
-      switch (pixel) {
-      case ' ':           break;
-      case '0':           break;
-      case '7': cx |= rv; break;
-      default:
-       MUST(!"wrong pixel",
-            MC(pixel);MP(here);MSB(ocr_celltype_name(ct));MR(r); );
-      }
+      if (pixel==' ') pixel= '0';
+      MUST( pixel >= '0' && pixel <= '0'+AAMAXVAL,
+           MC(pixel);MP(here);MSB(ocr_celltype_name(ct));MR(r); );
+      pixcol_p_add(&cols[x], y, pixel-'0');
     }
-    cols[x]= cx;
   }
-  cols[w]= 0;
+  FILLZERO(cols[w]);
 
   results= ocr(rd,ct,w,cols);
   for (res=results; res->s; res++)