5 unsigned long rgb; /* on screen */
6 char c; /* canonical */
9 static CanonImage *cim;
11 void debug_flush(void) {
12 eassert(!fflush(debug));
13 eassert(!ferror(debug));
20 typedef struct { /* both inclusive */
25 static inline char get(int x, int y) { return cim->d[y * cim->w + x]; }
26 static inline char get_p(Point p) { return get(p.x,p.y); }
29 #define START_MAIN {200,200}
31 #define INTERESTING_COLUMNS 6
32 #define TEXT_COLUMNS 2
35 static Rect mainr = { START_MAIN,START_MAIN };
36 static int commbasey, comminty;
37 static int colrightx[INTERESTING_COLUMNS];
41 static const CanonColourInfo canoncolourinfos[]= {
42 { 0x475A5E, '*' }, /* edge */
43 { 0x2C5F7A, '*' }, /* edge just under box heading shadow */
44 { 0x7D9094, '+' }, /* interbox */
46 { 0xBDC5BF, ' ' }, /* background - pale Sugar cane, etc. */
47 { 0xADB5AF, ' ' }, /* background - dark */
48 { 0xC7E1C3, ' ' }, /* background - pale Swill, etc. */
49 { 0xB5CFB1, ' ' }, /* background - dark */
50 { 0xD6CEB0, ' ' }, /* background - pale Madder, etc. */
51 { 0xC8C0A2, ' ' }, /* background - dark */
52 { 0xE0E1D3, ' ' }, /* background - pale Lorandite, etc. */
53 { 0xD0D1C3, ' ' }, /* background - dark */
54 { 0xE5E6C1, ' ' }, /* background - pale Cloth */
55 { 0xD7D8B3, ' ' }, /* background - dark */
56 { 0xEDDED9, ' ' }, /* background - pale Dye */
57 { 0xDACBC6, ' ' }, /* background - dark */
58 { 0xD3DEDF, ' ' }, /* background - pale Paint */
59 { 0xC5D0D1, ' ' }, /* background - dark */
60 { 0xDCD1CF, ' ' }, /* background - pale Enamel */
61 { 0xCEC3C1, ' ' }, /* background - dark */
62 { 0xF3F6F5, ' ' }, /* background - pale fruit */
63 { 0xE2E7E5, ' ' }, /* background - dark */
65 { 0x000000, 'o' }, /* foreground */
66 { 0xD4B356, ' ' }, /* background (cursor) */
67 { 0xFFFFFF, 'o' }, /* foreground (cursor) */
69 { 0x5B93BF, '_' }, /* selector dropdown background */
70 { 0xD7C94F, 'X' }, /* selector dropdown foreground */
74 static void require_rectangle(int tlx, int tly, int brx, int bry,
77 for (x=tlx; x<=brx; x++)
78 for (y=tly; y<=bry; y++) {
83 static void require_rectangle_r(Rect rr, const char *ok) {
84 require_rectangle(rr.tl.x,rr.tl.y, rr.br.x,rr.br.y, ok);
87 static void debug_rect(const char *what, int whati, Rect rr) {
88 #ifdef DEBUG_RECTANGLES
90 fprintf(debug, "%s %d: %d,%d..%d,%d:\n", what, whati,
91 rr.tl.x,rr.tl.y, rr.br.x,rr.br.y);
92 w= rr.br.x - rr.tl.x + 1;
93 for (y=rr.tl.y; y<=rr.br.y; y++) {
94 fprintf(debug, "%4d%*s|", y, rr.tl.x,"");
95 r= fwrite(cim->d + y*cim->w + rr.tl.x, 1, w, debug);
104 #define WALK_UNTIL(point,coord,increm,last,edge) \
106 if ((point).coord == (last)+(increm)) break; \
107 if (get_p((point)) == (edge)) { (point).coord -= (increm); break; } \
108 (point).coord += (increm); \
111 #define WALK_UNTIL_MUST(point,coord,increm,last,edge) \
113 WALK_UNTIL(point,coord,increm,last,edge); \
114 eassert((point).coord != (last)+(increm)); \
117 static void find_structure(void) {
118 Rect whole = { {0,0}, {cim->w-1,cim->h-1} };
120 WALK_UNTIL_MUST(mainr.tl, x,-1, whole.tl.x, '*');
121 WALK_UNTIL_MUST(mainr.tl, y,-1, whole.tl.y, '*');
122 WALK_UNTIL_MUST(mainr.br, x,+1, whole.br.x, '*');
123 WALK_UNTIL_MUST(mainr.br, y,+1, whole.br.y, '*');
125 require_rectangle(mainr.tl.x-1, mainr.tl.y, mainr.tl.x-1, mainr.br.y, "*");
126 require_rectangle(mainr.br.x+1, mainr.tl.y, mainr.br.x+1, mainr.br.y, "*");
127 require_rectangle(mainr.tl.x, mainr.tl.y-1, mainr.br.x, mainr.tl.y-1, "*");
128 require_rectangle(mainr.tl.x, mainr.br.y+1, mainr.br.x, mainr.br.y+1, "*");
130 #define CHECK_STRIP_BORDER(tlbr,xy,increm) \
135 csb_p.xy= mainr.tlbr.xy; \
136 if (get_p(csb_p)=='+') { \
138 csb_r.tl.xy= csb_p.xy; \
139 csb_r.br.xy= csb_p.xy; \
140 require_rectangle_r(csb_r, "+"); \
141 mainr.tlbr.xy += increm; \
145 debug_rect("mainr",0, mainr);
147 CHECK_STRIP_BORDER(tl,x,+1);
148 CHECK_STRIP_BORDER(tl,y,+1);
149 CHECK_STRIP_BORDER(br,x,-1);
150 CHECK_STRIP_BORDER(br,y,-1);
152 debug_rect("mainr",1, mainr);
154 Point up = START_MAIN;
155 WALK_UNTIL_MUST(up, y,-1, mainr.tl.y, '+');
157 Point down = START_MAIN;
159 WALK_UNTIL_MUST(down, y,+1, mainr.br.y, '+');
161 #ifdef DEBUG_RECTANGLES
163 for (y=0, xscaleunit=1; y<4; y++, xscaleunit*=10) {
165 for (x=0; x<=cim->w; x++) {
166 if (x % xscaleunit) fputc(' ',debug);
167 else fprintf(debug,"%d",(x / xscaleunit)%10);
174 comminty= down.y - up.y + 2;
176 Point across= { mainr.tl.x, commbasey };
179 eassert(get_p(across) != '+');
180 WALK_UNTIL(across, x,+1, mainr.br.x, '+');
181 eassert(colno < MAX_COLUMNS);
183 if (colrx > mainr.br.x) colrx= mainr.br.x;
184 if (colno < INTERESTING_COLUMNS)
185 colrightx[colno]= colrx;
189 if (across.x >= mainr.br.x-1)
193 require_rectangle(across.x,mainr.tl.y, across.x,mainr.br.y, "+");
196 eassert(colno >= MIN_COLUMNS);
198 text_h = comminty - 1;
201 static void find_commodity(int offset, Rect *rr) {
202 /* rr->tl.x==-1 if offset out of range */
203 rr->tl.y= commbasey - offset*comminty;
204 rr->br.y= rr->tl.y + comminty-2;
205 if (rr->tl.y < mainr.tl.y || rr->br.y > mainr.br.y) { rr->tl.x=-1; return; }
206 if (rr->tl.y > mainr.tl.y)
207 require_rectangle(rr->tl.x,rr->tl.y-1, rr->br.x,rr->tl.y-1, "+");
208 if (rr->br.y < mainr.tl.y)
209 require_rectangle(rr->tl.x,rr->br.y+1, rr->br.x,rr->br.y+1, "+");
211 rr->tl.x= mainr.tl.x;
212 rr->br.x= mainr.br.x;
215 static void find_table_entry(Rect commod, int colno, Rect *cellr) {
216 cellr->tl.y= commod.tl.y;
217 cellr->br.y= commod.br.y;
218 cellr->tl.x= !colno ? commod.tl.x : colrightx[colno-1]+2;
219 cellr->br.x= colrightx[colno];
220 debug_rect("cell", colno, *cellr);
221 require_rectangle_r(*cellr, " o");
224 CanonImage *file_read_image(FILE *f) {
226 unsigned char rgb[3];
228 const CanonColourInfo *cci;
230 pnm_readpaminit(f, &inpam, sizeof(inpam));
231 eassert(inpam.maxval == 255);
232 eassert(inpam.bytes_per_sample == 1);
234 CanonImage *im= malloc(sizeof(CanonImage) + inpam.width*inpam.height);
239 memset(im->d,'?',inpam.width*inpam.height);
241 for (y=0; y<inpam.height; y++) {
242 for (x=0; x<inpam.width; x++) {
243 r= fread(&rgb,1,3,f); eassert(r==3);
245 ((unsigned long)rgb[0]<<16) |
246 ((unsigned long)rgb[1]<<8) |
248 for (cci=canoncolourinfos; cci->c; cci++)
249 if (cci->rgb == rgb_l) {
250 im->d[y*inpam.width + x]= cci->c;
254 #ifdef DEBUG_RECTANGLES
255 fprintf(debug, "%4d ",y);
256 r= fwrite(im->d + y*inpam.width, 1,inpam.width, debug);
257 eassert(r==inpam.width);
265 static void load_image_and_canonify(void) {
266 cim= file_read_image(stdin);
269 static void ocr_rectangle(Rect r, const OcrCellType ct) {
270 OcrResultGlyph *results, *res;
272 int w= r.br.x - r.tl.x + 1;
275 for (x=0; x<w; x++) {
277 for (y=0, cx=0, rv=1; y<text_h; y++, rv<<=1) {
278 switch (get(x+r.tl.x, y+r.tl.y)) {
280 case 'o': cx |= rv; break;
281 default: eassert(!"wrong pixel");
288 results= ocr(rd,ct,w,cols);
290 for (res=results; res->s; res++)
293 eassert(!ferror(stdout));
294 eassert(!fflush(stdout));
297 int main_test(void) {
301 load_image_and_canonify();
303 rd= ocr_init(text_h);
305 for (tryrect= +cim->h; tryrect >= -cim->h; tryrect--) {
306 find_commodity(tryrect, &thisr);
309 debug_rect("commod",tryrect, thisr);
311 for (colno=0; colno<MIN_COLUMNS; colno++) {
312 find_table_entry(thisr,colno,&entryr);
313 ocr_rectangle(entryr,
316 : &ocr_celltype_number);
322 const char *get_vardir(void) { return "."; }