11 unsigned long rgb; /* on screen */
12 char c; /* canonical */
15 static int height, width;
18 static void debug_flush(void) {
19 eassert(!fflush(debug));
20 eassert(!ferror(debug));
27 typedef struct { /* both inclusive */
32 static inline char get(int x, int y) { return image[y * width + x]; }
33 static inline char get_p(Point p) { return get(p.x,p.y); }
36 #define START_MAIN {200,200}
38 #define INTERESTING_COLUMNS 6
41 static Rect mainr = { START_MAIN,START_MAIN };
42 static int commbasey, comminty;
43 static int colrightx[INTERESTING_COLUMNS];
46 static const CanonColourInfo canoncolourinfos[]= {
47 { 0x475A5E, '*' }, /* edge */
48 { 0x2C5F7A, '*' }, /* edge just under box heading shadow */
49 { 0x7D9094, '+' }, /* interbox */
50 { 0xBDC5BF, ' ' }, /* background - pale */
51 { 0xADB5AF, ' ' }, /* background - dark */
52 { 0x000000, 'o' }, /* foreground */
53 { 0xD4B356, ' ' }, /* background (cursor) */
54 { 0xFFFFFF, 'o' }, /* foreground (cursor) */
58 static void require_rectangle(int tlx, int tly, int brx, int bry,
61 for (x=tlx; x<=brx; x++)
62 for (y=tly; y<=bry; y++) {
67 static void require_rectangle_r(Rect rr, const char *ok) {
68 require_rectangle(rr.tl.x,rr.tl.y, rr.br.x,rr.br.y, ok);
71 static void debug_rect(const char *what, int whati, Rect rr) {
73 fprintf(debug, "%s %d: %d,%d..%d,%d:\n", what, whati,
74 rr.tl.x,rr.tl.y, rr.br.x,rr.br.y);
75 w= rr.br.x - rr.tl.x + 1;
76 for (y=rr.tl.y; y<=rr.br.y; y++) {
77 fprintf(debug, "%4d%*s|", y, rr.tl.x,"");
78 r= fwrite(image + y*width + rr.tl.x, 1, w, debug);
86 #define WALK_UNTIL(point,coord,increm,last,edge) \
88 if ((point).coord == (last)+(increm)) break; \
89 if (get_p((point)) == (edge)) { (point).coord -= (increm); break; } \
90 (point).coord += (increm); \
93 #define WALK_UNTIL_MUST(point,coord,increm,last,edge) \
95 WALK_UNTIL(point,coord,increm,last,edge); \
96 eassert((point).coord != (last)+(increm)); \
99 static void find_structure(void) {
100 Rect whole = { {0,0}, {width-1,height-1} };
102 WALK_UNTIL_MUST(mainr.tl, x,-1, whole.tl.x, '*');
103 WALK_UNTIL_MUST(mainr.tl, y,-1, whole.tl.y, '*');
104 WALK_UNTIL_MUST(mainr.br, x,+1, whole.br.x, '*');
105 WALK_UNTIL_MUST(mainr.br, y,+1, whole.br.y, '*');
107 require_rectangle(mainr.tl.x-1, mainr.tl.y, mainr.tl.x-1, mainr.br.y, "*");
108 require_rectangle(mainr.br.x+1, mainr.tl.y, mainr.br.x+1, mainr.br.y, "*");
109 require_rectangle(mainr.tl.x, mainr.tl.y-1, mainr.br.x, mainr.tl.y-1, "*");
110 require_rectangle(mainr.tl.x, mainr.br.y+1, mainr.br.x, mainr.br.y+1, "*");
112 #define CHECK_STRIP_BORDER(tlbr,xy,increm) \
117 csb_p.xy= mainr.tlbr.xy; \
118 if (get_p(csb_p)=='+') { \
120 csb_r.tl.xy= csb_p.xy; \
121 csb_r.br.xy= csb_p.xy; \
122 require_rectangle_r(csb_r, "+"); \
123 mainr.tlbr.xy += increm; \
127 debug_rect("mainr",0, mainr);
129 CHECK_STRIP_BORDER(tl,x,+1);
130 CHECK_STRIP_BORDER(tl,y,+1);
131 CHECK_STRIP_BORDER(br,x,-1);
132 CHECK_STRIP_BORDER(br,y,-1);
134 debug_rect("mainr",1, mainr);
136 Point up = START_MAIN;
137 WALK_UNTIL_MUST(up, y,-1, mainr.tl.y, '+');
139 Point down = START_MAIN;
141 WALK_UNTIL_MUST(down, y,+1, mainr.br.y, '+');
144 for (y=0, xscaleunit=1; y<4; y++, xscaleunit*=10) {
146 for (x=0; x<=width; x++) {
147 if (x % xscaleunit) fputc(' ',debug);
148 else fprintf(debug,"%d",(x / xscaleunit)%10);
154 comminty= down.y - up.y + 2;
155 fprintf(debug, "up.y=%d down.y=%d commbasey=%d comminty=%d\n",
156 up.y,down.y, commbasey,comminty);
158 Point across= { mainr.tl.x, commbasey };
161 eassert(get_p(across) != '+');
162 WALK_UNTIL(across, x,+1, mainr.br.x, '+');
163 eassert(colno < MAX_COLUMNS);
165 if (colrx > mainr.br.x) colrx= mainr.br.x;
166 if (colno < INTERESTING_COLUMNS) {
167 colrightx[colno]= colrx;
168 fprintf(debug,"colrightx[%d]= %d\n",colno,colrx);
170 fprintf(debug,"extra colr %d %d\n",colno,colrx);
175 if (across.x >= mainr.br.x-1)
179 require_rectangle(across.x,mainr.tl.y, across.x,mainr.br.y, "+");
182 eassert(colno >= MIN_COLUMNS);
185 static void find_commodity(int offset, Rect *rr) {
186 /* rr->tl.x==-1 if offset out of range */
187 rr->tl.y= commbasey - offset*comminty;
188 rr->br.y= rr->tl.y + comminty-2;
189 if (rr->tl.y < mainr.tl.y || rr->br.y > mainr.br.y) { rr->tl.x=-1; return; }
190 if (rr->tl.y > mainr.tl.y)
191 require_rectangle(rr->tl.x,rr->tl.y-1, rr->br.x,rr->tl.y-1, "+");
192 if (rr->br.y < mainr.tl.y)
193 require_rectangle(rr->tl.x,rr->br.y+1, rr->br.x,rr->br.y+1, "+");
195 rr->tl.x= mainr.tl.x;
196 rr->br.x= mainr.br.x;
199 static void find_table_entry(Rect commod, int colno, Rect *cellr) {
200 cellr->tl.y= commod.tl.y;
201 cellr->br.y= commod.br.y;
202 cellr->tl.x= !colno ? commod.tl.x : colrightx[colno-1]+2;
203 cellr->br.x= colrightx[colno];
204 debug_rect("cell", colno, *cellr);
205 require_rectangle_r(*cellr, " o");
208 static void load_image_and_canonify(void) {
210 unsigned char rgb[3];
212 const CanonColourInfo *cci;
214 pnm_readpaminit(stdin, &inpam, sizeof(inpam));
215 height= inpam.height;
217 eassert(inpam.maxval == 255);
218 eassert(inpam.bytes_per_sample == 1);
220 image= malloc(width*height);
222 memset(image,'?',width*height);
224 for (y=0; y<height; y++) {
225 for (x=0; x<width; x++) {
226 r= fread(&rgb,1,3,stdin); eassert(r==3);
228 ((unsigned long)rgb[0]<<16) |
229 ((unsigned long)rgb[1]<<8) |
231 for (cci=canoncolourinfos; cci->c; cci++)
232 if (cci->rgb == rgb_l) {
233 image[y*width + x]= cci->c;
237 fprintf(debug, "%4d ",y);
238 r= fwrite(image + y*width, 1,width, debug); eassert(r==width);
248 load_image_and_canonify();
251 for (tryrect= +height; tryrect >= -height; tryrect--) {
252 find_commodity(tryrect, &thisr);
255 debug_rect("commod",tryrect, thisr);
257 for (colno=0; colno<MIN_COLUMNS; colno++) {
258 find_table_entry(thisr,colno,&entryr);
259 // ocr_rectangle(entryr);