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