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 callout_unknown(OcrReader *rd, int w, Pixcol cols[],
150 int unk_l, int unk_r, unsigned unk_ctxmap) {
151 int jobpipe[2],donepipe[2], c, r,i, x,y;
152 const OcrResultGlyph *s;
158 r= pipe(jobpipe); eassert(!r);
159 r= pipe(donepipe); eassert(!r);
160 resolver_pid= fork();
161 eassert(resolver_pid!=-1);
163 r= dup2(jobpipe[0],0); eassert(r==0);
164 r= close(jobpipe[1]); eassert(!r);
165 r= close(donepipe[0]); eassert(!r);
166 /* we know donepipe[1] is >= 4 and we have dealt with all the others
167 * so we aren't in any danger of overwriting some other fd 4: */
168 r= dup2(donepipe[1],4); eassert(r==4);
169 execlp("./show-thing.tcl", "./show-thing.tcl",
170 "--automatic","1",(char*)0);
171 eassert(!"execlp failed");
173 r= close(jobpipe[0]); eassert(!r);
174 r= close(donepipe[1]); eassert(!r);
175 resolver= fdopen(jobpipe[1],"w"); eassert(resolver);
176 resolver_done= donepipe[0];
178 fprintf(resolver,"%d %d {",unk_l,unk_r);
181 for (ctxi=0; ctxi<NCONTEXTS; ctxi++) {
182 if (!(unk_ctxmap & (1u << ctxi))) continue;
183 fprintf(resolver,"%s%s",spc,context_names[ctxi]);
186 fprintf(resolver,"}");
187 for (i=0, s=rd->results; i<rd->nresults; i++, s++) {
188 if (!strcmp(s->s," ")) continue;
189 fprintf(resolver," %d %d %s ",s->l,s->r,context_names[s->ctx]);
190 for (p=s->s; (c= *p); p++) {
191 if (c=='\\') fprintf(resolver,"\\%c",c);
192 else if (c>=33 && c<=126) fputc(c,resolver);
193 else fprintf(resolver,"\\x%02x",(unsigned char)c);
196 fputc('\n',resolver);
200 "static char *t[] = {\n"
201 "/* columns rows colors chars-per-pixel */\n"
206 for (y=0, pv=1; y<rd->h; y++, pv<<=1) {
209 fputc(cols[x] & pv ? 'o' : ' ', resolver);
210 fputs("\",\n",resolver);
212 fputs("};\n",resolver);
213 eassert(!ferror(resolver));
214 eassert(!fflush(resolver));
219 r= read(resolver_done,&cb,1);
220 if (r==-1) { eassert(errno==EINTR); continue; }
227 pid= waitpid(resolver_pid, &r, 0);
228 if (pid==-1) { eassert(errno==EINTR); continue; }
231 eassert(pid==resolver_pid);
233 eassert(!WEXITSTATUS(r));
235 close(resolver_done);
237 } else if (WIFSIGNALED(r)) {
238 eassert(!"resolver child died due to signal");
240 eassert(!"weird wait status");
250 static void add_result(OcrReader *rd, const char *s, int l, int r, int ctx) {
251 if (rd->nresults >= rd->aresults) {
252 rd->aresults++; rd->aresults<<=1;
253 rd->results= realloc(rd->results,sizeof(*rd->results)*rd->aresults);
254 eassert(rd->results);
256 rd->results[rd->nresults].s= s;
257 rd->results[rd->nresults].l= l;
258 rd->results[rd->nresults].r= r;
259 rd->results[rd->nresults].ctx= ctx;
263 OcrResultGlyph *ocr(OcrReader *rd, int w, Pixcol cols[]) {
265 unsigned ctxmap=2; /* uppercase */
270 fprintf(debug,"OCR h=%d w=%d",rd->h,w);
271 for (x=0; x<w; x++) fprintf(debug," %"PSPIXCOL(PRIx),cols[x]);
287 fprintf(debug,"OCR x=%x nspaces=%d space\n",x,nspaces);
288 add_result(rd," ",x-nspaces,x+1,0);
289 ctxmap=3; /* either */
298 DatabaseNode *uniquematch= 0;
299 int uniquematch_rx=-1, uniquematch_ctxi=-1;
301 fprintf(debug,"OCR lx=%d ctxmap=%x ",lx,ctxmap);
303 for (ctxi=0; ctxi<NCONTEXTS; ctxi++) {
304 DatabaseNode *current= &rd->contexts[ctxi];;
305 DatabaseNode *bestmatch= 0;
309 if (!(ctxmap & (1u << ctxi))) continue;
310 fprintf(debug," || %s",context_names[ctxi]);
314 fprintf(debug," | x=%d",x);
317 fprintf(debug," cv=%"PSPIXCOL(PRIx),cv);
318 for (i=0; i<current->nlinks; i++)
319 if (current->links[i].col == cv)
326 current= current->links[i].then;
328 fprintf(debug," \"%s\"",current->s);
332 fprintf(debug," ...");
340 fprintf(debug, " ambiguous");
344 uniquematch= bestmatch;
345 uniquematch_rx= bestmatch_rx;
346 uniquematch_ctxi= ctxi;
351 fprintf(debug," || YES\n");
352 add_result(rd, uniquematch->s, lx, uniquematch_rx, uniquematch_ctxi);
354 ctxmap= 1; /* Lower only */
357 fprintf(debug," || UNKNOWN");
358 for (rx=lx; rx<w && cols[rx]; rx++);
359 fprintf(debug," x=%d ctxmap=%x %d..%d\n",x, ctxmap, lx,rx);
361 callout_unknown(rd, w,cols, lx,rx-1, ctxmap);
365 add_result(rd, 0,-1,-1,0);
366 fprintf(debug,"OCR finished %d glyphs\n",rd->nresults);
371 OcrReader *ocr_init(int h) {
374 rd= malloc(sizeof(*rd)); eassert(rd);
375 memset(rd,0,sizeof(*rd));