chiark / gitweb /
compatibility version check
[ypp-sc-tools.db-live.git] / pctb / ocr.c
1 /*
2   */
3
4 #include "ocr.h"
5
6 typedef struct {
7   Pixcol col;
8   struct OCRDatabaseNode *then;
9 } OCRDatabaseLink;
10
11 #define MAXGLYPHCHRS 3
12
13 typedef struct OCRDatabaseNode {
14   char s[MAXGLYPHCHRS+1]; /* null-terminated; "" means no match here */
15   int nlinks, alinks;
16   OCRDatabaseLink *links;
17 } OCRDatabaseNode;
18
19 #define N_OCR_CONTEXTS 2
20
21 static OCRDatabaseNode ocr_contexts[N_OCR_CONTEXTS];
22 static FILE *db;
23 static OcrResultGlyph *results;
24 static int aresults, nresults;
25
26 static FILE *resolver;
27 static pid_t resolver_pid;
28 static int resolver_done;
29
30 static void ocr_readdb(void) {
31   int ctx,nchrs;
32   OCRDatabaseNode *current, *additional;
33   char chrs[MAXGLYPHCHRS+1];
34   Pixcol cv;
35   int r,i,j;
36
37   assert(!db);
38   db= fopen("database","r");  eassert(db);
39
40   for (;;) {
41     r= fscanf(db, "%d %d", &ctx, &nchrs);
42     if (r==EOF) break;
43     eassert(r==2);
44     eassert(ctx>=0 && ctx<N_OCR_CONTEXTS);
45     eassert(nchrs>0 && nchrs<=MAXGLYPHCHRS);
46
47     for (i=0; i<nchrs; i++) {
48       int c;
49       r= fscanf(db, "%x", &c);  eassert(r==1);
50       eassert(c>0 && c<=255);
51       chrs[i]= c;
52     }
53     chrs[nchrs]= 0;
54
55     int twidth;
56     r= fscanf(db, "%d", &twidth);  eassert(r==1);
57     current= &ocr_contexts[ctx];
58     for (i=0; i<twidth; i++) {
59       r= fscanf(db, "%"PSPIXCOL(SCNx), &cv);  eassert(r==1);
60       for (j=0; j<current->nlinks; j++)
61         if (current->links[j].col == cv) {
62           current= current->links[j].then;
63           goto found_link;
64         }
65
66       additional= malloc(sizeof(*additional)); eassert(additional);
67       additional->s[0]= 0;
68       additional->nlinks= additional->alinks= 0;
69       additional->links= 0;
70       if (current->nlinks==current->alinks) {
71         current->alinks++;
72         current->alinks<<=1;
73         current->links= realloc(current->links,
74             sizeof(*current->links) * current->alinks);
75         eassert(current->links);
76       }
77       current->links[current->nlinks].col= cv;
78       current->links[current->nlinks].then= additional;
79       current->nlinks++;
80       current= additional;
81
82     found_link:;
83     }
84
85     eassert(!current->s[0]);
86     strcpy(current->s, chrs);
87   }
88   eassert(!ferror(db));
89   eassert(feof(db));
90 }      
91
92 static void callout_unknown(int w, int h, Pixcol cols[],
93                             int unk_l, int unk_r, int unk_ctx,
94                             const OcrResultGlyph *sofar, int nsofar) {
95   int jobpipe[2],donepipe[2], c, r,i, x,y;
96   const OcrResultGlyph *s;
97   const char *p;
98   char cb;
99   Pixcol pv;
100   
101   if (!resolver) {
102     r= pipe(jobpipe);  eassert(!r);
103     r= pipe(donepipe);  eassert(!r);
104     resolver_pid= fork();
105     eassert(resolver_pid!=-1);
106     if (!resolver_pid) {
107       r= dup2(jobpipe[0],0); eassert(r==0);
108       r= close(jobpipe[1]); eassert(!r);
109       r= close(donepipe[0]); eassert(!r);
110       /* we know donepipe[1] is >= 4 and we have dealt with all the others
111        * so we aren't in any danger of overwriting some other fd 4: */
112       r= dup2(donepipe[1],4); eassert(r==4);
113       execlp("./show-thing.tcl", "./show-thing.tcl",
114              "--automatic","1",(char*)0);
115       eassert(!"execlp failed");
116     }
117     r= close(jobpipe[0]); eassert(!r);
118     r= close(donepipe[1]); eassert(!r);
119     resolver= fdopen(jobpipe[1],"w"); eassert(resolver);
120     resolver_done= donepipe[0];
121   }
122   fprintf(resolver,"%d %d %d",unk_l,unk_r,unk_ctx);
123   for (i=0, s=sofar; i<nsofar; i++, s++) {
124     fprintf(resolver," %d %d %d ",s->l,s->r,s->ctx);
125     for (p=s->s; (c= *p); p++) {
126       if (c=='\\') fprintf(resolver,"\\%c",c);
127       else if (c>=33 && c<=126) fputc(c,resolver);
128       else fprintf(resolver,"\\x%02x",(unsigned char)c);
129     }
130   }
131   fputc('\n',resolver);
132
133   fprintf(resolver,
134           "/* XPM */\n"
135           "static char *t[] = {\n"
136           "/* columns rows colors chars-per-pixel */\n"
137           "\"%d %d 2 1\",\n"
138           "\"  c black\",\n"
139           "\"o c white\",\n",
140           w,h);
141   for (y=0, pv=1; y<h; y++, pv<<=1) {
142     fputc('"',resolver);
143     for (x=0; x<w; x++)
144       fputc(cols[x] & pv ? 'o' : ' ', resolver);
145     fputs("\",\n",resolver);
146   }
147   fputs("};\n",resolver);
148   eassert(!ferror(resolver));
149   eassert(!fflush(resolver));
150
151   eassert(resolver);
152
153   for (;;) {
154     r= read(resolver_done,&cb,1);
155     if (r==-1) { eassert(errno==EINTR); continue; }
156     break;
157   }
158
159   if (r==0) {
160     pid_t pid;
161     for (;;) {
162       pid= waitpid(resolver_pid, &r, 0);
163       if (pid==-1) { eassert(errno==EINTR); continue; }
164       break;
165     }
166     eassert(pid==resolver_pid);
167     if (WIFEXITED(r)) {
168       eassert(!WEXITSTATUS(r));
169       fclose(resolver);
170       close(resolver_done);
171       resolver= 0;
172     } else if (WIFSIGNALED(r)) {
173       eassert(!"resolver child died due to signal");
174     } else {
175       eassert(!"weird wait status");
176     }
177   } else {
178     eassert(r==1);
179     eassert(cb==0);
180   }
181
182   fclose(db);
183   db= 0;
184   ocr_readdb();
185 }
186
187 static void add_result(const char *s, int l, int r, int ctx) {
188   if (nresults >= aresults) {
189     aresults++; aresults<<=1;
190     results= realloc(results,sizeof(*results)*aresults);
191     eassert(results);
192   }
193   results[nresults].s= s;
194   results[nresults].l= l;
195   results[nresults].r= r;
196   results[nresults].ctx= ctx;
197   nresults++;
198 }
199
200 OcrResultGlyph *ocr(int w, int h, Pixcol cols[]) {
201   int nspaces=0;
202   int ctx=1,i, x;
203
204   nresults=0;
205   assert(db);
206
207   fprintf(debug,"OCR h=%d w=%d",w,h);
208   for (x=0; x<w; x++) fprintf(debug," %"PSPIXCOL(PRIx),cols[x]);
209   fprintf(debug,"\n");
210   debug_flush();
211
212  restart:
213   x=0;
214   for (;;) {
215     debug_flush();
216     /* skip spaces */
217     if (x>=w)
218       break;
219
220     if (!cols[x]) {
221       nspaces++;
222       x++;
223       if (nspaces>3) ctx=1;
224       continue;
225     }
226
227     /* find character */
228     OCRDatabaseNode *current=0, *bestmatch=0;
229     int lx=x;
230     int bestmatch_rx=-1;
231     current= &ocr_contexts[ctx];
232     fprintf(debug,"OCR  lx=%d ctx=%d  ",lx,ctx);
233
234     for (;;) {
235       debug_flush();
236       fprintf(debug,"| x=%d",x);
237       if (x>w) break;
238       Pixcol cv= cols[x];
239       fprintf(debug," cv=%"PSPIXCOL(PRIx),x);
240       for (i=0; i<current->nlinks; i++)
241         if (current->links[i].col == cv)
242           goto found;
243       /* not found */
244       fprintf(debug," ?");
245       break;
246
247     found:
248       current= current->links[i].then;
249       if (current->s[0]) {
250         fprintf(debug," \"%s\"",current->s);
251         bestmatch=current; bestmatch_rx=x;
252       } else {
253         fprintf(debug," ...");
254       }
255       x++;
256     }
257
258     if (bestmatch) {
259       fprintf(debug," YES\n");
260       add_result(bestmatch->s, lx, bestmatch_rx, ctx);
261       x= bestmatch_rx+1;
262       ctx= 0;
263     } else {
264       int rx;
265       fprintf(debug," UNKNOWN");
266       for (rx=lx; rx<w && cols[rx]; rx++);
267       fprintf(debug," x=%d ctx=%d %d..%d\n",x, ctx, lx,rx);
268       debug_flush();
269       callout_unknown(w,h,cols, lx,rx-1,ctx, results,nresults);
270       goto restart;
271     }
272   }
273   add_result(0,-1,-1,0);
274   fprintf(debug,"OCR  finished %d glyphs\n",nresults);
275   debug_flush();
276   return results;
277 }
278
279 void ocr_init(void) {
280   ocr_readdb();
281 }