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
33 #define TEXT_COLUMNS 2
36 static Rect mainr = { START_MAIN,START_MAIN };
37 static int commbasey, comminty;
38 static int colrightx[INTERESTING_COLUMNS];
42 static const CanonColourInfo canoncolourinfos[]= {
43 { 0x475A5E, '*' }, /* edge */
44 { 0x2C5F7A, '*' }, /* edge just under box heading shadow */
45 { 0x7D9094, '+' }, /* interbox */
46 { 0xBDC5BF, ' ' }, /* background - pale */
47 { 0xADB5AF, ' ' }, /* background - dark */
48 { 0x000000, 'o' }, /* foreground */
49 { 0xD4B356, ' ' }, /* background (cursor) */
50 { 0xFFFFFF, 'o' }, /* foreground (cursor) */
54 static void require_rectangle(int tlx, int tly, int brx, int bry,
57 for (x=tlx; x<=brx; x++)
58 for (y=tly; y<=bry; y++) {
63 static void require_rectangle_r(Rect rr, const char *ok) {
64 require_rectangle(rr.tl.x,rr.tl.y, rr.br.x,rr.br.y, ok);
67 static void debug_rect(const char *what, int whati, Rect rr) {
68 #ifdef DEBUG_RECTANGLES
70 fprintf(debug, "%s %d: %d,%d..%d,%d:\n", what, whati,
71 rr.tl.x,rr.tl.y, rr.br.x,rr.br.y);
72 w= rr.br.x - rr.tl.x + 1;
73 for (y=rr.tl.y; y<=rr.br.y; y++) {
74 fprintf(debug, "%4d%*s|", y, rr.tl.x,"");
75 r= fwrite(image + y*width + rr.tl.x, 1, w, debug);
84 #define WALK_UNTIL(point,coord,increm,last,edge) \
86 if ((point).coord == (last)+(increm)) break; \
87 if (get_p((point)) == (edge)) { (point).coord -= (increm); break; } \
88 (point).coord += (increm); \
91 #define WALK_UNTIL_MUST(point,coord,increm,last,edge) \
93 WALK_UNTIL(point,coord,increm,last,edge); \
94 eassert((point).coord != (last)+(increm)); \
97 static void find_structure(void) {
98 Rect whole = { {0,0}, {width-1,height-1} };
100 WALK_UNTIL_MUST(mainr.tl, x,-1, whole.tl.x, '*');
101 WALK_UNTIL_MUST(mainr.tl, y,-1, whole.tl.y, '*');
102 WALK_UNTIL_MUST(mainr.br, x,+1, whole.br.x, '*');
103 WALK_UNTIL_MUST(mainr.br, y,+1, whole.br.y, '*');
105 require_rectangle(mainr.tl.x-1, mainr.tl.y, mainr.tl.x-1, mainr.br.y, "*");
106 require_rectangle(mainr.br.x+1, mainr.tl.y, mainr.br.x+1, mainr.br.y, "*");
107 require_rectangle(mainr.tl.x, mainr.tl.y-1, mainr.br.x, mainr.tl.y-1, "*");
108 require_rectangle(mainr.tl.x, mainr.br.y+1, mainr.br.x, mainr.br.y+1, "*");
110 #define CHECK_STRIP_BORDER(tlbr,xy,increm) \
115 csb_p.xy= mainr.tlbr.xy; \
116 if (get_p(csb_p)=='+') { \
118 csb_r.tl.xy= csb_p.xy; \
119 csb_r.br.xy= csb_p.xy; \
120 require_rectangle_r(csb_r, "+"); \
121 mainr.tlbr.xy += increm; \
125 debug_rect("mainr",0, mainr);
127 CHECK_STRIP_BORDER(tl,x,+1);
128 CHECK_STRIP_BORDER(tl,y,+1);
129 CHECK_STRIP_BORDER(br,x,-1);
130 CHECK_STRIP_BORDER(br,y,-1);
132 debug_rect("mainr",1, mainr);
134 Point up = START_MAIN;
135 WALK_UNTIL_MUST(up, y,-1, mainr.tl.y, '+');
137 Point down = START_MAIN;
139 WALK_UNTIL_MUST(down, y,+1, mainr.br.y, '+');
141 #ifdef DEBUG_RECTANGLES
143 for (y=0, xscaleunit=1; y<4; y++, xscaleunit*=10) {
145 for (x=0; x<=width; x++) {
146 if (x % xscaleunit) fputc(' ',debug);
147 else fprintf(debug,"%d",(x / xscaleunit)%10);
154 comminty= down.y - up.y + 2;
156 Point across= { mainr.tl.x, commbasey };
159 eassert(get_p(across) != '+');
160 WALK_UNTIL(across, x,+1, mainr.br.x, '+');
161 eassert(colno < MAX_COLUMNS);
163 if (colrx > mainr.br.x) colrx= mainr.br.x;
164 if (colno < INTERESTING_COLUMNS)
165 colrightx[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);
178 text_h = comminty - 1;
181 static void find_commodity(int offset, Rect *rr) {
182 /* rr->tl.x==-1 if offset out of range */
183 rr->tl.y= commbasey - offset*comminty;
184 rr->br.y= rr->tl.y + comminty-2;
185 if (rr->tl.y < mainr.tl.y || rr->br.y > mainr.br.y) { rr->tl.x=-1; return; }
186 if (rr->tl.y > mainr.tl.y)
187 require_rectangle(rr->tl.x,rr->tl.y-1, rr->br.x,rr->tl.y-1, "+");
188 if (rr->br.y < mainr.tl.y)
189 require_rectangle(rr->tl.x,rr->br.y+1, rr->br.x,rr->br.y+1, "+");
191 rr->tl.x= mainr.tl.x;
192 rr->br.x= mainr.br.x;
195 static void find_table_entry(Rect commod, int colno, Rect *cellr) {
196 cellr->tl.y= commod.tl.y;
197 cellr->br.y= commod.br.y;
198 cellr->tl.x= !colno ? commod.tl.x : colrightx[colno-1]+2;
199 cellr->br.x= colrightx[colno];
200 debug_rect("cell", colno, *cellr);
201 require_rectangle_r(*cellr, " o");
204 static void load_image_and_canonify(void) {
206 unsigned char rgb[3];
208 const CanonColourInfo *cci;
210 pnm_readpaminit(stdin, &inpam, sizeof(inpam));
211 height= inpam.height;
213 eassert(inpam.maxval == 255);
214 eassert(inpam.bytes_per_sample == 1);
216 image= malloc(width*height);
218 memset(image,'?',width*height);
220 for (y=0; y<height; y++) {
221 for (x=0; x<width; x++) {
222 r= fread(&rgb,1,3,stdin); eassert(r==3);
224 ((unsigned long)rgb[0]<<16) |
225 ((unsigned long)rgb[1]<<8) |
227 for (cci=canoncolourinfos; cci->c; cci++)
228 if (cci->rgb == rgb_l) {
229 image[y*width + x]= cci->c;
233 #ifdef DEBUG_RECTANGLES
234 fprintf(debug, "%4d ",y);
235 r= fwrite(image + y*width, 1,width, debug); eassert(r==width);
242 static void ocr_rectangle(Rect r, const OcrCellType ct) {
243 OcrResultGlyph *results, *res;
245 int w= r.br.x - r.tl.x + 1;
248 for (x=0; x<w; x++) {
250 for (y=0, cx=0, rv=1; y<text_h; y++, rv<<=1) {
251 switch (get(x+r.tl.x, y+r.tl.y)) {
253 case 'o': cx |= rv; break;
254 default: eassert(!"wrong pixel");
261 results= ocr(rd,ct,w,cols);
263 for (res=results; res->s; res++)
266 eassert(!ferror(stdout));
267 eassert(!fflush(stdout));
274 load_image_and_canonify();
276 rd= ocr_init(text_h);
278 for (tryrect= +height; tryrect >= -height; tryrect--) {
279 find_commodity(tryrect, &thisr);
282 debug_rect("commod",tryrect, thisr);
284 for (colno=0; colno<MIN_COLUMNS; colno++) {
285 find_table_entry(thisr,colno,&entryr);
286 ocr_rectangle(entryr,
289 : &ocr_celltype_number);
295 const char *get_vardir(void) { return "."; }