chiark / gitweb /
Merge branch 'ijackson'
[ypp-sc-tools.db-test.git] / pctb / structure.c
1 /*
2  * Parsing of the structure of the YPP client's displayed image
3  */
4 /*
5  *  This is part of ypp-sc-tools, a set of third-party tools for assisting
6  *  players of Yohoho Puzzle Pirates.
7  * 
8  *  Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
9  * 
10  *  This program is free software: you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation, either version 3 of the License, or
13  *  (at your option) any later version.
14  * 
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  * 
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  * 
23  *  Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
24  *  are used without permission.  This program is not endorsed or
25  *  sponsored by Three Rings.
26  */
27
28 #include "structure.h"
29
30 static CanonImage *cim;
31
32 static inline char get(int x, int y) { return cim->d[y * cim->w + x]; }
33 static inline char get_p(Point p) { return get(p.x,p.y); }
34
35 DEBUG_DEFINE_DEBUGF(struct)
36
37 #define START_MAIN {200,200}
38 #define MIN_COLUMNS         6
39 #define INTERESTING_COLUMNS 7
40 #define TEXT_COLUMNS        2
41 #define MAX_COLUMNS         7
42
43 static Rect mainr;
44 static int commbasey, comminty;
45 static int colrightx[INTERESTING_COLUMNS];
46 static int text_h=-1, columns=-1;
47 static OcrReader *rd;
48
49 char *archipelago, *island;
50
51 #define OTHERCOORD_x y
52 #define OTHERCOORD_y x
53
54 typedef struct {
55   Rgb rgb; /* on screen */
56   char c; /* canonical */
57 } CanonColourInfo;
58
59 const CanonColourInfo canoncolourinfo_table[]= {
60   { 0x475A5E, '*' }, /* edge */
61   { 0x2C5F7A, '*' }, /* edge just under box heading shadow */
62   { 0xC5C7AE, '*' }, /* blank area of partial commodities list */
63   { 0x6B828C, '*' }, /* background of ship status meter area */
64   { 0x934405, '*' }, /* border of ship meter area */
65   { 0x7D9094, '+' }, /* interbox */
66   { 0x022158, 'O' }, /* ahoy /w output foreground */
67   { 0xB5B686, 'H' }, /* ahoy /w output heading background */
68
69   { 0xBDC5BF, ' ' }, /* background - pale  Sugar cane, etc. */
70   { 0xADB5AF, ' ' }, /* background - dark                   */
71   { 0xC7E1C3, ' ' }, /* background - pale  Swill, etc.      */
72   { 0xB5CFB1, ' ' }, /* background - dark                   */
73   { 0xD6CEB0, ' ' }, /* background - pale  Madder, etc.     */
74   { 0xC8C0A2, ' ' }, /* background - dark                   */
75   { 0xE0E1D3, ' ' }, /* background - pale  Lorandite, etc.  */
76   { 0xD0D1C3, ' ' }, /* background - dark                   */
77   { 0xE5E6C1, ' ' }, /* background - pale  Cloth            */
78   { 0xD7D8B3, ' ' }, /* background - dark                   */
79   { 0xEDDED9, ' ' }, /* background - pale  Dye              */
80   { 0xDACBC6, ' ' }, /* background - dark                   */
81   { 0xD3DEDF, ' ' }, /* background - pale  Paint            */
82   { 0xC5D0D1, ' ' }, /* background - dark                   */
83   { 0xDCD1CF, ' ' }, /* background - pale  Enamel           */
84   { 0xCEC3C1, ' ' }, /* background - dark                   */
85   { 0xF3F6F5, ' ' }, /* background - pale  fruit            */
86   { 0xE2E7E5, ' ' }, /* background - dark                   */
87
88   { 0x000000, 'o' }, /* foreground */
89   { 0xD4B356, ' ' }, /* background (cursor) */
90   { 0xFFFFFF, 'o' }, /* foreground (cursor) */
91
92   { 0x5B93BF, '_' }, /* selector dropdown background */
93   { 0xD7C94F, 'X' }, /* selector dropdown foreground */
94   { 0,0 }
95 };
96
97 CanonColourInfoReds canoncolourinfo_tree;
98
99 void canon_colour_prepare(void) {
100   const CanonColourInfo *cci;
101   for (cci=canoncolourinfo_table; cci->c; cci++) {
102     unsigned char r= cci->rgb >> 16;
103     unsigned char g= cci->rgb >>  8;
104     unsigned char b= cci->rgb;
105
106     CanonColourInfoGreens *greens= canoncolourinfo_tree.red2[r];
107     if (!greens) {
108       greens= canoncolourinfo_tree.red2[r]= mmalloc(sizeof(*greens));
109       FILLZERO(*greens);
110     }
111
112     CanonColourInfoBlues *blues= greens->green2[g];
113     if (!blues) {
114       blues= greens->green2[g]= mmalloc(sizeof(*blues));
115       memset(blues, '?', sizeof(*blues));
116     }
117
118     blues->blue2[b]= cci->c;
119   }
120 }
121
122
123 static void mustfail1(const char *file, int line, const char *what) {
124   fprintf(stderr,
125  "\n\n"
126  "Unable to figure out contents of YPP client display.\n"
127  "Please check the following:\n"
128  "   * YPP client is showing commodity listing screen\n"
129  "   * YPP client window is on top (we try to raise it but your window\n"
130  "      manager might have prevented that from succeeding)\n"
131  "\n"
132  "If all of these are true, please report this as a fault.\n\n"
133           "Technical details:"
134           " %s:%d: requirement failed:\n"
135           " %s\n",
136           file, line, what);
137 }
138 static void mustfail2(void) NORET;
139 static void mustfail2(void) {
140   fprintf(stderr, "\n\nGiving up.\n");
141   exit(8);
142 }
143
144 #define MUST(x, ifnot) do{                      \
145     if (!(x)) {                                 \
146       mustfail1(__FILE__,__LINE__,#x);          \
147       ifnot;                                    \
148       mustfail2();                              \
149     }                                           \
150   }while(0)
151
152 #define MP(v) fprintf(stderr," %s=%d,%d",#v,(v).x,(v).y)
153 #define MI(v) fprintf(stderr," %s=%d",   #v,(v))
154 #define MIL(v) fprintf(stderr," %s=%ld", #v,(v))
155 #define MRGB(v) fprintf(stderr," %s=%06lx", #v,(v))
156 #define MC(v) fprintf(stderr," %s='%c'", #v,(v))
157 #define MS(v) fprintf(stderr," %s=\"%s\"", #v,(v))
158 #define MF(v) fprintf(stderr," %s=%f", #v,(v))
159 #define MSB(v) fprintf(stderr," %s", (v))
160 #define MR(v) fprintf(stderr," %s=%d,%d..%d,%d",\
161                       #v,(v).tl.x,(v).tl.y,(v).br.x,(v).br.y)
162
163
164 #define REQUIRE_RECTANGLE(tlx,tly,brx,bry,ok) \
165  require_rectangle(tlx, tly, brx, bry, ok, __LINE__);
166
167 #define FOR_P_RECT(p,rr)                                \
168   for ((p).x=(rr).tl.x; (p).x<=(rr).br.x; (p).x++)      \
169     for ((p).y=(rr).tl.y; (p).y<=(rr).br.y; (p).y++)
170
171 static void require_rectangle_r(Rect rr, const char *ok, int lineno) {
172   Point p;
173   FOR_P_RECT(p,rr) {
174     int c= get_p(p);
175     MUST( strchr(ok,c), ({
176       MI(lineno),MR(rr);MP(p);MS(ok);
177     }));
178   }
179 }
180 static void require_rectangle(int tlx, int tly, int brx, int bry,
181                               const char *ok, int lineno) {
182   Rect rr= {{tlx,tly},{brx,bry}};
183   require_rectangle_r(rr, ok, lineno);
184 }
185
186 static void debug_rect(const char *what, int whati, Rect rr) {
187   if (!DEBUGP(rect)) return;
188   int y,w;
189   fprintf(debug, "%s %d: %d,%d..%d,%d:\n", what, whati,
190           rr.tl.x,rr.tl.y, rr.br.x,rr.br.y);
191   w= rr.br.x - rr.tl.x + 1;
192   for (y=rr.tl.y; y<=rr.br.y; y++) {
193     fprintf(debug, "%4d%*s|", y, rr.tl.x,"");
194     fwrite(cim->d + y*cim->w + rr.tl.x, 1, w, debug);
195     fputc('|',debug);
196     fputc('\n',debug);
197   }
198   debug_flush();
199 }
200
201 static int commod_selector_matches(Rect search, const char *const *all,
202                                    int allh, int allw) {
203   int alloffy, alloffx;
204   for (alloffy=0; alloffy < search.br.y; alloffy++) {
205     if (alloffy+allh-1 < search.tl.y) continue;
206     for (alloffx=search.tl.x; alloffx+allw-1 <= search.br.x; alloffx++) {
207       int good=0, bad=0;
208       int x,y;
209       for (x=0; x<allw; x++)
210         for (y=0; y<allh; y++) {
211           int want= all[y][x];
212           if (want==' ') continue;
213           if (get(alloffx+x, alloffy+y) == want)
214             good++;
215           else
216             bad++;
217         }
218       debugf("CHECKCOMMOD alloff=%d,%d good=%d bad=%d\n",
219              alloffx,alloffy, good,bad);
220       if (good > 20*bad)
221         return 1;
222     }
223   }
224   return 0;
225 }
226
227 #define WALK_UNTIL(point,coord,increm,last,edge)                        \
228   for (;;) {                                                            \
229     if ((point).coord == (last)+(increm)) break;                        \
230     if (get_p((point)) == (edge)) { (point).coord -= (increm); break; } \
231     (point).coord += (increm);                                          \
232   }
233
234 #define WALK_UNTIL_MUST(point,coord,increm,last,edge)   \
235   do {                                                  \
236     WALK_UNTIL(point,coord,increm,last,edge);           \
237     MUST( (point).coord != (last)+(increm),             \
238           MP(point); MI(increm); MI(last); MC(edge);    \
239           );                                            \
240   }while(0)
241
242 #define ADJUST_BOX(search,insidechrs,OP,want, lim,LIMIT_MUST, TLBR,XY,increm) \
243   for (;;) {                                                                  \
244     LIMIT_MUST( (search).tl.XY != (search).br.XY &&                           \
245                 (search).TLBR.XY != (lim),                                    \
246                 MR((search));MSB(#TLBR);MSB(#XY) );                           \
247     int got=0;                                                                \
248     Point p=(search).tl;                                                      \
249     for (p.XY=(search).TLBR.XY;                                               \
250          p.OTHERCOORD_##XY <= (search).br.OTHERCOORD_##XY;                    \
251          p.OTHERCOORD_##XY++)                                                 \
252       got += !!strchr(insidechrs, get_p(p));                                  \
253     if ((got) OP (want))                                                      \
254       break;                                                                  \
255     (search).TLBR.XY += increm;                                               \
256   }
257
258 void find_structure(CanonImage *im, int *max_relevant_y_r,
259                     Point *commod_focus_point_r,
260                     Point *commod_page_point_r,
261                     Point *commod_focuslast_point_r) {
262   cim= im;
263   
264   Rect whole = { {0,0}, {cim->w-1,cim->h-1} };
265
266   if (DEBUGP(rect)) {
267     int xscaleunit, y,x;
268     for (y=0, xscaleunit=1; y<4; y++, xscaleunit*=10) {
269       fprintf(debug,"     ");
270       for (x=0; x<=cim->w; x++) {
271         if (x % xscaleunit) fputc(' ',debug);
272         else fprintf(debug,"%d",(x / xscaleunit)%10);
273       }
274       fputc('\n',debug);
275     }
276   }
277
278   Point mainr_tl= START_MAIN;
279   mainr.tl= mainr_tl;
280   WALK_UNTIL_MUST(mainr.tl, y,-1, whole.tl.y, ' ');
281   mainr.br= mainr.tl;
282
283   WALK_UNTIL_MUST(mainr.tl, x,-1, whole.tl.x, '*');
284   WALK_UNTIL_MUST(mainr.tl, y,-1, whole.tl.y, '*');
285   WALK_UNTIL_MUST(mainr.br, x,+1, whole.br.x, '*');
286   WALK_UNTIL_MUST(mainr.br, y,+1, whole.br.y, '*');
287
288   REQUIRE_RECTANGLE(mainr.tl.x-1, mainr.tl.y, mainr.tl.x-1, mainr.br.y, "*");
289   REQUIRE_RECTANGLE(mainr.br.x+1, mainr.tl.y, mainr.br.x+1, mainr.br.y, "*");
290   REQUIRE_RECTANGLE(mainr.tl.x, mainr.tl.y-1, mainr.br.x, mainr.tl.y-1, "*");
291   REQUIRE_RECTANGLE(mainr.tl.x, mainr.br.y+1, mainr.br.x, mainr.br.y+1, "*");
292
293 #define CHECK_STRIP_BORDER(tlbr,xy,increm)              \
294   do {                                                  \
295     Point csb_p;                                        \
296     Rect csb_r;                                         \
297     csb_p= mainr.tl;                                    \
298     csb_p.x++; csb_p.y++;                               \
299     csb_p.xy= mainr.tlbr.xy;                            \
300     if (get_p(csb_p)=='+') {                            \
301       csb_r= mainr;                                     \
302       csb_r.tl.xy= csb_p.xy;                            \
303       csb_r.br.xy= csb_p.xy;                            \
304       require_rectangle_r(csb_r, "+", __LINE__);        \
305       mainr.tlbr.xy += increm;                          \
306     }                                                   \
307   } while(0)
308
309   debug_rect("mainr",0, mainr);
310
311   CHECK_STRIP_BORDER(tl,x,+1);
312   CHECK_STRIP_BORDER(tl,y,+1);
313   CHECK_STRIP_BORDER(br,x,-1);
314   CHECK_STRIP_BORDER(br,y,-1);
315
316   debug_rect("mainr",1, mainr);
317
318   Rect updown= {START_MAIN,START_MAIN};
319   const int chkw= 100;
320   updown.br.x += chkw-1;
321   updown.br.y++;
322   debug_rect("updown",__LINE__,updown);
323
324   ADJUST_BOX(updown, "+", >=,chkw, mainr.tl.y,   MUST, tl,y,-1);
325   debug_rect("updown",__LINE__,updown);
326   updown.br.y= updown.tl.y;
327   updown.tl.y= updown.tl.y-1;
328
329   ADJUST_BOX(updown, "+*",>=,chkw, mainr.tl.y-1, MUST, tl,y,-1);
330   debug_rect("updown",__LINE__,updown);
331
332   commbasey= updown.tl.y + 1;
333   comminty= updown.br.y - updown.tl.y;
334
335   Rect across= {{ mainr.tl.x - 1, commbasey              },
336                 { mainr.tl.x,     commbasey + comminty-2 }};
337   int colno=0;
338   for (;;) {
339
340 #define LIMIT_QUITEQ(cond,mp) { if (!(cond)) break; }
341     debug_rect("across",colno*1000000+__LINE__, across);
342     ADJUST_BOX(across, "+",>=,comminty-1, mainr.br.x, LIMIT_QUITEQ, br,x,+1);
343     debug_rect("across",colno*1000000+__LINE__, across);
344
345     MUST( colno < MAX_COLUMNS, MI(colno);MR(across);MR(mainr);MI(commbasey); );
346     int colrx= across.br.x-1;
347     if (colrx >= mainr.br.x) colrx= mainr.br.x;
348     if (colno < INTERESTING_COLUMNS)
349       colrightx[colno]= colrx;
350       
351     colno++;
352     
353     if (across.br.x >= mainr.br.x)
354       break;
355
356     REQUIRE_RECTANGLE(across.br.x,mainr.tl.y, across.br.x,mainr.br.y, "+");
357     across.br.x++;
358   }
359   MUST( colno >= MIN_COLUMNS, MI(colno);MR(mainr);MR(across); );
360
361   const int pagerh= 6;
362   Rect pager= {{ mainr.br.x,     mainr.br.y - (pagerh-1) },
363                { mainr.br.x + 1, mainr.br.y              }};
364
365   debug_rect("pager",__LINE__,pager);
366   ADJUST_BOX(pager, "o",>=,pagerh-2, whole.br.x,MUST, br,x,+1);
367   debug_rect("pager",__LINE__,pager);
368
369   pager.tl.x= pager.br.x;
370   pager.br.x= pager.br.x + 1;
371   debug_rect("pager",__LINE__,pager);
372   ADJUST_BOX(pager, "o",>=,pagerh-2, whole.br.x,MUST, br,x,+1);
373   debug_rect("pager",__LINE__,pager);
374
375   ADJUST_BOX(pager, "o",>=,RECT_W(pager)-2, mainr.tl.y,LIMIT_QUITEQ, tl,y,-1);
376   debug_rect("pager",__LINE__,pager);
377
378 #define SET_ONCE(var,val) do{                                           \
379     int v= (val);                                                       \
380     if ((var)==-1) (var)= v;                                            \
381     else MUST( (var) == v, MSB(#var);MI((var));MI(v);MR(mainr); );      \
382   }while(0)
383
384   SET_ONCE(columns, colno);
385   SET_ONCE(text_h, comminty - 1);
386   if (max_relevant_y_r)
387     SET_ONCE(*max_relevant_y_r, mainr.br.y + 10);
388
389   if (commod_focus_point_r) {
390     *commod_focus_point_r= mainr.tl;
391     commod_focus_point_r->x += 10;
392     commod_focus_point_r->y += comminty/3;
393   }
394   if (commod_focuslast_point_r) {
395     *commod_focuslast_point_r= mainr.br;
396     commod_focuslast_point_r->x -= 10;
397     commod_focuslast_point_r->y -= comminty/3;
398   }
399   if (commod_page_point_r) {
400     commod_page_point_r->x= (pager.tl.x + pager.br.x) / 2;
401     commod_page_point_r->y=  pager.tl.y - 1;
402   }
403
404   MUST( text_h <= OCR_MAX_H, MI(text_h) );
405 }                   
406
407 void check_correct_commodities(void) {
408   Rect search= { { 50,39 }, { 130,59 } };
409
410   ADJUST_BOX(search,"_",>=,10, cim->h, MUST, tl,y,+1);
411   ADJUST_BOX(search,"_",>=,10, 0,      MUST, br,y,-1);
412
413   debug_rect("commodselr",1, search);
414
415   static const char *all_small[]= {
416     "   ___________________________________   ",
417     "  ________X____X__X____________________  ",
418     " ________ X___ X_ X_____XXXXXXXXXXX_____ ",
419     "_________X_X__ X_ X______XXXXXXXXX_______",
420     "________ X X__ X_ X_______XXXXXXX________",
421     "________X_ _X_ X_ X________XXXXX_________",
422     "_______ X__ X_ X_ X_________XXX__________",
423     "_______XXXXXXX X_ X__________X___________",
424     " _____ X     X X_ X______________________",
425     "  ____X_____ _XX_ X______________________",
426     "   __ _______  __ ______________________ ",
427   };
428   static const char *all_big[]= {
429     "???_______________________________________???",
430     "??_________________________________________??",
431     "?_________X______X___X______________________?",
432     "_________?X_____?X__?X______XXXXXXXXXXX______",
433     "_________X_X____?X__?X_______XXXXXXXXX_______",
434     "________?X?X____?X__?X________XXXXXXX________",
435     "________X_?_X___?X__?X_________XXXXX_________",
436     "_______?X__?X___?X__?X__________XXX__________",
437     "_______?XXXXX___?X__?X___________X___________",
438     "_______X????_X__?X__?X_______________________",
439     "?_____?X____?X__?X__?X_______________________",
440     "??____X_____?_X_?X__?X_______________________",
441     "???__?_______?__?___?_______________________?",
442   };
443
444 #define COMMOD_SELECTOR_MATCHES(all)                            \
445   commod_selector_matches(search, all,                          \
446                           sizeof((all))/sizeof((all)[0]),       \
447                           strlen((all)[0]))
448
449   if (!(COMMOD_SELECTOR_MATCHES(all_small) ||
450         COMMOD_SELECTOR_MATCHES(all_big)))
451     fatal("Commodities selector not set to `All'.");
452 }
453
454 CanonImage *alloc_canon_image(int w, int h) {
455   CanonImage *im= mmalloc(sizeof(CanonImage) + w*h);
456   im->w= w;
457   im->h= h;
458   memset(im->d,'?',w*h);
459   return im;
460 }
461
462 static void file_read_image_ppm(FILE *f) {
463   struct pam inpam;
464   unsigned char rgb_buf[3];
465   CanonImage *im;
466
467   pnm_readpaminit(f, &inpam, sizeof(inpam));
468   if (!(inpam.maxval == 255 &&
469         inpam.bytes_per_sample == 1 &&
470         inpam.format == RPPM_FORMAT))
471     fatal("PNM screenshot(s) file must be 8bpp 1 byte-per-sample RGB raw");
472
473   CANONICALISE_IMAGE(im, inpam.width, inpam.height, {
474     int rr= fread(&rgb_buf,1,3,f);
475     sysassert(!ferror(f));
476     if (rr!=3) fatal("PNM screenshot(s) file ends unexpectedly");
477
478     r= rgb_buf[0];
479     g= rgb_buf[1];
480     b= rgb_buf[2];
481   });
482
483   sysassert(!ferror(screenshot_file));
484
485   if (!(npages < MAX_PAGES))
486     fatal("Too many images in screenshots file; max is %d.\n", MAX_PAGES);
487
488   page_images[npages++]= im;
489 }
490
491 void read_one_screenshot(void) {
492   progress("reading screenshot...");
493   file_read_image_ppm(screenshot_file);
494   progress_log("read screenshot.");
495 }
496
497 void read_screenshots(void) {
498   struct stat stab;
499   
500   sysassert(! fstat(fileno(screenshot_file), &stab) );
501   
502   for (;;) {
503     if (S_ISREG(stab.st_mode)) {
504       long pos= ftell(screenshot_file);
505       if (pos == stab.st_size) break;
506     } else {
507       int c= fgetc(screenshot_file);
508       if (c==EOF) break;
509       ungetc(c, screenshot_file);
510     }
511     progress("reading screenshot %d...",npages);
512     file_read_image_ppm(screenshot_file);
513   }
514   sysassert(!ferror(screenshot_file));
515   progress_log("read %d screenshots.",npages);
516 }
517
518 static double find_aa_density(const RgbImage *ri, Point p, long background,
519                               long foreground, int fg_extra) {
520   Rgb here= ri_rgb(ri, p.x, p.y);
521
522   double alpha[3], alpha_mean=0;
523   int i;
524   for (i=0; i<3; i++) {
525     unsigned char here_chan= here       >> (i*8);
526     unsigned char bg_chan=   background >> (i*8);
527     unsigned char fg_chan=   foreground >> (i*8);
528     double alpha_chan=
529       ((double)here_chan    - (double)bg_chan) /
530       ((fg_chan + fg_extra) - (double)bg_chan);
531     alpha[i]= alpha_chan;
532     alpha_mean += alpha_chan * (1/3.0);
533   }
534
535   double thresh= 1.5/AAMAXVAL;
536   double alpha_min= alpha_mean - thresh;
537   double alpha_max= alpha_mean + thresh;
538   for (i=0; i<3; i++)
539     MUST( alpha_min <= alpha[i] && alpha[i] <= alpha_max,
540           MP(p);
541           MRGB(here);MRGB(background);MRGB(foreground);MI(fg_extra);
542           MF(alpha_min); MI(i);MF(alpha[i]);MF(alpha_max) );
543
544   if (   -1e-5 <  alpha_mean && alpha_mean <= 0.0     ) alpha_mean= 0.0;
545   if (1.0      <= alpha_mean && alpha_mean <= 1.0+1e-5) alpha_mean= 1.0;
546
547   MUST( 0 <= alpha_mean &&
548         (fg_extra ? (alpha_mean < 0.999) : (alpha_mean <= 1.0)),
549         MP(p);
550         MRGB(here);MRGB(background);MRGB(foreground);MI(fg_extra);
551         MF(alpha_mean); MF(alpha[0]);MF(alpha[1]);MF(alpha[2]); );
552
553   return alpha_mean;
554 }
555
556 static void find_commodity(int offset, Rect *rr) {
557   /* rr->tl.x==-1 if offset out of range */
558   rr->tl.y= commbasey - offset*comminty;
559   rr->br.y= rr->tl.y + comminty-2;
560   if (rr->tl.y < mainr.tl.y || rr->br.y > mainr.br.y) { rr->tl.x=-1; return; }
561   
562   rr->tl.x= mainr.tl.x;
563   rr->br.x= mainr.br.x;
564
565   if (rr->tl.y > mainr.tl.y)
566     REQUIRE_RECTANGLE(rr->tl.x,rr->tl.y-1, rr->br.x,rr->tl.y-1, "+");
567   if (rr->br.y < mainr.tl.y)
568     REQUIRE_RECTANGLE(rr->tl.x,rr->br.y+1, rr->br.x,rr->br.y+1, "+");
569 }
570
571 static void find_table_entry(Rect commod, int colno, Rect *cell) {
572   cell->tl.y= commod.tl.y;
573   cell->br.y= commod.br.y;
574   cell->tl.x= !colno ? commod.tl.x : colrightx[colno-1]+2;
575   cell->br.x=                        colrightx[colno];
576   debug_rect("cell", colno, *cell);
577
578   const RgbImage *ri= cim->rgb;
579   
580   Rgb background;
581   unsigned char chanbg[3];
582   long bg_count=0, light_count=0, dark_count=0;
583   Point p;
584
585   background= ri_rgb(ri, cell->br.x, cell->br.y);
586   memcpy(chanbg, RI_PIXEL(ri, cell->br.x, cell->br.y), 3);
587
588   FOR_P_RECT(p,*cell) {
589     const unsigned char *here_pixel= RI_PIXEL(ri, p.x, p.y);
590     int i;
591     for (i=0; i<3; i++) {
592       unsigned here= here_pixel[i];
593       if (here == chanbg[i]) bg_count++;
594       else if (here < chanbg[i]) dark_count  += (chanbg[i] - here)/4 + 1;
595       else if (here > chanbg[i]) light_count += (here - chanbg[i])/4 + 1;
596     }
597   }
598   long total_count= RECT_W(*cell) * RECT_H(*cell) * 3;
599
600   MUST( bg_count > total_count / 2,
601         MR(*cell);MIL(total_count);MIL(bg_count);
602         MIL(light_count);MIL(dark_count) );
603
604   if (bg_count == total_count)
605     return;
606
607   Rgb foreground;
608   double fg_extra;
609
610   if (light_count/16 > dark_count) {
611     foreground= 0xffffffU;
612     fg_extra= +1;
613   } else if (dark_count/16 > light_count) {
614     foreground= 0;
615     fg_extra= -1;
616   } else {
617     MUST( !"tell light from dark",
618           MR(*cell);MIL(total_count);MIL(bg_count);
619           MIL(light_count);MIL(dark_count);MRGB(background); );
620   }
621
622   debugf("TABLEENTRY col=%d %d,%d..%d,%d bg=%ld light=%ld dark=%ld\n",
623          colno, cell->tl.x,cell->tl.y, cell->br.x,cell->br.y,
624          bg_count, light_count, dark_count);
625   
626   int monochrome= 1;
627
628   FOR_P_RECT(p,*cell) {
629     double alpha= find_aa_density(ri,p,background,foreground,fg_extra);
630
631     int here_int= floor((AAMAXVAL+1)*alpha);
632     assert(here_int <= AAMAXVAL);
633     if (!(here_int==0 || here_int==AAMAXVAL)) monochrome=0;
634     cim->d[p.y * cim->w + p.x]= '0' + here_int;
635   }
636
637   debug_rect("cell0M", colno, *cell);
638
639   require_rectangle_r(*cell, "0123456789", __LINE__);
640 }
641
642 static void ocr_rectangle(Rect r, const OcrCellType ct, FILE *tsv_output) {
643   OcrResultGlyph *results, *res;
644
645   int w= r.br.x - r.tl.x + 1;
646   Pixcol cols[w+1];
647   int x,y;
648   for (x=0; x<w; x++) {
649     FILLZERO(cols[x]);
650     for (y=0; y<text_h; y++) {
651       Point here= { x+r.tl.x, y+r.tl.y };
652       int pixel= get_p(here);
653       if (pixel==' ') pixel= '0';
654       MUST( pixel >= '0' && pixel <= '0'+AAMAXVAL,
655             MC(pixel);MP(here);MSB(ocr_celltype_name(ct));MR(r); );
656       pixcol_p_add(&cols[x], y, pixel-'0');
657     }
658   }
659   FILLZERO(cols[w]);
660
661   results= ocr(rd,ct,w,cols);
662   for (res=results; res->s; res++)
663     fputs(res->s,tsv_output);
664 }
665
666 void analyse(FILE *tsv_output) {
667   Rect thisr, entryr;
668   int page, tryrect, colno;
669
670   for (page=0; page<npages; page++) {
671     find_structure(page_images[page], 0,0,0,0);
672
673     if (!page)
674       check_correct_commodities();
675
676     if (!rd)
677       rd= ocr_init(text_h);
678
679     progress("Scanning page %d...",page);
680
681     for (tryrect= +cim->h; tryrect >= -cim->h; tryrect--) {
682       find_commodity(tryrect, &thisr);
683       if (thisr.tl.x < 0)
684         continue;
685       debug_rect("commod",tryrect, thisr);
686
687       const char *tab= "";
688       for (colno=0; colno<columns; colno++) {
689         find_table_entry(thisr,colno,&entryr);
690         fputs(tab, tsv_output);
691         ocr_rectangle(entryr,
692                       colno<TEXT_COLUMNS
693                       ? &ocr_celltype_text
694                       : &ocr_celltype_number,
695                       tsv_output);
696         tab= "\t";
697       }
698       fputs("\n", tsv_output);
699       sysassert(!ferror(tsv_output));
700       sysassert(!fflush(tsv_output));
701     }
702   }
703   progress("Commodity table scan complete.");
704 }
705
706 //static Rect islandnamer;
707
708 DEBUG_DEFINE_SOME_DEBUGF(structcolon,colondebugf)
709
710 Rect find_sunshine_widget(void) {
711   Rect sunshiner;
712
713   sunshiner.tl.x= cim->w - 1034 +  885;
714   sunshiner.br.x= cim->w - 1034 + 1020;
715   sunshiner.tl.y= 227;
716   sunshiner.br.y= 228;
717
718   ADJUST_BOX(sunshiner,"o*",>=,30, 100,MUST, tl,y,-1);
719   ADJUST_BOX(sunshiner,"o*",>=,30, 100,MUST, br,y,+1);
720   debug_rect("sunshiner",0, sunshiner);
721
722   MUST(sunshiner.br.y - sunshiner.tl.y > 20, MR(sunshiner));
723   sunshiner.br.y--;
724
725   ADJUST_BOX(sunshiner,"o",>=,20, (cim->w - 1034 + 700), MUST, tl,x,-1);
726   ADJUST_BOX(sunshiner,"o",>=,20,  cim->w,               MUST, br,x,+1);
727   debug_rect("sunshiner",1, sunshiner);
728   return sunshiner;
729 }
730
731 void find_islandname(void) {
732   find_structure(page_images[0], 0,0,0,0);
733
734   RgbImage *ri= alloc_rgb_image(page_images[0]->rgb->w,
735                                 page_images[0]->rgb->h);
736   memcpy(ri->data, page_images[0]->rgb->data, ri->w * ri->h * 3);
737   
738   Rect sunshiner= find_sunshine_widget();
739   char sunshine[MAXIMGIDENT], archisland[MAXIMGIDENT];
740
741   const unsigned char *srcp;
742   unsigned char *destp, *endp;
743   for (srcp=page_images[0]->rgb->data, destp=ri->data,
744          endp= ri->data + 3 * ri->w * ri->h;
745        destp < endp;
746        srcp++, destp++) {
747     unsigned char c= *srcp & 0xf0;
748     *destp= c | (c>>4);
749   }
750
751   identify_rgbimage(ri, sunshiner, sunshine, "sunshine widget");
752   
753   if (!memcmp(sunshine,"Vessel ",5)) {
754     Rect islandnamer;
755     
756     islandnamer.tl.x= cim->w - 1034 +  885;
757     islandnamer.br.x= cim->w - 1034 + 1020;
758     islandnamer.tl.y=                 128;
759     islandnamer.br.y=                 156;
760
761     ADJUST_BOX(islandnamer,"o",>=,5, 0,      MUST, tl,y,+1);
762     ADJUST_BOX(islandnamer,"o",>=,5, cim->h, MUST, br,y,-1);
763
764     ADJUST_BOX(islandnamer,"o",>=,1, 0,      MUST, tl,x,+1);
765     ADJUST_BOX(islandnamer,"o",>=,1, cim->w, MUST, br,x,-1);
766
767     debug_rect("islandnamer",0, islandnamer);
768 //    int larger_islandnamebry= islandnamer.tl.y + 25;
769 //    MUST(islandnamer.br.y < larger_islandnamebry,
770 //       MR(islandnamer);MI(larger_islandnamebry));
771 //    islandnamer.br.y = larger_islandnamebry;
772     debug_rect("islandnamer",1, islandnamer);
773
774     int x,y,i;
775     for (x=islandnamer.tl.x; x<=islandnamer.br.x; x++)
776       for (y=islandnamer.tl.y; y<=islandnamer.br.y; y++) {
777         if (RI_PIXEL(ri,x,y)[0] < 0x40) {
778           for (i=0; i<3; i++) {
779             RI_PIXEL(ri,x,y)[i]= 0;
780           }
781         }
782       }
783
784     identify_rgbimage(ri, islandnamer, archisland, "island");
785   } else if (!strcmp(sunshine,"Land - Ahoy!")) {
786     Rect islandnamer;
787
788     islandnamer.tl.x= (sunshiner.tl.x + sunshiner.br.x) / 2;
789     islandnamer.tl.y= sunshiner.tl.y + 100;
790     islandnamer.br= islandnamer.tl;
791     debug_rect("islandnamer",__LINE__, islandnamer);
792     
793     WALK_UNTIL_MUST(islandnamer.tl,y, -1, sunshiner.br.y, 'H');
794     WALK_UNTIL_MUST(islandnamer.tl,x, -1, 0,              'o');
795     WALK_UNTIL_MUST(islandnamer.br,x, +1, cim->w,         'o');
796     debug_rect("islandnamer",__LINE__, islandnamer);
797
798 #define RW (RECT_W(islandnamer))
799 #define RH (RECT_H(islandnamer))
800
801     ADJUST_BOX(islandnamer,"O",>=,RW-4, cim->h, MUST,br,y,+1);
802     debug_rect("islandnamer",__LINE__, islandnamer);
803
804     islandnamer.br.y += 2;
805
806     ADJUST_BOX(islandnamer,"*",<,RW, cim->h, MUST,br,y,+1);
807     debug_rect("islandnamer",__LINE__, islandnamer);
808
809     islandnamer.tl.y= islandnamer.br.y-1;
810     islandnamer.br.y= islandnamer.br.y+1;
811     debug_rect("islandnamer",__LINE__, islandnamer);
812
813     ADJUST_BOX(islandnamer,"*",>=,RW, cim->h, MUST,br,y,+1);
814     debug_rect("islandnamer",__LINE__, islandnamer);
815
816     ADJUST_BOX(islandnamer,"*",<, RH, cim->w, MUST,tl,x,+1);
817     debug_rect("islandnamer",__LINE__, islandnamer);
818
819     MUST( RECT_H(islandnamer) <= 30, MR(islandnamer));
820
821     Point p;
822     int nspaces=1, might_be_colon=0;
823     uint32_t colon_pattern= 0;
824     p.y=-1;
825
826     for (p.x=islandnamer.br.x; p.x>islandnamer.tl.x; p.x--) {
827       colondebugf("structcolon: x=%4d nsp=%2d mbc=%d cp=%08"PRIx32" ",
828                   p.x, nspaces, might_be_colon, colon_pattern);
829
830       uint32_t pattern=0;
831       int runs[32], nruns=0;
832       runs[0]=0; runs[1]=0;
833       
834       for (p.y=islandnamer.tl.y; p.y<=islandnamer.br.y; p.y++) {
835         pattern <<= 1;
836         double alpha= find_aa_density(ri,p, 0xCCCCAA,0x002255,0);
837         if (alpha >= 0.49) {
838            runs[nruns]++;
839            pattern |= 1u;
840         } else {
841           if (runs[nruns]) {
842             nruns++;
843             runs[nruns]=0;
844           }
845         }
846       }
847
848       colondebugf(" pat=%08"PRIx32" nruns=%d runs[]={%d,%d..} ",
849                   pattern, nruns, runs[0],runs[1]);
850
851       if (!pattern) {
852         if (might_be_colon)
853           /* omg it _is_ a colon */
854           goto colon_found;
855         nspaces++;
856         might_be_colon=0;
857       } else {
858         if (nruns==2 && runs[1]==runs[0]) {
859           if (!nspaces) {
860             if (pattern==colon_pattern)
861               goto ok_might_be_colon;
862           } else if (nspaces>=2) {
863             colon_pattern= pattern;
864             might_be_colon=1;
865             goto ok_might_be_colon;
866           }
867         } else if (nruns==1 && runs[0]==1 && might_be_colon) {
868           goto colon_found;
869         }
870         might_be_colon=0;
871       ok_might_be_colon:
872         nspaces= 0;
873       }
874       colondebugf(" nsp=%2d mbc=%d\n", nspaces, might_be_colon);
875     }
876     MUST(!"colon found", MP(p);MR(islandnamer) );
877
878   colon_found:
879     colondebugf(" found\n");
880     islandnamer.br.x= p.x;
881
882     identify_rgbimage(ri, islandnamer, archisland, "island");
883   } else {
884
885     MUST(!"sunshine shows ship or ahoy", MS(sunshine) );
886
887   }
888
889   char *delim= strstr(archisland," - ");
890   assert(delim);
891   archipelago= masprintf("%.*s", (int)(delim-archisland), archisland);
892   island= masprintf("%s", delim+3);
893
894 }