chiark / gitweb /
finds cells; now start work on real ocr
[ypp-sc-tools.db-test.git] / pctb / convert.c
1 #include <pam.h>
2 #include <stdint.h>
3 #include <inttypes.h>
4 #include <assert.h>
5 #include <string.h>
6
7 #define eassert assert
8 #define debug stdout
9
10 typedef struct {
11   unsigned long rgb; /* on screen */
12   char c; /* canonical */
13 } CanonColourInfo;
14
15 static int height, width;
16 static char *image;
17
18 static void debug_flush(void) {
19   eassert(!fflush(debug));
20   eassert(!ferror(debug));
21 }
22
23 typedef struct {
24   int x, y;
25 } Point;
26
27 typedef struct { /* both inclusive */
28   Point tl;
29   Point br;
30 } Rect;
31
32 static inline char get(int x, int y) { return image[y * width + x]; }
33 static inline char get_p(Point p) { return get(p.x,p.y); }
34
35
36 #define START_MAIN {200,200}
37 #define MIN_COLUMNS         6
38 #define INTERESTING_COLUMNS 6
39 #define MAX_COLUMNS         7
40
41 static Rect mainr = { START_MAIN,START_MAIN };
42 static int commbasey, comminty;
43 static int colrightx[INTERESTING_COLUMNS];
44
45
46 static const CanonColourInfo canoncolourinfos[]= {
47   { 0x475A5E, '*' }, /* edge */
48   { 0x2C5F7A, '*' }, /* edge just under box heading shadow */
49   { 0x7D9094, '+' }, /* interbox */
50   { 0xBDC5BF, ' ' }, /* background - pale */
51   { 0xADB5AF, ' ' }, /* background - dark */
52   { 0x000000, 'o' }, /* foreground */
53   { 0xD4B356, ' ' }, /* background (cursor) */
54   { 0xFFFFFF, 'o' }, /* foreground (cursor) */
55   { 0,0 }
56 };
57
58 static void require_rectangle(int tlx, int tly, int brx, int bry,
59                               const char *ok) {
60   int x,y;
61   for (x=tlx; x<=brx; x++)
62     for (y=tly; y<=bry; y++) {
63       int c= get(x,y);
64       assert(strchr(ok,c));
65     }
66 }
67 static void require_rectangle_r(Rect rr, const char *ok) {
68   require_rectangle(rr.tl.x,rr.tl.y, rr.br.x,rr.br.y, ok);
69 }
70
71 static void debug_rect(const char *what, int whati, Rect rr) {
72   int y,r,w;
73   fprintf(debug, "%s %d: %d,%d..%d,%d:\n", what, whati,
74           rr.tl.x,rr.tl.y, rr.br.x,rr.br.y);
75   w= rr.br.x - rr.tl.x + 1;
76   for (y=rr.tl.y; y<=rr.br.y; y++) {
77     fprintf(debug, "%4d%*s|", y, rr.tl.x,"");
78     r= fwrite(image + y*width + rr.tl.x, 1, w, debug);
79     eassert(r==w);
80     fputc('|',debug);
81     fputc('\n',debug);
82   }
83   debug_flush();
84 }
85
86 #define WALK_UNTIL(point,coord,increm,last,edge)                        \
87   for (;;) {                                                            \
88     if ((point).coord == (last)+(increm)) break;                        \
89     if (get_p((point)) == (edge)) { (point).coord -= (increm); break; } \
90     (point).coord += (increm);                                          \
91   }
92
93 #define WALK_UNTIL_MUST(point,coord,increm,last,edge)   \
94   do {                                                  \
95     WALK_UNTIL(point,coord,increm,last,edge);           \
96     eassert((point).coord != (last)+(increm));          \
97   } while(0)
98
99 static void find_structure(void) {
100   Rect whole = { {0,0}, {width-1,height-1} };
101
102   WALK_UNTIL_MUST(mainr.tl, x,-1, whole.tl.x, '*');
103   WALK_UNTIL_MUST(mainr.tl, y,-1, whole.tl.y, '*');
104   WALK_UNTIL_MUST(mainr.br, x,+1, whole.br.x, '*');
105   WALK_UNTIL_MUST(mainr.br, y,+1, whole.br.y, '*');
106
107   require_rectangle(mainr.tl.x-1, mainr.tl.y, mainr.tl.x-1, mainr.br.y, "*");
108   require_rectangle(mainr.br.x+1, mainr.tl.y, mainr.br.x+1, mainr.br.y, "*");
109   require_rectangle(mainr.tl.x, mainr.tl.y-1, mainr.br.x, mainr.tl.y-1, "*");
110   require_rectangle(mainr.tl.x, mainr.br.y+1, mainr.br.x, mainr.br.y+1, "*");
111
112 #define CHECK_STRIP_BORDER(tlbr,xy,increm)      \
113   do {                                          \
114     Point csb_p;                                \
115     Rect csb_r;                                 \
116     csb_p= mainr.tl;                            \
117     csb_p.xy= mainr.tlbr.xy;                    \
118     if (get_p(csb_p)=='+') {                    \
119       csb_r= mainr;                             \
120       csb_r.tl.xy= csb_p.xy;                    \
121       csb_r.br.xy= csb_p.xy;                    \
122       require_rectangle_r(csb_r, "+");          \
123       mainr.tlbr.xy += increm;                  \
124     }                                           \
125   } while(0)
126
127   debug_rect("mainr",0, mainr);
128
129   CHECK_STRIP_BORDER(tl,x,+1);
130   CHECK_STRIP_BORDER(tl,y,+1);
131   CHECK_STRIP_BORDER(br,x,-1);
132   CHECK_STRIP_BORDER(br,y,-1);
133
134   debug_rect("mainr",1, mainr);
135
136   Point up = START_MAIN;
137   WALK_UNTIL_MUST(up, y,-1, mainr.tl.y, '+');
138
139   Point down = START_MAIN;
140   down.y++;
141   WALK_UNTIL_MUST(down, y,+1, mainr.br.y, '+');
142
143   int xscaleunit, y,x;
144   for (y=0, xscaleunit=1; y<4; y++, xscaleunit*=10) {
145     fprintf(debug,"     ");
146     for (x=0; x<=width; x++) {
147       if (x % xscaleunit) fputc(' ',debug);
148       else fprintf(debug,"%d",(x / xscaleunit)%10);
149     }
150     fputc('\n',debug);
151   }
152
153   commbasey= up.y;
154   comminty= down.y - up.y + 2;
155   fprintf(debug, "up.y=%d down.y=%d commbasey=%d comminty=%d\n",
156           up.y,down.y, commbasey,comminty);
157
158   Point across= { mainr.tl.x, commbasey };
159   int colno=0;
160   for (;;) {
161     eassert(get_p(across) != '+');
162     WALK_UNTIL(across, x,+1, mainr.br.x, '+');
163     eassert(colno < MAX_COLUMNS);
164     int colrx= across.x;
165     if (colrx > mainr.br.x) colrx= mainr.br.x;
166     if (colno < INTERESTING_COLUMNS) {
167       colrightx[colno]= colrx;
168       fprintf(debug,"colrightx[%d]= %d\n",colno,colrx);
169     } else {
170       fprintf(debug,"extra colr %d  %d\n",colno,colrx);
171     }
172       
173     colno++;
174     
175     if (across.x >= mainr.br.x-1)
176       break;
177
178     across.x++;
179     require_rectangle(across.x,mainr.tl.y, across.x,mainr.br.y, "+");
180     across.x++;
181   }
182   eassert(colno >= MIN_COLUMNS);
183 }                   
184
185 static void find_commodity(int offset, Rect *rr) {
186   /* rr->tl.x==-1 if offset out of range */
187   rr->tl.y= commbasey - offset*comminty;
188   rr->br.y= rr->tl.y + comminty-2;
189   if (rr->tl.y < mainr.tl.y || rr->br.y > mainr.br.y) { rr->tl.x=-1; return; }
190   if (rr->tl.y > mainr.tl.y)
191     require_rectangle(rr->tl.x,rr->tl.y-1, rr->br.x,rr->tl.y-1, "+");
192   if (rr->br.y < mainr.tl.y)
193     require_rectangle(rr->tl.x,rr->br.y+1, rr->br.x,rr->br.y+1, "+");
194   
195   rr->tl.x= mainr.tl.x;
196   rr->br.x= mainr.br.x;
197 }
198
199 static void find_table_entry(Rect commod, int colno, Rect *cellr) {
200   cellr->tl.y= commod.tl.y;
201   cellr->br.y= commod.br.y;
202   cellr->tl.x= !colno ? commod.tl.x : colrightx[colno-1]+2;
203   cellr->br.x=                        colrightx[colno];
204   debug_rect("cell", colno, *cellr);
205   require_rectangle_r(*cellr, " o");
206 }
207
208 static void load_image_and_canonify(void) {
209   struct pam inpam;
210   unsigned char rgb[3];
211   int x,y,r;
212   const CanonColourInfo *cci;
213
214   pnm_readpaminit(stdin, &inpam, sizeof(inpam));
215   height= inpam.height;
216   width= inpam.width;
217   eassert(inpam.maxval == 255);
218   eassert(inpam.bytes_per_sample == 1);
219
220   image= malloc(width*height);
221   eassert(image);
222   memset(image,'?',width*height);
223
224   for (y=0; y<height; y++) {
225     for (x=0; x<width; x++) {
226       r= fread(&rgb,1,3,stdin);  eassert(r==3);
227       unsigned long rgb_l=
228         ((unsigned long)rgb[0]<<16) |
229         ((unsigned long)rgb[1]<<8) |
230                        (rgb[2]);
231       for (cci=canoncolourinfos; cci->c; cci++)
232         if (cci->rgb == rgb_l) {
233           image[y*width + x]= cci->c;
234           break;
235         }
236     }
237     fprintf(debug, "%4d ",y);
238     r= fwrite(image + y*width, 1,width, debug);  eassert(r==width);
239     fputc('\n',debug);
240   }
241   debug_flush();
242 }
243
244 int main(void) {
245   Rect thisr, entryr;
246   int tryrect, colno;
247   
248   load_image_and_canonify();
249   find_structure();
250
251   for (tryrect= +height; tryrect >= -height; tryrect--) {
252     find_commodity(tryrect, &thisr);
253     if (thisr.tl.x < 0)
254       continue;
255     debug_rect("commod",tryrect, thisr);
256     
257     for (colno=0; colno<MIN_COLUMNS; colno++) {
258       find_table_entry(thisr,colno,&entryr);
259 //      ocr_rectangle(entryr);
260     }
261   }
262   return 0;
263 }