chiark / gitweb /
page with the mouse to avoid the silly page-back-to-top effect
[ypp-sc-tools.main.git] / pctb / structure.c
index d574e2960737d622629761190a8d569ac37436a2..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) );
 }                  
 
@@ -616,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();
@@ -677,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);