From 5235400ea0618cdc6e4585f9b032edbc000ad8e8 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sun, 14 Jun 2009 22:49:13 +0100 Subject: [PATCH] WIP island determination; factor out resolve_{start,finish}! --- pctb/Makefile | 2 +- pctb/ocr.c | 65 ++++++++------------------------------------------- pctb/ocr.h | 4 ++++ 3 files changed, 15 insertions(+), 56 deletions(-) diff --git a/pctb/Makefile b/pctb/Makefile index 93e5860..f798e52 100644 --- a/pctb/Makefile +++ b/pctb/Makefile @@ -35,7 +35,7 @@ TARGETS= ypp-commodities all: $(TARGETS) -CONVERT_OBJS= convert.o ocr.o pages.o structure.o common.o rgbimage.o +CONVERT_OBJS= convert.o ocr.o pages.o structure.o common.o rgbimage.o resolve.o ypp-commodities: $(CONVERT_OBJS) -lnetpbm -lXtst -lX11 $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS) diff --git a/pctb/ocr.c b/pctb/ocr.c index fafdba8..927b93a 100644 --- a/pctb/ocr.c +++ b/pctb/ocr.c @@ -75,10 +75,6 @@ struct OcrReader { int aresults, nresults; }; -static FILE *resolver; -static pid_t resolver_pid; -static int resolver_done; - DEBUG_DEFINE_DEBUGF(ocr) #define FGETSLINE (dbfile_getsline(lbuf,sizeof(lbuf),__FILE__,__LINE__)) @@ -189,7 +185,7 @@ static void readdb(OcrReader *rd) { free(dbfname); } -static void cu_pr_ctxmap(unsigned ctxmap) { +static void cu_pr_ctxmap(FILE *resolver, unsigned ctxmap) { fprintf(resolver,"{"); const char *spc=""; int ctxi; @@ -203,46 +199,25 @@ static void cu_pr_ctxmap(unsigned ctxmap) { 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,i, x,y; + int c,i, x,y; const OcrResultGlyph *s; const char *p; - char cb; Pixcol pv; - if (!o_resolver) + FILE *resolver= resolve_start(); + if (!resolver) fatal("OCR failed - unrecognised characters or ligatures.\n" "Character set database needs to be updated or augmented.\n" "See README.charset.\n"); - if (!resolver) { - sysassert(! pipe(jobpipe) ); - sysassert(! pipe(donepipe) ); - resolver_pid= fork(); - sysassert(resolver_pid!=-1); - if (!resolver_pid) { - sysassert( dup2(jobpipe[0],0) ==0 ); - sysassert(! close(jobpipe[1]) ); - sysassert(! close(donepipe[0]) ); - /* we know donepipe[1] is >= 4 and we have dealt with all the others - * so we aren't in any danger of overwriting some other fd 4: */ - sysassert( dup2(donepipe[1],4) ==4 ); - execlp(o_resolver, o_resolver, - DEBUGP(callout) ? "--debug" : "--noop-arg", - "--automatic-1", - (char*)0); - sysassert(!"execlp ocr-resolver failed"); - } - sysassert(! close(jobpipe[0]) ); - sysassert(! close(donepipe[1]) ); - resolver= fdopen(jobpipe[1],"w"); sysassert(resolver); - resolver_done= donepipe[0]; - } - fprintf(resolver,"%d %d ",unk_l,unk_r); - cu_pr_ctxmap(unk_ctxmap); + fprintf(resolver, + "char\n" + "%d %d ",unk_l,unk_r); + cu_pr_ctxmap(resolver,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); + cu_pr_ctxmap(resolver,s->ctxmap); fprintf(resolver," "); for (p=s->s; (c= *p); p++) { if (c=='\\') fprintf(resolver,"\\%c",c); @@ -267,28 +242,8 @@ static void callout_unknown(OcrReader *rd, int w, Pixcol cols[], fputs("\",\n",resolver); } fputs("};\n",resolver); - sysassert(!ferror(resolver)); - sysassert(!fflush(resolver)); - - sysassert(resolver); - - int r; - for (;;) { - r= read(resolver_done,&cb,1); - if (r==-1) { sysassert(errno==EINTR); continue; } - break; - } - - if (r==0) { - waitpid_check_exitstatus(resolver_pid, "character resolver"); - fclose(resolver); - close(resolver_done); - resolver= 0; - } else { - assert(r==1); - sysassert(cb==0); - } + resolve_finish(); readdb(rd); } diff --git a/pctb/ocr.h b/pctb/ocr.h index 943c210..97a5c66 100644 --- a/pctb/ocr.h +++ b/pctb/ocr.h @@ -56,7 +56,11 @@ OcrResultGlyph *ocr(OcrReader *rd, OcrCellType, int w, Pixcol cols[]); * array is valid until next call to ocr() */ + extern const char *o_resolver; +FILE *resolve_start(void); +void resolve_finish(void); + #endif /*OCR_H*/ -- 2.30.2