chiark / gitweb /
rationalise debugging output
[ypp-sc-tools.db-live.git] / pctb / ocr.c
index fd4fcf4f8de99642291fb3e9c5410a2884c642fb..7eb871a68844acc1d802747e67346aed760b3729 100644 (file)
@@ -19,11 +19,13 @@ typedef struct DatabaseNode {
 static const char *context_names[]= {
   "Lower",
   "Upper",
 static const char *context_names[]= {
   "Lower",
   "Upper",
-/*  "Digit"*/
+  "Digit"
 };
 
 #define NCONTEXTS (sizeof(context_names)/sizeof(context_names[0]))
 
 };
 
 #define NCONTEXTS (sizeof(context_names)/sizeof(context_names[0]))
 
+#define SPACE_SPACES 3
+
 struct OcrReader {
   int h;
   DatabaseNode contexts[NCONTEXTS];
 struct OcrReader {
   int h;
   DatabaseNode contexts[NCONTEXTS];
@@ -61,12 +63,19 @@ static void readdb(OcrReader *rd) {
   char lbuf[100];
   FILE *db;
 
   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);
   
   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);
   free(dbfname);
+  if (!db) {
+    eassert(errno==ENOENT);
+    return;
+  }
 
   FGETSLINE(db,lbuf);
   eassert(!strcmp(lbuf,"# ypp-sc-tools pctb font v1"));
 
   FGETSLINE(db,lbuf);
   eassert(!strcmp(lbuf,"# ypp-sc-tools pctb font v1"));
@@ -75,9 +84,6 @@ static void readdb(OcrReader *rd) {
   eassert(r==1);
   eassert(h==rd->h);
 
   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;
   for (;;) {
     FGETSLINE(db,lbuf);
     if (!lbuf || lbuf[0]=='#') continue;
@@ -144,7 +150,19 @@ static void readdb(OcrReader *rd) {
   }
   eassert(!ferror(db));
   eassert(!fclose(db));
   }
   eassert(!ferror(db));
   eassert(!fclose(db));
-}      
+}
+
+static void cu_pr_ctxmap(unsigned ctxmap) {
+  fprintf(resolver,"{");
+  const char *spc="";
+  int ctxi;
+  for (ctxi=0; ctxi<NCONTEXTS; ctxi++) {
+    if (!(ctxmap & (1u << ctxi))) continue;
+    fprintf(resolver,"%s%s",spc,context_names[ctxi]);
+    spc=" ";
+  }
+  fprintf(resolver,"}");
+}
 
 static void callout_unknown(OcrReader *rd, int w, Pixcol cols[],
                            int unk_l, int unk_r, unsigned unk_ctxmap) {
 
 static void callout_unknown(OcrReader *rd, int w, Pixcol cols[],
                            int unk_l, int unk_r, unsigned unk_ctxmap) {
@@ -175,18 +193,13 @@ static void callout_unknown(OcrReader *rd, int w, Pixcol cols[],
     resolver= fdopen(jobpipe[1],"w"); eassert(resolver);
     resolver_done= donepipe[0];
   }
     resolver= fdopen(jobpipe[1],"w"); eassert(resolver);
     resolver_done= donepipe[0];
   }
-  fprintf(resolver,"%d %d {",unk_l,unk_r);
-  const char *spc="";
-  int ctxi;
-  for (ctxi=0; ctxi<NCONTEXTS; ctxi++) {
-    if (!(unk_ctxmap & (1u << ctxi))) continue;
-    fprintf(resolver,"%s%s",spc,context_names[ctxi]);
-    spc=" ";
-  }
-  fprintf(resolver,"}");
+  fprintf(resolver,"%d %d ",unk_l,unk_r);
+  cu_pr_ctxmap(unk_ctxmap);
   for (i=0, s=rd->results; i<rd->nresults; i++, s++) {
     if (!strcmp(s->s," ")) continue;
   for (i=0, s=rd->results; i<rd->nresults; i++, s++) {
     if (!strcmp(s->s," ")) continue;
-    fprintf(resolver," %d %d %s ",s->l,s->r,context_names[s->ctx]);
+    fprintf(resolver," %d %d ",s->l,s->r);
+    cu_pr_ctxmap(s->ctxmap);
+    fprintf(resolver," ");
     for (p=s->s; (c= *p); p++) {
       if (c=='\\') fprintf(resolver,"\\%c",c);
       else if (c>=33 && c<=126) fputc(c,resolver);
     for (p=s->s; (c= *p); p++) {
       if (c=='\\') fprintf(resolver,"\\%c",c);
       else if (c>=33 && c<=126) fputc(c,resolver);
@@ -247,7 +260,8 @@ static void callout_unknown(OcrReader *rd, int w, Pixcol cols[],
   readdb(rd);
 }
 
   readdb(rd);
 }
 
-static void add_result(OcrReader *rd, const char *s, int l, int r, int ctx) {
+static void add_result(OcrReader *rd, const char *s, int l, int r,
+                      unsigned ctxmap) {
   if (rd->nresults >= rd->aresults) {
     rd->aresults++; rd->aresults<<=1;
     rd->results= realloc(rd->results,sizeof(*rd->results)*rd->aresults);
   if (rd->nresults >= rd->aresults) {
     rd->aresults++; rd->aresults<<=1;
     rd->results= realloc(rd->results,sizeof(*rd->results)*rd->aresults);
@@ -256,23 +270,46 @@ static void add_result(OcrReader *rd, const char *s, int l, int r, int ctx) {
   rd->results[rd->nresults].s= s;
   rd->results[rd->nresults].l= l;
   rd->results[rd->nresults].r= r;
   rd->results[rd->nresults].s= s;
   rd->results[rd->nresults].l= l;
   rd->results[rd->nresults].r= r;
-  rd->results[rd->nresults].ctx= ctx;
+  rd->results[rd->nresults].ctxmap= ctxmap;
   rd->nresults++;
 }
 
   rd->nresults++;
 }
 
-OcrResultGlyph *ocr(OcrReader *rd, int w, Pixcol cols[]) {
-  int nspaces=-w;
-  unsigned ctxmap=2; /* uppercase */
+struct OcrCellTypeInfo {
+  unsigned initial, nextword, midword;
+};
+const struct OcrCellTypeInfo ocr_celltype_number= {
+  4,4,4
+};
+const struct OcrCellTypeInfo ocr_celltype_text= {
+  .initial=2 /* Uppercase */,
+  .nextword=3 /* Either */,
+  .midword=1 /* Lower only */
+};
+
+static void vdebugf(const char *fmt, va_list al) {
+#ifdef DEBUG_OCR
+  vfprintf(debug,fmt,al);
+#endif
+}
+static void debugf(const char *fmt, ...) {
+  va_list al;  va_start(al,fmt);  vdebugf(fmt,al);  va_end(al);
+}
+
+OcrResultGlyph *ocr(OcrReader *rd, OcrCellType ct, int w, Pixcol cols[]) {
+  int nspaces;
+  unsigned ctxmap;
   int ctxi, i, x;
 
   int ctxi, i, x;
 
-  rd->nresults=0;
+ restart:
 
 
-  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");
+  nspaces=- w;
+  ctxmap= ct->initial;
+  rd->nresults=0;
+  debugf("OCR h=%d w=%d",rd->h,w);
+  for (x=0; x<w; x++) debugf(" %"PSPIXCOL(PRIx),cols[x]);
+  debugf("\n");
   debug_flush();
 
   debug_flush();
 
- restart:
   x=0;
   for (;;) {
     debug_flush();
   x=0;
   for (;;) {
     debug_flush();
@@ -283,22 +320,25 @@ OcrResultGlyph *ocr(OcrReader *rd, int w, Pixcol cols[]) {
     if (!cols[x]) {
       nspaces++;
       x++;
     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);
-       ctxmap=3; /* either */
+      if (nspaces==SPACE_SPACES) {
+       debugf("OCR  x=%x nspaces=%d space\n",x,nspaces);
+       ctxmap= ct->nextword;
       }
       continue;
     }
       }
       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 */
     int lx=x;
 
     DatabaseNode *uniquematch= 0;
     nspaces=0;
 
     /* find character */
     int lx=x;
 
     DatabaseNode *uniquematch= 0;
-    int uniquematch_rx=-1, uniquematch_ctxi=-1;
+    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];;
 
     for (ctxi=0; ctxi<NCONTEXTS; ctxi++) {
       DatabaseNode *current= &rd->contexts[ctxi];;
@@ -307,63 +347,62 @@ OcrResultGlyph *ocr(OcrReader *rd, int w, Pixcol cols[]) {
 
       x= lx;
       if (!(ctxmap & (1u << ctxi))) continue;
 
       x= lx;
       if (!(ctxmap & (1u << ctxi))) continue;
-      fprintf(debug," || %s",context_names[ctxi]);
+      debugf(" || %s",context_names[ctxi]);
 
       for (;;) {
        debug_flush();
 
       for (;;) {
        debug_flush();
-       fprintf(debug," | x=%d",x);
+       debugf(" | x=%d",x);
        if (x>w) break;
        Pixcol cv= cols[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 */
        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]) {
        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 {
          bestmatch= current;
          bestmatch_rx= x;
        } else {
-         fprintf(debug," ...");
+         debugf(" ...");
        }
 
        x++;
       }
       
       if (bestmatch) {
        }
 
        x++;
       }
       
       if (bestmatch) {
-       if (uniquematch) {
-         fprintf(debug, " ambiguous");
+       if (uniquematch && strcmp(bestmatch->s, uniquematch->s)) {
+         debugf( " ambiguous");
          uniquematch= 0;
          break;
        }
        uniquematch= bestmatch;
        uniquematch_rx= bestmatch_rx;
          uniquematch= 0;
          break;
        }
        uniquematch= bestmatch;
        uniquematch_rx= bestmatch_rx;
-       uniquematch_ctxi= ctxi;
       }
     }
 
     if (uniquematch) {
       }
     }
 
     if (uniquematch) {
-      fprintf(debug," || YES\n");
-      add_result(rd, uniquematch->s, lx, uniquematch_rx, uniquematch_ctxi);
+      debugf(" || YES\n");
+      add_result(rd, uniquematch->s, lx, uniquematch_rx, ctxmap);
       x= uniquematch_rx+1;
       x= uniquematch_rx+1;
-      ctxmap= 1; /* Lower only */
+      ctxmap= ct->midword;
     } else {
       int rx;
     } else {
       int rx;
-      fprintf(debug," || UNKNOWN");
+      debugf(" || UNKNOWN");
       for (rx=lx; rx<w && cols[rx]; rx++);
       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);
       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;
 }
   debug_flush();
   return rd->results;
 }