4 static CanonImage *cim;
6 void debug_flush(void) {
7 eassert(!fflush(debug));
8 eassert(!ferror(debug));
15 typedef struct { /* both inclusive */
20 static inline char get(int x, int y) { return cim->d[y * cim->w + x]; }
21 static inline char get_p(Point p) { return get(p.x,p.y); }
24 #define START_MAIN {200,200}
26 #define INTERESTING_COLUMNS 6
27 #define TEXT_COLUMNS 2
30 static Rect mainr = { START_MAIN,START_MAIN };
31 static int commbasey, comminty;
32 static int colrightx[INTERESTING_COLUMNS];
36 const CanonColourInfo canoncolourinfos[]= {
37 { 0x475A5E, '*' }, /* edge */
38 { 0x2C5F7A, '*' }, /* edge just under box heading shadow */
39 { 0x7D9094, '+' }, /* interbox */
41 { 0xBDC5BF, ' ' }, /* background - pale Sugar cane, etc. */
42 { 0xADB5AF, ' ' }, /* background - dark */
43 { 0xC7E1C3, ' ' }, /* background - pale Swill, etc. */
44 { 0xB5CFB1, ' ' }, /* background - dark */
45 { 0xD6CEB0, ' ' }, /* background - pale Madder, etc. */
46 { 0xC8C0A2, ' ' }, /* background - dark */
47 { 0xE0E1D3, ' ' }, /* background - pale Lorandite, etc. */
48 { 0xD0D1C3, ' ' }, /* background - dark */
49 { 0xE5E6C1, ' ' }, /* background - pale Cloth */
50 { 0xD7D8B3, ' ' }, /* background - dark */
51 { 0xEDDED9, ' ' }, /* background - pale Dye */
52 { 0xDACBC6, ' ' }, /* background - dark */
53 { 0xD3DEDF, ' ' }, /* background - pale Paint */
54 { 0xC5D0D1, ' ' }, /* background - dark */
55 { 0xDCD1CF, ' ' }, /* background - pale Enamel */
56 { 0xCEC3C1, ' ' }, /* background - dark */
57 { 0xF3F6F5, ' ' }, /* background - pale fruit */
58 { 0xE2E7E5, ' ' }, /* background - dark */
60 { 0x000000, 'o' }, /* foreground */
61 { 0xD4B356, ' ' }, /* background (cursor) */
62 { 0xFFFFFF, 'o' }, /* foreground (cursor) */
64 { 0x5B93BF, '_' }, /* selector dropdown background */
65 { 0xD7C94F, 'X' }, /* selector dropdown foreground */
69 static void require_rectangle(int tlx, int tly, int brx, int bry,
72 for (x=tlx; x<=brx; x++)
73 for (y=tly; y<=bry; y++) {
78 static void require_rectangle_r(Rect rr, const char *ok) {
79 require_rectangle(rr.tl.x,rr.tl.y, rr.br.x,rr.br.y, ok);
82 static void debug_rect(const char *what, int whati, Rect rr) {
83 #ifdef DEBUG_RECTANGLES
85 fprintf(debug, "%s %d: %d,%d..%d,%d:\n", what, whati,
86 rr.tl.x,rr.tl.y, rr.br.x,rr.br.y);
87 w= rr.br.x - rr.tl.x + 1;
88 for (y=rr.tl.y; y<=rr.br.y; y++) {
89 fprintf(debug, "%4d%*s|", y, rr.tl.x,"");
90 r= fwrite(cim->d + y*cim->w + rr.tl.x, 1, w, debug);
99 #define WALK_UNTIL(point,coord,increm,last,edge) \
101 if ((point).coord == (last)+(increm)) break; \
102 if (get_p((point)) == (edge)) { (point).coord -= (increm); break; } \
103 (point).coord += (increm); \
106 #define WALK_UNTIL_MUST(point,coord,increm,last,edge) \
108 WALK_UNTIL(point,coord,increm,last,edge); \
109 eassert((point).coord != (last)+(increm)); \
112 static void find_structure(void) {
113 Rect whole = { {0,0}, {cim->w-1,cim->h-1} };
115 WALK_UNTIL_MUST(mainr.tl, x,-1, whole.tl.x, '*');
116 WALK_UNTIL_MUST(mainr.tl, y,-1, whole.tl.y, '*');
117 WALK_UNTIL_MUST(mainr.br, x,+1, whole.br.x, '*');
118 WALK_UNTIL_MUST(mainr.br, y,+1, whole.br.y, '*');
120 require_rectangle(mainr.tl.x-1, mainr.tl.y, mainr.tl.x-1, mainr.br.y, "*");
121 require_rectangle(mainr.br.x+1, mainr.tl.y, mainr.br.x+1, mainr.br.y, "*");
122 require_rectangle(mainr.tl.x, mainr.tl.y-1, mainr.br.x, mainr.tl.y-1, "*");
123 require_rectangle(mainr.tl.x, mainr.br.y+1, mainr.br.x, mainr.br.y+1, "*");
125 #define CHECK_STRIP_BORDER(tlbr,xy,increm) \
130 csb_p.xy= mainr.tlbr.xy; \
131 if (get_p(csb_p)=='+') { \
133 csb_r.tl.xy= csb_p.xy; \
134 csb_r.br.xy= csb_p.xy; \
135 require_rectangle_r(csb_r, "+"); \
136 mainr.tlbr.xy += increm; \
140 debug_rect("mainr",0, mainr);
142 CHECK_STRIP_BORDER(tl,x,+1);
143 CHECK_STRIP_BORDER(tl,y,+1);
144 CHECK_STRIP_BORDER(br,x,-1);
145 CHECK_STRIP_BORDER(br,y,-1);
147 debug_rect("mainr",1, mainr);
149 Point up = START_MAIN;
150 WALK_UNTIL_MUST(up, y,-1, mainr.tl.y, '+');
152 Point down = START_MAIN;
154 WALK_UNTIL_MUST(down, y,+1, mainr.br.y, '+');
156 #ifdef DEBUG_RECTANGLES
158 for (y=0, xscaleunit=1; y<4; y++, xscaleunit*=10) {
160 for (x=0; x<=cim->w; x++) {
161 if (x % xscaleunit) fputc(' ',debug);
162 else fprintf(debug,"%d",(x / xscaleunit)%10);
169 comminty= down.y - up.y + 2;
171 Point across= { mainr.tl.x, commbasey };
174 eassert(get_p(across) != '+');
175 WALK_UNTIL(across, x,+1, mainr.br.x, '+');
176 eassert(colno < MAX_COLUMNS);
178 if (colrx > mainr.br.x) colrx= mainr.br.x;
179 if (colno < INTERESTING_COLUMNS)
180 colrightx[colno]= colrx;
184 if (across.x >= mainr.br.x-1)
188 require_rectangle(across.x,mainr.tl.y, across.x,mainr.br.y, "+");
191 eassert(colno >= MIN_COLUMNS);
193 text_h = comminty - 1;
196 static void find_commodity(int offset, Rect *rr) {
197 /* rr->tl.x==-1 if offset out of range */
198 rr->tl.y= commbasey - offset*comminty;
199 rr->br.y= rr->tl.y + comminty-2;
200 if (rr->tl.y < mainr.tl.y || rr->br.y > mainr.br.y) { rr->tl.x=-1; return; }
201 if (rr->tl.y > mainr.tl.y)
202 require_rectangle(rr->tl.x,rr->tl.y-1, rr->br.x,rr->tl.y-1, "+");
203 if (rr->br.y < mainr.tl.y)
204 require_rectangle(rr->tl.x,rr->br.y+1, rr->br.x,rr->br.y+1, "+");
206 rr->tl.x= mainr.tl.x;
207 rr->br.x= mainr.br.x;
210 static void find_table_entry(Rect commod, int colno, Rect *cellr) {
211 cellr->tl.y= commod.tl.y;
212 cellr->br.y= commod.br.y;
213 cellr->tl.x= !colno ? commod.tl.x : colrightx[colno-1]+2;
214 cellr->br.x= colrightx[colno];
215 debug_rect("cell", colno, *cellr);
216 require_rectangle_r(*cellr, " o");
219 CanonImage *alloc_canon_image(int w, int h) {
220 CanonImage *im= malloc(sizeof(CanonImage) + w*h);
224 memset(im->d,'?',w*h);
228 CanonImage *file_read_image(FILE *f) {
230 unsigned char rgb_buf[3];
233 pnm_readpaminit(f, &inpam, sizeof(inpam));
234 eassert(inpam.maxval == 255);
235 eassert(inpam.bytes_per_sample == 1);
237 CANONICALISE_IMAGE(im, inpam.width, inpam.height, {
238 r= fread(&rgb_buf,1,3,f); eassert(r==3);
241 ((unsigned long)rgb_buf[0]<<16) |
242 ((unsigned long)rgb_buf[1]<<8) |
249 static void load_image_and_canonify(void) {
250 cim= file_read_image(stdin);
253 static void ocr_rectangle(Rect r, const OcrCellType ct) {
254 OcrResultGlyph *results, *res;
256 int w= r.br.x - r.tl.x + 1;
259 for (x=0; x<w; x++) {
261 for (y=0, cx=0, rv=1; y<text_h; y++, rv<<=1) {
262 switch (get(x+r.tl.x, y+r.tl.y)) {
264 case 'o': cx |= rv; break;
265 default: eassert(!"wrong pixel");
272 results= ocr(rd,ct,w,cols);
274 for (res=results; res->s; res++)
277 eassert(!ferror(stdout));
278 eassert(!fflush(stdout));
281 int main_test(void) {
285 load_image_and_canonify();
287 rd= ocr_init(text_h);
289 for (tryrect= +cim->h; tryrect >= -cim->h; tryrect--) {
290 find_commodity(tryrect, &thisr);
293 debug_rect("commod",tryrect, thisr);
295 for (colno=0; colno<MIN_COLUMNS; colno++) {
296 find_table_entry(thisr,colno,&entryr);
297 ocr_rectangle(entryr,
300 : &ocr_celltype_number);
306 const char *get_vardir(void) { return "."; }