X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?p=ypp-sc-tools.web-live.git;a=blobdiff_plain;f=pctb%2Focr.c;h=712f90df77c21e239b8f47c256564aae512acaf4;hp=fd4fcf4f8de99642291fb3e9c5410a2884c642fb;hb=24391f052b3c6f01f987b3cd08dbcc7a876afe06;hpb=8787ee59f6840de63bac432b516a30d0dfe22c84 diff --git a/pctb/ocr.c b/pctb/ocr.c index fd4fcf4..712f90d 100644 --- a/pctb/ocr.c +++ b/pctb/ocr.c @@ -8,22 +8,25 @@ typedef struct { struct DatabaseNode *then; } DatabaseLink; -#define MAXGLYPHCHRS 3 +#define MAXGLYPHCHRS 7 typedef struct DatabaseNode { char s[MAXGLYPHCHRS+1]; /* null-terminated; "" means no match here */ int nlinks, alinks; + unsigned endsword:1; DatabaseLink *links; } DatabaseNode; static const char *context_names[]= { "Lower", "Upper", -/* "Digit"*/ + "Digit" }; #define NCONTEXTS (sizeof(context_names)/sizeof(context_names[0])) +#define SPACE_SPACES 4 + struct OcrReader { int h; DatabaseNode contexts[NCONTEXTS]; @@ -35,6 +38,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); @@ -57,16 +62,23 @@ static void readdb(OcrReader *rd) { char chrs[MAXGLYPHCHRS+1]; Pixcol cv; int r,j,ctxi; - int h; + int h, endsword; char lbuf[100]; FILE *db; + for (ctxi=0; ctxicontexts[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 +87,6 @@ static void readdb(OcrReader *rd) { eassert(r==1); eassert(h==rd->h); - for (ctxi=0; ctxicontexts[ctxi]); - for (;;) { FGETSLINE(db,lbuf); if (!lbuf || lbuf[0]=='#') continue; @@ -104,6 +113,11 @@ static void readdb(OcrReader *rd) { } chrs[nchrs++]= c; } + endsword= 0; + if (nchrs>1 && chrs[nchrs-1]==' ') { + endsword= 1; + nchrs--; + } chrs[nchrs]= 0; current= &rd->contexts[ctxi]; @@ -141,10 +155,23 @@ static void readdb(OcrReader *rd) { eassert(!current->s[0]); strcpy(current->s, chrs); + current->endsword= endsword; } eassert(!ferror(db)); eassert(!fclose(db)); -} +} + +static void cu_pr_ctxmap(unsigned ctxmap) { + fprintf(resolver,"{"); + const char *spc=""; + int ctxi; + for (ctxi=0; ctxiresults; inresults; 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); @@ -247,7 +271,8 @@ static void callout_unknown(OcrReader *rd, int w, Pixcol cols[], 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); @@ -256,23 +281,37 @@ 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].ctx= ctx; + rd->results[rd->nresults].ctxmap= ctxmap; 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 */ +}; + +OcrResultGlyph *ocr(OcrReader *rd, OcrCellType ct, int w, Pixcol cols[]) { + int nspaces; + unsigned ctxmap; int ctxi, i, x; - rd->nresults=0; + restart: - fprintf(debug,"OCR h=%d w=%d",rd->h,w); - for (x=0; xinitial; + rd->nresults=0; + debugf("OCR h=%d w=%d",rd->h,w); + for (x=0; xnextword; } 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; - 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; ctxicontexts[ctxi];; @@ -307,63 +349,69 @@ OcrResultGlyph *ocr(OcrReader *rd, 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; inlinks; 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\"%s",current->s,current->endsword?"_":""); bestmatch= current; bestmatch_rx= x; } else { - fprintf(debug," ..."); + debugf(" ..."); } 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_ctxi= ctxi; } } if (uniquematch) { - fprintf(debug," || YES\n"); - add_result(rd, uniquematch->s, lx, uniquematch_rx, uniquematch_ctxi); + debugf(" || YES"); + add_result(rd, uniquematch->s, lx, uniquematch_rx, ctxmap); x= uniquematch_rx+1; - ctxmap= 1; /* Lower only */ + if (uniquematch->s[0]) ctxmap= ct->midword; + else debugf(" (empty)"); + if (uniquematch->endsword) { + nspaces= SPACE_SPACES; + debugf("_"); + ctxmap= ct->nextword; + } + debugf("\n"); } else { int rx; - fprintf(debug," || UNKNOWN"); + debugf(" || UNKNOWN"); for (rx=lx; rxnresults); + debugf("OCR finished %d glyphs\n",rd->nresults); debug_flush(); return rd->results; }