chiark / gitweb /
factor out DEBUG_DEFINE_DEBUGF
[ypp-sc-tools.db-live.git] / pctb / ocr.c
index 04aecc0d48a0431642d73c85f8b65144fa74b491..f5826dfe84a01e14b8ba1032aba301595eef367c 100644 (file)
@@ -24,6 +24,8 @@ static const char *context_names[]= {
 
 #define NCONTEXTS (sizeof(context_names)/sizeof(context_names[0]))
 
+#define SPACE_SPACES 4
+
 struct OcrReader {
   int h;
   DatabaseNode contexts[NCONTEXTS];
@@ -35,6 +37,8 @@ static FILE *resolver;
 static pid_t resolver_pid;
 static int resolver_done;
 
+DEBUG_DEFINE_DEBUGF(ocr)
+
 static void fgetsline(FILE *f, char *lbuf, size_t lbufsz) {
   char *s= fgets(lbuf,lbufsz,f);
   eassert(s);
@@ -61,12 +65,19 @@ static void readdb(OcrReader *rd) {
   char lbuf[100];
   FILE *db;
 
+  for (ctxi=0; ctxi<NCONTEXTS; ctxi++)
+    cleardb_node(&rd->contexts[ctxi]);
+
   char *dbfname=0;
   asprintf(&dbfname,"%s/charset-%d.txt",get_vardir(),rd->h);
   eassert(dbfname);
   
-  db= fopen(dbfname,"r");  eassert(db);
+  db= fopen(dbfname,"r");
   free(dbfname);
+  if (!db) {
+    eassert(errno==ENOENT);
+    return;
+  }
 
   FGETSLINE(db,lbuf);
   eassert(!strcmp(lbuf,"# ypp-sc-tools pctb font v1"));
@@ -75,9 +86,6 @@ static void readdb(OcrReader *rd) {
   eassert(r==1);
   eassert(h==rd->h);
 
-  for (ctxi=0; ctxi<NCONTEXTS; ctxi++)
-    cleardb_node(&rd->contexts[ctxi]);
-
   for (;;) {
     FGETSLINE(db,lbuf);
     if (!lbuf || lbuf[0]=='#') continue;
@@ -179,7 +187,9 @@ static void callout_unknown(OcrReader *rd, int w, Pixcol cols[],
        * so we aren't in any danger of overwriting some other fd 4: */
       r= dup2(donepipe[1],4); eassert(r==4);
       execlp("./show-thing.tcl", "./show-thing.tcl",
-            "--automatic","1",(char*)0);
+            DEBUGP(callout) ? "--debug" : "--noop-arg",
+            "--automatic-1",
+            (char*)0);
       eassert(!"execlp failed");
     }
     r= close(jobpipe[0]); eassert(!r);
@@ -290,9 +300,9 @@ OcrResultGlyph *ocr(OcrReader *rd, OcrCellType ct, int w, Pixcol cols[]) {
   nspaces=- w;
   ctxmap= ct->initial;
   rd->nresults=0;
-  fprintf(debug,"OCR h=%d w=%d",rd->h,w);
-  for (x=0; x<w; x++) fprintf(debug," %"PSPIXCOL(PRIx),cols[x]);
-  fprintf(debug,"\n");
+  debugf("OCR h=%d w=%d",rd->h,w);
+  for (x=0; x<w; x++) debugf(" %"PSPIXCOL(PRIx),cols[x]);
+  debugf("\n");
   debug_flush();
 
   x=0;
@@ -305,13 +315,16 @@ OcrResultGlyph *ocr(OcrReader *rd, OcrCellType ct, int w, Pixcol cols[]) {
     if (!cols[x]) {
       nspaces++;
       x++;
-      if (nspaces==3) {
-       fprintf(debug,"OCR  x=%x nspaces=%d space\n",x,nspaces);
-       add_result(rd," ",x-nspaces,x+1,0);
+      if (nspaces==SPACE_SPACES) {
+       debugf("OCR  x=%x nspaces=%d space\n",x,nspaces);
        ctxmap= ct->nextword;
       }
       continue;
     }
+
+    /* something here, so we need to add the spaces */
+    if (nspaces>=SPACE_SPACES)
+      add_result(rd," ",x-nspaces,x+1,0);
     nspaces=0;
 
     /* find character */
@@ -320,7 +333,7 @@ OcrResultGlyph *ocr(OcrReader *rd, OcrCellType ct, int w, Pixcol cols[]) {
     DatabaseNode *uniquematch= 0;
     int uniquematch_rx=-1;
     
-    fprintf(debug,"OCR  lx=%d ctxmap=%x  ",lx,ctxmap);
+    debugf("OCR  lx=%d ctxmap=%x  ",lx,ctxmap);
 
     for (ctxi=0; ctxi<NCONTEXTS; ctxi++) {
       DatabaseNode *current= &rd->contexts[ctxi];;
@@ -329,29 +342,29 @@ OcrResultGlyph *ocr(OcrReader *rd, OcrCellType ct, int w, Pixcol cols[]) {
 
       x= lx;
       if (!(ctxmap & (1u << ctxi))) continue;
-      fprintf(debug," || %s",context_names[ctxi]);
+      debugf(" || %s",context_names[ctxi]);
 
       for (;;) {
        debug_flush();
-       fprintf(debug," | x=%d",x);
+       debugf(" | x=%d",x);
        if (x>w) break;
        Pixcol cv= cols[x];
-       fprintf(debug," cv=%"PSPIXCOL(PRIx),cv);
+       debugf(" cv=%"PSPIXCOL(PRIx),cv);
        for (i=0; i<current->nlinks; i++)
          if (current->links[i].col == cv)
            goto found;
        /* not found */
-       fprintf(debug," ?");
+       debugf(" ?");
        break;
 
       found:
        current= current->links[i].then;
        if (current->s[0]) {
-         fprintf(debug," \"%s\"",current->s);
+         debugf(" \"%s\"",current->s);
          bestmatch= current;
          bestmatch_rx= x;
        } else {
-         fprintf(debug," ...");
+         debugf(" ...");
        }
 
        x++;
@@ -359,7 +372,7 @@ OcrResultGlyph *ocr(OcrReader *rd, OcrCellType ct, int w, Pixcol cols[]) {
       
       if (bestmatch) {
        if (uniquematch && strcmp(bestmatch->s, uniquematch->s)) {
-         fprintf(debug, " ambiguous");
+         debugf( " ambiguous");
          uniquematch= 0;
          break;
        }
@@ -369,22 +382,22 @@ OcrResultGlyph *ocr(OcrReader *rd, OcrCellType ct, int w, Pixcol cols[]) {
     }
 
     if (uniquematch) {
-      fprintf(debug," || YES\n");
+      debugf(" || YES\n");
       add_result(rd, uniquematch->s, lx, uniquematch_rx, ctxmap);
       x= uniquematch_rx+1;
       ctxmap= ct->midword;
     } else {
       int rx;
-      fprintf(debug," || UNKNOWN");
+      debugf(" || UNKNOWN");
       for (rx=lx; rx<w && cols[rx]; rx++);
-      fprintf(debug," x=%d ctxmap=%x %d..%d\n",x, ctxmap, lx,rx);
+      debugf(" x=%d ctxmap=%x %d..%d\n",x, ctxmap, lx,rx);
       debug_flush();
       callout_unknown(rd, w,cols, lx,rx-1, ctxmap);
       goto restart;
     }
   }
   add_result(rd, 0,-1,-1,0);
-  fprintf(debug,"OCR  finished %d glyphs\n",rd->nresults);
+  debugf("OCR  finished %d glyphs\n",rd->nresults);
   debug_flush();
   return rd->results;
 }