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]))
27 #define SPACE_SPACES 3
31 DatabaseNode contexts[NCONTEXTS];
32 OcrResultGlyph *results;
33 int aresults, nresults;
36 static FILE *resolver;
37 static pid_t resolver_pid;
38 static int resolver_done;
40 static void fgetsline(FILE *f, char *lbuf, size_t lbufsz) {
41 char *s= fgets(lbuf,lbufsz,f);
44 eassert(l>0); eassert(lbuf[--l]='\n');
47 #define FGETSLINE(f,buf) (fgetsline(f,buf,sizeof(buf)))
49 static void cleardb_node(DatabaseNode *n) {
52 for (i=0; i<n->nlinks; i++)
53 cleardb_node(n->links[i].then);
56 static void readdb(OcrReader *rd) {
58 DatabaseNode *current, *additional;
59 char chrs[MAXGLYPHCHRS+1];
67 asprintf(&dbfname,"%s/charset-%d.txt",get_vardir(),rd->h);
70 db= fopen(dbfname,"r"); eassert(db);
74 eassert(!strcmp(lbuf,"# ypp-sc-tools pctb font v1"));
76 r= fscanf(db, "%d", &h);
80 for (ctxi=0; ctxi<NCONTEXTS; ctxi++)
81 cleardb_node(&rd->contexts[ctxi]);
85 if (!lbuf || lbuf[0]=='#') continue;
86 if (!strcmp(lbuf,".")) break;
88 for (ctxi=0; ctxi<NCONTEXTS; ctxi++)
89 if (!strcmp(lbuf,context_names[ctxi]))
91 /* not found, just skip */
92 for (;;) { FGETSLINE(db,lbuf); if (!lbuf[0]) break; }
97 int c= fgetc(db); eassert(c!=EOF);
98 if (c=='\n') { eassert(nchrs); break; }
99 eassert(nchrs<MAXGLYPHCHRS);
102 c= fgetc(db); eassert(c=='x');
103 r= fscanf(db, "%2x", &cr); eassert(r==1);
104 assert(cr>0 && cr<=255);
111 current= &rd->contexts[ctxi];
114 if (!lbuf[0]) { eassert(current != &rd->contexts[ctxi]); break; }
116 cv= strtoul(lbuf,&ep,16); eassert(!*ep);
117 eassert(!(cv & ~((1UL << rd->h)-1)));
119 for (j=0; j<current->nlinks; j++)
120 if (current->links[j].col == cv) {
121 current= current->links[j].then;
125 additional= malloc(sizeof(*additional)); eassert(additional);
127 additional->nlinks= additional->alinks= 0;
128 additional->links= 0;
129 if (current->nlinks==current->alinks) {
132 current->links= realloc(current->links,
133 sizeof(*current->links) * current->alinks);
134 eassert(current->links);
136 current->links[current->nlinks].col= cv;
137 current->links[current->nlinks].then= additional;
144 eassert(!current->s[0]);
145 strcpy(current->s, chrs);
147 eassert(!ferror(db));
148 eassert(!fclose(db));
151 static void cu_pr_ctxmap(unsigned ctxmap) {
152 fprintf(resolver,"{");
155 for (ctxi=0; ctxi<NCONTEXTS; ctxi++) {
156 if (!(ctxmap & (1u << ctxi))) continue;
157 fprintf(resolver,"%s%s",spc,context_names[ctxi]);
160 fprintf(resolver,"}");
163 static void callout_unknown(OcrReader *rd, int w, Pixcol cols[],
164 int unk_l, int unk_r, unsigned unk_ctxmap) {
165 int jobpipe[2],donepipe[2], c, r,i, x,y;
166 const OcrResultGlyph *s;
172 r= pipe(jobpipe); eassert(!r);
173 r= pipe(donepipe); eassert(!r);
174 resolver_pid= fork();
175 eassert(resolver_pid!=-1);
177 r= dup2(jobpipe[0],0); eassert(r==0);
178 r= close(jobpipe[1]); eassert(!r);
179 r= close(donepipe[0]); eassert(!r);
180 /* we know donepipe[1] is >= 4 and we have dealt with all the others
181 * so we aren't in any danger of overwriting some other fd 4: */
182 r= dup2(donepipe[1],4); eassert(r==4);
183 execlp("./show-thing.tcl", "./show-thing.tcl",
184 "--automatic","1",(char*)0);
185 eassert(!"execlp failed");
187 r= close(jobpipe[0]); eassert(!r);
188 r= close(donepipe[1]); eassert(!r);
189 resolver= fdopen(jobpipe[1],"w"); eassert(resolver);
190 resolver_done= donepipe[0];
192 fprintf(resolver,"%d %d ",unk_l,unk_r);
193 cu_pr_ctxmap(unk_ctxmap);
194 for (i=0, s=rd->results; i<rd->nresults; i++, s++) {
195 if (!strcmp(s->s," ")) continue;
196 fprintf(resolver," %d %d ",s->l,s->r);
197 cu_pr_ctxmap(s->ctxmap);
198 fprintf(resolver," ");
199 for (p=s->s; (c= *p); p++) {
200 if (c=='\\') fprintf(resolver,"\\%c",c);
201 else if (c>=33 && c<=126) fputc(c,resolver);
202 else fprintf(resolver,"\\x%02x",(unsigned char)c);
205 fputc('\n',resolver);
209 "static char *t[] = {\n"
210 "/* columns rows colors chars-per-pixel */\n"
215 for (y=0, pv=1; y<rd->h; y++, pv<<=1) {
218 fputc(cols[x] & pv ? 'o' : ' ', resolver);
219 fputs("\",\n",resolver);
221 fputs("};\n",resolver);
222 eassert(!ferror(resolver));
223 eassert(!fflush(resolver));
228 r= read(resolver_done,&cb,1);
229 if (r==-1) { eassert(errno==EINTR); continue; }
236 pid= waitpid(resolver_pid, &r, 0);
237 if (pid==-1) { eassert(errno==EINTR); continue; }
240 eassert(pid==resolver_pid);
242 eassert(!WEXITSTATUS(r));
244 close(resolver_done);
246 } else if (WIFSIGNALED(r)) {
247 eassert(!"resolver child died due to signal");
249 eassert(!"weird wait status");
259 static void add_result(OcrReader *rd, const char *s, int l, int r,
261 if (rd->nresults >= rd->aresults) {
262 rd->aresults++; rd->aresults<<=1;
263 rd->results= realloc(rd->results,sizeof(*rd->results)*rd->aresults);
264 eassert(rd->results);
266 rd->results[rd->nresults].s= s;
267 rd->results[rd->nresults].l= l;
268 rd->results[rd->nresults].r= r;
269 rd->results[rd->nresults].ctxmap= ctxmap;
273 struct OcrCellTypeInfo {
274 unsigned initial, nextword, midword;
276 const struct OcrCellTypeInfo ocr_celltype_number= {
279 const struct OcrCellTypeInfo ocr_celltype_text= {
280 .initial=2 /* Uppercase */,
281 .nextword=3 /* Either */,
282 .midword=1 /* Lower only */
285 OcrResultGlyph *ocr(OcrReader *rd, OcrCellType ct, int w, Pixcol cols[]) {
295 fprintf(debug,"OCR h=%d w=%d",rd->h,w);
296 for (x=0; x<w; x++) fprintf(debug," %"PSPIXCOL(PRIx),cols[x]);
310 if (nspaces==SPACE_SPACES) {
311 fprintf(debug,"OCR x=%x nspaces=%d space\n",x,nspaces);
312 ctxmap= ct->nextword;
317 /* something here, so we need to add the spaces */
318 if (nspaces>=SPACE_SPACES)
319 add_result(rd," ",x-nspaces,x+1,0);
325 DatabaseNode *uniquematch= 0;
326 int uniquematch_rx=-1;
328 fprintf(debug,"OCR lx=%d ctxmap=%x ",lx,ctxmap);
330 for (ctxi=0; ctxi<NCONTEXTS; ctxi++) {
331 DatabaseNode *current= &rd->contexts[ctxi];;
332 DatabaseNode *bestmatch= 0;
336 if (!(ctxmap & (1u << ctxi))) continue;
337 fprintf(debug," || %s",context_names[ctxi]);
341 fprintf(debug," | x=%d",x);
344 fprintf(debug," cv=%"PSPIXCOL(PRIx),cv);
345 for (i=0; i<current->nlinks; i++)
346 if (current->links[i].col == cv)
353 current= current->links[i].then;
355 fprintf(debug," \"%s\"",current->s);
359 fprintf(debug," ...");
366 if (uniquematch && strcmp(bestmatch->s, uniquematch->s)) {
367 fprintf(debug, " ambiguous");
371 uniquematch= bestmatch;
372 uniquematch_rx= bestmatch_rx;
377 fprintf(debug," || YES\n");
378 add_result(rd, uniquematch->s, lx, uniquematch_rx, ctxmap);
383 fprintf(debug," || UNKNOWN");
384 for (rx=lx; rx<w && cols[rx]; rx++);
385 fprintf(debug," x=%d ctxmap=%x %d..%d\n",x, ctxmap, lx,rx);
387 callout_unknown(rd, w,cols, lx,rx-1, ctxmap);
391 add_result(rd, 0,-1,-1,0);
392 fprintf(debug,"OCR finished %d glyphs\n",rd->nresults);
397 OcrReader *ocr_init(int h) {
400 rd= malloc(sizeof(*rd)); eassert(rd);
401 memset(rd,0,sizeof(*rd));