chiark / gitweb /
wip. before remove sigstop based thing which is mad
[ypp-sc-tools.db-live.git] / pctb / ocr.c
index a90ecea64e590641911a3faa5e250373ce16863c..43636f2d1c18861d77d890e8f49c2373a1bff627 100644 (file)
@@ -88,7 +88,8 @@ static void ocr_readdb(void) {
   eassert(feof(db));
 }      
 
-static void callout_unknown(int w, int h, Pixcol cols[], int unk_l, int unk_r,
+static void callout_unknown(int w, int h, Pixcol cols[],
+                           int unk_l, int unk_r, int unk_ctx,
                            const OcrResultGlyph *sofar, int nsofar) {
   int pfd[2], c, r,i, x,y;
   const OcrResultGlyph *s;
@@ -108,7 +109,7 @@ static void callout_unknown(int w, int h, Pixcol cols[], int unk_l, int unk_r,
     r= close(pfd[0]); eassert(!r);
     resolver= fdopen(pfd[1],"w"); eassert(resolver);
   }
-  fprintf(resolver,"%d %d",unk_l,unk_r);
+  fprintf(resolver,"%d %d %d",unk_l,unk_r,unk_ctx);
   for (i=0, s=sofar; i<nsofar; i++, s++) {
     fprintf(resolver," %d %d %d ",s->l,s->r,s->ctx);
     for (p=s->s; (c= *p); p++) {
@@ -186,11 +187,18 @@ OcrResultGlyph *ocr(int w, int h, Pixcol cols[]) {
   nresults=0;
   assert(db);
 
+  fprintf(debug,"OCR h=%d w=%d",w,h);
+  for (x=0; x<w; x++) fprintf(debug," %"PSPIXCOL(PRIx),cols[x]);
+  fprintf(debug,"\n");
+  debug_flush();
+
  restart:
   x=0;
   for (;;) {
+    debug_flush();
     /* skip spaces */
-    if (x>=w) break;
+    if (x>=w)
+      break;
 
     if (!cols[x]) {
       nspaces++;
@@ -204,34 +212,50 @@ OcrResultGlyph *ocr(int w, int h, Pixcol cols[]) {
     int lx=x;
     int bestmatch_rx=-1;
     current= &ocr_contexts[ctx];
+    fprintf(debug,"OCR  lx=%d ctx=%d  ",lx,ctx);
+
     for (;;) {
+      debug_flush();
+      fprintf(debug,"| x=%d",x);
       if (x>w) break;
       Pixcol cv= cols[x];
+      fprintf(debug," cv=%"PSPIXCOL(PRIx),x);
       for (i=0; i<current->nlinks; i++)
        if (current->links[i].col == cv)
          goto found;
       /* not found */
+      fprintf(debug," ?");
       break;
+
     found:
       current= current->links[i].then;
-      if (current->s[0]) { bestmatch=current; bestmatch_rx=x; }
+      if (current->s[0]) {
+       fprintf(debug," \"%s\"",current->s);
+       bestmatch=current; bestmatch_rx=x;
+      } else {
+       fprintf(debug," ...");
+      }
       x++;
     }
 
-    if (!bestmatch) {
+    if (bestmatch) {
+      fprintf(debug," YES\n");
       add_result(bestmatch->s, lx, bestmatch_rx, ctx);
       x= bestmatch_rx+1;
       ctx= 0;
     } else {
-      
       int rx;
-      for (rx=lx+1; rx<w && cols[rx]; rx++);
-      printf("UNKNOWN x=%d ctx=%d %d..%d\n",x, ctx, lx,rx);
-      callout_unknown(w,h,cols, lx,rx, results,nresults);
+      fprintf(debug," UNKNOWN");
+      for (rx=lx; rx<w && cols[rx]; rx++);
+      fprintf(debug," x=%d ctx=%d %d..%d\n",x, ctx, lx,rx);
+      debug_flush();
+      callout_unknown(w,h,cols, lx,rx-1,ctx, results,nresults);
       goto restart;
     }
   }
   add_result(0,-1,-1,0);
+  fprintf(debug,"OCR  finished %d glyphs\n",nresults);
+  debug_flush();
   return results;
 }