chiark / gitweb /
Cope if very few commodities on screen. Use ADJUST_BOX much more.
[ypp-sc-tools.web-live.git] / pctb / structure.c
index 139180878012d92ae6b0589c19d262c370a02a21..e9df7334a73f04cf53c4754a0a14f7a21228180f 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;
@@ -112,7 +112,7 @@ void canon_colour_prepare(void) {
     CanonColourInfoBlues *blues= greens->green2[g];
     if (!blues) {
       blues= greens->green2[g]= mmalloc(sizeof(*blues));
-      memset(blues, '?', sizeof(blues));
+      memset(blues, '?', sizeof(*blues));
     }
 
     blues->blue2[b]= cci->c;
@@ -128,7 +128,6 @@ static void mustfail1(const char *file, int line, const char *what) {
  "   * YPP client is showing commodity listing screen\n"
  "   * YPP client window is on top (we try to raise it but your window\n"
  "      manager might have prevented that from succeeding)\n"
- "   * Your X display is 24bpp (NB some VNC servers use 16bpp by default)\n"
  "\n"
  "If all of these are true, please report this as a fault.\n\n"
          "Technical details:"
@@ -243,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;                                                     \
@@ -273,6 +272,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, '*');
@@ -307,37 +311,48 @@ 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); );
 
 #define SET_ONCE(var,val) do{                                          \
     int v= (val);                                                      \
@@ -486,11 +501,16 @@ static double find_aa_density(const RgbImage *ri, Point p, long background,
   double alpha_max= alpha_mean + thresh;
   for (i=0; i<3; i++)
     MUST( alpha_min <= alpha[i] && alpha[i] <= alpha_max,
+         MP(p);
          MRGB(here);MRGB(background);MRGB(foreground);MI(fg_extra);
          MF(alpha_min); MI(i);MF(alpha[i]);MF(alpha_max) );
 
+  if (   -1e-5 <  alpha_mean && alpha_mean <= 0.0     ) alpha_mean= 0.0;
+  if (1.0      <= alpha_mean && alpha_mean <= 1.0+1e-5) alpha_mean= 1.0;
+
   MUST( 0 <= alpha_mean &&
-       (fg_extra ? alpha_mean < 0.999 : alpha_mean >= 1.0),
+       (fg_extra ? (alpha_mean < 0.999) : (alpha_mean <= 1.0)),
+       MP(p);
        MRGB(here);MRGB(background);MRGB(foreground);MI(fg_extra);
        MF(alpha_mean); MF(alpha[0]);MF(alpha[1]);MF(alpha[2]); );
 
@@ -521,39 +541,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) );
-
-  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) {
@@ -659,9 +692,13 @@ Rect find_sunshine_widget(void) {
   return sunshiner;
 }
 
-void find_islandname(RgbImage *ri) {
+void find_islandname(void) {
   find_structure(page_images[0], 0);
 
+  RgbImage *ri= alloc_rgb_image(page_images[0]->rgb->w,
+                               page_images[0]->rgb->h);
+  memcpy(ri->data, page_images[0]->rgb->data, ri->w * ri->h * 3);
+  
   Rect sunshiner= find_sunshine_widget();
   char sunshine[MAXIMGIDENT], archisland[MAXIMGIDENT];
 
@@ -760,10 +797,15 @@ void find_islandname(RgbImage *ri) {
       
       for (p.y=islandnamer.tl.y; p.y<=islandnamer.br.y; p.y++) {
        pattern <<= 1;
-       switch (get_p(p)) {
-       case '*': if (runs[nruns]) { nruns++; runs[nruns]=0; }          break;
-       case 'O': runs[nruns]++;              pattern |= 1u;            break;
-       default:                              pattern |= 1UL<<31;       break;
+       double alpha= find_aa_density(ri,p, 0xCCCCAA,0x002255,0);
+       if (alpha >= 0.49) {
+          runs[nruns]++;
+          pattern |= 1u;
+       } else {
+         if (runs[nruns]) {
+           nruns++;
+           runs[nruns]=0;
+         }
        }
       }
 
@@ -777,7 +819,7 @@ void find_islandname(RgbImage *ri) {
        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;
@@ -786,6 +828,8 @@ void find_islandname(RgbImage *ri) {
            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: