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)
43 static void fgetsline(FILE *f, char *lbuf, size_t lbufsz) {
44 char *s= fgets(lbuf,lbufsz,f);
47 eassert(l>0); eassert(lbuf[--l]='\n');
50 #define FGETSLINE(f,buf) (fgetsline(f,buf,sizeof(buf)))
52 static void cleardb_node(DatabaseNode *n) {
55 for (i=0; i<n->nlinks; i++)
56 cleardb_node(n->links[i].then);
59 static void readdb(OcrReader *rd) {
61 DatabaseNode *current, *additional;
62 char chrs[MAXGLYPHCHRS+1];
69 for (ctxi=0; ctxi<NCONTEXTS; ctxi++)
70 cleardb_node(&rd->contexts[ctxi]);
73 asprintf(&dbfname,"%s/charset-%d.txt",get_vardir(),rd->h);
76 db= fopen(dbfname,"r");
79 eassert(errno==ENOENT);
84 eassert(!strcmp(lbuf,"# ypp-sc-tools pctb font v1"));
86 r= fscanf(db, "%d", &h);
92 if (!lbuf || lbuf[0]=='#') continue;
93 if (!strcmp(lbuf,".")) break;
95 for (ctxi=0; ctxi<NCONTEXTS; ctxi++)
96 if (!strcmp(lbuf,context_names[ctxi]))
98 /* not found, just skip */
99 for (;;) { FGETSLINE(db,lbuf); if (!lbuf[0]) break; }
104 int c= fgetc(db); eassert(c!=EOF);
105 if (c=='\n') { eassert(nchrs); break; }
106 eassert(nchrs<MAXGLYPHCHRS);
109 c= fgetc(db); eassert(c=='x');
110 r= fscanf(db, "%2x", &cr); eassert(r==1);
111 assert(cr>0 && cr<=255);
117 if (nchrs>1 && chrs[nchrs-1]==' ') {
123 current= &rd->contexts[ctxi];
126 if (!lbuf[0]) { eassert(current != &rd->contexts[ctxi]); break; }
128 cv= strtoul(lbuf,&ep,16); eassert(!*ep);
129 eassert(!(cv & ~((1UL << rd->h)-1)));
131 for (j=0; j<current->nlinks; j++)
132 if (current->links[j].col == cv) {
133 current= current->links[j].then;
137 additional= malloc(sizeof(*additional)); eassert(additional);
139 additional->nlinks= additional->alinks= 0;
140 additional->links= 0;
141 if (current->nlinks==current->alinks) {
144 current->links= realloc(current->links,
145 sizeof(*current->links) * current->alinks);
146 eassert(current->links);
148 current->links[current->nlinks].col= cv;
149 current->links[current->nlinks].then= additional;
156 eassert(!current->s[0]);
157 strcpy(current->s, chrs);
158 current->endsword= endsword;
160 eassert(!ferror(db));
161 eassert(!fclose(db));
164 static void cu_pr_ctxmap(unsigned ctxmap) {
165 fprintf(resolver,"{");
168 for (ctxi=0; ctxi<NCONTEXTS; ctxi++) {
169 if (!(ctxmap & (1u << ctxi))) continue;
170 fprintf(resolver,"%s%s",spc,context_names[ctxi]);
173 fprintf(resolver,"}");
176 static void callout_unknown(OcrReader *rd, int w, Pixcol cols[],
177 int unk_l, int unk_r, unsigned unk_ctxmap) {
178 int jobpipe[2],donepipe[2], c, r,i, x,y;
179 const OcrResultGlyph *s;
185 r= pipe(jobpipe); eassert(!r);
186 r= pipe(donepipe); eassert(!r);
187 resolver_pid= fork();
188 eassert(resolver_pid!=-1);
190 r= dup2(jobpipe[0],0); eassert(r==0);
191 r= close(jobpipe[1]); eassert(!r);
192 r= close(donepipe[0]); eassert(!r);
193 /* we know donepipe[1] is >= 4 and we have dealt with all the others
194 * so we aren't in any danger of overwriting some other fd 4: */
195 r= dup2(donepipe[1],4); eassert(r==4);
196 execlp("./show-thing.tcl", "./show-thing.tcl",
197 DEBUGP(callout) ? "--debug" : "--noop-arg",
200 eassert(!"execlp failed");
202 r= close(jobpipe[0]); eassert(!r);
203 r= close(donepipe[1]); eassert(!r);
204 resolver= fdopen(jobpipe[1],"w"); eassert(resolver);
205 resolver_done= donepipe[0];
207 fprintf(resolver,"%d %d ",unk_l,unk_r);
208 cu_pr_ctxmap(unk_ctxmap);
209 for (i=0, s=rd->results; i<rd->nresults; i++, s++) {
210 if (!strcmp(s->s," ")) continue;
211 fprintf(resolver," %d %d ",s->l,s->r);
212 cu_pr_ctxmap(s->ctxmap);
213 fprintf(resolver," ");
214 for (p=s->s; (c= *p); p++) {
215 if (c=='\\') fprintf(resolver,"\\%c",c);
216 else if (c>=33 && c<=126) fputc(c,resolver);
217 else fprintf(resolver,"\\x%02x",(unsigned char)c);
220 fputc('\n',resolver);
224 "static char *t[] = {\n"
225 "/* columns rows colors chars-per-pixel */\n"
230 for (y=0, pv=1; y<rd->h; y++, pv<<=1) {
233 fputc(cols[x] & pv ? 'o' : ' ', resolver);
234 fputs("\",\n",resolver);
236 fputs("};\n",resolver);
237 eassert(!ferror(resolver));
238 eassert(!fflush(resolver));
243 r= read(resolver_done,&cb,1);
244 if (r==-1) { eassert(errno==EINTR); continue; }
251 pid= waitpid(resolver_pid, &r, 0);
252 if (pid==-1) { eassert(errno==EINTR); continue; }
255 eassert(pid==resolver_pid);
257 eassert(!WEXITSTATUS(r));
259 close(resolver_done);
261 } else if (WIFSIGNALED(r)) {
262 eassert(!"resolver child died due to signal");
264 eassert(!"weird wait status");
274 static void add_result(OcrReader *rd, const char *s, int l, int r,
276 if (rd->nresults >= rd->aresults) {
277 rd->aresults++; rd->aresults<<=1;
278 rd->results= realloc(rd->results,sizeof(*rd->results)*rd->aresults);
279 eassert(rd->results);
281 rd->results[rd->nresults].s= s;
282 rd->results[rd->nresults].l= l;
283 rd->results[rd->nresults].r= r;
284 rd->results[rd->nresults].ctxmap= ctxmap;
288 struct OcrCellTypeInfo {
289 unsigned initial, nextword, midword;
291 const struct OcrCellTypeInfo ocr_celltype_number= {
294 const struct OcrCellTypeInfo ocr_celltype_text= {
295 .initial=2 /* Uppercase */,
296 .nextword=3 /* Either */,
297 .midword=1 /* Lower only */
300 OcrResultGlyph *ocr(OcrReader *rd, OcrCellType ct, int w, Pixcol cols[]) {
310 debugf("OCR h=%d w=%d",rd->h,w);
311 for (x=0; x<w; x++) debugf(" %"PSPIXCOL(PRIx),cols[x]);
325 if (nspaces==SPACE_SPACES) {
326 debugf("OCR x=%x nspaces=%d space\n",x,nspaces);
327 ctxmap= ct->nextword;
332 /* something here, so we need to add the spaces */
333 if (nspaces>=SPACE_SPACES)
334 add_result(rd," ",x-nspaces,x+1,0);
340 DatabaseNode *uniquematch= 0;
341 int uniquematch_rx=-1;
343 debugf("OCR lx=%d ctxmap=%x ",lx,ctxmap);
345 for (ctxi=0; ctxi<NCONTEXTS; ctxi++) {
346 DatabaseNode *current= &rd->contexts[ctxi];;
347 DatabaseNode *bestmatch= 0;
351 if (!(ctxmap & (1u << ctxi))) continue;
352 debugf(" || %s",context_names[ctxi]);
359 debugf(" cv=%"PSPIXCOL(PRIx),cv);
360 for (i=0; i<current->nlinks; i++)
361 if (current->links[i].col == cv)
368 current= current->links[i].then;
370 debugf(" \"%s\"%s",current->s,current->endsword?"_":"");
381 if (uniquematch && strcmp(bestmatch->s, uniquematch->s)) {
382 debugf( " ambiguous");
386 uniquematch= bestmatch;
387 uniquematch_rx= bestmatch_rx;
393 add_result(rd, uniquematch->s, lx, uniquematch_rx, ctxmap);
395 if (uniquematch->s[0]) ctxmap= ct->midword;
396 else debugf(" (empty)");
397 if (uniquematch->endsword) {
398 nspaces= SPACE_SPACES;
400 ctxmap= ct->nextword;
405 debugf(" || UNKNOWN");
406 for (rx=lx; rx<w && cols[rx]; rx++);
407 debugf(" x=%d ctxmap=%x %d..%d\n",x, ctxmap, lx,rx);
409 callout_unknown(rd, w,cols, lx,rx-1, ctxmap);
413 add_result(rd, 0,-1,-1,0);
414 debugf("OCR finished %d glyphs\n",rd->nresults);
419 OcrReader *ocr_init(int h) {
422 rd= malloc(sizeof(*rd)); eassert(rd);
423 memset(rd,0,sizeof(*rd));