chiark / gitweb /
page with the mouse to avoid the silly page-back-to-top effect
[ypp-sc-tools.db-test.git] / pctb / structure.c
index 967de1e990a778049f4aaaad2d32823a4812b54c..ad95cb508791b5213e64bd8170258b302ff0d7cb 100644 (file)
@@ -40,7 +40,7 @@ DEBUG_DEFINE_DEBUGF(struct)
 #define TEXT_COLUMNS        2
 #define MAX_COLUMNS         7
 
-static Rect mainr = { START_MAIN,START_MAIN };
+static Rect mainr;
 static int commbasey, comminty;
 static int colrightx[INTERESTING_COLUMNS];
 static int text_h=-1, columns=-1;
@@ -242,7 +242,7 @@ static int commod_selector_matches(Rect search, const char *const *all,
 #define ADJUST_BOX(search,insidechrs,OP,want, lim,LIMIT_MUST, TLBR,XY,increm) \
   for (;;) {                                                                 \
     LIMIT_MUST( (search).tl.XY != (search).br.XY &&                          \
-               (search).tl.XY != (lim),                                      \
+               (search).TLBR.XY != (lim),                                    \
                MR((search));MSB(#TLBR);MSB(#XY) );                           \
     int got=0;                                                               \
     Point p=(search).tl;                                                     \
@@ -255,7 +255,10 @@ static int commod_selector_matches(Rect search, const char *const *all,
     (search).TLBR.XY += increm;                                                      \
   }
 
-void find_structure(CanonImage *im, int *max_relevant_y_r) {
+void find_structure(CanonImage *im, int *max_relevant_y_r,
+                   Point *commod_focus_point_r,
+                   Point *commod_page_point_r,
+                   Point *commod_focuslast_point_r) {
   cim= im;
   
   Rect whole = { {0,0}, {cim->w-1,cim->h-1} };
@@ -272,6 +275,11 @@ void find_structure(CanonImage *im, int *max_relevant_y_r) {
     }
   }
 
+  Point mainr_tl= START_MAIN;
+  mainr.tl= mainr_tl;
+  WALK_UNTIL_MUST(mainr.tl, y,-1, whole.tl.y, ' ');
+  mainr.br= mainr.tl;
+
   WALK_UNTIL_MUST(mainr.tl, x,-1, whole.tl.x, '*');
   WALK_UNTIL_MUST(mainr.tl, y,-1, whole.tl.y, '*');
   WALK_UNTIL_MUST(mainr.br, x,+1, whole.br.x, '*');
@@ -287,6 +295,7 @@ void find_structure(CanonImage *im, int *max_relevant_y_r) {
     Point csb_p;                                       \
     Rect csb_r;                                                \
     csb_p= mainr.tl;                                   \
+    csb_p.x++; csb_p.y++;                              \
     csb_p.xy= mainr.tlbr.xy;                           \
     if (get_p(csb_p)=='+') {                           \
       csb_r= mainr;                                    \
@@ -306,37 +315,65 @@ void find_structure(CanonImage *im, int *max_relevant_y_r) {
 
   debug_rect("mainr",1, mainr);
 
-  Point up = START_MAIN;
-  WALK_UNTIL_MUST(up, y,-1, mainr.tl.y, '+');
+  Rect updown= {START_MAIN,START_MAIN};
+  const int chkw= 100;
+  updown.br.x += chkw-1;
+  updown.br.y++;
+  debug_rect("updown",__LINE__,updown);
+
+  ADJUST_BOX(updown, "+", >=,chkw, mainr.tl.y,   MUST, tl,y,-1);
+  debug_rect("updown",__LINE__,updown);
+  updown.br.y= updown.tl.y;
+  updown.tl.y= updown.tl.y-1;
 
-  Point down = START_MAIN;
-  down.y++;
-  WALK_UNTIL_MUST(down, y,+1, mainr.br.y, '+');
+  ADJUST_BOX(updown, "+*",>=,chkw, mainr.tl.y-1, MUST, tl,y,-1);
+  debug_rect("updown",__LINE__,updown);
 
-  commbasey= up.y;
-  comminty= down.y - up.y + 2;
+  commbasey= updown.tl.y + 1;
+  comminty= updown.br.y - updown.tl.y;
 
-  Point across= { mainr.tl.x, commbasey };
+  Rect across= {{ mainr.tl.x - 1, commbasey              },
+               { mainr.tl.x,     commbasey + comminty-2 }};
   int colno=0;
   for (;;) {
-    MUST( get_p(across) != '+', MI(colno);MP(across);MR(mainr);MI(commbasey) );
-    WALK_UNTIL(across, x,+1, mainr.br.x, '+');
-    MUST( colno < MAX_COLUMNS, MP(across);MR(mainr);MI(commbasey); );
-    int colrx= across.x;
-    if (colrx > mainr.br.x) colrx= mainr.br.x;
+
+#define LIMIT_QUITEQ(cond,mp) { if (!(cond)) break; }
+    debug_rect("across",colno*1000000+__LINE__, across);
+    ADJUST_BOX(across, "+",>=,comminty-1, mainr.br.x, LIMIT_QUITEQ, br,x,+1);
+    debug_rect("across",colno*1000000+__LINE__, across);
+
+    MUST( colno < MAX_COLUMNS, MI(colno);MR(across);MR(mainr);MI(commbasey); );
+    int colrx= across.br.x-1;
+    if (colrx >= mainr.br.x) colrx= mainr.br.x;
     if (colno < INTERESTING_COLUMNS)
       colrightx[colno]= colrx;
       
     colno++;
     
-    if (across.x >= mainr.br.x-1)
+    if (across.br.x >= mainr.br.x)
       break;
 
-    across.x++;
-    REQUIRE_RECTANGLE(across.x,mainr.tl.y, across.x,mainr.br.y, "+");
-    across.x++;
+    REQUIRE_RECTANGLE(across.br.x,mainr.tl.y, across.br.x,mainr.br.y, "+");
+    across.br.x++;
   }
-  MUST( colno >= MIN_COLUMNS, MI(colno);MR(mainr);MP(across); );
+  MUST( colno >= MIN_COLUMNS, MI(colno);MR(mainr);MR(across); );
+
+  const int pagerh= 6;
+  Rect pager= {{ mainr.br.x,     mainr.br.y - (pagerh-1) },
+              { mainr.br.x + 1, mainr.br.y              }};
+
+  debug_rect("pager",__LINE__,pager);
+  ADJUST_BOX(pager, "o",>=,pagerh-2, whole.br.x,MUST, br,x,+1);
+  debug_rect("pager",__LINE__,pager);
+
+  pager.tl.x= pager.br.x;
+  pager.br.x= pager.br.x + 1;
+  debug_rect("pager",__LINE__,pager);
+  ADJUST_BOX(pager, "o",>=,pagerh-2, whole.br.x,MUST, br,x,+1);
+  debug_rect("pager",__LINE__,pager);
+
+  ADJUST_BOX(pager, "o",>=,RECT_W(pager)-2, mainr.tl.y,LIMIT_QUITEQ, tl,y,-1);
+  debug_rect("pager",__LINE__,pager);
 
 #define SET_ONCE(var,val) do{                                          \
     int v= (val);                                                      \
@@ -349,6 +386,21 @@ void find_structure(CanonImage *im, int *max_relevant_y_r) {
   if (max_relevant_y_r)
     SET_ONCE(*max_relevant_y_r, mainr.br.y + 10);
 
+  if (commod_focus_point_r) {
+    *commod_focus_point_r= mainr.tl;
+    commod_focus_point_r->x += 10;
+    commod_focus_point_r->y += comminty/3;
+  }
+  if (commod_focuslast_point_r) {
+    *commod_focuslast_point_r= mainr.br;
+    commod_focuslast_point_r->x -= 10;
+    commod_focuslast_point_r->y -= comminty/3;
+  }
+  if (commod_page_point_r) {
+    commod_page_point_r->x= (pager.tl.x + pager.br.x) / 2;
+    commod_page_point_r->y=  pager.tl.y - 1;
+  }
+
   MUST( text_h <= OCR_MAX_H, MI(text_h) );
 }                  
 
@@ -525,39 +577,52 @@ static void find_table_entry(Rect commod, int colno, Rect *cell) {
 
   const RgbImage *ri= cim->rgb;
   
-  Rgb background= ri_rgb(ri, cell->br.x, cell->br.y);
+  Rgb background;
+  unsigned char chanbg[3];
   long bg_count=0, light_count=0, dark_count=0;
   Point p;
+
+  background= ri_rgb(ri, cell->br.x, cell->br.y);
+  memcpy(chanbg, RI_PIXEL(ri, cell->br.x, cell->br.y), 3);
+
   FOR_P_RECT(p,*cell) {
-    Rgb here= ri_rgb(ri, p.x, p.y);
-    if (here == background) bg_count++;
-    else if (here < background) dark_count++;
-    else if (here > background) light_count++;
+    const unsigned char *here_pixel= RI_PIXEL(ri, p.x, p.y);
+    int i;
+    for (i=0; i<3; i++) {
+      unsigned here= here_pixel[i];
+      if (here == chanbg[i]) bg_count++;
+      else if (here < chanbg[i]) dark_count  += (chanbg[i] - here)/4 + 1;
+      else if (here > chanbg[i]) light_count += (here - chanbg[i])/4 + 1;
+    }
   }
-  long total_count= RECT_W(*cell) * RECT_H(*cell);
+  long total_count= RECT_W(*cell) * RECT_H(*cell) * 3;
+
   MUST( bg_count > total_count / 2,
        MR(*cell);MIL(total_count);MIL(bg_count);
        MIL(light_count);MIL(dark_count) );
+
   if (bg_count == total_count)
     return;
 
-  MUST( !!dark_count != !!light_count,
-       MR(*cell);MIL(total_count);MIL(bg_count);
-       MIL(light_count);MIL(dark_count);MRGB(background); );
-
-  debugf("TABLEENTRY col=%d %d,%d..%d,%d bg=%ld light=%ld dark=%ld\n",
-        colno, cell->tl.x,cell->tl.y, cell->br.x,cell->br.y,
-        bg_count, light_count, dark_count);
-  
   Rgb foreground;
   double fg_extra;
-  if (light_count) {
+
+  if (light_count/16 > dark_count) {
     foreground= 0xffffffU;
     fg_extra= +1;
-  } else {
+  } else if (dark_count/16 > light_count) {
     foreground= 0;
     fg_extra= -1;
+  } else {
+    MUST( !"tell light from dark",
+         MR(*cell);MIL(total_count);MIL(bg_count);
+         MIL(light_count);MIL(dark_count);MRGB(background); );
   }
+
+  debugf("TABLEENTRY col=%d %d,%d..%d,%d bg=%ld light=%ld dark=%ld\n",
+        colno, cell->tl.x,cell->tl.y, cell->br.x,cell->br.y,
+        bg_count, light_count, dark_count);
+  
   int monochrome= 1;
 
   FOR_P_RECT(p,*cell) {
@@ -603,7 +668,7 @@ void analyse(FILE *tsv_output) {
   int page, tryrect, colno;
 
   for (page=0; page<npages; page++) {
-    find_structure(page_images[page], 0);
+    find_structure(page_images[page], 0,0,0,0);
 
     if (!page)
       check_correct_commodities();
@@ -664,7 +729,7 @@ Rect find_sunshine_widget(void) {
 }
 
 void find_islandname(void) {
-  find_structure(page_images[0], 0);
+  find_structure(page_images[0], 0,0,0,0);
 
   RgbImage *ri= alloc_rgb_image(page_images[0]->rgb->w,
                                page_images[0]->rgb->h);
@@ -790,7 +855,7 @@ void find_islandname(void) {
        nspaces++;
        might_be_colon=0;
       } else {
-       if (!(pattern & 1UL<<31) && nruns==2 && runs[1]==runs[0]) {
+       if (nruns==2 && runs[1]==runs[0]) {
          if (!nspaces) {
            if (pattern==colon_pattern)
              goto ok_might_be_colon;
@@ -799,6 +864,8 @@ void find_islandname(void) {
            might_be_colon=1;
            goto ok_might_be_colon;
          }
+       } else if (nruns==1 && runs[0]==1 && might_be_colon) {
+         goto colon_found;
        }
        might_be_colon=0;
       ok_might_be_colon: