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