11 typedef struct { /* both inclusive */
16 static CanonImage *cim;
18 static inline char get(int x, int y) { return cim->d[y * cim->w + x]; }
19 static inline char get_p(Point p) { return get(p.x,p.y); }
21 #define START_MAIN {200,200}
23 #define INTERESTING_COLUMNS 7
24 #define TEXT_COLUMNS 2
27 static Rect mainr = { START_MAIN,START_MAIN };
28 static int commbasey, comminty;
29 static int colrightx[INTERESTING_COLUMNS];
30 static int text_h=-1, columns=-1;
33 const CanonColourInfo canoncolourinfos[]= {
34 { 0x475A5E, '*' }, /* edge */
35 { 0x2C5F7A, '*' }, /* edge just under box heading shadow */
36 { 0x7D9094, '+' }, /* interbox */
38 { 0xBDC5BF, ' ' }, /* background - pale Sugar cane, etc. */
39 { 0xADB5AF, ' ' }, /* background - dark */
40 { 0xC7E1C3, ' ' }, /* background - pale Swill, etc. */
41 { 0xB5CFB1, ' ' }, /* background - dark */
42 { 0xD6CEB0, ' ' }, /* background - pale Madder, etc. */
43 { 0xC8C0A2, ' ' }, /* background - dark */
44 { 0xE0E1D3, ' ' }, /* background - pale Lorandite, etc. */
45 { 0xD0D1C3, ' ' }, /* background - dark */
46 { 0xE5E6C1, ' ' }, /* background - pale Cloth */
47 { 0xD7D8B3, ' ' }, /* background - dark */
48 { 0xEDDED9, ' ' }, /* background - pale Dye */
49 { 0xDACBC6, ' ' }, /* background - dark */
50 { 0xD3DEDF, ' ' }, /* background - pale Paint */
51 { 0xC5D0D1, ' ' }, /* background - dark */
52 { 0xDCD1CF, ' ' }, /* background - pale Enamel */
53 { 0xCEC3C1, ' ' }, /* background - dark */
54 { 0xF3F6F5, ' ' }, /* background - pale fruit */
55 { 0xE2E7E5, ' ' }, /* background - dark */
57 { 0x000000, 'o' }, /* foreground */
58 { 0xD4B356, ' ' }, /* background (cursor) */
59 { 0xFFFFFF, 'o' }, /* foreground (cursor) */
61 { 0x5B93BF, '_' }, /* selector dropdown background */
62 { 0xD7C94F, 'X' }, /* selector dropdown foreground */
66 static void require_rectangle(int tlx, int tly, int brx, int bry,
69 for (x=tlx; x<=brx; x++)
70 for (y=tly; y<=bry; y++) {
75 static void require_rectangle_r(Rect rr, const char *ok) {
76 require_rectangle(rr.tl.x,rr.tl.y, rr.br.x,rr.br.y, ok);
79 static void debug_rect(const char *what, int whati, Rect rr) {
80 #ifdef DEBUG_RECTANGLES
82 fprintf(debug, "%s %d: %d,%d..%d,%d:\n", what, whati,
83 rr.tl.x,rr.tl.y, rr.br.x,rr.br.y);
84 w= rr.br.x - rr.tl.x + 1;
85 for (y=rr.tl.y; y<=rr.br.y; y++) {
86 fprintf(debug, "%4d%*s|", y, rr.tl.x,"");
87 r= fwrite(cim->d + y*cim->w + rr.tl.x, 1, w, debug);
96 #define WALK_UNTIL(point,coord,increm,last,edge) \
98 if ((point).coord == (last)+(increm)) break; \
99 if (get_p((point)) == (edge)) { (point).coord -= (increm); break; } \
100 (point).coord += (increm); \
103 #define WALK_UNTIL_MUST(point,coord,increm,last,edge) \
105 WALK_UNTIL(point,coord,increm,last,edge); \
106 eassert((point).coord != (last)+(increm)); \
109 void find_structure(CanonImage *im) {
112 Rect whole = { {0,0}, {cim->w-1,cim->h-1} };
114 WALK_UNTIL_MUST(mainr.tl, x,-1, whole.tl.x, '*');
115 WALK_UNTIL_MUST(mainr.tl, y,-1, whole.tl.y, '*');
116 WALK_UNTIL_MUST(mainr.br, x,+1, whole.br.x, '*');
117 WALK_UNTIL_MUST(mainr.br, y,+1, whole.br.y, '*');
119 require_rectangle(mainr.tl.x-1, mainr.tl.y, mainr.tl.x-1, mainr.br.y, "*");
120 require_rectangle(mainr.br.x+1, mainr.tl.y, mainr.br.x+1, mainr.br.y, "*");
121 require_rectangle(mainr.tl.x, mainr.tl.y-1, mainr.br.x, mainr.tl.y-1, "*");
122 require_rectangle(mainr.tl.x, mainr.br.y+1, mainr.br.x, mainr.br.y+1, "*");
124 #define CHECK_STRIP_BORDER(tlbr,xy,increm) \
129 csb_p.xy= mainr.tlbr.xy; \
130 if (get_p(csb_p)=='+') { \
132 csb_r.tl.xy= csb_p.xy; \
133 csb_r.br.xy= csb_p.xy; \
134 require_rectangle_r(csb_r, "+"); \
135 mainr.tlbr.xy += increm; \
139 debug_rect("mainr",0, mainr);
141 CHECK_STRIP_BORDER(tl,x,+1);
142 CHECK_STRIP_BORDER(tl,y,+1);
143 CHECK_STRIP_BORDER(br,x,-1);
144 CHECK_STRIP_BORDER(br,y,-1);
146 debug_rect("mainr",1, mainr);
148 Point up = START_MAIN;
149 WALK_UNTIL_MUST(up, y,-1, mainr.tl.y, '+');
151 Point down = START_MAIN;
153 WALK_UNTIL_MUST(down, y,+1, mainr.br.y, '+');
155 #ifdef DEBUG_RECTANGLES
157 for (y=0, xscaleunit=1; y<4; y++, xscaleunit*=10) {
159 for (x=0; x<=cim->w; x++) {
160 if (x % xscaleunit) fputc(' ',debug);
161 else fprintf(debug,"%d",(x / xscaleunit)%10);
168 comminty= down.y - up.y + 2;
170 Point across= { mainr.tl.x, commbasey };
173 eassert(get_p(across) != '+');
174 WALK_UNTIL(across, x,+1, mainr.br.x, '+');
175 eassert(colno < MAX_COLUMNS);
177 if (colrx > mainr.br.x) colrx= mainr.br.x;
178 if (colno < INTERESTING_COLUMNS)
179 colrightx[colno]= colrx;
183 if (across.x >= mainr.br.x-1)
187 require_rectangle(across.x,mainr.tl.y, across.x,mainr.br.y, "+");
190 eassert(colno >= MIN_COLUMNS);
192 #define SET_ONCE(var,val) do{ \
194 if ((var)==-1) (var)= v; \
195 else eassert((var) == v); \
198 SET_ONCE(columns, colno);
199 SET_ONCE(text_h, comminty - 1);
202 CanonImage *alloc_canon_image(int w, int h) {
203 CanonImage *im= malloc(sizeof(CanonImage) + w*h);
207 memset(im->d,'?',w*h);
211 CanonImage *file_read_image_ppm(FILE *f) {
213 unsigned char rgb_buf[3];
216 pnm_readpaminit(f, &inpam, sizeof(inpam));
217 eassert(inpam.maxval == 255);
218 eassert(inpam.bytes_per_sample == 1);
220 CANONICALISE_IMAGE(im, inpam.width, inpam.height, {
221 int r= fread(&rgb_buf,1,3,f); eassert(r==3);
224 ((unsigned long)rgb_buf[0]<<16) |
225 ((unsigned long)rgb_buf[1]<<8) |
232 void read_screenshots(void) {
234 while ((c= fgetc(screenshots_file) != EOF)) {
235 ungetc(c, screenshots_file);
237 eassert(npages < MAX_PAGES);
238 page_images[npages++]= file_read_image_ppm(screenshots_file);
242 static void find_commodity(int offset, Rect *rr) {
243 /* rr->tl.x==-1 if offset out of range */
244 rr->tl.y= commbasey - offset*comminty;
245 rr->br.y= rr->tl.y + comminty-2;
246 if (rr->tl.y < mainr.tl.y || rr->br.y > mainr.br.y) { rr->tl.x=-1; return; }
247 if (rr->tl.y > mainr.tl.y)
248 require_rectangle(rr->tl.x,rr->tl.y-1, rr->br.x,rr->tl.y-1, "+");
249 if (rr->br.y < mainr.tl.y)
250 require_rectangle(rr->tl.x,rr->br.y+1, rr->br.x,rr->br.y+1, "+");
252 rr->tl.x= mainr.tl.x;
253 rr->br.x= mainr.br.x;
256 static void find_table_entry(Rect commod, int colno, Rect *cellr) {
257 cellr->tl.y= commod.tl.y;
258 cellr->br.y= commod.br.y;
259 cellr->tl.x= !colno ? commod.tl.x : colrightx[colno-1]+2;
260 cellr->br.x= colrightx[colno];
261 debug_rect("cell", colno, *cellr);
262 require_rectangle_r(*cellr, " o");
265 static void ocr_rectangle(Rect r, const OcrCellType ct) {
266 OcrResultGlyph *results, *res;
268 int w= r.br.x - r.tl.x + 1;
271 for (x=0; x<w; x++) {
273 for (y=0, cx=0, rv=1; y<text_h; y++, rv<<=1) {
274 switch (get(x+r.tl.x, y+r.tl.y)) {
276 case 'o': cx |= rv; break;
277 default: eassert(!"wrong pixel");
284 results= ocr(rd,ct,w,cols);
285 for (res=results; res->s; res++)
291 int page, tryrect, colno;
293 for (page=0; page<npages; page++) {
294 find_structure(page_images[page]);
297 rd= ocr_init(text_h);
299 for (tryrect= +cim->h; tryrect >= -cim->h; tryrect--) {
300 find_commodity(tryrect, &thisr);
303 debug_rect("commod",tryrect, thisr);
306 for (colno=0; colno<columns; colno++) {
307 find_table_entry(thisr,colno,&entryr);
309 ocr_rectangle(entryr,
312 : &ocr_celltype_number);
316 eassert(!ferror(stdout));
317 eassert(!fflush(stdout));