5 unsigned long rgb; /* on screen */
6 char c; /* canonical */
9 static int height, width;
12 void debug_flush(void) {
13 eassert(!fflush(debug));
14 eassert(!ferror(debug));
21 typedef struct { /* both inclusive */
26 static inline char get(int x, int y) { return image[y * width + x]; }
27 static inline char get_p(Point p) { return get(p.x,p.y); }
30 #define START_MAIN {200,200}
32 #define INTERESTING_COLUMNS 6
35 static Rect mainr = { START_MAIN,START_MAIN };
36 static int commbasey, comminty;
37 static int colrightx[INTERESTING_COLUMNS];
40 static const CanonColourInfo canoncolourinfos[]= {
41 { 0x475A5E, '*' }, /* edge */
42 { 0x2C5F7A, '*' }, /* edge just under box heading shadow */
43 { 0x7D9094, '+' }, /* interbox */
44 { 0xBDC5BF, ' ' }, /* background - pale */
45 { 0xADB5AF, ' ' }, /* background - dark */
46 { 0x000000, 'o' }, /* foreground */
47 { 0xD4B356, ' ' }, /* background (cursor) */
48 { 0xFFFFFF, 'o' }, /* foreground (cursor) */
52 static void require_rectangle(int tlx, int tly, int brx, int bry,
55 for (x=tlx; x<=brx; x++)
56 for (y=tly; y<=bry; y++) {
61 static void require_rectangle_r(Rect rr, const char *ok) {
62 require_rectangle(rr.tl.x,rr.tl.y, rr.br.x,rr.br.y, ok);
65 static void debug_rect(const char *what, int whati, Rect rr) {
67 fprintf(debug, "%s %d: %d,%d..%d,%d:\n", what, whati,
68 rr.tl.x,rr.tl.y, rr.br.x,rr.br.y);
69 w= rr.br.x - rr.tl.x + 1;
70 for (y=rr.tl.y; y<=rr.br.y; y++) {
71 fprintf(debug, "%4d%*s|", y, rr.tl.x,"");
72 r= fwrite(image + y*width + rr.tl.x, 1, w, debug);
80 #define WALK_UNTIL(point,coord,increm,last,edge) \
82 if ((point).coord == (last)+(increm)) break; \
83 if (get_p((point)) == (edge)) { (point).coord -= (increm); break; } \
84 (point).coord += (increm); \
87 #define WALK_UNTIL_MUST(point,coord,increm,last,edge) \
89 WALK_UNTIL(point,coord,increm,last,edge); \
90 eassert((point).coord != (last)+(increm)); \
93 static void find_structure(void) {
94 Rect whole = { {0,0}, {width-1,height-1} };
96 WALK_UNTIL_MUST(mainr.tl, x,-1, whole.tl.x, '*');
97 WALK_UNTIL_MUST(mainr.tl, y,-1, whole.tl.y, '*');
98 WALK_UNTIL_MUST(mainr.br, x,+1, whole.br.x, '*');
99 WALK_UNTIL_MUST(mainr.br, y,+1, whole.br.y, '*');
101 require_rectangle(mainr.tl.x-1, mainr.tl.y, mainr.tl.x-1, mainr.br.y, "*");
102 require_rectangle(mainr.br.x+1, mainr.tl.y, mainr.br.x+1, mainr.br.y, "*");
103 require_rectangle(mainr.tl.x, mainr.tl.y-1, mainr.br.x, mainr.tl.y-1, "*");
104 require_rectangle(mainr.tl.x, mainr.br.y+1, mainr.br.x, mainr.br.y+1, "*");
106 #define CHECK_STRIP_BORDER(tlbr,xy,increm) \
111 csb_p.xy= mainr.tlbr.xy; \
112 if (get_p(csb_p)=='+') { \
114 csb_r.tl.xy= csb_p.xy; \
115 csb_r.br.xy= csb_p.xy; \
116 require_rectangle_r(csb_r, "+"); \
117 mainr.tlbr.xy += increm; \
121 debug_rect("mainr",0, mainr);
123 CHECK_STRIP_BORDER(tl,x,+1);
124 CHECK_STRIP_BORDER(tl,y,+1);
125 CHECK_STRIP_BORDER(br,x,-1);
126 CHECK_STRIP_BORDER(br,y,-1);
128 debug_rect("mainr",1, mainr);
130 Point up = START_MAIN;
131 WALK_UNTIL_MUST(up, y,-1, mainr.tl.y, '+');
133 Point down = START_MAIN;
135 WALK_UNTIL_MUST(down, y,+1, mainr.br.y, '+');
138 for (y=0, xscaleunit=1; y<4; y++, xscaleunit*=10) {
140 for (x=0; x<=width; x++) {
141 if (x % xscaleunit) fputc(' ',debug);
142 else fprintf(debug,"%d",(x / xscaleunit)%10);
148 comminty= down.y - up.y + 2;
149 fprintf(debug, "up.y=%d down.y=%d commbasey=%d comminty=%d\n",
150 up.y,down.y, commbasey,comminty);
152 Point across= { mainr.tl.x, commbasey };
155 eassert(get_p(across) != '+');
156 WALK_UNTIL(across, x,+1, mainr.br.x, '+');
157 eassert(colno < MAX_COLUMNS);
159 if (colrx > mainr.br.x) colrx= mainr.br.x;
160 if (colno < INTERESTING_COLUMNS) {
161 colrightx[colno]= colrx;
162 fprintf(debug,"colrightx[%d]= %d\n",colno,colrx);
164 fprintf(debug,"extra colr %d %d\n",colno,colrx);
169 if (across.x >= mainr.br.x-1)
173 require_rectangle(across.x,mainr.tl.y, across.x,mainr.br.y, "+");
176 eassert(colno >= MIN_COLUMNS);
179 static void find_commodity(int offset, Rect *rr) {
180 /* rr->tl.x==-1 if offset out of range */
181 rr->tl.y= commbasey - offset*comminty;
182 rr->br.y= rr->tl.y + comminty-2;
183 if (rr->tl.y < mainr.tl.y || rr->br.y > mainr.br.y) { rr->tl.x=-1; return; }
184 if (rr->tl.y > mainr.tl.y)
185 require_rectangle(rr->tl.x,rr->tl.y-1, rr->br.x,rr->tl.y-1, "+");
186 if (rr->br.y < mainr.tl.y)
187 require_rectangle(rr->tl.x,rr->br.y+1, rr->br.x,rr->br.y+1, "+");
189 rr->tl.x= mainr.tl.x;
190 rr->br.x= mainr.br.x;
193 static void find_table_entry(Rect commod, int colno, Rect *cellr) {
194 cellr->tl.y= commod.tl.y;
195 cellr->br.y= commod.br.y;
196 cellr->tl.x= !colno ? commod.tl.x : colrightx[colno-1]+2;
197 cellr->br.x= colrightx[colno];
198 debug_rect("cell", colno, *cellr);
199 require_rectangle_r(*cellr, " o");
202 static void load_image_and_canonify(void) {
204 unsigned char rgb[3];
206 const CanonColourInfo *cci;
208 pnm_readpaminit(stdin, &inpam, sizeof(inpam));
209 height= inpam.height;
211 eassert(inpam.maxval == 255);
212 eassert(inpam.bytes_per_sample == 1);
214 image= malloc(width*height);
216 memset(image,'?',width*height);
218 for (y=0; y<height; y++) {
219 for (x=0; x<width; x++) {
220 r= fread(&rgb,1,3,stdin); eassert(r==3);
222 ((unsigned long)rgb[0]<<16) |
223 ((unsigned long)rgb[1]<<8) |
225 for (cci=canoncolourinfos; cci->c; cci++)
226 if (cci->rgb == rgb_l) {
227 image[y*width + x]= cci->c;
231 fprintf(debug, "%4d ",y);
232 r= fwrite(image + y*width, 1,width, debug); eassert(r==width);
238 static void ocr_rectangle(Rect r) {
239 OcrResultGlyph *results, *res;
241 int w= r.br.x - r.tl.x + 1;
242 int h= r.br.y - r.tl.y + 1;
245 for (x=0; x<w; x++) {
247 for (y=0, cx=0, rv=1; y<h; y++, rv<<=1) {
248 switch (get(x+r.tl.x, y+r.tl.y)) {
250 case 'o': cx |= rv; break;
251 default: eassert(!"wrong pixel");
258 results= ocr(w,h,cols);
260 for (res=0; res->s; res++)
263 eassert(!ferror(stdout));
264 eassert(!fflush(stdout));
272 load_image_and_canonify();
275 for (tryrect= +height; tryrect >= -height; tryrect--) {
276 find_commodity(tryrect, &thisr);
279 debug_rect("commod",tryrect, thisr);
281 for (colno=0; colno<MIN_COLUMNS; colno++) {
282 find_table_entry(thisr,colno,&entryr);
283 ocr_rectangle(entryr);