chiark / gitweb /
WIP now has most of the ocr parts but does not work yet
[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  restart:
190   x=0;
191   for (;;) {
192     /* skip spaces */
193     if (x>=w) break;
194
195     if (!cols[x]) {
196       nspaces++;
197       x++;
198       if (nspaces>3) ctx=1;
199       continue;
200     }
201
202     /* find character */
203     OCRDatabaseNode *current=0, *bestmatch=0;
204     int lx=x;
205     int bestmatch_rx=-1;
206     current= &ocr_contexts[ctx];
207     for (;;) {
208       if (x>w) break;
209       Pixcol cv= cols[x];
210       for (i=0; i<current->nlinks; i++)
211         if (current->links[i].col == cv)
212           goto found;
213       /* not found */
214       break;
215     found:
216       current= current->links[i].then;
217       if (current->s[0]) { bestmatch=current; bestmatch_rx=x; }
218       x++;
219     }
220
221     if (!bestmatch) {
222       add_result(bestmatch->s, lx, bestmatch_rx, ctx);
223       x= bestmatch_rx+1;
224       ctx= 0;
225     } else {
226       
227       int rx;
228       for (rx=lx+1; rx<w && cols[rx]; rx++);
229       printf("UNKNOWN x=%d ctx=%d %d..%d\n",x, ctx, lx,rx);
230       callout_unknown(w,h,cols, lx,rx, results,nresults);
231       goto restart;
232     }
233   }
234   add_result(0,-1,-1,0);
235   return results;
236 }
237
238 void ocr_init(void) {
239   ocr_readdb();
240 }