8 struct DatabaseNode *then;
11 #define MAXGLYPHCHRS 7
13 typedef struct DatabaseNode {
14 char s[MAXGLYPHCHRS+1]; /* null-terminated; "" means no match here */
20 static const char *context_names[]= {
26 #define NCONTEXTS (sizeof(context_names)/sizeof(context_names[0]))
28 #define SPACE_SPACES 4
32 DatabaseNode contexts[NCONTEXTS];
33 OcrResultGlyph *results;
34 int aresults, nresults;
37 static FILE *resolver;
38 static pid_t resolver_pid;
39 static int resolver_done;
41 DEBUG_DEFINE_DEBUGF(ocr)
45 fatal("Error in character set database.\n" \
46 " Requirement not met: %s:%d: %s", __FILE__,__LINE__, #x))
48 static void fgetsline(FILE *f, char *lbuf, size_t lbufsz) {
50 char *s= fgets(lbuf,lbufsz,f);
51 sysassert(!ferror(f));
55 dbassert(l>0); dbassert(lbuf[--l]='\n');
58 #define FGETSLINE(f,buf) (fgetsline(f,buf,sizeof(buf)))
60 static void cleardb_node(DatabaseNode *n) {
63 for (i=0; i<n->nlinks; i++)
64 cleardb_node(n->links[i].then);
67 static void readdb(OcrReader *rd) {
69 DatabaseNode *current, *additional;
70 char chrs[MAXGLYPHCHRS+1];
77 for (ctxi=0; ctxi<NCONTEXTS; ctxi++)
78 cleardb_node(&rd->contexts[ctxi]);
81 asprintf(&dbfname,"%s/charset-%d.txt",get_vardir(),rd->h);
84 db= fopen(dbfname,"r");
87 sysassert(errno==ENOENT);
92 dbassert(!strcmp(lbuf,"# ypp-sc-tools pctb font v1"));
94 r= fscanf(db, "%d", &h);
100 if (!lbuf || lbuf[0]=='#') continue;
101 if (!strcmp(lbuf,".")) break;
103 for (ctxi=0; ctxi<NCONTEXTS; ctxi++)
104 if (!strcmp(lbuf,context_names[ctxi]))
106 /* not found, just skip */
107 for (;;) { FGETSLINE(db,lbuf); if (!lbuf[0]) break; }
112 int c= fgetc(db); sysassert(!ferror(db)); dbassert(c!=EOF);
113 if (c=='\n') { dbassert(nchrs); break; }
114 dbassert(nchrs<MAXGLYPHCHRS);
117 c= fgetc(db); sysassert(!ferror(db)); dbassert(c=='x');
118 r= fscanf(db, "%2x", &cr); sysassert(!ferror(db)); dbassert(r==1);
119 assert(cr>0 && cr<=255);
125 if (nchrs>1 && chrs[nchrs-1]==' ') {
131 current= &rd->contexts[ctxi];
134 if (!lbuf[0]) { dbassert(current != &rd->contexts[ctxi]); break; }
136 cv= strtoul(lbuf,&ep,16); dbassert(!*ep);
137 dbassert(!(cv & ~((1UL << rd->h)-1)));
139 for (j=0; j<current->nlinks; j++)
140 if (current->links[j].col == cv) {
141 current= current->links[j].then;
145 additional= mmalloc(sizeof(*additional));
147 additional->nlinks= additional->alinks= 0;
148 additional->links= 0;
149 if (current->nlinks==current->alinks) {
152 current->links= mrealloc(current->links,
153 sizeof(*current->links) * current->alinks);
155 current->links[current->nlinks].col= cv;
156 current->links[current->nlinks].then= additional;
163 dbassert(!current->s[0]);
164 strcpy(current->s, chrs);
165 current->endsword= endsword;
167 sysassert(!ferror(db));
168 sysassert(!fclose(db));
171 static void cu_pr_ctxmap(unsigned ctxmap) {
172 fprintf(resolver,"{");
175 for (ctxi=0; ctxi<NCONTEXTS; ctxi++) {
176 if (!(ctxmap & (1u << ctxi))) continue;
177 fprintf(resolver,"%s%s",spc,context_names[ctxi]);
180 fprintf(resolver,"}");
183 static void callout_unknown(OcrReader *rd, int w, Pixcol cols[],
184 int unk_l, int unk_r, unsigned unk_ctxmap) {
185 int jobpipe[2],donepipe[2], c,i, x,y;
186 const OcrResultGlyph *s;
192 sysassert(! pipe(jobpipe) );
193 sysassert(! pipe(donepipe) );
194 resolver_pid= fork();
195 sysassert(resolver_pid!=-1);
197 sysassert( dup2(jobpipe[0],0) ==0 );
198 sysassert(! close(jobpipe[1]) );
199 sysassert(! close(donepipe[0]) );
200 /* we know donepipe[1] is >= 4 and we have dealt with all the others
201 * so we aren't in any danger of overwriting some other fd 4: */
202 sysassert( dup2(donepipe[1],4) ==4 );
203 execlp("./show-thing.tcl", "./show-thing.tcl",
204 DEBUGP(callout) ? "--debug" : "--noop-arg",
207 sysassert(!"execlp failed");
209 sysassert(! close(jobpipe[0]) );
210 sysassert(! close(donepipe[1]) );
211 resolver= fdopen(jobpipe[1],"w"); sysassert(resolver);
212 resolver_done= donepipe[0];
214 fprintf(resolver,"%d %d ",unk_l,unk_r);
215 cu_pr_ctxmap(unk_ctxmap);
216 for (i=0, s=rd->results; i<rd->nresults; i++, s++) {
217 if (!strcmp(s->s," ")) continue;
218 fprintf(resolver," %d %d ",s->l,s->r);
219 cu_pr_ctxmap(s->ctxmap);
220 fprintf(resolver," ");
221 for (p=s->s; (c= *p); p++) {
222 if (c=='\\') fprintf(resolver,"\\%c",c);
223 else if (c>=33 && c<=126) fputc(c,resolver);
224 else fprintf(resolver,"\\x%02x",(unsigned char)c);
227 fputc('\n',resolver);
231 "static char *t[] = {\n"
232 "/* columns rows colors chars-per-pixel */\n"
237 for (y=0, pv=1; y<rd->h; y++, pv<<=1) {
240 fputc(cols[x] & pv ? 'o' : ' ', resolver);
241 fputs("\",\n",resolver);
243 fputs("};\n",resolver);
244 sysassert(!ferror(resolver));
245 sysassert(!fflush(resolver));
251 r= read(resolver_done,&cb,1);
252 if (r==-1) { sysassert(errno==EINTR); continue; }
260 pid= waitpid(resolver_pid, &st, 0);
261 if (pid==-1) { sysassert(errno==EINTR); continue; }
264 sysassert(pid==resolver_pid);
267 fatal("character resolver failed with nonzero exit status %d",
270 close(resolver_done);
272 } else if (WIFSIGNALED(st)) {
273 fatal("character resolver died due to signal %s%s",
274 strsignal(WTERMSIG(st)), WCOREDUMP(st)?" (core dumped)":"");
276 fatal("character resolver gave strange wait status %d",st);
286 static void add_result(OcrReader *rd, const char *s, int l, int r,
288 if (rd->nresults >= rd->aresults) {
289 rd->aresults++; rd->aresults<<=1;
290 rd->results= mrealloc(rd->results, sizeof(*rd->results)*rd->aresults);
292 rd->results[rd->nresults].s= s;
293 rd->results[rd->nresults].l= l;
294 rd->results[rd->nresults].r= r;
295 rd->results[rd->nresults].ctxmap= ctxmap;
299 struct OcrCellTypeInfo {
300 unsigned initial, nextword, midword;
303 const struct OcrCellTypeInfo ocr_celltype_number= {
307 const struct OcrCellTypeInfo ocr_celltype_text= {
308 .initial=2, /* Uppercase */
309 .nextword=3, /* Either */
310 .midword=1, /* Lower only */
314 const char *ocr_celltype_name(OcrCellType ct) { return ct->name; }
316 OcrResultGlyph *ocr(OcrReader *rd, OcrCellType ct, int w, Pixcol cols[]) {
326 debugf("OCR h=%d w=%d",rd->h,w);
327 for (x=0; x<w; x++) debugf(" %"PSPIXCOL(PRIx),cols[x]);
341 if (nspaces==SPACE_SPACES) {
342 debugf("OCR x=%x nspaces=%d space\n",x,nspaces);
343 ctxmap= ct->nextword;
348 /* something here, so we need to add the spaces */
349 if (nspaces>=SPACE_SPACES)
350 add_result(rd," ",x-nspaces,x+1,0);
356 DatabaseNode *uniquematch= 0;
357 int uniquematch_rx=-1;
359 debugf("OCR lx=%d ctxmap=%x ",lx,ctxmap);
361 for (ctxi=0; ctxi<NCONTEXTS; ctxi++) {
362 DatabaseNode *current= &rd->contexts[ctxi];;
363 DatabaseNode *bestmatch= 0;
367 if (!(ctxmap & (1u << ctxi))) continue;
368 debugf(" || %s",context_names[ctxi]);
375 debugf(" cv=%"PSPIXCOL(PRIx),cv);
376 for (i=0; i<current->nlinks; i++)
377 if (current->links[i].col == cv)
384 current= current->links[i].then;
386 debugf(" \"%s\"%s",current->s,current->endsword?"_":"");
397 if (uniquematch && strcmp(bestmatch->s, uniquematch->s)) {
398 debugf( " ambiguous");
402 uniquematch= bestmatch;
403 uniquematch_rx= bestmatch_rx;
409 add_result(rd, uniquematch->s, lx, uniquematch_rx, ctxmap);
411 if (uniquematch->s[0]) ctxmap= ct->midword;
412 else debugf(" (empty)");
413 if (uniquematch->endsword) {
414 nspaces= SPACE_SPACES;
416 ctxmap= ct->nextword;
421 debugf(" || UNKNOWN");
422 for (rx=lx; rx<w && cols[rx]; rx++);
423 debugf(" x=%d ctxmap=%x %d..%d\n",x, ctxmap, lx,rx);
425 callout_unknown(rd, w,cols, lx,rx-1, ctxmap);
429 add_result(rd, 0,-1,-1,0);
430 debugf("OCR finished %d glyphs\n",rd->nresults);
435 OcrReader *ocr_init(int h) {
438 rd= mmalloc(sizeof(*rd));
439 memset(rd,0,sizeof(*rd));