8 struct DatabaseNode *then;
11 #define MAXGLYPHCHRS 3
13 typedef struct DatabaseNode {
14 char s[MAXGLYPHCHRS+1]; /* null-terminated; "" means no match here */
19 static const char *context_names[]= {
25 #define NCONTEXTS (sizeof(context_names)/sizeof(context_names[0]))
29 DatabaseNode contexts[NCONTEXTS];
30 OcrResultGlyph *results;
31 int aresults, nresults;
34 static FILE *resolver;
35 static pid_t resolver_pid;
36 static int resolver_done;
38 static void fgetsline(FILE *f, char *lbuf, size_t lbufsz) {
39 char *s= fgets(lbuf,lbufsz,f);
42 eassert(l>0); eassert(lbuf[--l]='\n');
45 #define FGETSLINE(f,buf) (fgetsline(f,buf,sizeof(buf)))
47 static void cleardb_node(DatabaseNode *n) {
50 for (i=0; i<n->nlinks; i++)
51 cleardb_node(n->links[i].then);
54 static void readdb(OcrReader *rd) {
56 DatabaseNode *current, *additional;
57 char chrs[MAXGLYPHCHRS+1];
65 asprintf(&dbfname,"%s/charset-%d.txt",get_vardir(),rd->h);
68 db= fopen(dbfname,"r"); eassert(db);
72 eassert(!strcmp(lbuf,"# ypp-sc-tools pctb font v1"));
74 r= fscanf(db, "%d", &h);
78 for (ctxi=0; ctxi<NCONTEXTS; ctxi++)
79 cleardb_node(&rd->contexts[ctxi]);
83 if (!lbuf || lbuf[0]=='#') continue;
84 if (!strcmp(lbuf,".")) break;
86 for (ctxi=0; ctxi<NCONTEXTS; ctxi++)
87 if (!strcmp(lbuf,context_names[ctxi]))
89 /* not found, just skip */
90 for (;;) { FGETSLINE(db,lbuf); if (!lbuf[0]) break; }
95 int c= fgetc(db); eassert(c!=EOF);
96 if (c=='\n') { eassert(nchrs); break; }
97 eassert(nchrs<MAXGLYPHCHRS);
100 c= fgetc(db); eassert(c=='x');
101 r= fscanf(db, "%2x", &cr); eassert(r==1);
102 assert(cr>0 && cr<=255);
109 current= &rd->contexts[ctxi];
112 if (!lbuf[0]) { eassert(current != &rd->contexts[ctxi]); break; }
114 cv= strtoul(lbuf,&ep,16); eassert(!*ep);
115 eassert(!(cv & ~((1UL << rd->h)-1)));
117 for (j=0; j<current->nlinks; j++)
118 if (current->links[j].col == cv) {
119 current= current->links[j].then;
123 additional= malloc(sizeof(*additional)); eassert(additional);
125 additional->nlinks= additional->alinks= 0;
126 additional->links= 0;
127 if (current->nlinks==current->alinks) {
130 current->links= realloc(current->links,
131 sizeof(*current->links) * current->alinks);
132 eassert(current->links);
134 current->links[current->nlinks].col= cv;
135 current->links[current->nlinks].then= additional;
142 eassert(!current->s[0]);
143 strcpy(current->s, chrs);
145 eassert(!ferror(db));
146 eassert(!fclose(db));
149 static void cu_pr_ctxmap(unsigned ctxmap) {
150 fprintf(resolver,"{");
153 for (ctxi=0; ctxi<NCONTEXTS; ctxi++) {
154 if (!(ctxmap & (1u << ctxi))) continue;
155 fprintf(resolver,"%s%s",spc,context_names[ctxi]);
158 fprintf(resolver,"}");
161 static void callout_unknown(OcrReader *rd, int w, Pixcol cols[],
162 int unk_l, int unk_r, unsigned unk_ctxmap) {
163 int jobpipe[2],donepipe[2], c, r,i, x,y;
164 const OcrResultGlyph *s;
170 r= pipe(jobpipe); eassert(!r);
171 r= pipe(donepipe); eassert(!r);
172 resolver_pid= fork();
173 eassert(resolver_pid!=-1);
175 r= dup2(jobpipe[0],0); eassert(r==0);
176 r= close(jobpipe[1]); eassert(!r);
177 r= close(donepipe[0]); eassert(!r);
178 /* we know donepipe[1] is >= 4 and we have dealt with all the others
179 * so we aren't in any danger of overwriting some other fd 4: */
180 r= dup2(donepipe[1],4); eassert(r==4);
181 execlp("./show-thing.tcl", "./show-thing.tcl",
182 "--automatic","1",(char*)0);
183 eassert(!"execlp failed");
185 r= close(jobpipe[0]); eassert(!r);
186 r= close(donepipe[1]); eassert(!r);
187 resolver= fdopen(jobpipe[1],"w"); eassert(resolver);
188 resolver_done= donepipe[0];
190 fprintf(resolver,"%d %d ",unk_l,unk_r);
191 cu_pr_ctxmap(unk_ctxmap);
192 for (i=0, s=rd->results; i<rd->nresults; i++, s++) {
193 if (!strcmp(s->s," ")) continue;
194 fprintf(resolver," %d %d ",s->l,s->r);
195 cu_pr_ctxmap(s->ctxmap);
196 fprintf(resolver," ");
197 for (p=s->s; (c= *p); p++) {
198 if (c=='\\') fprintf(resolver,"\\%c",c);
199 else if (c>=33 && c<=126) fputc(c,resolver);
200 else fprintf(resolver,"\\x%02x",(unsigned char)c);
203 fputc('\n',resolver);
207 "static char *t[] = {\n"
208 "/* columns rows colors chars-per-pixel */\n"
213 for (y=0, pv=1; y<rd->h; y++, pv<<=1) {
216 fputc(cols[x] & pv ? 'o' : ' ', resolver);
217 fputs("\",\n",resolver);
219 fputs("};\n",resolver);
220 eassert(!ferror(resolver));
221 eassert(!fflush(resolver));
226 r= read(resolver_done,&cb,1);
227 if (r==-1) { eassert(errno==EINTR); continue; }
234 pid= waitpid(resolver_pid, &r, 0);
235 if (pid==-1) { eassert(errno==EINTR); continue; }
238 eassert(pid==resolver_pid);
240 eassert(!WEXITSTATUS(r));
242 close(resolver_done);
244 } else if (WIFSIGNALED(r)) {
245 eassert(!"resolver child died due to signal");
247 eassert(!"weird wait status");
257 static void add_result(OcrReader *rd, const char *s, int l, int r,
259 if (rd->nresults >= rd->aresults) {
260 rd->aresults++; rd->aresults<<=1;
261 rd->results= realloc(rd->results,sizeof(*rd->results)*rd->aresults);
262 eassert(rd->results);
264 rd->results[rd->nresults].s= s;
265 rd->results[rd->nresults].l= l;
266 rd->results[rd->nresults].r= r;
267 rd->results[rd->nresults].ctxmap= ctxmap;
271 struct OcrCellTypeInfo {
272 unsigned initial, nextword, midword;
274 const struct OcrCellTypeInfo ocr_celltype_number= {
277 const struct OcrCellTypeInfo ocr_celltype_text= {
278 .initial=2 /* Uppercase */,
279 .nextword=3 /* Either */,
280 .midword=1 /* Lower only */
283 OcrResultGlyph *ocr(OcrReader *rd, OcrCellType ct, int w, Pixcol cols[]) {
293 fprintf(debug,"OCR h=%d w=%d",rd->h,w);
294 for (x=0; x<w; x++) fprintf(debug," %"PSPIXCOL(PRIx),cols[x]);
309 fprintf(debug,"OCR x=%x nspaces=%d space\n",x,nspaces);
310 add_result(rd," ",x-nspaces,x+1,0);
311 ctxmap= ct->nextword;
320 DatabaseNode *uniquematch= 0;
321 int uniquematch_rx=-1;
323 fprintf(debug,"OCR lx=%d ctxmap=%x ",lx,ctxmap);
325 for (ctxi=0; ctxi<NCONTEXTS; ctxi++) {
326 DatabaseNode *current= &rd->contexts[ctxi];;
327 DatabaseNode *bestmatch= 0;
331 if (!(ctxmap & (1u << ctxi))) continue;
332 fprintf(debug," || %s",context_names[ctxi]);
336 fprintf(debug," | x=%d",x);
339 fprintf(debug," cv=%"PSPIXCOL(PRIx),cv);
340 for (i=0; i<current->nlinks; i++)
341 if (current->links[i].col == cv)
348 current= current->links[i].then;
350 fprintf(debug," \"%s\"",current->s);
354 fprintf(debug," ...");
361 if (uniquematch && strcmp(bestmatch->s, uniquematch->s)) {
362 fprintf(debug, " ambiguous");
366 uniquematch= bestmatch;
367 uniquematch_rx= bestmatch_rx;
372 fprintf(debug," || YES\n");
373 add_result(rd, uniquematch->s, lx, uniquematch_rx, ctxmap);
378 fprintf(debug," || UNKNOWN");
379 for (rx=lx; rx<w && cols[rx]; rx++);
380 fprintf(debug," x=%d ctxmap=%x %d..%d\n",x, ctxmap, lx,rx);
382 callout_unknown(rd, w,cols, lx,rx-1, ctxmap);
386 add_result(rd, 0,-1,-1,0);
387 fprintf(debug,"OCR finished %d glyphs\n",rd->nresults);
392 OcrReader *ocr_init(int h) {
395 rd= malloc(sizeof(*rd)); eassert(rd);
396 memset(rd,0,sizeof(*rd));