2 * Core OCR algorithm (first exact bitmap match)
5 * This is part of ypp-sc-tools, a set of third-party tools for assisting
6 * players of Yohoho Puzzle Pirates.
8 * Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 * Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
24 * are used without permission. This program is not endorsed or
25 * sponsored by Three Rings.
33 struct DatabaseNode *then;
36 typedef struct DatabaseNode {
39 unsigned match:1, defined:1, endsword:1;
43 static const char *context_names[]= {
48 struct OcrCellTypeInfo {
49 /* bitmaps of indices into context_names: */
50 unsigned initial, nextword, midword;
54 const struct OcrCellTypeInfo ocr_celltype_number= {
59 const struct OcrCellTypeInfo ocr_celltype_text= {
60 .initial=2, /* Uppercase */
61 .nextword=3, /* Either */
62 .midword=1, /* Lower only */
68 #define NCONTEXTS (sizeof(context_names)/sizeof(context_names[0]))
72 DatabaseNode contexts[NCONTEXTS];
73 OcrResultGlyph *results;
74 int aresults, nresults;
77 DEBUG_DEFINE_DEBUGF(ocr)
79 #define FGETSLINE (dbfile_getsline(lbuf,sizeof(lbuf),__FILE__,__LINE__))
81 static void cleardb_node(DatabaseNode *n) {
83 free(n->str); n->str=0;
84 n->defined=n->match=n->endsword= 0;
85 for (i=0; i<n->nlinks; i++)
86 cleardb_node(n->links[i].then);
89 static void readdb1(OcrReader *rd, const char *which);
91 static void readdb(OcrReader *rd) {
94 for (ctxi=0; ctxi<NCONTEXTS; ctxi++)
95 cleardb_node(&rd->contexts[ctxi]);
97 readdb1(rd, "master");
101 static void readdb1(OcrReader *rd, const char *which) {
103 DatabaseNode *current, *additional;
110 char *dbfname= masprintf("%s/#%s-char%d#.txt",
111 get_vardir(), which, rd->h);
113 if (!dbfile_open(dbfname))
117 dbassert(!strcmp(lbuf,"# ypp-sc-tools pctb font v1"));
119 dbassert( dbfile_scanf("%d", &h) == 1);
124 if (!lbuf[0] || lbuf[0]=='#') continue;
125 if (!strcmp(lbuf,".")) break;
127 for (ctxi=0; ctxi<NCONTEXTS; ctxi++)
128 if (!strcmp(lbuf,context_names[ctxi]))
130 /* not found, just skip */
131 for (;;) { FGETSLINE; if (!lbuf[0]) break; }
136 int c= fgetc(dbfile); sysassert(!ferror(dbfile)); dbassert(c!=EOF);
137 if (c=='\n') break; /* forces no match */
138 dbassert(nchrs<sizeof(chrs));
142 if (nchrs>0 && chrs[nchrs-1]==' ') {
147 current= &rd->contexts[ctxi];
150 if (!lbuf[0]) { dbassert(current != &rd->contexts[ctxi]); break; }
152 cv= strtoul(lbuf,&ep,16); dbassert(!*ep);
153 dbassert(!(cv & ~((1UL << rd->h)-1)));
155 for (j=0; j<current->nlinks; j++)
156 if (current->links[j].col == cv) {
157 current= current->links[j].then;
161 additional= mmalloc(sizeof(*additional));
163 additional->defined= 0;
164 additional->match= 0;
165 additional->endsword= 0;
166 additional->nlinks= additional->alinks= 0;
167 additional->links= 0;
168 if (current->nlinks==current->alinks) {
171 current->links= mrealloc(current->links,
172 sizeof(*current->links) * current->alinks);
174 current->links[current->nlinks].col= cv;
175 current->links[current->nlinks].then= additional;
182 if (!current->defined) {
189 current->str= mmalloc(nchrs+1);
190 memcpy(current->str, chrs, nchrs);
191 current->str[nchrs]= 0;
193 current->endsword= endsword;
202 static void cu_pr_ctxmap(FILE *resolver, unsigned ctxmap) {
203 fprintf(resolver,"{");
206 for (ctxi=0; ctxi<NCONTEXTS; ctxi++) {
207 if (!(ctxmap & (1u << ctxi))) continue;
208 fprintf(resolver,"%s%s",spc,context_names[ctxi]);
211 fprintf(resolver,"}");
214 static void callout_unknown(OcrReader *rd, int w, Pixcol cols[],
215 int unk_l, int unk_r, unsigned unk_ctxmap) {
217 const OcrResultGlyph *s;
221 FILE *resolver= resolve_start();
223 fatal("OCR failed - unrecognised characters or ligatures.\n"
224 "Character set database needs to be updated or augmented.\n"
225 "See README.charset.\n");
229 "%d %d ",unk_l,unk_r);
230 cu_pr_ctxmap(resolver,unk_ctxmap);
231 for (i=0, s=rd->results; i<rd->nresults; i++, s++) {
232 if (!strcmp(s->s," ")) continue;
233 fprintf(resolver," %d %d ",s->l,s->r);
234 cu_pr_ctxmap(resolver,s->ctxmap);
235 fprintf(resolver," ");
236 for (p=s->s; (c= *p); p++) {
237 if (c=='\\') fprintf(resolver,"\\%c",c);
238 else if (c>=33 && c<=126) fputc(c,resolver);
239 else fprintf(resolver,"\\x%02x",(unsigned char)c);
242 fputc('\n',resolver);
246 "static char *t[] = {\n"
247 "/* columns rows colors chars-per-pixel */\n"
252 for (y=0, pv=1; y<rd->h; y++, pv<<=1) {
255 fputc(cols[x] & pv ? 'o' : ' ', resolver);
256 fputs("\",\n",resolver);
258 fputs("};\n",resolver);
264 static void add_result(OcrReader *rd, const char *s, int l, int r,
266 if (rd->nresults >= rd->aresults) {
267 rd->aresults++; rd->aresults<<=1;
268 rd->results= mrealloc(rd->results, sizeof(*rd->results)*rd->aresults);
270 rd->results[rd->nresults].s= s;
271 rd->results[rd->nresults].l= l;
272 rd->results[rd->nresults].r= r;
273 rd->results[rd->nresults].ctxmap= ctxmap;
278 const char *ocr_celltype_name(OcrCellType ct) { return ct->name; }
280 OcrResultGlyph *ocr(OcrReader *rd, OcrCellType ct, int w, Pixcol cols[]) {
290 debugf("OCR h=%d w=%d",rd->h,w);
291 for (x=0; x<w; x++) debugf(" %"PSPIXCOL(PRIx),cols[x]);
305 if (nspaces == ct->space_spaces) {
306 debugf("OCR x=%x nspaces=%d space\n",x,nspaces);
307 ctxmap= ct->nextword;
312 /* something here, so we need to add the spaces */
313 if (nspaces >= ct->space_spaces)
314 add_result(rd," ",x-nspaces,x+1,0);
320 DatabaseNode *uniquematch= 0;
321 int uniquematch_rx=-1;
323 debugf("OCR lx=%d ctxmap=%x ",lx,ctxmap);
325 for (ctxi=0; ctxi<NCONTEXTS; ctxi++) {
326 DatabaseNode *current= &rd->contexts[ctxi];;
327 DatabaseNode *bestmatch= 0;
331 if (!(ctxmap & (1u << ctxi))) continue;
332 debugf(" || %s",context_names[ctxi]);
339 debugf(" cv=%"PSPIXCOL(PRIx),cv);
340 for (i=0; i<current->nlinks; i++)
341 if (current->links[i].col == cv)
348 current= current->links[i].then;
349 if (current->match) {
350 debugf(" \"%s\"%s",current->str,current->endsword?"_":"");
361 if (uniquematch && strcmp(bestmatch->str, uniquematch->str)) {
362 debugf( " ambiguous");
366 uniquematch= bestmatch;
367 uniquematch_rx= bestmatch_rx;
373 add_result(rd, uniquematch->str, lx, uniquematch_rx, ctxmap);
375 if (uniquematch->match) ctxmap= ct->midword;
376 else debugf(" (empty)");
377 if (uniquematch->endsword) {
378 nspaces= ct->space_spaces;
380 ctxmap= ct->nextword;
385 debugf(" || UNKNOWN");
386 for (rx=lx; rx<w && cols[rx]; rx++);
387 debugf(" x=%d ctxmap=%x %d..%d\n",x, ctxmap, lx,rx);
389 callout_unknown(rd, w,cols, lx,rx-1, ctxmap);
393 add_result(rd, 0,-1,-1,0);
394 debugf("OCR finished %d glyphs\n",rd->nresults);
399 OcrReader *ocr_init(int h) {
402 if (o_flags & ff_dict_fetch) {
403 char *fetchfile= masprintf("char%d",h);
404 progress("Updating %s...",fetchfile);
405 fetch_with_rsync(fetchfile);
409 rd= mmalloc(sizeof(*rd));
410 memset(rd,0,sizeof(*rd));