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