chiark / gitweb /
Merge branch 'stable-5.x'
[ypp-sc-tools.main.git] / yarrg / structure.c
index f10addc56eed9c65f77ede4809407333a4ca780a..cc1e71360d6c0c9ff9d6997b76c7438ee8d7489a 100644 (file)
@@ -37,7 +37,7 @@ DEBUG_DEFINE_DEBUGF(struct)
 
 struct PageStruct {
   Rect mr;
-  int commbasey, comminty;
+  int commbasey, comminty, pagerheight;
   int colrightx[INTERESTING_COLUMNS];
 };
 
@@ -313,12 +313,16 @@ void find_structure(const CanonImage *im,
 
 #define CHECK_STRIP_BORDER(tlbr,xy,increm)             \
   do {                                                 \
-    Point csb_p;                                       \
+    Point csb_p, csb_p2;                               \
     Rect csb_r;                                                \
     csb_p= s.mr.tl;                                    \
     csb_p.x++; csb_p.y++;                              \
+    csb_p2= csb_p;                                     \
+    csb_p2.x++; csb_p2.y++;                            \
     csb_p.xy= s.mr.tlbr.xy;                            \
-    if (get_p(csb_p)=='+') {                           \
+    csb_p2.xy= s.mr.tlbr.xy;                           \
+    if (get_p(csb_p)=='+' &&                           \
+       get_p(csb_p2)=='+') {                           \
       csb_r= s.mr;                                     \
       csb_r.tl.xy= csb_p.xy;                           \
       csb_r.br.xy= csb_p.xy;                           \
@@ -397,6 +401,13 @@ void find_structure(const CanonImage *im,
   ADJUST_BOX(pager, "o",>=,RECT_W(pager)-2, s.mr.tl.y,LIMIT_QUITEQ, tl,y,-1);
   debug_rect("pager",__LINE__,pager);
 
+  pager.br.y= pager.tl.y;
+  pager.tl.y= pager.tl.y-1;
+  if (pager.tl.y > s.mr.tl.y)
+    ADJUST_BOX(pager, "+",>,1, s.mr.tl.y,LIMIT_QUITEQ, tl,y,-1);
+  debug_rect("pager",__LINE__,pager);
+  s.pagerheight= pager.br.y - pager.tl.y;
+
 #define SET_ONCE(var,val) do{                                          \
     int v= (val);                                                      \
     if ((var)==-1) (var)= v;                                           \
@@ -426,18 +437,23 @@ void find_structure(const CanonImage *im,
   }
   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;
+    commod_page_point_r->y=  pager.br.y - 1;
   }
 
   MUST( text_h <= OCR_MAX_H, MI(text_h) );
 }                  
 
 void check_correct_commodities(void) {
-  Rect search= { { 50,39 }, { 130,59 } };
+  Rect search;
 
+  search.tl= s.mr.tl;  search.tl.x +=  34;  search.tl.y -= 44;
+  search.br= s.mr.tl;  search.br.x += 114;  search.br.y -= 23;
+  MUST(search.br.x < cim->w-1 && search.tl.y > 0,
+       MR(search);MI(cim->w);MI(cim->h));
+  
+  debug_rect("commodselr",0, search);
   ADJUST_BOX(search,"_",>=,10, cim->h, MUST, tl,y,+1);
   ADJUST_BOX(search,"_",>=,10, 0,      MUST, br,y,-1);
-
   debug_rect("commodselr",1, search);
 
   static const char *all_small[]= {
@@ -468,6 +484,19 @@ void check_correct_commodities(void) {
     "??____X_____?_X_?X__?X_______________________",
     "???__?_______?__?___?_______________________?",
   };
+  static const char *all_fuzzy[]= {
+    "???___________________________________???",
+    "??_______???___X__X____________________??",
+    "?_______????__?X_?X_____XXXXXXXXXXX_____?",
+    "________?????_?X_?X______XXXXXXXXX_______",
+    "________?????_?X_?X_______XXXXXXX________",
+    "_______??????_?X_?X________XXXXX_________",
+    "_______??_?????X_?X_________XXX__________",
+    "______??XXXXX??X_?X__________X___________",
+    "?_____?????????X_?X______________________",
+    "??___???____???X_?X______________________",
+    "???__??_____???__?______________________?",
+  };
 
 #define COMMOD_SELECTOR_MATCHES(all)                           \
   commod_selector_matches(search, all,                         \
@@ -475,7 +504,8 @@ void check_correct_commodities(void) {
                          strlen((all)[0]))
 
   if (!(COMMOD_SELECTOR_MATCHES(all_small) ||
-       COMMOD_SELECTOR_MATCHES(all_big)))
+       COMMOD_SELECTOR_MATCHES(all_big) ||
+       COMMOD_SELECTOR_MATCHES(all_fuzzy)))
     fatal("Commodities selector not set to `All'.");
 }
 
@@ -529,6 +559,7 @@ void store_current_page(CanonImage *ci, PageStruct *pstruct, RgbImage *rgb) {
   else free(rgb);
   page_images[npages]= cim;
   page_structs[npages]= *pstruct;
+  debugf("STORED page %d  pagerheight=%d\n", npages, pstruct->pagerheight);
   free(pstruct);
 }
 
@@ -556,6 +587,12 @@ void read_screenshots(void) {
   }
   sysassert(!ferror(screenshot_file));
   progress_log("read %d screenshots.",npages);
+
+  check_pager_motion(1,npages);
+  /* When we are reading screenshots, the pages file contains the
+   * `determine where we're to click' page as well as the first
+   * actual data page, which we have to skip.
+   */
 }
 
 #define FIXPT_SHIFT 15
@@ -609,15 +646,6 @@ static inline Fixpt find_aa_density(const RgbImage *ri, Point p) {
   Fixpt alpha_mean= fixpt_mul(alpha_total, one_third);
   
   Fixpt thresh= dbl2fixpt(1.5/AAMAXVAL);
-  Fixpt alpha_min= alpha_mean - thresh*2;
-  Fixpt alpha_max= alpha_mean + thresh*2;
-
-  for (i=0; i<3; i++)
-    MUST( alpha_min <= alpha[i] && alpha[i] <= alpha_max,
-         MP(p);
-         MRGB(here);MRGB(aa_background);MRGB(aa_foreground);
-         MFP(aa_alpha_mean_max);MFP(thresh);MFP(alpha_mean);
-         MFP(alpha_min);MI(i);MFP(alpha[i]);MFP(alpha_max) );
 
   MUST( -thresh <= alpha_mean && alpha_mean <= aa_alpha_mean_max + thresh,
        MP(p);
@@ -1006,3 +1034,55 @@ void find_islandname(void) {
   island= masprintf("%s", delim+3);
 
 }
+
+void check_pager_motion(int first, int stop) {
+  /* We check that the pager moved by an appropriate amount.
+   * The last gap can be smaller but not bigger.
+   */
+  int count= stop-first;
+
+#define PH(p) (page_structs[(p)].pagerheight)
+
+  debugf("CHECK_PAGER_MOTION %d..%d count=%d\n", first,stop,count);
+
+  if (count <= 1) return; /* only one page */
+
+  double firstheight= PH(first);
+  double max= count>2 ? firstheight / (count-2) : 1e6;
+  double min=           firstheight / (count-1);
+  max *= 1.1;
+  min /= 1.1;
+  max += 1.0;
+  min -= 1.0;
+  debugf("CHECK_PAGER_MOTION min=%g max=%g\n", min,max);
+  assert(max>min);
+
+  int skips=0, firstskip=1;
+  int stops=0, firststop=1;
+
+#define REPORT(skipstop,msg) do{                       \
+      skipstop##s++;                                   \
+      if (first##skipstop<0) first##skipstop= page;    \
+      if (skipstop##s<5)                               \
+       fprintf(stderr,msg " (page %d)\n",page);        \
+    }while(0)
+
+  int page;
+  for (page=first+1; page<stop; page++) {
+    int gap= PH(page-1) - PH(page);
+    debugf("CHECK_PAGER_MOTION  page=%2d gap=%2d\n", page, gap);
+    if (gap>max)
+      REPORT(skip, "scrollbar motion probable page skip detected!");
+    if (gap<min && page<stop-1)
+      REPORT(stop, "scrollbar short motion gives lie to our ideas!");
+  }
+  MUST(!skips && !stops,
+       MI(skips);MI(firstskip);MF(max);MI(PH(firstskip));MI(PH(firstskip-1));
+       MI(stops);MI(firststop);MF(min);MI(PH(firststop));MI(PH(firststop-1));
+       MF(firstheight);MI(PH(stop-2));MI(PH(stop-1));
+       MSB(
+"\n\n"
+"Your YPP client seems to be paging irregularly.  Perhaps your computer\n"
+"is too slow or overloaded or perhaps you are messing with the mouse ?\n"
+       ));
+}