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 4
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 DEBUG_DEFINE_DEBUGF(ocr)
42 static void fgetsline(FILE *f, char *lbuf, size_t lbufsz) {
43 char *s= fgets(lbuf,lbufsz,f);
46 eassert(l>0); eassert(lbuf[--l]='\n');
49 #define FGETSLINE(f,buf) (fgetsline(f,buf,sizeof(buf)))
51 static void cleardb_node(DatabaseNode *n) {
54 for (i=0; i<n->nlinks; i++)
55 cleardb_node(n->links[i].then);
58 static void readdb(OcrReader *rd) {
60 DatabaseNode *current, *additional;
61 char chrs[MAXGLYPHCHRS+1];
68 for (ctxi=0; ctxi<NCONTEXTS; ctxi++)
69 cleardb_node(&rd->contexts[ctxi]);
72 asprintf(&dbfname,"%s/charset-%d.txt",get_vardir(),rd->h);
75 db= fopen(dbfname,"r");
78 eassert(errno==ENOENT);
83 eassert(!strcmp(lbuf,"# ypp-sc-tools pctb font v1"));
85 r= fscanf(db, "%d", &h);
91 if (!lbuf || lbuf[0]=='#') continue;
92 if (!strcmp(lbuf,".")) break;
94 for (ctxi=0; ctxi<NCONTEXTS; ctxi++)
95 if (!strcmp(lbuf,context_names[ctxi]))
97 /* not found, just skip */
98 for (;;) { FGETSLINE(db,lbuf); if (!lbuf[0]) break; }
103 int c= fgetc(db); eassert(c!=EOF);
104 if (c=='\n') { eassert(nchrs); break; }
105 eassert(nchrs<MAXGLYPHCHRS);
108 c= fgetc(db); eassert(c=='x');
109 r= fscanf(db, "%2x", &cr); eassert(r==1);
110 assert(cr>0 && cr<=255);
117 current= &rd->contexts[ctxi];
120 if (!lbuf[0]) { eassert(current != &rd->contexts[ctxi]); break; }
122 cv= strtoul(lbuf,&ep,16); eassert(!*ep);
123 eassert(!(cv & ~((1UL << rd->h)-1)));
125 for (j=0; j<current->nlinks; j++)
126 if (current->links[j].col == cv) {
127 current= current->links[j].then;
131 additional= malloc(sizeof(*additional)); eassert(additional);
133 additional->nlinks= additional->alinks= 0;
134 additional->links= 0;
135 if (current->nlinks==current->alinks) {
138 current->links= realloc(current->links,
139 sizeof(*current->links) * current->alinks);
140 eassert(current->links);
142 current->links[current->nlinks].col= cv;
143 current->links[current->nlinks].then= additional;
150 eassert(!current->s[0]);
151 strcpy(current->s, chrs);
153 eassert(!ferror(db));
154 eassert(!fclose(db));
157 static void cu_pr_ctxmap(unsigned ctxmap) {
158 fprintf(resolver,"{");
161 for (ctxi=0; ctxi<NCONTEXTS; ctxi++) {
162 if (!(ctxmap & (1u << ctxi))) continue;
163 fprintf(resolver,"%s%s",spc,context_names[ctxi]);
166 fprintf(resolver,"}");
169 static void callout_unknown(OcrReader *rd, int w, Pixcol cols[],
170 int unk_l, int unk_r, unsigned unk_ctxmap) {
171 int jobpipe[2],donepipe[2], c, r,i, x,y;
172 const OcrResultGlyph *s;
178 r= pipe(jobpipe); eassert(!r);
179 r= pipe(donepipe); eassert(!r);
180 resolver_pid= fork();
181 eassert(resolver_pid!=-1);
183 r= dup2(jobpipe[0],0); eassert(r==0);
184 r= close(jobpipe[1]); eassert(!r);
185 r= close(donepipe[0]); eassert(!r);
186 /* we know donepipe[1] is >= 4 and we have dealt with all the others
187 * so we aren't in any danger of overwriting some other fd 4: */
188 r= dup2(donepipe[1],4); eassert(r==4);
189 execlp("./show-thing.tcl", "./show-thing.tcl",
190 DEBUGP(callout) ? "--debug" : "--noop-arg",
193 eassert(!"execlp failed");
195 r= close(jobpipe[0]); eassert(!r);
196 r= close(donepipe[1]); eassert(!r);
197 resolver= fdopen(jobpipe[1],"w"); eassert(resolver);
198 resolver_done= donepipe[0];
200 fprintf(resolver,"%d %d ",unk_l,unk_r);
201 cu_pr_ctxmap(unk_ctxmap);
202 for (i=0, s=rd->results; i<rd->nresults; i++, s++) {
203 if (!strcmp(s->s," ")) continue;
204 fprintf(resolver," %d %d ",s->l,s->r);
205 cu_pr_ctxmap(s->ctxmap);
206 fprintf(resolver," ");
207 for (p=s->s; (c= *p); p++) {
208 if (c=='\\') fprintf(resolver,"\\%c",c);
209 else if (c>=33 && c<=126) fputc(c,resolver);
210 else fprintf(resolver,"\\x%02x",(unsigned char)c);
213 fputc('\n',resolver);
217 "static char *t[] = {\n"
218 "/* columns rows colors chars-per-pixel */\n"
223 for (y=0, pv=1; y<rd->h; y++, pv<<=1) {
226 fputc(cols[x] & pv ? 'o' : ' ', resolver);
227 fputs("\",\n",resolver);
229 fputs("};\n",resolver);
230 eassert(!ferror(resolver));
231 eassert(!fflush(resolver));
236 r= read(resolver_done,&cb,1);
237 if (r==-1) { eassert(errno==EINTR); continue; }
244 pid= waitpid(resolver_pid, &r, 0);
245 if (pid==-1) { eassert(errno==EINTR); continue; }
248 eassert(pid==resolver_pid);
250 eassert(!WEXITSTATUS(r));
252 close(resolver_done);
254 } else if (WIFSIGNALED(r)) {
255 eassert(!"resolver child died due to signal");
257 eassert(!"weird wait status");
267 static void add_result(OcrReader *rd, const char *s, int l, int r,
269 if (rd->nresults >= rd->aresults) {
270 rd->aresults++; rd->aresults<<=1;
271 rd->results= realloc(rd->results,sizeof(*rd->results)*rd->aresults);
272 eassert(rd->results);
274 rd->results[rd->nresults].s= s;
275 rd->results[rd->nresults].l= l;
276 rd->results[rd->nresults].r= r;
277 rd->results[rd->nresults].ctxmap= ctxmap;
281 struct OcrCellTypeInfo {
282 unsigned initial, nextword, midword;
284 const struct OcrCellTypeInfo ocr_celltype_number= {
287 const struct OcrCellTypeInfo ocr_celltype_text= {
288 .initial=2 /* Uppercase */,
289 .nextword=3 /* Either */,
290 .midword=1 /* Lower only */
293 OcrResultGlyph *ocr(OcrReader *rd, OcrCellType ct, int w, Pixcol cols[]) {
303 debugf("OCR h=%d w=%d",rd->h,w);
304 for (x=0; x<w; x++) debugf(" %"PSPIXCOL(PRIx),cols[x]);
318 if (nspaces==SPACE_SPACES) {
319 debugf("OCR x=%x nspaces=%d space\n",x,nspaces);
320 ctxmap= ct->nextword;
325 /* something here, so we need to add the spaces */
326 if (nspaces>=SPACE_SPACES)
327 add_result(rd," ",x-nspaces,x+1,0);
333 DatabaseNode *uniquematch= 0;
334 int uniquematch_rx=-1;
336 debugf("OCR lx=%d ctxmap=%x ",lx,ctxmap);
338 for (ctxi=0; ctxi<NCONTEXTS; ctxi++) {
339 DatabaseNode *current= &rd->contexts[ctxi];;
340 DatabaseNode *bestmatch= 0;
344 if (!(ctxmap & (1u << ctxi))) continue;
345 debugf(" || %s",context_names[ctxi]);
352 debugf(" cv=%"PSPIXCOL(PRIx),cv);
353 for (i=0; i<current->nlinks; i++)
354 if (current->links[i].col == cv)
361 current= current->links[i].then;
363 debugf(" \"%s\"",current->s);
374 if (uniquematch && strcmp(bestmatch->s, uniquematch->s)) {
375 debugf( " ambiguous");
379 uniquematch= bestmatch;
380 uniquematch_rx= bestmatch_rx;
386 add_result(rd, uniquematch->s, lx, uniquematch_rx, ctxmap);
391 debugf(" || UNKNOWN");
392 for (rx=lx; rx<w && cols[rx]; rx++);
393 debugf(" x=%d ctxmap=%x %d..%d\n",x, ctxmap, lx,rx);
395 callout_unknown(rd, w,cols, lx,rx-1, ctxmap);
399 add_result(rd, 0,-1,-1,0);
400 debugf("OCR finished %d glyphs\n",rd->nresults);
405 OcrReader *ocr_init(int h) {
408 rd= malloc(sizeof(*rd)); eassert(rd);
409 memset(rd,0,sizeof(*rd));