chiark / gitweb /
Show begin of XShmGetImage
[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 const 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 = { START_MAIN,START_MAIN };
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 const CanonColourInfo canoncolourinfos[]= {
55   { 0x475A5E, '*' }, /* edge */
56   { 0x2C5F7A, '*' }, /* edge just under box heading shadow */
57   { 0xC5C7AE, '*' }, /* blank area of partial commodities list */
58   { 0x6B828C, '*' }, /* background of ship status meter area */
59   { 0x934405, '*' }, /* border of ship meter area */
60   { 0x7D9094, '+' }, /* interbox */
61   { 0x022158, 'O' }, /* ahoy /w output foreground */
62   { 0xB5B686, 'H' }, /* ahoy /w output heading background */
63
64   { 0xBDC5BF, ' ' }, /* background - pale  Sugar cane, etc. */
65   { 0xADB5AF, ' ' }, /* background - dark                   */
66   { 0xC7E1C3, ' ' }, /* background - pale  Swill, etc.      */
67   { 0xB5CFB1, ' ' }, /* background - dark                   */
68   { 0xD6CEB0, ' ' }, /* background - pale  Madder, etc.     */
69   { 0xC8C0A2, ' ' }, /* background - dark                   */
70   { 0xE0E1D3, ' ' }, /* background - pale  Lorandite, etc.  */
71   { 0xD0D1C3, ' ' }, /* background - dark                   */
72   { 0xE5E6C1, ' ' }, /* background - pale  Cloth            */
73   { 0xD7D8B3, ' ' }, /* background - dark                   */
74   { 0xEDDED9, ' ' }, /* background - pale  Dye              */
75   { 0xDACBC6, ' ' }, /* background - dark                   */
76   { 0xD3DEDF, ' ' }, /* background - pale  Paint            */
77   { 0xC5D0D1, ' ' }, /* background - dark                   */
78   { 0xDCD1CF, ' ' }, /* background - pale  Enamel           */
79   { 0xCEC3C1, ' ' }, /* background - dark                   */
80   { 0xF3F6F5, ' ' }, /* background - pale  fruit            */
81   { 0xE2E7E5, ' ' }, /* background - dark                   */
82
83   { 0x000000, 'o' }, /* foreground */
84   { 0xD4B356, ' ' }, /* background (cursor) */
85   { 0xFFFFFF, 'o' }, /* foreground (cursor) */
86
87   { 0x5B93BF, '_' }, /* selector dropdown background */
88   { 0xD7C94F, 'X' }, /* selector dropdown foreground */
89   { 0,0 }
90 };
91
92
93 static void mustfail1(const char *file, int line, const char *what) {
94   fprintf(stderr,
95           "\n\n"
96           "Unable to figure out contents of YPP client display.\n"
97           " Check that your client is logged in has the correct display.\n"
98           " If that isn't the problem, please report this as a fault.\n\n"
99           "Technical details:"
100           " %s:%d: requirement failed:\n"
101           " %s\n",
102           file, line, what);
103 }
104 static void mustfail2(void) NORET;
105 static void mustfail2(void) {
106   fprintf(stderr, "\n\nGiving up.\n");
107   exit(8);
108 }
109
110 #define MUST(x, ifnot) do{                      \
111     if (!(x)) {                                 \
112       mustfail1(__FILE__,__LINE__,#x);          \
113       ifnot;                                    \
114       mustfail2();                              \
115     }                                           \
116   }while(0)
117
118 #define MP(v) fprintf(stderr," %s=%d,%d",#v,(v).x,(v).y)
119 #define MI(v) fprintf(stderr," %s=%d",   #v,(v))
120 #define MC(v) fprintf(stderr," %s='%c'", #v,(v))
121 #define MS(v) fprintf(stderr," %s=\"%s\"", #v,(v))
122 #define MSB(v) fprintf(stderr," %s", (v))
123 #define MR(v) fprintf(stderr," %s=%d,%d..%d,%d",\
124                       #v,(v).tl.x,(v).tl.y,(v).br.x,(v).br.y)
125
126
127 #define REQUIRE_RECTANGLE(tlx,tly,brx,bry,ok) \
128  require_rectangle(tlx, tly, brx, bry, ok, __LINE__);
129
130 static void require_rectangle(int tlx, int tly, int brx, int bry,
131                               const char *ok, int lineno) {
132   Point p;
133   for (p.x=tlx; p.x<=brx; p.x++)
134     for (p.y=tly; p.y<=bry; p.y++) {
135       int c= get_p(p);
136       MUST( strchr(ok,c), ({
137              Rect rm={{tlx,tly},{brx,bry}};
138              MI(lineno),MR(rm);MP(p);MS(ok);
139       }));
140     }
141 }
142 static void require_rectangle_r(Rect rr, const char *ok, int lineno) {
143   require_rectangle(rr.tl.x,rr.tl.y, rr.br.x,rr.br.y, ok, lineno);
144 }
145
146 static void debug_rect(const char *what, int whati, Rect rr) {
147   if (!DEBUGP(rect)) return;
148   int y,w;
149   fprintf(debug, "%s %d: %d,%d..%d,%d:\n", what, whati,
150           rr.tl.x,rr.tl.y, rr.br.x,rr.br.y);
151   w= rr.br.x - rr.tl.x + 1;
152   for (y=rr.tl.y; y<=rr.br.y; y++) {
153     fprintf(debug, "%4d%*s|", y, rr.tl.x,"");
154     fwrite(cim->d + y*cim->w + rr.tl.x, 1, w, debug);
155     fputc('|',debug);
156     fputc('\n',debug);
157   }
158   debug_flush();
159 }
160
161 static int commod_selector_matches(Rect search, const char *const *all,
162                                    int allh, int allw) {
163   int alloffy, alloffx;
164   for (alloffy=0; alloffy < search.br.y; alloffy++) {
165     if (alloffy+allh-1 < search.tl.y) continue;
166     for (alloffx=search.tl.x; alloffx+allw-1 <= search.br.x; alloffx++) {
167       int good=0, bad=0;
168       int x,y;
169       for (x=0; x<allw; x++)
170         for (y=0; y<allh; y++) {
171           int want= all[y][x];
172           if (want==' ') continue;
173           if (get(alloffx+x, alloffy+y) == want)
174             good++;
175           else
176             bad++;
177         }
178       debugf("CHECKCOMMOD alloff=%d,%d good=%d bad=%d\n",
179              alloffx,alloffy, good,bad);
180       if (good > 20*bad)
181         return 1;
182     }
183   }
184   return 0;
185 }
186
187 #define WALK_UNTIL(point,coord,increm,last,edge)                        \
188   for (;;) {                                                            \
189     if ((point).coord == (last)+(increm)) break;                        \
190     if (get_p((point)) == (edge)) { (point).coord -= (increm); break; } \
191     (point).coord += (increm);                                          \
192   }
193
194 #define WALK_UNTIL_MUST(point,coord,increm,last,edge)   \
195   do {                                                  \
196     WALK_UNTIL(point,coord,increm,last,edge);           \
197     MUST( (point).coord != (last)+(increm),             \
198           MP(point); MI(increm); MI(last); MC(edge);    \
199           );                                            \
200   }while(0)
201
202 #define ADJUST_BOX(search,insidechrs,want, lim,LIMIT_MUST, TLBR,XY,increm) \
203   for (;;) {                                                               \
204     LIMIT_MUST( (search).tl.XY != (search).br.XY &&                        \
205                 (search).tl.XY != (lim),                                   \
206                 MR((search));MSB(#TLBR);MSB(#XY) );                        \
207     int got=0;                                                             \
208     Point p=(search).tl;                                                   \
209     for (p.XY=(search).TLBR.XY;                                            \
210          p.OTHERCOORD_##XY <= (search).br.OTHERCOORD_##XY;                 \
211          p.OTHERCOORD_##XY++)                                              \
212       got += !!strchr(insidechrs, get_p(p));                               \
213     if (got >= (want))                                                     \
214       break;                                                               \
215     (search).TLBR.XY += increm;                                            \
216   }
217
218 void find_structure(const CanonImage *im, int *max_relevant_y_r) {
219   cim= im;
220   
221   Rect whole = { {0,0}, {cim->w-1,cim->h-1} };
222
223   if (DEBUGP(rect)) {
224     int xscaleunit, y,x;
225     for (y=0, xscaleunit=1; y<4; y++, xscaleunit*=10) {
226       fprintf(debug,"     ");
227       for (x=0; x<=cim->w; x++) {
228         if (x % xscaleunit) fputc(' ',debug);
229         else fprintf(debug,"%d",(x / xscaleunit)%10);
230       }
231       fputc('\n',debug);
232     }
233   }
234
235   WALK_UNTIL_MUST(mainr.tl, x,-1, whole.tl.x, '*');
236   WALK_UNTIL_MUST(mainr.tl, y,-1, whole.tl.y, '*');
237   WALK_UNTIL_MUST(mainr.br, x,+1, whole.br.x, '*');
238   WALK_UNTIL_MUST(mainr.br, y,+1, whole.br.y, '*');
239
240   REQUIRE_RECTANGLE(mainr.tl.x-1, mainr.tl.y, mainr.tl.x-1, mainr.br.y, "*");
241   REQUIRE_RECTANGLE(mainr.br.x+1, mainr.tl.y, mainr.br.x+1, mainr.br.y, "*");
242   REQUIRE_RECTANGLE(mainr.tl.x, mainr.tl.y-1, mainr.br.x, mainr.tl.y-1, "*");
243   REQUIRE_RECTANGLE(mainr.tl.x, mainr.br.y+1, mainr.br.x, mainr.br.y+1, "*");
244
245 #define CHECK_STRIP_BORDER(tlbr,xy,increm)              \
246   do {                                                  \
247     Point csb_p;                                        \
248     Rect csb_r;                                         \
249     csb_p= mainr.tl;                                    \
250     csb_p.xy= mainr.tlbr.xy;                            \
251     if (get_p(csb_p)=='+') {                            \
252       csb_r= mainr;                                     \
253       csb_r.tl.xy= csb_p.xy;                            \
254       csb_r.br.xy= csb_p.xy;                            \
255       require_rectangle_r(csb_r, "+", __LINE__);        \
256       mainr.tlbr.xy += increm;                          \
257     }                                                   \
258   } while(0)
259
260   debug_rect("mainr",0, mainr);
261
262   CHECK_STRIP_BORDER(tl,x,+1);
263   CHECK_STRIP_BORDER(tl,y,+1);
264   CHECK_STRIP_BORDER(br,x,-1);
265   CHECK_STRIP_BORDER(br,y,-1);
266
267   debug_rect("mainr",1, mainr);
268
269   Point up = START_MAIN;
270   WALK_UNTIL_MUST(up, y,-1, mainr.tl.y, '+');
271
272   Point down = START_MAIN;
273   down.y++;
274   WALK_UNTIL_MUST(down, y,+1, mainr.br.y, '+');
275
276   commbasey= up.y;
277   comminty= down.y - up.y + 2;
278
279   Point across= { mainr.tl.x, commbasey };
280   int colno=0;
281   for (;;) {
282     MUST( get_p(across) != '+', MI(colno);MP(across);MR(mainr);MI(commbasey) );
283     WALK_UNTIL(across, x,+1, mainr.br.x, '+');
284     MUST( colno < MAX_COLUMNS, MP(across);MR(mainr);MI(commbasey); );
285     int colrx= across.x;
286     if (colrx > mainr.br.x) colrx= mainr.br.x;
287     if (colno < INTERESTING_COLUMNS)
288       colrightx[colno]= colrx;
289       
290     colno++;
291     
292     if (across.x >= mainr.br.x-1)
293       break;
294
295     across.x++;
296     REQUIRE_RECTANGLE(across.x,mainr.tl.y, across.x,mainr.br.y, "+");
297     across.x++;
298   }
299   MUST( colno >= MIN_COLUMNS, MI(colno);MR(mainr);MP(across); );
300
301 #define SET_ONCE(var,val) do{                                           \
302     int v= (val);                                                       \
303     if ((var)==-1) (var)= v;                                            \
304     else MUST( (var) == v, MSB(#var);MI((var));MI(v);MR(mainr); );      \
305   }while(0)
306
307   SET_ONCE(columns, colno);
308   SET_ONCE(text_h, comminty - 1);
309   if (max_relevant_y_r)
310     SET_ONCE(*max_relevant_y_r, mainr.br.y + 10);
311 }                   
312
313 void check_correct_commodities(void) {
314   Rect search= { { 50,39 }, { 130,59 } };
315
316   ADJUST_BOX(search,"_",10, cim->h, MUST, tl,y,+1);
317   ADJUST_BOX(search,"_",10, 0,      MUST, br,y,-1);
318
319   debug_rect("commodselr",1, search);
320
321   static const char *all_small[]= {
322     "   ___________________________________   ",
323     "  ________X____X__X____________________  ",
324     " ________ X___ X_ X_____XXXXXXXXXXX_____ ",
325     "_________X_X__ X_ X______XXXXXXXXX_______",
326     "________ X X__ X_ X_______XXXXXXX________",
327     "________X_ _X_ X_ X________XXXXX_________",
328     "_______ X__ X_ X_ X_________XXX__________",
329     "_______XXXXXXX X_ X__________X___________",
330     " _____ X     X X_ X______________________",
331     "  ____X_____ _XX_ X______________________",
332     "   __ _______  __ ______________________ ",
333   };
334   static const char *all_big[]= {
335     "???_______________________________________???",
336     "??_________________________________________??",
337     "?_________X______X___X______________________?",
338     "_________?X_____?X__?X______XXXXXXXXXXX______",
339     "_________X_X____?X__?X_______XXXXXXXXX_______",
340     "________?X?X____?X__?X________XXXXXXX________",
341     "________X_?_X___?X__?X_________XXXXX_________",
342     "_______?X__?X___?X__?X__________XXX__________",
343     "_______?XXXXX___?X__?X___________X___________",
344     "_______X????_X__?X__?X_______________________",
345     "?_____?X____?X__?X__?X_______________________",
346     "??____X_____?_X_?X__?X_______________________",
347     "???__?_______?__?___?_______________________?",
348   };
349
350 #define COMMOD_SELECTOR_MATCHES(all)                            \
351   commod_selector_matches(search, all,                          \
352                           sizeof((all))/sizeof((all)[0]),       \
353                           strlen((all)[0]))
354
355   if (!(COMMOD_SELECTOR_MATCHES(all_small) ||
356         COMMOD_SELECTOR_MATCHES(all_big)))
357     fatal("Commodities selector not set to `All'.");
358 }
359
360 CanonImage *alloc_canon_image(int w, int h) {
361   CanonImage *im= mmalloc(sizeof(CanonImage) + w*h);
362   im->w= w;
363   im->h= h;
364   memset(im->d,'?',w*h);
365   return im;
366 }
367
368 static void file_read_image_ppm(FILE *f) {
369   struct pam inpam;
370   unsigned char rgb_buf[3];
371   CanonImage *im;
372   RgbImage *ri=0;
373
374   pnm_readpaminit(f, &inpam, sizeof(inpam));
375   if (!(inpam.maxval == 255 &&
376         inpam.bytes_per_sample == 1 &&
377         inpam.format == RPPM_FORMAT))
378     fatal("PNM screenshot(s) file must be 8bpp 1 byte per sample RGB");
379
380   if (!npages)
381     page0_rgbimage= ri= alloc_rgb_image(inpam.width, inpam.height);
382
383   CANONICALISE_IMAGE(im, inpam.width, inpam.height, {
384     int r= fread(&rgb_buf,1,3,f);
385     sysassert(!ferror(f));
386     if (r!=3) fatal("PNM screenshot(s) file ends unexpectedly");
387
388     rgb=
389         ((unsigned long)rgb_buf[0]<<16) |
390         ((unsigned long)rgb_buf[1]<<8) |
391                        (rgb_buf[2]);
392
393     if (ri)
394       CANONIMG_ALSO_STORERGB(ri);
395   });
396
397   sysassert(!ferror(screenshot_file));
398
399   if (!(npages < MAX_PAGES))
400     fatal("Too many images in screenshots file; max is %d.\n", MAX_PAGES);
401
402   page_images[npages++]= im;
403 }
404
405 void read_one_screenshot(void) {
406   progress("reading screenshot...");
407   file_read_image_ppm(screenshot_file);
408   progress_log("read screenshot.");
409 }
410
411 void read_screenshots(void) {
412   struct stat stab;
413   
414   sysassert(! fstat(fileno(screenshot_file), &stab) );
415   
416   for (;;) {
417     if (S_ISREG(stab.st_mode)) {
418       long pos= ftell(screenshot_file);
419       if (pos == stab.st_size) break;
420     } else {
421       int c= fgetc(screenshot_file);
422       if (c==EOF) break;
423       ungetc(c, screenshot_file);
424     }
425     progress("reading screenshot %d...",npages);
426     file_read_image_ppm(screenshot_file);
427   }
428   sysassert(!ferror(screenshot_file));
429   progress_log("read %d screenshots.",npages);
430 }
431
432 static void find_commodity(int offset, Rect *rr) {
433   /* rr->tl.x==-1 if offset out of range */
434   rr->tl.y= commbasey - offset*comminty;
435   rr->br.y= rr->tl.y + comminty-2;
436   if (rr->tl.y < mainr.tl.y || rr->br.y > mainr.br.y) { rr->tl.x=-1; return; }
437   
438   rr->tl.x= mainr.tl.x;
439   rr->br.x= mainr.br.x;
440
441   if (rr->tl.y > mainr.tl.y)
442     REQUIRE_RECTANGLE(rr->tl.x,rr->tl.y-1, rr->br.x,rr->tl.y-1, "+");
443   if (rr->br.y < mainr.tl.y)
444     REQUIRE_RECTANGLE(rr->tl.x,rr->br.y+1, rr->br.x,rr->br.y+1, "+");
445 }
446
447 static void find_table_entry(Rect commod, int colno, Rect *cellr) {
448   cellr->tl.y= commod.tl.y;
449   cellr->br.y= commod.br.y;
450   cellr->tl.x= !colno ? commod.tl.x : colrightx[colno-1]+2;
451   cellr->br.x=                        colrightx[colno];
452   debug_rect("cell", colno, *cellr);
453   require_rectangle_r(*cellr, " o", __LINE__);
454 }
455
456 static void ocr_rectangle(Rect r, const OcrCellType ct, FILE *tsv_output) {
457   OcrResultGlyph *results, *res;
458
459   int w= r.br.x - r.tl.x + 1;
460   Pixcol cols[w+1];
461   int x,y;
462   for (x=0; x<w; x++) {
463     Pixcol cx, rv;
464     for (y=0, cx=0, rv=1; y<text_h; y++, rv<<=1) {
465       Point here= { x+r.tl.x, y+r.tl.y };
466       int pixel= get_p(here);
467       switch (pixel) {
468       case ' ':           break;
469       case 'o': cx |= rv; break;
470       default:
471         MUST(!"wrong pixel",
472              MC(pixel);MP(here);MSB(ocr_celltype_name(ct));MR(r); );
473       }
474     }
475     cols[x]= cx;
476   }
477   cols[w]= 0;
478
479   results= ocr(rd,ct,w,cols);
480   for (res=results; res->s; res++)
481     fputs(res->s,tsv_output);
482 }
483
484 void analyse(FILE *tsv_output) {
485   Rect thisr, entryr;
486   int page, tryrect, colno;
487
488   for (page=0; page<npages; page++) {
489     find_structure(page_images[page], 0);
490
491     if (!page)
492       check_correct_commodities();
493
494     if (!rd)
495       rd= ocr_init(text_h);
496
497     progress("Scanning page %d...",page);
498
499     for (tryrect= +cim->h; tryrect >= -cim->h; tryrect--) {
500       find_commodity(tryrect, &thisr);
501       if (thisr.tl.x < 0)
502         continue;
503       debug_rect("commod",tryrect, thisr);
504
505       const char *tab= "";
506       for (colno=0; colno<columns; colno++) {
507         find_table_entry(thisr,colno,&entryr);
508         fputs(tab, tsv_output);
509         ocr_rectangle(entryr,
510                       colno<TEXT_COLUMNS
511                       ? &ocr_celltype_text
512                       : &ocr_celltype_number,
513                       tsv_output);
514         tab= "\t";
515       }
516       fputs("\n", tsv_output);
517       sysassert(!ferror(tsv_output));
518       sysassert(!fflush(tsv_output));
519     }
520   }
521   progress("Commodity table scan complete.");
522 }
523
524 //static Rect islandnamer;
525
526 DEBUG_DEFINE_SOME_DEBUGF(structcolon,colondebugf)
527
528 Rect find_sunshine_widget(void) {
529   Rect sunshiner;
530
531   sunshiner.tl.x= cim->w - 1034 +  885;
532   sunshiner.br.x= cim->w - 1034 + 1020;
533   sunshiner.tl.y= 227;
534   sunshiner.br.y= 228;
535
536   ADJUST_BOX(sunshiner,"o*",30, 100,MUST, tl,y,-1);
537   ADJUST_BOX(sunshiner,"o*",30, 100,MUST, br,y,+1);
538   debug_rect("sunshiner",0, sunshiner);
539
540   MUST(sunshiner.br.y - sunshiner.tl.y > 20, MR(sunshiner));
541   sunshiner.br.y--;
542
543   ADJUST_BOX(sunshiner,"o",20, (cim->w - 1034 + 700), MUST, tl,x,-1);
544   ADJUST_BOX(sunshiner,"o",20,  cim->w,               MUST, br,x,+1);
545   debug_rect("sunshiner",1, sunshiner);
546   return sunshiner;
547 }
548
549 void find_islandname(RgbImage *ri) {
550   Rect sunshiner= find_sunshine_widget();
551   char sunshine[MAXIMGIDENT], archisland[MAXIMGIDENT];
552
553   const unsigned char *srcp;
554   unsigned char *destp, *endp;
555   for (srcp=page0_rgbimage->data, destp=ri->data,
556          endp= ri->data + 3 * ri->w * ri->h;
557        destp < endp;
558        srcp++, destp++) {
559     unsigned char c= *srcp & 0xf0;
560     *destp= c | (c>>4);
561   }
562
563   identify_rgbimage(ri, sunshiner, sunshine, "sunshine widget");
564   
565   if (!memcmp(sunshine,"Vessel ",5)) {
566     Rect islandnamer;
567     
568     islandnamer.tl.x= cim->w - 1034 +  885;
569     islandnamer.br.x= cim->w - 1034 + 1020;
570     islandnamer.tl.y=                 128;
571     islandnamer.br.y=                 156;
572
573     ADJUST_BOX(islandnamer,"o",5, 0,      MUST, tl,y,+1);
574     ADJUST_BOX(islandnamer,"o",5, cim->h, MUST, br,y,-1);
575
576     ADJUST_BOX(islandnamer,"o",1, 0,      MUST, tl,x,+1);
577     ADJUST_BOX(islandnamer,"o",1, cim->w, MUST, br,x,-1);
578
579     debug_rect("islandnamer",0, islandnamer);
580 //    int larger_islandnamebry= islandnamer.tl.y + 25;
581 //    MUST(islandnamer.br.y < larger_islandnamebry,
582 //       MR(islandnamer);MI(larger_islandnamebry));
583 //    islandnamer.br.y = larger_islandnamebry;
584     debug_rect("islandnamer",1, islandnamer);
585
586     int x,y,i;
587     for (x=islandnamer.tl.x; x<=islandnamer.br.x; x++)
588       for (y=islandnamer.tl.y; y<=islandnamer.br.y; y++) {
589         if (RI_PIXEL(ri,x,y)[0] < 0x40) {
590           for (i=0; i<3; i++) {
591             RI_PIXEL(ri,x,y)[i]= 0;
592           }
593         }
594       }
595
596     identify_rgbimage(ri, islandnamer, archisland, "island");
597   } else {
598     Rect islandnamer;
599
600     islandnamer.tl.x= (sunshiner.tl.x + sunshiner.br.x) / 2;
601     islandnamer.tl.y= sunshiner.tl.y + 100;
602     islandnamer.br= islandnamer.tl;
603     debug_rect("islandnamer",__LINE__, islandnamer);
604     
605     WALK_UNTIL_MUST(islandnamer.tl,y, -1, sunshiner.br.y, 'H');
606     WALK_UNTIL_MUST(islandnamer.tl,x, -1, 0,              'o');
607     WALK_UNTIL_MUST(islandnamer.br,x, +1, cim->w,         'o');
608     debug_rect("islandnamer",__LINE__, islandnamer);
609
610     require_rectangle_r(islandnamer, "O*", __LINE__);
611
612     int rw= RECT_W(islandnamer);
613     ADJUST_BOX(islandnamer,"O",rw-4, cim->h, MUST,br,y,+1);
614     debug_rect("islandnamer",__LINE__, islandnamer);
615
616     islandnamer.br.y += 2;
617     ADJUST_BOX(islandnamer,"O",1,    cim->h, MUST,br,y,+1);
618     debug_rect("islandnamer",__LINE__, islandnamer);
619
620     islandnamer.tl.y= islandnamer.br.y-1;
621     islandnamer.br.y= islandnamer.br.y+1;
622     ADJUST_BOX(islandnamer,"*",rw,   cim->h, MUST,br,y,+1);
623     ADJUST_BOX(islandnamer,"O",1,    cim->w, MUST,tl,x,+1);
624     debug_rect("islandnamer",__LINE__, islandnamer);
625
626     MUST( RECT_H(islandnamer) <= 31, MR(islandnamer));
627
628     Point p;
629     int nspaces=1, might_be_colon=0;
630     uint32_t colon_pattern= 0;
631
632     for (p.x=islandnamer.br.x; p.x>islandnamer.tl.x; p.x--) {
633       colondebugf("structcolon: x=%4d nsp=%2d mbc=%d cp=%08"PRIx32" ",
634                   p.x, nspaces, might_be_colon, colon_pattern);
635
636       uint32_t pattern=0;
637       int runs[32], nruns=0;
638       runs[0]=0; runs[1]=0;
639       
640       for (p.y=islandnamer.tl.y; p.y<=islandnamer.br.y; p.y++) {
641         pattern <<= 1;
642         switch (get_p(p)) {
643         case 'O': runs[nruns]++;                         pattern |= 1u; break;
644         case '*': if (runs[nruns]) { nruns++; runs[nruns]=0; }          break;
645         default: abort();
646         }
647       }
648
649       colondebugf(" pat=%08"PRIx32" nruns=%d runs[]={%d,%d..} ",
650                   pattern, nruns, runs[0],runs[1]);
651
652       if (!pattern) {
653         if (might_be_colon)
654           /* omg it _is_ a colon */
655           goto colon_found;
656         nspaces++;
657         might_be_colon=0;
658       } else {
659         if (nruns==2 && runs[1]==runs[0]) {
660           if (!nspaces) {
661             if (pattern==colon_pattern)
662               goto ok_might_be_colon;
663           } else if (nspaces>=2) {
664             colon_pattern= pattern;
665             might_be_colon=1;
666             goto ok_might_be_colon;
667           }
668         }
669         might_be_colon=0;
670       ok_might_be_colon:
671         nspaces= 0;
672       }
673       colondebugf(" nsp=%2d mbc=%d\n", nspaces, might_be_colon);
674     }
675     MUST(!"colon found", MP(p);MR(islandnamer) );
676
677   colon_found:
678     colondebugf(" found\n");
679     islandnamer.br.x= p.x;
680
681     identify_rgbimage(ri, islandnamer, archisland, "island");
682
683     assert(!"not vessel");
684   }
685
686   char *delim= strstr(archisland," - ");
687   assert(delim);
688   archipelago= masprintf("%.*s", delim-archisland, archisland);
689   island= masprintf("%s", delim+3);
690
691   free(ri);
692 }