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];
66 for (ctxi=0; ctxi<NCONTEXTS; ctxi++)
67 cleardb_node(&rd->contexts[ctxi]);
70 asprintf(&dbfname,"%s/charset-%d.txt",get_vardir(),rd->h);
73 db= fopen(dbfname,"r");
76 eassert(errno==ENOENT);
81 eassert(!strcmp(lbuf,"# ypp-sc-tools pctb font v1"));
83 r= fscanf(db, "%d", &h);
89 if (!lbuf || lbuf[0]=='#') continue;
90 if (!strcmp(lbuf,".")) break;
92 for (ctxi=0; ctxi<NCONTEXTS; ctxi++)
93 if (!strcmp(lbuf,context_names[ctxi]))
95 /* not found, just skip */
96 for (;;) { FGETSLINE(db,lbuf); if (!lbuf[0]) break; }
101 int c= fgetc(db); eassert(c!=EOF);
102 if (c=='\n') { eassert(nchrs); break; }
103 eassert(nchrs<MAXGLYPHCHRS);
106 c= fgetc(db); eassert(c=='x');
107 r= fscanf(db, "%2x", &cr); eassert(r==1);
108 assert(cr>0 && cr<=255);
115 current= &rd->contexts[ctxi];
118 if (!lbuf[0]) { eassert(current != &rd->contexts[ctxi]); break; }
120 cv= strtoul(lbuf,&ep,16); eassert(!*ep);
121 eassert(!(cv & ~((1UL << rd->h)-1)));
123 for (j=0; j<current->nlinks; j++)
124 if (current->links[j].col == cv) {
125 current= current->links[j].then;
129 additional= malloc(sizeof(*additional)); eassert(additional);
131 additional->nlinks= additional->alinks= 0;
132 additional->links= 0;
133 if (current->nlinks==current->alinks) {
136 current->links= realloc(current->links,
137 sizeof(*current->links) * current->alinks);
138 eassert(current->links);
140 current->links[current->nlinks].col= cv;
141 current->links[current->nlinks].then= additional;
148 eassert(!current->s[0]);
149 strcpy(current->s, chrs);
151 eassert(!ferror(db));
152 eassert(!fclose(db));
155 static void cu_pr_ctxmap(unsigned ctxmap) {
156 fprintf(resolver,"{");
159 for (ctxi=0; ctxi<NCONTEXTS; ctxi++) {
160 if (!(ctxmap & (1u << ctxi))) continue;
161 fprintf(resolver,"%s%s",spc,context_names[ctxi]);
164 fprintf(resolver,"}");
167 static void callout_unknown(OcrReader *rd, int w, Pixcol cols[],
168 int unk_l, int unk_r, unsigned unk_ctxmap) {
169 int jobpipe[2],donepipe[2], c, r,i, x,y;
170 const OcrResultGlyph *s;
176 r= pipe(jobpipe); eassert(!r);
177 r= pipe(donepipe); eassert(!r);
178 resolver_pid= fork();
179 eassert(resolver_pid!=-1);
181 r= dup2(jobpipe[0],0); eassert(r==0);
182 r= close(jobpipe[1]); eassert(!r);
183 r= close(donepipe[0]); eassert(!r);
184 /* we know donepipe[1] is >= 4 and we have dealt with all the others
185 * so we aren't in any danger of overwriting some other fd 4: */
186 r= dup2(donepipe[1],4); eassert(r==4);
187 execlp("./show-thing.tcl", "./show-thing.tcl",
188 "--automatic","1",(char*)0);
189 eassert(!"execlp failed");
191 r= close(jobpipe[0]); eassert(!r);
192 r= close(donepipe[1]); eassert(!r);
193 resolver= fdopen(jobpipe[1],"w"); eassert(resolver);
194 resolver_done= donepipe[0];
196 fprintf(resolver,"%d %d ",unk_l,unk_r);
197 cu_pr_ctxmap(unk_ctxmap);
198 for (i=0, s=rd->results; i<rd->nresults; i++, s++) {
199 if (!strcmp(s->s," ")) continue;
200 fprintf(resolver," %d %d ",s->l,s->r);
201 cu_pr_ctxmap(s->ctxmap);
202 fprintf(resolver," ");
203 for (p=s->s; (c= *p); p++) {
204 if (c=='\\') fprintf(resolver,"\\%c",c);
205 else if (c>=33 && c<=126) fputc(c,resolver);
206 else fprintf(resolver,"\\x%02x",(unsigned char)c);
209 fputc('\n',resolver);
213 "static char *t[] = {\n"
214 "/* columns rows colors chars-per-pixel */\n"
219 for (y=0, pv=1; y<rd->h; y++, pv<<=1) {
222 fputc(cols[x] & pv ? 'o' : ' ', resolver);
223 fputs("\",\n",resolver);
225 fputs("};\n",resolver);
226 eassert(!ferror(resolver));
227 eassert(!fflush(resolver));
232 r= read(resolver_done,&cb,1);
233 if (r==-1) { eassert(errno==EINTR); continue; }
240 pid= waitpid(resolver_pid, &r, 0);
241 if (pid==-1) { eassert(errno==EINTR); continue; }
244 eassert(pid==resolver_pid);
246 eassert(!WEXITSTATUS(r));
248 close(resolver_done);
250 } else if (WIFSIGNALED(r)) {
251 eassert(!"resolver child died due to signal");
253 eassert(!"weird wait status");
263 static void add_result(OcrReader *rd, const char *s, int l, int r,
265 if (rd->nresults >= rd->aresults) {
266 rd->aresults++; rd->aresults<<=1;
267 rd->results= realloc(rd->results,sizeof(*rd->results)*rd->aresults);
268 eassert(rd->results);
270 rd->results[rd->nresults].s= s;
271 rd->results[rd->nresults].l= l;
272 rd->results[rd->nresults].r= r;
273 rd->results[rd->nresults].ctxmap= ctxmap;
277 struct OcrCellTypeInfo {
278 unsigned initial, nextword, midword;
280 const struct OcrCellTypeInfo ocr_celltype_number= {
283 const struct OcrCellTypeInfo ocr_celltype_text= {
284 .initial=2 /* Uppercase */,
285 .nextword=3 /* Either */,
286 .midword=1 /* Lower only */
289 static void vdebugf(const char *fmt, va_list al) {
291 vfprintf(debug,fmt,al);
294 static void debugf(const char *fmt, ...) {
295 va_list al; va_start(al,fmt); vdebugf(fmt,al); va_end(al);
298 OcrResultGlyph *ocr(OcrReader *rd, OcrCellType ct, int w, Pixcol cols[]) {
308 debugf("OCR h=%d w=%d",rd->h,w);
309 for (x=0; x<w; x++) debugf(" %"PSPIXCOL(PRIx),cols[x]);
323 if (nspaces==SPACE_SPACES) {
324 debugf("OCR x=%x nspaces=%d space\n",x,nspaces);
325 ctxmap= ct->nextword;
330 /* something here, so we need to add the spaces */
331 if (nspaces>=SPACE_SPACES)
332 add_result(rd," ",x-nspaces,x+1,0);
338 DatabaseNode *uniquematch= 0;
339 int uniquematch_rx=-1;
341 debugf("OCR lx=%d ctxmap=%x ",lx,ctxmap);
343 for (ctxi=0; ctxi<NCONTEXTS; ctxi++) {
344 DatabaseNode *current= &rd->contexts[ctxi];;
345 DatabaseNode *bestmatch= 0;
349 if (!(ctxmap & (1u << ctxi))) continue;
350 debugf(" || %s",context_names[ctxi]);
357 debugf(" cv=%"PSPIXCOL(PRIx),cv);
358 for (i=0; i<current->nlinks; i++)
359 if (current->links[i].col == cv)
366 current= current->links[i].then;
368 debugf(" \"%s\"",current->s);
379 if (uniquematch && strcmp(bestmatch->s, uniquematch->s)) {
380 debugf( " ambiguous");
384 uniquematch= bestmatch;
385 uniquematch_rx= bestmatch_rx;
391 add_result(rd, uniquematch->s, lx, uniquematch_rx, ctxmap);
396 debugf(" || UNKNOWN");
397 for (rx=lx; rx<w && cols[rx]; rx++);
398 debugf(" x=%d ctxmap=%x %d..%d\n",x, ctxmap, lx,rx);
400 callout_unknown(rd, w,cols, lx,rx-1, ctxmap);
404 add_result(rd, 0,-1,-1,0);
405 debugf("OCR finished %d glyphs\n",rd->nresults);
410 OcrReader *ocr_init(int h) {
413 rd= malloc(sizeof(*rd)); eassert(rd);
414 memset(rd,0,sizeof(*rd));