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;
29 static void ocr_readdb(void) {
31 OCRDatabaseNode *current, *additional;
32 char chrs[MAXGLYPHCHRS+1];
37 db= fopen("database","r"); eassert(db);
40 r= fscanf(db, "%d %d", &ctx, &nchrs);
43 eassert(ctx>=0 && ctx<N_OCR_CONTEXTS);
44 eassert(nchrs>0 && nchrs<=MAXGLYPHCHRS);
46 for (i=0; i<nchrs; i++) {
48 r= fscanf(db, "%x", &c); eassert(r==1);
49 eassert(c>0 && c<=255);
55 r= fscanf(db, "%d", &twidth); eassert(r==1);
56 current= &ocr_contexts[ctx];
57 for (i=0; i<twidth; i++) {
58 r= fscanf(db, "%"PSPIXCOL(SCNx), &cv); eassert(r==1);
59 for (j=0; j<current->nlinks; j++)
60 if (current->links[j].col == cv) {
61 current= current->links[j].then;
65 additional= malloc(sizeof(*additional)); eassert(additional);
67 additional->nlinks= additional->alinks= 0;
69 if (current->nlinks==current->alinks) {
72 current->links= realloc(current->links,
73 sizeof(*current->links) * current->alinks);
74 eassert(current->links);
76 current->links[current->nlinks].col= cv;
77 current->links[current->nlinks].then= additional;
84 eassert(!current->s[0]);
85 strcpy(current->s, chrs);
91 static void callout_unknown(int w, int h, Pixcol cols[],
92 int unk_l, int unk_r, int unk_ctx,
93 const OcrResultGlyph *sofar, int nsofar) {
94 int pfd[2], c, r,i, x,y;
95 const OcrResultGlyph *s;
100 r= pipe(pfd); eassert(!r);
101 resolver_pid= fork();
102 eassert(resolver_pid!=-1);
104 r= dup2(pfd[0],0); eassert(!r);
105 r= close(pfd[1]); eassert(!r);
106 execlp("./show-thing.tcl", "./show-thing.tcl",(char*)0);
107 eassert(!"execlp failed");
109 r= close(pfd[0]); eassert(!r);
110 resolver= fdopen(pfd[1],"w"); eassert(resolver);
112 fprintf(resolver,"%d %d %d",unk_l,unk_r,unk_ctx);
113 for (i=0, s=sofar; i<nsofar; i++, s++) {
114 fprintf(resolver," %d %d %d ",s->l,s->r,s->ctx);
115 for (p=s->s; (c= *p); p++) {
116 if (c=='\\') fprintf(resolver,"\\%c",c);
117 else if (c>=33 && c<=126) fputc(c,resolver);
118 else fprintf(resolver,"\\x%02x",(unsigned char)c);
121 fputc('\n',resolver);
125 "static char *t[] = {\n"
126 "/* columns rows colors chars-per-pixel */\n"
131 for (y=0, pv=1; y<h; y++, pv<<=1) {
134 fputc(cols[x] & pv ? 'o' : ' ', resolver);
135 fputs("\",\n",resolver);
137 fputs("};\n",resolver);
138 eassert(!ferror(resolver));
139 eassert(!fflush(resolver));
143 pid_t pid= waitpid(resolver_pid, &r, WUNTRACED);
144 if (pid==-1) { eassert(errno==EINTR); continue; }
145 eassert(pid==resolver_pid);
147 eassert(!WEXITSTATUS(r));
150 } else if (WIFSTOPPED(r)) {
151 r= kill(resolver_pid,SIGCONT);
153 } else if (WIFSIGNALED(r)) {
154 eassert(!"resolver child died due to signal");
156 eassert(!"weird wait status");
158 struct stat stab, fstab;
159 r= stat("database",&stab); eassert(!r);
160 r= fstat(fileno(db),&fstab); eassert(!r);
161 if (stab.st_ino != fstab.st_ino ||
162 stab.st_dev != fstab.st_dev)
170 static void add_result(const char *s, int l, int r, int ctx) {
171 if (nresults >= aresults) {
172 aresults++; aresults<<=1;
173 results= realloc(results,sizeof(*results)*aresults);
176 results[nresults].s= s;
177 results[nresults].l= l;
178 results[nresults].r= r;
179 results[nresults].ctx= ctx;
183 OcrResultGlyph *ocr(int w, int h, Pixcol cols[]) {
190 fprintf(debug,"OCR h=%d w=%d",w,h);
191 for (x=0; x<w; x++) fprintf(debug," %"PSPIXCOL(PRIx),cols[x]);
206 if (nspaces>3) ctx=1;
211 OCRDatabaseNode *current=0, *bestmatch=0;
214 current= &ocr_contexts[ctx];
215 fprintf(debug,"OCR lx=%d ctx=%d ",lx,ctx);
219 fprintf(debug,"| x=%d",x);
222 fprintf(debug," cv=%"PSPIXCOL(PRIx),x);
223 for (i=0; i<current->nlinks; i++)
224 if (current->links[i].col == cv)
231 current= current->links[i].then;
233 fprintf(debug," \"%s\"",current->s);
234 bestmatch=current; bestmatch_rx=x;
236 fprintf(debug," ...");
242 fprintf(debug," YES\n");
243 add_result(bestmatch->s, lx, bestmatch_rx, ctx);
248 fprintf(debug," UNKNOWN");
249 for (rx=lx; rx<w && cols[rx]; rx++);
250 fprintf(debug," x=%d ctx=%d %d..%d\n",x, ctx, lx,rx);
252 callout_unknown(w,h,cols, lx,rx-1,ctx, results,nresults);
256 add_result(0,-1,-1,0);
257 fprintf(debug,"OCR finished %d glyphs\n",nresults);
262 void ocr_init(void) {