X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?p=ypp-sc-tools.db-test.git;a=blobdiff_plain;f=pctb%2Focr.c;h=fd4fcf4f8de99642291fb3e9c5410a2884c642fb;hp=be479f83b8d626442899c77f08ffdab049e26747;hb=8787ee59f6840de63bac432b516a30d0dfe22c84;hpb=cd6a4f773c32f73aff27f97e8994c6b7c1019bb7 diff --git a/pctb/ocr.c b/pctb/ocr.c index be479f8..fd4fcf4 100644 --- a/pctb/ocr.c +++ b/pctb/ocr.c @@ -5,58 +5,115 @@ typedef struct { Pixcol col; - struct OCRDatabaseNode *then; -} OCRDatabaseLink; + struct DatabaseNode *then; +} DatabaseLink; #define MAXGLYPHCHRS 3 -typedef struct OCRDatabaseNode { +typedef struct DatabaseNode { char s[MAXGLYPHCHRS+1]; /* null-terminated; "" means no match here */ int nlinks, alinks; - OCRDatabaseLink *links; -} OCRDatabaseNode; + DatabaseLink *links; +} DatabaseNode; -#define N_OCR_CONTEXTS 2 +static const char *context_names[]= { + "Lower", + "Upper", +/* "Digit"*/ +}; -static OCRDatabaseNode ocr_contexts[N_OCR_CONTEXTS]; -static FILE *db; -static OcrResultGlyph *results; -static int aresults, nresults; +#define NCONTEXTS (sizeof(context_names)/sizeof(context_names[0])) + +struct OcrReader { + int h; + DatabaseNode contexts[NCONTEXTS]; + OcrResultGlyph *results; + int aresults, nresults; +}; static FILE *resolver; static pid_t resolver_pid; static int resolver_done; -static void ocr_readdb(void) { - int ctx,nchrs; - OCRDatabaseNode *current, *additional; +static void fgetsline(FILE *f, char *lbuf, size_t lbufsz) { + char *s= fgets(lbuf,lbufsz,f); + eassert(s); + int l= strlen(lbuf); + eassert(l>0); eassert(lbuf[--l]='\n'); + lbuf[l]= 0; +} +#define FGETSLINE(f,buf) (fgetsline(f,buf,sizeof(buf))) + +static void cleardb_node(DatabaseNode *n) { + int i; + n->s[0]= 0; + for (i=0; inlinks; i++) + cleardb_node(n->links[i].then); +} + +static void readdb(OcrReader *rd) { + int nchrs; + DatabaseNode *current, *additional; char chrs[MAXGLYPHCHRS+1]; Pixcol cv; - int r,i,j; + int r,j,ctxi; + int h; + char lbuf[100]; + FILE *db; + + char *dbfname=0; + asprintf(&dbfname,"%s/charset-%d.txt",get_vardir(),rd->h); + eassert(dbfname); + + db= fopen(dbfname,"r"); eassert(db); + free(dbfname); + + FGETSLINE(db,lbuf); + eassert(!strcmp(lbuf,"# ypp-sc-tools pctb font v1")); + + r= fscanf(db, "%d", &h); + eassert(r==1); + eassert(h==rd->h); - assert(!db); - db= fopen("database","r"); eassert(db); + for (ctxi=0; ctxicontexts[ctxi]); for (;;) { - r= fscanf(db, "%d %d", &ctx, &nchrs); - if (r==EOF) break; - eassert(r==2); - eassert(ctx>=0 && ctx0 && nchrs<=MAXGLYPHCHRS); - - for (i=0; i0 && c<=255); - chrs[i]= c; + FGETSLINE(db,lbuf); + if (!lbuf || lbuf[0]=='#') continue; + if (!strcmp(lbuf,".")) break; + + for (ctxi=0; ctxi0 && cr<=255); + c= cr; + } + chrs[nchrs++]= c; } chrs[nchrs]= 0; - int twidth; - r= fscanf(db, "%d", &twidth); eassert(r==1); - current= &ocr_contexts[ctx]; - for (i=0; icontexts[ctxi]; + for (;;) { + FGETSLINE(db,lbuf); + if (!lbuf[0]) { eassert(current != &rd->contexts[ctxi]); break; } + char *ep; + cv= strtoul(lbuf,&ep,16); eassert(!*ep); + eassert(!(cv & ~((1UL << rd->h)-1))); + for (j=0; jnlinks; j++) if (current->links[j].col == cv) { current= current->links[j].then; @@ -71,7 +128,7 @@ static void ocr_readdb(void) { current->alinks++; current->alinks<<=1; current->links= realloc(current->links, - sizeof(*current->links) * current->alinks); + sizeof(*current->links) * current->alinks); eassert(current->links); } current->links[current->nlinks].col= cv; @@ -86,12 +143,11 @@ static void ocr_readdb(void) { strcpy(current->s, chrs); } eassert(!ferror(db)); - eassert(feof(db)); + eassert(!fclose(db)); } -static void callout_unknown(int w, int h, Pixcol cols[], - int unk_l, int unk_r, int unk_ctx, - const OcrResultGlyph *sofar, int nsofar) { +static void callout_unknown(OcrReader *rd, int w, Pixcol cols[], + int unk_l, int unk_r, unsigned unk_ctxmap) { int jobpipe[2],donepipe[2], c, r,i, x,y; const OcrResultGlyph *s; const char *p; @@ -119,10 +175,18 @@ static void callout_unknown(int w, int h, Pixcol cols[], resolver= fdopen(jobpipe[1],"w"); eassert(resolver); resolver_done= donepipe[0]; } - fprintf(resolver,"%d %d %d",unk_l,unk_r,unk_ctx); - for (i=0, s=sofar; iresults; inresults; i++, s++) { if (!strcmp(s->s," ")) continue; - fprintf(resolver," %d %d %d ",s->l,s->r,s->ctx); + fprintf(resolver," %d %d %s ",s->l,s->r,context_names[s->ctx]); for (p=s->s; (c= *p); p++) { if (c=='\\') fprintf(resolver,"\\%c",c); else if (c>=33 && c<=126) fputc(c,resolver); @@ -138,8 +202,8 @@ static void callout_unknown(int w, int h, Pixcol cols[], "\"%d %d 2 1\",\n" "\" c black\",\n" "\"o c white\",\n", - w,h); - for (y=0, pv=1; yh); + for (y=0, pv=1; yh; y++, pv<<=1) { fputc('"',resolver); for (x=0; x= aresults) { - aresults++; aresults<<=1; - results= realloc(results,sizeof(*results)*aresults); - eassert(results); +static void add_result(OcrReader *rd, const char *s, int l, int r, int ctx) { + if (rd->nresults >= rd->aresults) { + rd->aresults++; rd->aresults<<=1; + rd->results= realloc(rd->results,sizeof(*rd->results)*rd->aresults); + eassert(rd->results); } - results[nresults].s= s; - results[nresults].l= l; - results[nresults].r= r; - results[nresults].ctx= ctx; - nresults++; + 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->nresults++; } -OcrResultGlyph *ocr(int w, int h, Pixcol cols[]) { +OcrResultGlyph *ocr(OcrReader *rd, int w, Pixcol cols[]) { int nspaces=-w; - int ctx=1,i, x; + unsigned ctxmap=2; /* uppercase */ + int ctxi, i, x; - nresults=0; - assert(db); + rd->nresults=0; - fprintf(debug,"OCR h=%d w=%d",w,h); + fprintf(debug,"OCR h=%d w=%d",rd->h,w); for (x=0; xw) break; - Pixcol cv= cols[x]; - fprintf(debug," cv=%"PSPIXCOL(PRIx),x); - for (i=0; inlinks; i++) - if (current->links[i].col == cv) - goto found; - /* not found */ - fprintf(debug," ?"); - break; + DatabaseNode *uniquematch= 0; + int uniquematch_rx=-1, uniquematch_ctxi=-1; + + fprintf(debug,"OCR lx=%d ctxmap=%x ",lx,ctxmap); + + for (ctxi=0; ctxicontexts[ctxi];; + DatabaseNode *bestmatch= 0; + int bestmatch_rx=-1; + + x= lx; + if (!(ctxmap & (1u << ctxi))) continue; + fprintf(debug," || %s",context_names[ctxi]); + + for (;;) { + debug_flush(); + fprintf(debug," | x=%d",x); + if (x>w) break; + Pixcol cv= cols[x]; + fprintf(debug," cv=%"PSPIXCOL(PRIx),cv); + for (i=0; inlinks; i++) + if (current->links[i].col == cv) + goto found; + /* not found */ + fprintf(debug," ?"); + break; + + found: + current= current->links[i].then; + if (current->s[0]) { + fprintf(debug," \"%s\"",current->s); + bestmatch= current; + bestmatch_rx= x; + } else { + fprintf(debug," ..."); + } - found: - current= current->links[i].then; - if (current->s[0]) { - fprintf(debug," \"%s\"",current->s); - bestmatch=current; bestmatch_rx=x; - } else { - fprintf(debug," ..."); + x++; + } + + if (bestmatch) { + if (uniquematch) { + fprintf(debug, " ambiguous"); + uniquematch= 0; + break; + } + uniquematch= bestmatch; + uniquematch_rx= bestmatch_rx; + uniquematch_ctxi= ctxi; } - x++; } - if (bestmatch) { - fprintf(debug," YES\n"); - add_result(bestmatch->s, lx, bestmatch_rx, ctx); - x= bestmatch_rx+1; - ctx= 0; + if (uniquematch) { + fprintf(debug," || YES\n"); + add_result(rd, uniquematch->s, lx, uniquematch_rx, uniquematch_ctxi); + x= uniquematch_rx+1; + ctxmap= 1; /* Lower only */ } else { int rx; - fprintf(debug," UNKNOWN"); + fprintf(debug," || UNKNOWN"); for (rx=lx; rxnresults); debug_flush(); - return results; + return rd->results; } -void ocr_init(void) { - ocr_readdb(); +OcrReader *ocr_init(int h) { + OcrReader *rd; + + rd= malloc(sizeof(*rd)); eassert(rd); + memset(rd,0,sizeof(*rd)); + rd->h= h; + readdb(rd); + return rd; }