2 * Parsing of the structure of the YPP client's displayed image
5 * This is part of ypp-sc-tools, a set of third-party tools for assisting
6 * players of Yohoho Puzzle Pirates.
8 * Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
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.
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.
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/>.
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.
28 #include "structure.h"
30 static CanonImage *cim;
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); }
35 DEBUG_DEFINE_DEBUGF(struct)
37 #define START_MAIN {200,200}
39 #define INTERESTING_COLUMNS 7
40 #define TEXT_COLUMNS 2
44 static int commbasey, comminty;
45 static int colrightx[INTERESTING_COLUMNS];
46 static int text_h=-1, columns=-1;
49 char *archipelago, *island;
51 #define OTHERCOORD_x y
52 #define OTHERCOORD_y x
55 Rgb rgb; /* on screen */
56 char c; /* canonical */
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 */
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 */
88 { 0x000000, 'o' }, /* foreground */
89 { 0xD4B356, ' ' }, /* background (cursor) */
90 { 0xFFFFFF, 'o' }, /* foreground (cursor) */
92 { 0x5B93BF, '_' }, /* selector dropdown background */
93 { 0xD7C94F, 'X' }, /* selector dropdown foreground */
97 CanonColourInfoReds canoncolourinfo_tree;
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;
106 CanonColourInfoGreens *greens= canoncolourinfo_tree.red2[r];
108 greens= canoncolourinfo_tree.red2[r]= mmalloc(sizeof(*greens));
112 CanonColourInfoBlues *blues= greens->green2[g];
114 blues= greens->green2[g]= mmalloc(sizeof(*blues));
115 memset(blues, '?', sizeof(*blues));
118 blues->blue2[b]= cci->c;
123 static void mustfail1(const char *file, int line, const char *what) {
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"
132 "If all of these are true, please report this as a fault.\n\n"
134 " %s:%d: requirement failed:\n"
138 static void mustfail2(void) NORET;
139 static void mustfail2(void) {
140 fprintf(stderr, "\n\nGiving up.\n");
144 #define MUST(x, ifnot) do{ \
146 mustfail1(__FILE__,__LINE__,#x); \
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)
164 #define REQUIRE_RECTANGLE(tlx,tly,brx,bry,ok) \
165 require_rectangle(tlx, tly, brx, bry, ok, __LINE__);
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++)
171 static void require_rectangle_r(Rect rr, const char *ok, int lineno) {
175 MUST( strchr(ok,c), ({
176 MI(lineno),MR(rr);MP(p);MS(ok);
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);
186 static void debug_rect(const char *what, int whati, Rect rr) {
187 if (!DEBUGP(rect)) return;
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);
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++) {
209 for (x=0; x<allw; x++)
210 for (y=0; y<allh; y++) {
212 if (want==' ') continue;
213 if (get(alloffx+x, alloffy+y) == want)
218 debugf("CHECKCOMMOD alloff=%d,%d good=%d bad=%d\n",
219 alloffx,alloffy, good,bad);
227 #define WALK_UNTIL(point,coord,increm,last,edge) \
229 if ((point).coord == (last)+(increm)) break; \
230 if (get_p((point)) == (edge)) { (point).coord -= (increm); break; } \
231 (point).coord += (increm); \
234 #define WALK_UNTIL_MUST(point,coord,increm,last,edge) \
236 WALK_UNTIL(point,coord,increm,last,edge); \
237 MUST( (point).coord != (last)+(increm), \
238 MP(point); MI(increm); MI(last); MC(edge); \
242 #define ADJUST_BOX(search,insidechrs,OP,want, lim,LIMIT_MUST, TLBR,XY,increm) \
244 LIMIT_MUST( (search).tl.XY != (search).br.XY && \
245 (search).TLBR.XY != (lim), \
246 MR((search));MSB(#TLBR);MSB(#XY) ); \
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)) \
255 (search).TLBR.XY += increm; \
258 void find_structure(CanonImage *im, int *max_relevant_y_r,
259 Point *commod_focus_point_r) {
262 Rect whole = { {0,0}, {cim->w-1,cim->h-1} };
266 for (y=0, xscaleunit=1; y<4; y++, xscaleunit*=10) {
268 for (x=0; x<=cim->w; x++) {
269 if (x % xscaleunit) fputc(' ',debug);
270 else fprintf(debug,"%d",(x / xscaleunit)%10);
276 Point mainr_tl= START_MAIN;
278 WALK_UNTIL_MUST(mainr.tl, y,-1, whole.tl.y, ' ');
281 WALK_UNTIL_MUST(mainr.tl, x,-1, whole.tl.x, '*');
282 WALK_UNTIL_MUST(mainr.tl, y,-1, whole.tl.y, '*');
283 WALK_UNTIL_MUST(mainr.br, x,+1, whole.br.x, '*');
284 WALK_UNTIL_MUST(mainr.br, y,+1, whole.br.y, '*');
286 REQUIRE_RECTANGLE(mainr.tl.x-1, mainr.tl.y, mainr.tl.x-1, mainr.br.y, "*");
287 REQUIRE_RECTANGLE(mainr.br.x+1, mainr.tl.y, mainr.br.x+1, mainr.br.y, "*");
288 REQUIRE_RECTANGLE(mainr.tl.x, mainr.tl.y-1, mainr.br.x, mainr.tl.y-1, "*");
289 REQUIRE_RECTANGLE(mainr.tl.x, mainr.br.y+1, mainr.br.x, mainr.br.y+1, "*");
291 #define CHECK_STRIP_BORDER(tlbr,xy,increm) \
296 csb_p.xy= mainr.tlbr.xy; \
297 if (get_p(csb_p)=='+') { \
299 csb_r.tl.xy= csb_p.xy; \
300 csb_r.br.xy= csb_p.xy; \
301 require_rectangle_r(csb_r, "+", __LINE__); \
302 mainr.tlbr.xy += increm; \
306 debug_rect("mainr",0, mainr);
308 CHECK_STRIP_BORDER(tl,x,+1);
309 CHECK_STRIP_BORDER(tl,y,+1);
310 CHECK_STRIP_BORDER(br,x,-1);
311 CHECK_STRIP_BORDER(br,y,-1);
313 debug_rect("mainr",1, mainr);
315 Rect updown= {START_MAIN,START_MAIN};
317 updown.br.x += chkw-1;
319 debug_rect("updown",__LINE__,updown);
321 ADJUST_BOX(updown, "+", >=,chkw, mainr.tl.y, MUST, tl,y,-1);
322 debug_rect("updown",__LINE__,updown);
323 updown.br.y= updown.tl.y;
324 updown.tl.y= updown.tl.y-1;
326 ADJUST_BOX(updown, "+*",>=,chkw, mainr.tl.y-1, MUST, tl,y,-1);
327 debug_rect("updown",__LINE__,updown);
329 commbasey= updown.tl.y + 1;
330 comminty= updown.br.y - updown.tl.y;
332 Rect across= {{ mainr.tl.x - 1, commbasey },
333 { mainr.tl.x, commbasey + comminty-2 }};
337 #define LIMIT_QUITEQ(cond,mp) { if (!(cond)) break; }
338 debug_rect("across",colno*1000000+__LINE__, across);
339 ADJUST_BOX(across, "+",>=,comminty-1, mainr.br.x, LIMIT_QUITEQ, br,x,+1);
340 debug_rect("across",colno*1000000+__LINE__, across);
342 MUST( colno < MAX_COLUMNS, MI(colno);MR(across);MR(mainr);MI(commbasey); );
343 int colrx= across.br.x-1;
344 if (colrx >= mainr.br.x) colrx= mainr.br.x;
345 if (colno < INTERESTING_COLUMNS)
346 colrightx[colno]= colrx;
350 if (across.br.x >= mainr.br.x)
353 REQUIRE_RECTANGLE(across.br.x,mainr.tl.y, across.br.x,mainr.br.y, "+");
356 MUST( colno >= MIN_COLUMNS, MI(colno);MR(mainr);MR(across); );
358 #define SET_ONCE(var,val) do{ \
360 if ((var)==-1) (var)= v; \
361 else MUST( (var) == v, MSB(#var);MI((var));MI(v);MR(mainr); ); \
364 SET_ONCE(columns, colno);
365 SET_ONCE(text_h, comminty - 1);
366 if (max_relevant_y_r)
367 SET_ONCE(*max_relevant_y_r, mainr.br.y + 10);
369 if (commod_focus_point_r) {
370 *commod_focus_point_r= mainr.tl;
371 commod_focus_point_r->x += 10;
372 commod_focus_point_r->y += comminty/3;
375 MUST( text_h <= OCR_MAX_H, MI(text_h) );
378 void check_correct_commodities(void) {
379 Rect search= { { 50,39 }, { 130,59 } };
381 ADJUST_BOX(search,"_",>=,10, cim->h, MUST, tl,y,+1);
382 ADJUST_BOX(search,"_",>=,10, 0, MUST, br,y,-1);
384 debug_rect("commodselr",1, search);
386 static const char *all_small[]= {
387 " ___________________________________ ",
388 " ________X____X__X____________________ ",
389 " ________ X___ X_ X_____XXXXXXXXXXX_____ ",
390 "_________X_X__ X_ X______XXXXXXXXX_______",
391 "________ X X__ X_ X_______XXXXXXX________",
392 "________X_ _X_ X_ X________XXXXX_________",
393 "_______ X__ X_ X_ X_________XXX__________",
394 "_______XXXXXXX X_ X__________X___________",
395 " _____ X X X_ X______________________",
396 " ____X_____ _XX_ X______________________",
397 " __ _______ __ ______________________ ",
399 static const char *all_big[]= {
400 "???_______________________________________???",
401 "??_________________________________________??",
402 "?_________X______X___X______________________?",
403 "_________?X_____?X__?X______XXXXXXXXXXX______",
404 "_________X_X____?X__?X_______XXXXXXXXX_______",
405 "________?X?X____?X__?X________XXXXXXX________",
406 "________X_?_X___?X__?X_________XXXXX_________",
407 "_______?X__?X___?X__?X__________XXX__________",
408 "_______?XXXXX___?X__?X___________X___________",
409 "_______X????_X__?X__?X_______________________",
410 "?_____?X____?X__?X__?X_______________________",
411 "??____X_____?_X_?X__?X_______________________",
412 "???__?_______?__?___?_______________________?",
415 #define COMMOD_SELECTOR_MATCHES(all) \
416 commod_selector_matches(search, all, \
417 sizeof((all))/sizeof((all)[0]), \
420 if (!(COMMOD_SELECTOR_MATCHES(all_small) ||
421 COMMOD_SELECTOR_MATCHES(all_big)))
422 fatal("Commodities selector not set to `All'.");
425 CanonImage *alloc_canon_image(int w, int h) {
426 CanonImage *im= mmalloc(sizeof(CanonImage) + w*h);
429 memset(im->d,'?',w*h);
433 static void file_read_image_ppm(FILE *f) {
435 unsigned char rgb_buf[3];
438 pnm_readpaminit(f, &inpam, sizeof(inpam));
439 if (!(inpam.maxval == 255 &&
440 inpam.bytes_per_sample == 1 &&
441 inpam.format == RPPM_FORMAT))
442 fatal("PNM screenshot(s) file must be 8bpp 1 byte-per-sample RGB raw");
444 CANONICALISE_IMAGE(im, inpam.width, inpam.height, {
445 int rr= fread(&rgb_buf,1,3,f);
446 sysassert(!ferror(f));
447 if (rr!=3) fatal("PNM screenshot(s) file ends unexpectedly");
454 sysassert(!ferror(screenshot_file));
456 if (!(npages < MAX_PAGES))
457 fatal("Too many images in screenshots file; max is %d.\n", MAX_PAGES);
459 page_images[npages++]= im;
462 void read_one_screenshot(void) {
463 progress("reading screenshot...");
464 file_read_image_ppm(screenshot_file);
465 progress_log("read screenshot.");
468 void read_screenshots(void) {
471 sysassert(! fstat(fileno(screenshot_file), &stab) );
474 if (S_ISREG(stab.st_mode)) {
475 long pos= ftell(screenshot_file);
476 if (pos == stab.st_size) break;
478 int c= fgetc(screenshot_file);
480 ungetc(c, screenshot_file);
482 progress("reading screenshot %d...",npages);
483 file_read_image_ppm(screenshot_file);
485 sysassert(!ferror(screenshot_file));
486 progress_log("read %d screenshots.",npages);
489 static double find_aa_density(const RgbImage *ri, Point p, long background,
490 long foreground, int fg_extra) {
491 Rgb here= ri_rgb(ri, p.x, p.y);
493 double alpha[3], alpha_mean=0;
495 for (i=0; i<3; i++) {
496 unsigned char here_chan= here >> (i*8);
497 unsigned char bg_chan= background >> (i*8);
498 unsigned char fg_chan= foreground >> (i*8);
500 ((double)here_chan - (double)bg_chan) /
501 ((fg_chan + fg_extra) - (double)bg_chan);
502 alpha[i]= alpha_chan;
503 alpha_mean += alpha_chan * (1/3.0);
506 double thresh= 1.5/AAMAXVAL;
507 double alpha_min= alpha_mean - thresh;
508 double alpha_max= alpha_mean + thresh;
510 MUST( alpha_min <= alpha[i] && alpha[i] <= alpha_max,
512 MRGB(here);MRGB(background);MRGB(foreground);MI(fg_extra);
513 MF(alpha_min); MI(i);MF(alpha[i]);MF(alpha_max) );
515 if ( -1e-5 < alpha_mean && alpha_mean <= 0.0 ) alpha_mean= 0.0;
516 if (1.0 <= alpha_mean && alpha_mean <= 1.0+1e-5) alpha_mean= 1.0;
518 MUST( 0 <= alpha_mean &&
519 (fg_extra ? (alpha_mean < 0.999) : (alpha_mean <= 1.0)),
521 MRGB(here);MRGB(background);MRGB(foreground);MI(fg_extra);
522 MF(alpha_mean); MF(alpha[0]);MF(alpha[1]);MF(alpha[2]); );
527 static void find_commodity(int offset, Rect *rr) {
528 /* rr->tl.x==-1 if offset out of range */
529 rr->tl.y= commbasey - offset*comminty;
530 rr->br.y= rr->tl.y + comminty-2;
531 if (rr->tl.y < mainr.tl.y || rr->br.y > mainr.br.y) { rr->tl.x=-1; return; }
533 rr->tl.x= mainr.tl.x;
534 rr->br.x= mainr.br.x;
536 if (rr->tl.y > mainr.tl.y)
537 REQUIRE_RECTANGLE(rr->tl.x,rr->tl.y-1, rr->br.x,rr->tl.y-1, "+");
538 if (rr->br.y < mainr.tl.y)
539 REQUIRE_RECTANGLE(rr->tl.x,rr->br.y+1, rr->br.x,rr->br.y+1, "+");
542 static void find_table_entry(Rect commod, int colno, Rect *cell) {
543 cell->tl.y= commod.tl.y;
544 cell->br.y= commod.br.y;
545 cell->tl.x= !colno ? commod.tl.x : colrightx[colno-1]+2;
546 cell->br.x= colrightx[colno];
547 debug_rect("cell", colno, *cell);
549 const RgbImage *ri= cim->rgb;
552 unsigned char chanbg[3];
553 long bg_count=0, light_count=0, dark_count=0;
556 background= ri_rgb(ri, cell->br.x, cell->br.y);
557 memcpy(chanbg, RI_PIXEL(ri, cell->br.x, cell->br.y), 3);
559 FOR_P_RECT(p,*cell) {
560 const unsigned char *here_pixel= RI_PIXEL(ri, p.x, p.y);
562 for (i=0; i<3; i++) {
563 unsigned here= here_pixel[i];
564 if (here == chanbg[i]) bg_count++;
565 else if (here < chanbg[i]) dark_count += (chanbg[i] - here)/4 + 1;
566 else if (here > chanbg[i]) light_count += (here - chanbg[i])/4 + 1;
569 long total_count= RECT_W(*cell) * RECT_H(*cell) * 3;
571 MUST( bg_count > total_count / 2,
572 MR(*cell);MIL(total_count);MIL(bg_count);
573 MIL(light_count);MIL(dark_count) );
575 if (bg_count == total_count)
581 if (light_count/16 > dark_count) {
582 foreground= 0xffffffU;
584 } else if (dark_count/16 > light_count) {
588 MUST( !"tell light from dark",
589 MR(*cell);MIL(total_count);MIL(bg_count);
590 MIL(light_count);MIL(dark_count);MRGB(background); );
593 debugf("TABLEENTRY col=%d %d,%d..%d,%d bg=%ld light=%ld dark=%ld\n",
594 colno, cell->tl.x,cell->tl.y, cell->br.x,cell->br.y,
595 bg_count, light_count, dark_count);
599 FOR_P_RECT(p,*cell) {
600 double alpha= find_aa_density(ri,p,background,foreground,fg_extra);
602 int here_int= floor((AAMAXVAL+1)*alpha);
603 assert(here_int <= AAMAXVAL);
604 if (!(here_int==0 || here_int==AAMAXVAL)) monochrome=0;
605 cim->d[p.y * cim->w + p.x]= '0' + here_int;
608 debug_rect("cell0M", colno, *cell);
610 require_rectangle_r(*cell, "0123456789", __LINE__);
613 static void ocr_rectangle(Rect r, const OcrCellType ct, FILE *tsv_output) {
614 OcrResultGlyph *results, *res;
616 int w= r.br.x - r.tl.x + 1;
619 for (x=0; x<w; x++) {
621 for (y=0; y<text_h; y++) {
622 Point here= { x+r.tl.x, y+r.tl.y };
623 int pixel= get_p(here);
624 if (pixel==' ') pixel= '0';
625 MUST( pixel >= '0' && pixel <= '0'+AAMAXVAL,
626 MC(pixel);MP(here);MSB(ocr_celltype_name(ct));MR(r); );
627 pixcol_p_add(&cols[x], y, pixel-'0');
632 results= ocr(rd,ct,w,cols);
633 for (res=results; res->s; res++)
634 fputs(res->s,tsv_output);
637 void analyse(FILE *tsv_output) {
639 int page, tryrect, colno;
641 for (page=0; page<npages; page++) {
642 find_structure(page_images[page], 0, 0);
645 check_correct_commodities();
648 rd= ocr_init(text_h);
650 progress("Scanning page %d...",page);
652 for (tryrect= +cim->h; tryrect >= -cim->h; tryrect--) {
653 find_commodity(tryrect, &thisr);
656 debug_rect("commod",tryrect, thisr);
659 for (colno=0; colno<columns; colno++) {
660 find_table_entry(thisr,colno,&entryr);
661 fputs(tab, tsv_output);
662 ocr_rectangle(entryr,
665 : &ocr_celltype_number,
669 fputs("\n", tsv_output);
670 sysassert(!ferror(tsv_output));
671 sysassert(!fflush(tsv_output));
674 progress("Commodity table scan complete.");
677 //static Rect islandnamer;
679 DEBUG_DEFINE_SOME_DEBUGF(structcolon,colondebugf)
681 Rect find_sunshine_widget(void) {
684 sunshiner.tl.x= cim->w - 1034 + 885;
685 sunshiner.br.x= cim->w - 1034 + 1020;
689 ADJUST_BOX(sunshiner,"o*",>=,30, 100,MUST, tl,y,-1);
690 ADJUST_BOX(sunshiner,"o*",>=,30, 100,MUST, br,y,+1);
691 debug_rect("sunshiner",0, sunshiner);
693 MUST(sunshiner.br.y - sunshiner.tl.y > 20, MR(sunshiner));
696 ADJUST_BOX(sunshiner,"o",>=,20, (cim->w - 1034 + 700), MUST, tl,x,-1);
697 ADJUST_BOX(sunshiner,"o",>=,20, cim->w, MUST, br,x,+1);
698 debug_rect("sunshiner",1, sunshiner);
702 void find_islandname(void) {
703 find_structure(page_images[0], 0, 0);
705 RgbImage *ri= alloc_rgb_image(page_images[0]->rgb->w,
706 page_images[0]->rgb->h);
707 memcpy(ri->data, page_images[0]->rgb->data, ri->w * ri->h * 3);
709 Rect sunshiner= find_sunshine_widget();
710 char sunshine[MAXIMGIDENT], archisland[MAXIMGIDENT];
712 const unsigned char *srcp;
713 unsigned char *destp, *endp;
714 for (srcp=page_images[0]->rgb->data, destp=ri->data,
715 endp= ri->data + 3 * ri->w * ri->h;
718 unsigned char c= *srcp & 0xf0;
722 identify_rgbimage(ri, sunshiner, sunshine, "sunshine widget");
724 if (!memcmp(sunshine,"Vessel ",5)) {
727 islandnamer.tl.x= cim->w - 1034 + 885;
728 islandnamer.br.x= cim->w - 1034 + 1020;
729 islandnamer.tl.y= 128;
730 islandnamer.br.y= 156;
732 ADJUST_BOX(islandnamer,"o",>=,5, 0, MUST, tl,y,+1);
733 ADJUST_BOX(islandnamer,"o",>=,5, cim->h, MUST, br,y,-1);
735 ADJUST_BOX(islandnamer,"o",>=,1, 0, MUST, tl,x,+1);
736 ADJUST_BOX(islandnamer,"o",>=,1, cim->w, MUST, br,x,-1);
738 debug_rect("islandnamer",0, islandnamer);
739 // int larger_islandnamebry= islandnamer.tl.y + 25;
740 // MUST(islandnamer.br.y < larger_islandnamebry,
741 // MR(islandnamer);MI(larger_islandnamebry));
742 // islandnamer.br.y = larger_islandnamebry;
743 debug_rect("islandnamer",1, islandnamer);
746 for (x=islandnamer.tl.x; x<=islandnamer.br.x; x++)
747 for (y=islandnamer.tl.y; y<=islandnamer.br.y; y++) {
748 if (RI_PIXEL(ri,x,y)[0] < 0x40) {
749 for (i=0; i<3; i++) {
750 RI_PIXEL(ri,x,y)[i]= 0;
755 identify_rgbimage(ri, islandnamer, archisland, "island");
756 } else if (!strcmp(sunshine,"Land - Ahoy!")) {
759 islandnamer.tl.x= (sunshiner.tl.x + sunshiner.br.x) / 2;
760 islandnamer.tl.y= sunshiner.tl.y + 100;
761 islandnamer.br= islandnamer.tl;
762 debug_rect("islandnamer",__LINE__, islandnamer);
764 WALK_UNTIL_MUST(islandnamer.tl,y, -1, sunshiner.br.y, 'H');
765 WALK_UNTIL_MUST(islandnamer.tl,x, -1, 0, 'o');
766 WALK_UNTIL_MUST(islandnamer.br,x, +1, cim->w, 'o');
767 debug_rect("islandnamer",__LINE__, islandnamer);
769 #define RW (RECT_W(islandnamer))
770 #define RH (RECT_H(islandnamer))
772 ADJUST_BOX(islandnamer,"O",>=,RW-4, cim->h, MUST,br,y,+1);
773 debug_rect("islandnamer",__LINE__, islandnamer);
775 islandnamer.br.y += 2;
777 ADJUST_BOX(islandnamer,"*",<,RW, cim->h, MUST,br,y,+1);
778 debug_rect("islandnamer",__LINE__, islandnamer);
780 islandnamer.tl.y= islandnamer.br.y-1;
781 islandnamer.br.y= islandnamer.br.y+1;
782 debug_rect("islandnamer",__LINE__, islandnamer);
784 ADJUST_BOX(islandnamer,"*",>=,RW, cim->h, MUST,br,y,+1);
785 debug_rect("islandnamer",__LINE__, islandnamer);
787 ADJUST_BOX(islandnamer,"*",<, RH, cim->w, MUST,tl,x,+1);
788 debug_rect("islandnamer",__LINE__, islandnamer);
790 MUST( RECT_H(islandnamer) <= 30, MR(islandnamer));
793 int nspaces=1, might_be_colon=0;
794 uint32_t colon_pattern= 0;
797 for (p.x=islandnamer.br.x; p.x>islandnamer.tl.x; p.x--) {
798 colondebugf("structcolon: x=%4d nsp=%2d mbc=%d cp=%08"PRIx32" ",
799 p.x, nspaces, might_be_colon, colon_pattern);
802 int runs[32], nruns=0;
803 runs[0]=0; runs[1]=0;
805 for (p.y=islandnamer.tl.y; p.y<=islandnamer.br.y; p.y++) {
807 double alpha= find_aa_density(ri,p, 0xCCCCAA,0x002255,0);
819 colondebugf(" pat=%08"PRIx32" nruns=%d runs[]={%d,%d..} ",
820 pattern, nruns, runs[0],runs[1]);
824 /* omg it _is_ a colon */
829 if (nruns==2 && runs[1]==runs[0]) {
831 if (pattern==colon_pattern)
832 goto ok_might_be_colon;
833 } else if (nspaces>=2) {
834 colon_pattern= pattern;
836 goto ok_might_be_colon;
838 } else if (nruns==1 && runs[0]==1 && might_be_colon) {
845 colondebugf(" nsp=%2d mbc=%d\n", nspaces, might_be_colon);
847 MUST(!"colon found", MP(p);MR(islandnamer) );
850 colondebugf(" found\n");
851 islandnamer.br.x= p.x;
853 identify_rgbimage(ri, islandnamer, archisland, "island");
856 MUST(!"sunshine shows ship or ahoy", MS(sunshine) );
860 char *delim= strstr(archisland," - ");
862 archipelago= masprintf("%.*s", (int)(delim-archisland), archisland);
863 island= masprintf("%s", delim+3);