chiark / gitweb /
Seems to be able to do the whole thing
[ypp-sc-tools.db-test.git] / pctb / convert.c
1
2 #include "ocr.h"
3
4 typedef struct {
5   unsigned long rgb; /* on screen */
6   char c; /* canonical */
7 } CanonColourInfo;
8
9 static int height, width;
10 static char *image;
11
12 void debug_flush(void) {
13   eassert(!fflush(debug));
14   eassert(!ferror(debug));
15 }
16
17 typedef struct {
18   int x, y;
19 } Point;
20
21 typedef struct { /* both inclusive */
22   Point tl;
23   Point br;
24 } Rect;
25
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); }
28
29
30 #define START_MAIN {200,200}
31 #define MIN_COLUMNS         6
32 #define INTERESTING_COLUMNS 6
33 #define TEXT_COLUMNS        2
34 #define MAX_COLUMNS         7
35
36 static Rect mainr = { START_MAIN,START_MAIN };
37 static int commbasey, comminty;
38 static int colrightx[INTERESTING_COLUMNS];
39 static int text_h;
40 static OcrReader *rd;
41
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) */
51   { 0,0 }
52 };
53
54 static void require_rectangle(int tlx, int tly, int brx, int bry,
55                               const char *ok) {
56   int x,y;
57   for (x=tlx; x<=brx; x++)
58     for (y=tly; y<=bry; y++) {
59       int c= get(x,y);
60       assert(strchr(ok,c));
61     }
62 }
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);
65 }
66
67 static void debug_rect(const char *what, int whati, Rect rr) {
68   int y,r,w;
69   fprintf(debug, "%s %d: %d,%d..%d,%d:\n", what, whati,
70           rr.tl.x,rr.tl.y, rr.br.x,rr.br.y);
71   w= rr.br.x - rr.tl.x + 1;
72   for (y=rr.tl.y; y<=rr.br.y; y++) {
73     fprintf(debug, "%4d%*s|", y, rr.tl.x,"");
74     r= fwrite(image + y*width + rr.tl.x, 1, w, debug);
75     eassert(r==w);
76     fputc('|',debug);
77     fputc('\n',debug);
78   }
79   debug_flush();
80 }
81
82 #define WALK_UNTIL(point,coord,increm,last,edge)                        \
83   for (;;) {                                                            \
84     if ((point).coord == (last)+(increm)) break;                        \
85     if (get_p((point)) == (edge)) { (point).coord -= (increm); break; } \
86     (point).coord += (increm);                                          \
87   }
88
89 #define WALK_UNTIL_MUST(point,coord,increm,last,edge)   \
90   do {                                                  \
91     WALK_UNTIL(point,coord,increm,last,edge);           \
92     eassert((point).coord != (last)+(increm));          \
93   } while(0)
94
95 static void find_structure(void) {
96   Rect whole = { {0,0}, {width-1,height-1} };
97
98   WALK_UNTIL_MUST(mainr.tl, x,-1, whole.tl.x, '*');
99   WALK_UNTIL_MUST(mainr.tl, y,-1, whole.tl.y, '*');
100   WALK_UNTIL_MUST(mainr.br, x,+1, whole.br.x, '*');
101   WALK_UNTIL_MUST(mainr.br, y,+1, whole.br.y, '*');
102
103   require_rectangle(mainr.tl.x-1, mainr.tl.y, mainr.tl.x-1, mainr.br.y, "*");
104   require_rectangle(mainr.br.x+1, mainr.tl.y, mainr.br.x+1, mainr.br.y, "*");
105   require_rectangle(mainr.tl.x, mainr.tl.y-1, mainr.br.x, mainr.tl.y-1, "*");
106   require_rectangle(mainr.tl.x, mainr.br.y+1, mainr.br.x, mainr.br.y+1, "*");
107
108 #define CHECK_STRIP_BORDER(tlbr,xy,increm)      \
109   do {                                          \
110     Point csb_p;                                \
111     Rect csb_r;                                 \
112     csb_p= mainr.tl;                            \
113     csb_p.xy= mainr.tlbr.xy;                    \
114     if (get_p(csb_p)=='+') {                    \
115       csb_r= mainr;                             \
116       csb_r.tl.xy= csb_p.xy;                    \
117       csb_r.br.xy= csb_p.xy;                    \
118       require_rectangle_r(csb_r, "+");          \
119       mainr.tlbr.xy += increm;                  \
120     }                                           \
121   } while(0)
122
123   debug_rect("mainr",0, mainr);
124
125   CHECK_STRIP_BORDER(tl,x,+1);
126   CHECK_STRIP_BORDER(tl,y,+1);
127   CHECK_STRIP_BORDER(br,x,-1);
128   CHECK_STRIP_BORDER(br,y,-1);
129
130   debug_rect("mainr",1, mainr);
131
132   Point up = START_MAIN;
133   WALK_UNTIL_MUST(up, y,-1, mainr.tl.y, '+');
134
135   Point down = START_MAIN;
136   down.y++;
137   WALK_UNTIL_MUST(down, y,+1, mainr.br.y, '+');
138
139   int xscaleunit, y,x;
140   for (y=0, xscaleunit=1; y<4; y++, xscaleunit*=10) {
141     fprintf(debug,"     ");
142     for (x=0; x<=width; x++) {
143       if (x % xscaleunit) fputc(' ',debug);
144       else fprintf(debug,"%d",(x / xscaleunit)%10);
145     }
146     fputc('\n',debug);
147   }
148
149   commbasey= up.y;
150   comminty= down.y - up.y + 2;
151   fprintf(debug, "up.y=%d down.y=%d commbasey=%d comminty=%d\n",
152           up.y,down.y, commbasey,comminty);
153
154   Point across= { mainr.tl.x, commbasey };
155   int colno=0;
156   for (;;) {
157     eassert(get_p(across) != '+');
158     WALK_UNTIL(across, x,+1, mainr.br.x, '+');
159     eassert(colno < MAX_COLUMNS);
160     int colrx= across.x;
161     if (colrx > mainr.br.x) colrx= mainr.br.x;
162     if (colno < INTERESTING_COLUMNS) {
163       colrightx[colno]= colrx;
164       fprintf(debug,"colrightx[%d]= %d\n",colno,colrx);
165     } else {
166       fprintf(debug,"extra colr %d  %d\n",colno,colrx);
167     }
168       
169     colno++;
170     
171     if (across.x >= mainr.br.x-1)
172       break;
173
174     across.x++;
175     require_rectangle(across.x,mainr.tl.y, across.x,mainr.br.y, "+");
176     across.x++;
177   }
178   eassert(colno >= MIN_COLUMNS);
179
180   text_h = comminty - 1;
181 }                   
182
183 static void find_commodity(int offset, Rect *rr) {
184   /* rr->tl.x==-1 if offset out of range */
185   rr->tl.y= commbasey - offset*comminty;
186   rr->br.y= rr->tl.y + comminty-2;
187   if (rr->tl.y < mainr.tl.y || rr->br.y > mainr.br.y) { rr->tl.x=-1; return; }
188   if (rr->tl.y > mainr.tl.y)
189     require_rectangle(rr->tl.x,rr->tl.y-1, rr->br.x,rr->tl.y-1, "+");
190   if (rr->br.y < mainr.tl.y)
191     require_rectangle(rr->tl.x,rr->br.y+1, rr->br.x,rr->br.y+1, "+");
192   
193   rr->tl.x= mainr.tl.x;
194   rr->br.x= mainr.br.x;
195 }
196
197 static void find_table_entry(Rect commod, int colno, Rect *cellr) {
198   cellr->tl.y= commod.tl.y;
199   cellr->br.y= commod.br.y;
200   cellr->tl.x= !colno ? commod.tl.x : colrightx[colno-1]+2;
201   cellr->br.x=                        colrightx[colno];
202   debug_rect("cell", colno, *cellr);
203   require_rectangle_r(*cellr, " o");
204 }
205
206 static void load_image_and_canonify(void) {
207   struct pam inpam;
208   unsigned char rgb[3];
209   int x,y,r;
210   const CanonColourInfo *cci;
211
212   pnm_readpaminit(stdin, &inpam, sizeof(inpam));
213   height= inpam.height;
214   width= inpam.width;
215   eassert(inpam.maxval == 255);
216   eassert(inpam.bytes_per_sample == 1);
217
218   image= malloc(width*height);
219   eassert(image);
220   memset(image,'?',width*height);
221
222   for (y=0; y<height; y++) {
223     for (x=0; x<width; x++) {
224       r= fread(&rgb,1,3,stdin);  eassert(r==3);
225       unsigned long rgb_l=
226         ((unsigned long)rgb[0]<<16) |
227         ((unsigned long)rgb[1]<<8) |
228                        (rgb[2]);
229       for (cci=canoncolourinfos; cci->c; cci++)
230         if (cci->rgb == rgb_l) {
231           image[y*width + x]= cci->c;
232           break;
233         }
234     }
235     fprintf(debug, "%4d ",y);
236     r= fwrite(image + y*width, 1,width, debug);  eassert(r==width);
237     fputc('\n',debug);
238   }
239   debug_flush();
240 }
241
242 static void ocr_rectangle(Rect r, const OcrCellType ct) {
243   OcrResultGlyph *results, *res;
244
245   int w= r.br.x - r.tl.x + 1;
246   Pixcol cols[w+1];
247   int x,y;
248   for (x=0; x<w; x++) {
249     Pixcol cx, rv;
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)) {
252       case ' ':           break;
253       case 'o': cx |= rv; break;
254       default: eassert(!"wrong pixel");
255       }
256     }
257     cols[x]= cx;
258   }
259   cols[w]= 0;
260
261   results= ocr(rd,ct,w,cols);
262   printf("YES! \"");
263   for (res=results; res->s; res++)
264     printf("%s",res->s);
265   printf("\"\n");
266   eassert(!ferror(stdout));
267   eassert(!fflush(stdout));
268 }
269
270 int main(void) {
271   Rect thisr, entryr;
272   int tryrect, colno;
273
274   load_image_and_canonify();
275   find_structure();
276   rd= ocr_init(text_h);
277
278   for (tryrect= +height; tryrect >= -height; tryrect--) {
279     find_commodity(tryrect, &thisr);
280     if (thisr.tl.x < 0)
281       continue;
282     debug_rect("commod",tryrect, thisr);
283     
284     for (colno=0; colno<MIN_COLUMNS; colno++) {
285       find_table_entry(thisr,colno,&entryr);
286       ocr_rectangle(entryr,
287                     colno<TEXT_COLUMNS
288                     ? &ocr_celltype_text
289                     : &ocr_celltype_number);
290     }
291   }
292   return 0;
293 }
294
295 const char *get_vardir(void) { return "."; }