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