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