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=7f3be26a0eaee8c90906129c674d56514d74ad54;hb=24391f052b3c6f01f987b3cd08dbcc7a876afe06;hpb=909b1d69c7cafc7b50d932a0c1f790c9f91cb430 diff --git a/pctb/ocr.c b/pctb/ocr.c index 7f3be26..712f90d 100644 --- a/pctb/ocr.c +++ b/pctb/ocr.c @@ -5,58 +5,129 @@ typedef struct { Pixcol col; - struct OCRDatabaseNode *then; -} OCRDatabaseLink; + struct DatabaseNode *then; +} DatabaseLink; -#define MAXGLYPHCHRS 3 +#define MAXGLYPHCHRS 7 -typedef struct OCRDatabaseNode { +typedef struct DatabaseNode { char s[MAXGLYPHCHRS+1]; /* null-terminated; "" means no match here */ int nlinks, alinks; - OCRDatabaseLink *links; -} OCRDatabaseNode; + unsigned endsword:1; + 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])) + +#define SPACE_SPACES 4 + +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; +DEBUG_DEFINE_DEBUGF(ocr) + +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, endsword; + char lbuf[100]; + FILE *db; + + for (ctxi=0; ctxicontexts[ctxi]); - assert(!db); - db= fopen("database","r"); eassert(db); + char *dbfname=0; + asprintf(&dbfname,"%s/charset-%d.txt",get_vardir(),rd->h); + eassert(dbfname); + + db= fopen(dbfname,"r"); + free(dbfname); + if (!db) { + eassert(errno==ENOENT); + return; + } + + FGETSLINE(db,lbuf); + eassert(!strcmp(lbuf,"# ypp-sc-tools pctb font v1")); + + r= fscanf(db, "%d", &h); + eassert(r==1); + eassert(h==rd->h); 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; + } + endsword= 0; + if (nchrs>1 && chrs[nchrs-1]==' ') { + endsword= 1; + nchrs--; } 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 +142,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; @@ -84,14 +155,26 @@ static void ocr_readdb(void) { eassert(!current->s[0]); strcpy(current->s, chrs); + current->endsword= endsword; } 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 cu_pr_ctxmap(unsigned ctxmap) { + fprintf(resolver,"{"); + const char *spc=""; + int ctxi; + for (ctxi=0; ctxi= 4 and we have dealt with all the others * 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",(char*)0); + execlp("./show-thing.tcl", "./show-thing.tcl", + DEBUGP(callout) ? "--debug" : "--noop-arg", + "--automatic-1", + (char*)0); eassert(!"execlp failed"); } r= close(jobpipe[0]); eassert(!r); @@ -118,9 +204,13 @@ 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; il,s->r,s->ctx); + fprintf(resolver,"%d %d ",unk_l,unk_r); + cu_pr_ctxmap(unk_ctxmap); + for (i=0, s=rd->results; inresults; i++, s++) { + if (!strcmp(s->s," ")) continue; + 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); @@ -136,8 +226,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, + unsigned ctxmap) { + 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].ctxmap= ctxmap; + rd->nresults++; } -OcrResultGlyph *ocr(int w, int h, Pixcol cols[]) { - int nspaces=0; - int ctx=1,i, x; +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; - nresults=0; - assert(db); + restart: - fprintf(debug,"OCR h=%d w=%d",w,h); - for (x=0; xinitial; + rd->nresults=0; + debugf("OCR h=%d w=%d",rd->h,w); + for (x=0; x3) ctx=1; + 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 */ - OCRDatabaseNode *current=0, *bestmatch=0; 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; inlinks; i++) - if (current->links[i].col == cv) - goto found; - /* not found */ - fprintf(debug," ?"); - break; + DatabaseNode *uniquematch= 0; + int uniquematch_rx=-1; + + debugf("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; + debugf(" || %s",context_names[ctxi]); + + for (;;) { + debug_flush(); + debugf(" | x=%d",x); + if (x>w) break; + Pixcol cv= cols[x]; + debugf(" cv=%"PSPIXCOL(PRIx),cv); + for (i=0; inlinks; i++) + if (current->links[i].col == cv) + goto found; + /* not found */ + debugf(" ?"); + break; + + found: + current= current->links[i].then; + if (current->s[0]) { + debugf(" \"%s\"%s",current->s,current->endsword?"_":""); + bestmatch= current; + bestmatch_rx= x; + } else { + debugf(" ..."); + } - 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 && strcmp(bestmatch->s, uniquematch->s)) { + debugf( " ambiguous"); + uniquematch= 0; + break; + } + uniquematch= bestmatch; + uniquematch_rx= bestmatch_rx; } - x++; } - if (bestmatch) { - fprintf(debug," YES\n"); - add_result(bestmatch->s, lx, bestmatch_rx, ctx); - x= bestmatch_rx+1; - ctx= 0; + if (uniquematch) { + debugf(" || YES"); + add_result(rd, uniquematch->s, lx, uniquematch_rx, ctxmap); + x= uniquematch_rx+1; + 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); 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; }