chiark
/
gitweb
/
~yarrgweb
/
ypp-sc-tools.db-test.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
before do away with xpm
[ypp-sc-tools.db-test.git]
/
pctb
/
ocr.c
diff --git
a/pctb/ocr.c
b/pctb/ocr.c
index 48c25c085661579d75d4eca2622802dcfb19151c..7eb871a68844acc1d802747e67346aed760b3729 100644
(file)
--- a/
pctb/ocr.c
+++ b/
pctb/ocr.c
@@
-63,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"));
@@
-77,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;
@@
-282,6
+286,15
@@
const struct OcrCellTypeInfo ocr_celltype_text= {
.midword=1 /* Lower only */
};
.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;
OcrResultGlyph *ocr(OcrReader *rd, OcrCellType ct, int w, Pixcol cols[]) {
int nspaces;
unsigned ctxmap;
@@
-292,9
+305,9
@@
OcrResultGlyph *ocr(OcrReader *rd, OcrCellType ct, int w, Pixcol cols[]) {
nspaces=- w;
ctxmap= ct->initial;
rd->nresults=0;
nspaces=- w;
ctxmap= ct->initial;
rd->nresults=0;
-
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");
+
debugf(
"OCR h=%d w=%d",rd->h,w);
+ for (x=0; x<w; x++)
debugf(
" %"PSPIXCOL(PRIx),cols[x]);
+
debugf(
"\n");
debug_flush();
x=0;
debug_flush();
x=0;
@@
-308,7
+321,7
@@
OcrResultGlyph *ocr(OcrReader *rd, OcrCellType ct, int w, Pixcol cols[]) {
nspaces++;
x++;
if (nspaces==SPACE_SPACES) {
nspaces++;
x++;
if (nspaces==SPACE_SPACES) {
-
fprintf(debug,
"OCR x=%x nspaces=%d space\n",x,nspaces);
+
debugf(
"OCR x=%x nspaces=%d space\n",x,nspaces);
ctxmap= ct->nextword;
}
continue;
ctxmap= ct->nextword;
}
continue;
@@
-325,7
+338,7
@@
OcrResultGlyph *ocr(OcrReader *rd, OcrCellType ct, int w, Pixcol cols[]) {
DatabaseNode *uniquematch= 0;
int uniquematch_rx=-1;
DatabaseNode *uniquematch= 0;
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];;
@@
-334,29
+347,29
@@
OcrResultGlyph *ocr(OcrReader *rd, OcrCellType ct, 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++;
}
x++;
@@
-364,7
+377,7
@@
OcrResultGlyph *ocr(OcrReader *rd, OcrCellType ct, int w, Pixcol cols[]) {
if (bestmatch) {
if (uniquematch && strcmp(bestmatch->s, uniquematch->s)) {
if (bestmatch) {
if (uniquematch && strcmp(bestmatch->s, uniquematch->s)) {
-
fprintf(debug,
" ambiguous");
+
debugf(
" ambiguous");
uniquematch= 0;
break;
}
uniquematch= 0;
break;
}
@@
-374,22
+387,22
@@
OcrResultGlyph *ocr(OcrReader *rd, OcrCellType ct, int w, Pixcol cols[]) {
}
if (uniquematch) {
}
if (uniquematch) {
-
fprintf(debug,
" || YES\n");
+
debugf(
" || YES\n");
add_result(rd, uniquematch->s, lx, uniquematch_rx, ctxmap);
x= uniquematch_rx+1;
ctxmap= ct->midword;
} else {
int rx;
add_result(rd, uniquematch->s, lx, uniquematch_rx, ctxmap);
x= uniquematch_rx+1;
ctxmap= ct->midword;
} 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;
}