8 struct OCRDatabaseNode *then;
11 #define MAXGLYPHCHRS 3
13 typedef struct OCRDatabaseNode {
14 char s[MAXGLYPHCHRS+1]; /* null-terminated; "" means no match here */
16 OCRDatabaseLink *links;
19 #define N_OCR_CONTEXTS 2
21 static OCRDatabaseNode ocr_contexts[N_OCR_CONTEXTS];
23 static OcrResultGlyph *results;
24 static int aresults, nresults;
26 static FILE *resolver;
27 static pid_t resolver_pid;
28 static int resolver_done;
30 static void ocr_readdb(void) {
32 OCRDatabaseNode *current, *additional;
33 char chrs[MAXGLYPHCHRS+1];
38 db= fopen("database","r"); eassert(db);
41 r= fscanf(db, "%d %d", &ctx, &nchrs);
44 eassert(ctx>=0 && ctx<N_OCR_CONTEXTS);
45 eassert(nchrs>0 && nchrs<=MAXGLYPHCHRS);
47 for (i=0; i<nchrs; i++) {
49 r= fscanf(db, "%x", &c); eassert(r==1);
50 eassert(c>0 && c<=255);
56 r= fscanf(db, "%d", &twidth); eassert(r==1);
57 current= &ocr_contexts[ctx];
58 for (i=0; i<twidth; i++) {
59 r= fscanf(db, "%"PSPIXCOL(SCNx), &cv); eassert(r==1);
60 for (j=0; j<current->nlinks; j++)
61 if (current->links[j].col == cv) {
62 current= current->links[j].then;
66 additional= malloc(sizeof(*additional)); eassert(additional);
68 additional->nlinks= additional->alinks= 0;
70 if (current->nlinks==current->alinks) {
73 current->links= realloc(current->links,
74 sizeof(*current->links) * current->alinks);
75 eassert(current->links);
77 current->links[current->nlinks].col= cv;
78 current->links[current->nlinks].then= additional;
85 eassert(!current->s[0]);
86 strcpy(current->s, chrs);
92 static void callout_unknown(int w, int h, Pixcol cols[],
93 int unk_l, int unk_r, int unk_ctx,
94 const OcrResultGlyph *sofar, int nsofar) {
95 int jobpipe[2],donepipe[2], c, r,i, x,y;
96 const OcrResultGlyph *s;
102 r= pipe(jobpipe); eassert(!r);
103 r= pipe(donepipe); eassert(!r);
104 resolver_pid= fork();
105 eassert(resolver_pid!=-1);
107 r= dup2(jobpipe[0],0); eassert(r==0);
108 r= close(jobpipe[1]); eassert(!r);
109 r= close(donepipe[0]); eassert(!r);
110 /* we know donepipe[1] is >= 4 and we have dealt with all the others
111 * so we aren't in any danger of overwriting some other fd 4: */
112 r= dup2(donepipe[1],4); eassert(r==4);
113 execlp("./show-thing.tcl", "./show-thing.tcl",
114 "--automatic","1",(char*)0);
115 eassert(!"execlp failed");
117 r= close(jobpipe[0]); eassert(!r);
118 r= close(donepipe[1]); eassert(!r);
119 resolver= fdopen(jobpipe[1],"w"); eassert(resolver);
120 resolver_done= donepipe[0];
122 fprintf(resolver,"%d %d %d",unk_l,unk_r,unk_ctx);
123 for (i=0, s=sofar; i<nsofar; i++, s++) {
124 fprintf(resolver," %d %d %d ",s->l,s->r,s->ctx);
125 for (p=s->s; (c= *p); p++) {
126 if (c=='\\') fprintf(resolver,"\\%c",c);
127 else if (c>=33 && c<=126) fputc(c,resolver);
128 else fprintf(resolver,"\\x%02x",(unsigned char)c);
131 fputc('\n',resolver);
135 "static char *t[] = {\n"
136 "/* columns rows colors chars-per-pixel */\n"
141 for (y=0, pv=1; y<h; y++, pv<<=1) {
144 fputc(cols[x] & pv ? 'o' : ' ', resolver);
145 fputs("\",\n",resolver);
147 fputs("};\n",resolver);
148 eassert(!ferror(resolver));
149 eassert(!fflush(resolver));
154 r= read(resolver_done,&cb,1);
155 if (r==-1) { eassert(errno==EINTR); continue; }
162 pid= waitpid(resolver_pid, &r, 0);
163 if (pid==-1) { eassert(errno==EINTR); continue; }
166 eassert(pid==resolver_pid);
168 eassert(!WEXITSTATUS(r));
170 close(resolver_done);
172 } else if (WIFSIGNALED(r)) {
173 eassert(!"resolver child died due to signal");
175 eassert(!"weird wait status");
187 static void add_result(const char *s, int l, int r, int ctx) {
188 if (nresults >= aresults) {
189 aresults++; aresults<<=1;
190 results= realloc(results,sizeof(*results)*aresults);
193 results[nresults].s= s;
194 results[nresults].l= l;
195 results[nresults].r= r;
196 results[nresults].ctx= ctx;
200 OcrResultGlyph *ocr(int w, int h, Pixcol cols[]) {
207 fprintf(debug,"OCR h=%d w=%d",w,h);
208 for (x=0; x<w; x++) fprintf(debug," %"PSPIXCOL(PRIx),cols[x]);
223 if (nspaces>3) ctx=1;
228 OCRDatabaseNode *current=0, *bestmatch=0;
231 current= &ocr_contexts[ctx];
232 fprintf(debug,"OCR lx=%d ctx=%d ",lx,ctx);
236 fprintf(debug,"| x=%d",x);
239 fprintf(debug," cv=%"PSPIXCOL(PRIx),x);
240 for (i=0; i<current->nlinks; i++)
241 if (current->links[i].col == cv)
248 current= current->links[i].then;
250 fprintf(debug," \"%s\"",current->s);
251 bestmatch=current; bestmatch_rx=x;
253 fprintf(debug," ...");
259 fprintf(debug," YES\n");
260 add_result(bestmatch->s, lx, bestmatch_rx, ctx);
265 fprintf(debug," UNKNOWN");
266 for (rx=lx; rx<w && cols[rx]; rx++);
267 fprintf(debug," x=%d ctx=%d %d..%d\n",x, ctx, lx,rx);
269 callout_unknown(w,h,cols, lx,rx-1,ctx, results,nresults);
273 add_result(0,-1,-1,0);
274 fprintf(debug,"OCR finished %d glyphs\n",nresults);
279 void ocr_init(void) {