chiark / gitweb /
87bf80842a510280e30826d8fe36c877911b59e8
[ypp-sc-tools.db-test.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
29 static void ocr_readdb(void) {
30   int ctx,nchrs;
31   OCRDatabaseNode *current, *additional;
32   char chrs[MAXGLYPHCHRS+1];
33   Pixcol cv;
34   int r,i,j;
35
36   assert(!db);
37   db= fopen("database","r");  eassert(db);
38
39   for (;;) {
40     r= fscanf(db, "%d %d", &ctx, &nchrs);
41     if (r==EOF) break;
42     eassert(r==2);
43     eassert(ctx>=0 && ctx<N_OCR_CONTEXTS);
44     eassert(nchrs>0 && nchrs<=MAXGLYPHCHRS);
45
46     for (i=0; i<nchrs; i++) {
47       int c;
48       r= fscanf(db, "%x", &c);  eassert(r==1);
49       eassert(c>0 && c<=255);
50       chrs[i]= c;
51     }
52     chrs[nchrs]= 0;
53
54     int twidth;
55     r= fscanf(db, "%d", &twidth);  eassert(r==1);
56     current= &ocr_contexts[ctx];
57     for (i=0; i<twidth; i++) {
58       r= fscanf(db, "%"PSPIXCOL(SCNx), &cv);  eassert(r==1);
59       for (j=0; j<current->nlinks; j++)
60         if (current->links[j].col == cv) {
61           current= current->links[j].then;
62           goto found_link;
63         }
64
65       additional= malloc(sizeof(*additional)); eassert(additional);
66       additional->s[0]= 0;
67       additional->nlinks= additional->alinks= 0;
68       additional->links= 0;
69       if (current->nlinks==current->alinks) {
70         current->alinks++;
71         current->alinks<<=1;
72         current->links= realloc(current->links,
73             sizeof(*current->links) * current->alinks);
74         eassert(current->links);
75       }
76       current->links[current->nlinks].col= cv;
77       current->links[current->nlinks].then= additional;
78       current->nlinks++;
79       current= additional;
80
81     found_link:;
82     }
83
84     eassert(!current->s[0]);
85     strcpy(current->s, chrs);
86   }
87   eassert(!ferror(db));
88   eassert(feof(db));
89 }      
90
91 static void callout_unknown(int w, int h, Pixcol cols[], int unk_l, int unk_r,
92                             const OcrResultGlyph *sofar, int nsofar) {
93   int pfd[2], c, r,i, x,y;
94   const OcrResultGlyph *s;
95   const char *p;
96   Pixcol pv;
97   
98   if (!resolver) {
99     r= pipe(pfd);  eassert(!r);
100     resolver_pid= fork();
101     eassert(resolver_pid!=-1);
102     if (!resolver_pid) {
103       r= dup2(pfd[0],0); eassert(!r);
104       r= close(pfd[1]); eassert(!r);
105       execlp("./show-thing.tcl", "./show-thing.tcl",(char*)0);
106       eassert(!"execlp failed");
107     }
108     r= close(pfd[0]); eassert(!r);
109     resolver= fdopen(pfd[1],"w"); eassert(resolver);
110   }
111   fprintf(resolver,"%d %d",unk_l,unk_r);
112   for (i=0, s=sofar; i<nsofar; i++, s++) {
113     fprintf(resolver," %d %d %d ",s->l,s->r,s->ctx);
114     for (p=s->s; (c= *p); p++) {
115       if (c=='\\') fprintf(resolver,"\\%c",c);
116       else if (c>=33 && c<=126) fputc(c,resolver);
117       else fprintf(resolver,"\\x%02x",(unsigned char)c);
118     }
119   }
120   fputc('\n',resolver);
121
122   fprintf(resolver,
123           "/* XPM */\n"
124           "static char *t[] = {\n"
125           "/* columns rows colors chars-per-pixel */\n"
126           "\"%d %d 2 1\",\n"
127           "\"  c black\",\n"
128           "\"o c white\",\n",
129           w,h);
130   for (y=0, pv=1; y<h; y++, pv<<=1) {
131     fputc('"',resolver);
132     for (x=0; x<w; x++)
133       fputc(cols[x] & pv ? 'o' : ' ', resolver);
134     fputs("\",\n",resolver);
135   }
136   fputs("};\n",resolver);
137   eassert(!ferror(resolver));
138   eassert(!fflush(resolver));
139
140   for (;;) {
141     eassert(resolver);
142     pid_t pid= waitpid(resolver_pid, &r, WUNTRACED);
143     if (pid==-1) { eassert(errno==EINTR); continue; }
144     eassert(pid==resolver_pid);
145     if (WIFEXITED(r)) {
146       eassert(!WEXITSTATUS(r));
147       fclose(resolver);
148       resolver= 0;
149     } else if (WIFSTOPPED(r)) {
150       r= kill(resolver_pid,SIGCONT);
151       eassert(!r);
152     } else if (WIFSIGNALED(r)) {
153       eassert(!"resolver child died due to signal");
154     } else {
155       eassert(!"weird wait status");
156     }
157     struct stat stab, fstab;
158     r= stat("database",&stab);  eassert(!r);
159     r= fstat(fileno(db),&fstab);  eassert(!r);
160     if (stab.st_ino != fstab.st_ino ||
161         stab.st_dev != fstab.st_dev)
162       break;
163   }
164   fclose(db);
165   db= 0;
166   ocr_readdb();
167 }
168
169 static void add_result(const char *s, int l, int r, int ctx) {
170   if (nresults >= aresults) {
171     aresults++; aresults<<=1;
172     results= realloc(results,sizeof(*results)*aresults);
173     eassert(results);
174   }
175   results[nresults].s= s;
176   results[nresults].l= l;
177   results[nresults].r= r;
178   results[nresults].ctx= ctx;
179   nresults++;
180 }
181
182 OcrResultGlyph *ocr(int w, int h, Pixcol cols[]) {
183   int nspaces=0;
184   int ctx=1,i, x;
185
186   nresults=0;
187   assert(db);
188
189   fprintf(debug,"OCR h=%d w=%d",w,h);
190   for (x=0; x<w; x++) fprintf(debug," %"PSPIXCOL(PRIx),cols[x]);
191   fprintf(debug,"\n");
192   debug_flush();
193
194  restart:
195   x=0;
196   for (;;) {
197     debug_flush();
198     /* skip spaces */
199     if (x>=w)
200       break;
201
202     if (!cols[x]) {
203       nspaces++;
204       x++;
205       if (nspaces>3) ctx=1;
206       continue;
207     }
208
209     /* find character */
210     OCRDatabaseNode *current=0, *bestmatch=0;
211     int lx=x;
212     int bestmatch_rx=-1;
213     current= &ocr_contexts[ctx];
214     fprintf(debug,"OCR  lx=%d ctx=%d  ",lx,ctx);
215
216     for (;;) {
217       debug_flush();
218       fprintf(debug,"| x=%d",x);
219       if (x>w) break;
220       Pixcol cv= cols[x];
221       fprintf(debug," cv=%"PSPIXCOL(PRIx),x);
222       for (i=0; i<current->nlinks; i++)
223         if (current->links[i].col == cv)
224           goto found;
225       /* not found */
226       fprintf(debug," ?");
227       break;
228
229     found:
230       current= current->links[i].then;
231       if (current->s[0]) {
232         fprintf(debug," \"%s\"",current->s);
233         bestmatch=current; bestmatch_rx=x;
234       } else {
235         fprintf(debug," ...");
236       }
237       x++;
238     }
239
240     if (bestmatch) {
241       fprintf(debug," YES\n");
242       add_result(bestmatch->s, lx, bestmatch_rx, ctx);
243       x= bestmatch_rx+1;
244       ctx= 0;
245     } else {
246       int rx;
247       fprintf(debug," UNKNOWN");
248       for (rx=lx+1; rx<w && cols[rx]; rx++);
249       fprintf(debug," x=%d ctx=%d %d..%d\n",x, ctx, lx,rx);
250       debug_flush();
251       callout_unknown(w,h,cols, lx,rx, results,nresults);
252       goto restart;
253     }
254   }
255   add_result(0,-1,-1,0);
256   fprintf(debug,"OCR  finished %d glyphs\n",nresults);
257   debug_flush();
258   return results;
259 }
260
261 void ocr_init(void) {
262   ocr_readdb();
263 }