chiark / gitweb /
before rip out timing stuff and use just minimum snapshot delay
[ypp-sc-tools.web-live.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 CanonImage *cim;
10
11 void debug_flush(void) {
12   eassert(!fflush(debug));
13   eassert(!ferror(debug));
14 }
15
16 typedef struct {
17   int x, y;
18 } Point;
19
20 typedef struct { /* both inclusive */
21   Point tl;
22   Point br;
23 } Rect;
24
25 static inline char get(int x, int y) { return cim->d[y * cim->w + x]; }
26 static inline char get_p(Point p) { return get(p.x,p.y); }
27
28
29 #define START_MAIN {200,200}
30 #define MIN_COLUMNS         6
31 #define INTERESTING_COLUMNS 6
32 #define TEXT_COLUMNS        2
33 #define MAX_COLUMNS         7
34
35 static Rect mainr = { START_MAIN,START_MAIN };
36 static int commbasey, comminty;
37 static int colrightx[INTERESTING_COLUMNS];
38 static int text_h;
39 static OcrReader *rd;
40
41 static const CanonColourInfo canoncolourinfos[]= {
42   { 0x475A5E, '*' }, /* edge */
43   { 0x2C5F7A, '*' }, /* edge just under box heading shadow */
44   { 0x7D9094, '+' }, /* interbox */
45
46   { 0xBDC5BF, ' ' }, /* background - pale  Sugar cane, etc. */
47   { 0xADB5AF, ' ' }, /* background - dark                   */
48   { 0xC7E1C3, ' ' }, /* background - pale  Swill, etc.      */
49   { 0xB5CFB1, ' ' }, /* background - dark                   */
50   { 0xD6CEB0, ' ' }, /* background - pale  Madder, etc.     */
51   { 0xC8C0A2, ' ' }, /* background - dark                   */
52   { 0xE0E1D3, ' ' }, /* background - pale  Lorandite, etc.  */
53   { 0xD0D1C3, ' ' }, /* background - dark                   */
54   { 0xE5E6C1, ' ' }, /* background - pale  Cloth            */
55   { 0xD7D8B3, ' ' }, /* background - dark                   */
56   { 0xEDDED9, ' ' }, /* background - pale  Dye              */
57   { 0xDACBC6, ' ' }, /* background - dark                   */
58   { 0xD3DEDF, ' ' }, /* background - pale  Paint            */
59   { 0xC5D0D1, ' ' }, /* background - dark                   */
60   { 0xDCD1CF, ' ' }, /* background - pale  Enamel           */
61   { 0xCEC3C1, ' ' }, /* background - dark                   */
62   { 0xF3F6F5, ' ' }, /* background - pale  fruit            */
63   { 0xE2E7E5, ' ' }, /* background - dark                   */
64
65   { 0x000000, 'o' }, /* foreground */
66   { 0xD4B356, ' ' }, /* background (cursor) */
67   { 0xFFFFFF, 'o' }, /* foreground (cursor) */
68
69   { 0x5B93BF, '_' }, /* selector dropdown background */
70   { 0xD7C94F, 'X' }, /* selector dropdown foreground */
71   { 0,0 }
72 };
73
74 static void require_rectangle(int tlx, int tly, int brx, int bry,
75                               const char *ok) {
76   int x,y;
77   for (x=tlx; x<=brx; x++)
78     for (y=tly; y<=bry; y++) {
79       int c= get(x,y);
80       assert(strchr(ok,c));
81     }
82 }
83 static void require_rectangle_r(Rect rr, const char *ok) {
84   require_rectangle(rr.tl.x,rr.tl.y, rr.br.x,rr.br.y, ok);
85 }
86
87 static void debug_rect(const char *what, int whati, Rect rr) {
88 #ifdef DEBUG_RECTANGLES
89   int y,r,w;
90   fprintf(debug, "%s %d: %d,%d..%d,%d:\n", what, whati,
91           rr.tl.x,rr.tl.y, rr.br.x,rr.br.y);
92   w= rr.br.x - rr.tl.x + 1;
93   for (y=rr.tl.y; y<=rr.br.y; y++) {
94     fprintf(debug, "%4d%*s|", y, rr.tl.x,"");
95     r= fwrite(cim->d + y*cim->w + rr.tl.x, 1, w, debug);
96     eassert(r==w);
97     fputc('|',debug);
98     fputc('\n',debug);
99   }
100 #endif
101   debug_flush();
102 }
103
104 #define WALK_UNTIL(point,coord,increm,last,edge)                        \
105   for (;;) {                                                            \
106     if ((point).coord == (last)+(increm)) break;                        \
107     if (get_p((point)) == (edge)) { (point).coord -= (increm); break; } \
108     (point).coord += (increm);                                          \
109   }
110
111 #define WALK_UNTIL_MUST(point,coord,increm,last,edge)   \
112   do {                                                  \
113     WALK_UNTIL(point,coord,increm,last,edge);           \
114     eassert((point).coord != (last)+(increm));          \
115   } while(0)
116
117 static void find_structure(void) {
118   Rect whole = { {0,0}, {cim->w-1,cim->h-1} };
119
120   WALK_UNTIL_MUST(mainr.tl, x,-1, whole.tl.x, '*');
121   WALK_UNTIL_MUST(mainr.tl, y,-1, whole.tl.y, '*');
122   WALK_UNTIL_MUST(mainr.br, x,+1, whole.br.x, '*');
123   WALK_UNTIL_MUST(mainr.br, y,+1, whole.br.y, '*');
124
125   require_rectangle(mainr.tl.x-1, mainr.tl.y, mainr.tl.x-1, mainr.br.y, "*");
126   require_rectangle(mainr.br.x+1, mainr.tl.y, mainr.br.x+1, mainr.br.y, "*");
127   require_rectangle(mainr.tl.x, mainr.tl.y-1, mainr.br.x, mainr.tl.y-1, "*");
128   require_rectangle(mainr.tl.x, mainr.br.y+1, mainr.br.x, mainr.br.y+1, "*");
129
130 #define CHECK_STRIP_BORDER(tlbr,xy,increm)      \
131   do {                                          \
132     Point csb_p;                                \
133     Rect csb_r;                                 \
134     csb_p= mainr.tl;                            \
135     csb_p.xy= mainr.tlbr.xy;                    \
136     if (get_p(csb_p)=='+') {                    \
137       csb_r= mainr;                             \
138       csb_r.tl.xy= csb_p.xy;                    \
139       csb_r.br.xy= csb_p.xy;                    \
140       require_rectangle_r(csb_r, "+");          \
141       mainr.tlbr.xy += increm;                  \
142     }                                           \
143   } while(0)
144
145   debug_rect("mainr",0, mainr);
146
147   CHECK_STRIP_BORDER(tl,x,+1);
148   CHECK_STRIP_BORDER(tl,y,+1);
149   CHECK_STRIP_BORDER(br,x,-1);
150   CHECK_STRIP_BORDER(br,y,-1);
151
152   debug_rect("mainr",1, mainr);
153
154   Point up = START_MAIN;
155   WALK_UNTIL_MUST(up, y,-1, mainr.tl.y, '+');
156
157   Point down = START_MAIN;
158   down.y++;
159   WALK_UNTIL_MUST(down, y,+1, mainr.br.y, '+');
160
161 #ifdef DEBUG_RECTANGLES
162   int xscaleunit, y,x;
163   for (y=0, xscaleunit=1; y<4; y++, xscaleunit*=10) {
164     fprintf(debug,"     ");
165     for (x=0; x<=cim->w; x++) {
166       if (x % xscaleunit) fputc(' ',debug);
167       else fprintf(debug,"%d",(x / xscaleunit)%10);
168     }
169     fputc('\n',debug);
170   }
171 #endif
172
173   commbasey= up.y;
174   comminty= down.y - up.y + 2;
175
176   Point across= { mainr.tl.x, commbasey };
177   int colno=0;
178   for (;;) {
179     eassert(get_p(across) != '+');
180     WALK_UNTIL(across, x,+1, mainr.br.x, '+');
181     eassert(colno < MAX_COLUMNS);
182     int colrx= across.x;
183     if (colrx > mainr.br.x) colrx= mainr.br.x;
184     if (colno < INTERESTING_COLUMNS)
185       colrightx[colno]= colrx;
186       
187     colno++;
188     
189     if (across.x >= mainr.br.x-1)
190       break;
191
192     across.x++;
193     require_rectangle(across.x,mainr.tl.y, across.x,mainr.br.y, "+");
194     across.x++;
195   }
196   eassert(colno >= MIN_COLUMNS);
197
198   text_h = comminty - 1;
199 }                   
200
201 static void find_commodity(int offset, Rect *rr) {
202   /* rr->tl.x==-1 if offset out of range */
203   rr->tl.y= commbasey - offset*comminty;
204   rr->br.y= rr->tl.y + comminty-2;
205   if (rr->tl.y < mainr.tl.y || rr->br.y > mainr.br.y) { rr->tl.x=-1; return; }
206   if (rr->tl.y > mainr.tl.y)
207     require_rectangle(rr->tl.x,rr->tl.y-1, rr->br.x,rr->tl.y-1, "+");
208   if (rr->br.y < mainr.tl.y)
209     require_rectangle(rr->tl.x,rr->br.y+1, rr->br.x,rr->br.y+1, "+");
210   
211   rr->tl.x= mainr.tl.x;
212   rr->br.x= mainr.br.x;
213 }
214
215 static void find_table_entry(Rect commod, int colno, Rect *cellr) {
216   cellr->tl.y= commod.tl.y;
217   cellr->br.y= commod.br.y;
218   cellr->tl.x= !colno ? commod.tl.x : colrightx[colno-1]+2;
219   cellr->br.x=                        colrightx[colno];
220   debug_rect("cell", colno, *cellr);
221   require_rectangle_r(*cellr, " o");
222 }
223
224 CanonImage *file_read_image(FILE *f) {
225   struct pam inpam;
226   unsigned char rgb[3];
227   int x,y,r;
228   const CanonColourInfo *cci;
229
230   pnm_readpaminit(f, &inpam, sizeof(inpam));
231   eassert(inpam.maxval == 255);
232   eassert(inpam.bytes_per_sample == 1);
233
234   CanonImage *im= malloc(sizeof(CanonImage) + inpam.width*inpam.height);
235   eassert(im);
236   im->h= inpam.height;
237   im->w= inpam.width;
238
239   memset(im->d,'?',inpam.width*inpam.height);
240
241   for (y=0; y<inpam.height; y++) {
242     for (x=0; x<inpam.width; x++) {
243       r= fread(&rgb,1,3,f);  eassert(r==3);
244       unsigned long rgb_l=
245         ((unsigned long)rgb[0]<<16) |
246         ((unsigned long)rgb[1]<<8) |
247                        (rgb[2]);
248       for (cci=canoncolourinfos; cci->c; cci++)
249         if (cci->rgb == rgb_l) {
250           im->d[y*inpam.width + x]= cci->c;
251           break;
252         }
253     }
254 #ifdef DEBUG_RECTANGLES
255     fprintf(debug, "%4d ",y);
256     r= fwrite(im->d + y*inpam.width, 1,inpam.width, debug);
257     eassert(r==inpam.width);
258     fputc('\n',debug);
259 #endif
260   }
261   debug_flush();
262   return im;
263 }
264
265 static void load_image_and_canonify(void) {
266   cim= file_read_image(stdin);
267 }
268
269 static void ocr_rectangle(Rect r, const OcrCellType ct) {
270   OcrResultGlyph *results, *res;
271
272   int w= r.br.x - r.tl.x + 1;
273   Pixcol cols[w+1];
274   int x,y;
275   for (x=0; x<w; x++) {
276     Pixcol cx, rv;
277     for (y=0, cx=0, rv=1; y<text_h; y++, rv<<=1) {
278       switch (get(x+r.tl.x, y+r.tl.y)) {
279       case ' ':           break;
280       case 'o': cx |= rv; break;
281       default: eassert(!"wrong pixel");
282       }
283     }
284     cols[x]= cx;
285   }
286   cols[w]= 0;
287
288   results= ocr(rd,ct,w,cols);
289   printf("YES! \"");
290   for (res=results; res->s; res++)
291     printf("%s",res->s);
292   printf("\"\n");
293   eassert(!ferror(stdout));
294   eassert(!fflush(stdout));
295 }
296
297 int main_test(void) {
298   Rect thisr, entryr;
299   int tryrect, colno;
300
301   load_image_and_canonify();
302   find_structure();
303   rd= ocr_init(text_h);
304
305   for (tryrect= +cim->h; tryrect >= -cim->h; tryrect--) {
306     find_commodity(tryrect, &thisr);
307     if (thisr.tl.x < 0)
308       continue;
309     debug_rect("commod",tryrect, thisr);
310     
311     for (colno=0; colno<MIN_COLUMNS; colno++) {
312       find_table_entry(thisr,colno,&entryr);
313       ocr_rectangle(entryr,
314                     colno<TEXT_COLUMNS
315                     ? &ocr_celltype_text
316                     : &ocr_celltype_number);
317     }
318   }
319   return 0;
320 }
321
322 const char *get_vardir(void) { return "."; }