chiark / gitweb /
antialiasing text conversion: seems to handle it in the C code so far ....
[ypp-sc-tools.db-test.git] / pctb / ocr.h
1 /*
2  * ocr.c forms a mostly-self-contained bit
3  * so we put its declarations in this separate file
4  */
5 /*
6  *  This is part of ypp-sc-tools, a set of third-party tools for assisting
7  *  players of Yohoho Puzzle Pirates.
8  * 
9  *  Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
10  * 
11  *  This program is free software: you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation, either version 3 of the License, or
14  *  (at your option) any later version.
15  * 
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  * 
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  * 
24  *  Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
25  *  are used without permission.  This program is not endorsed or
26  *  sponsored by Three Rings.
27  */
28
29 #ifndef OCR_H
30 #define OCR_H
31
32 #include "common.h"
33
34
35 #define AADEPTH 3
36 #define AAMAXVAL ((1<<AADEPTH)-1)
37
38
39 typedef uint32_t Pixcolv;
40 #define PIXCOL_P_PER_WORD (32 / AADEPTH)
41
42 #define OCR_MAX_H    30
43 #define PIXCOL_WORDS ((OCR_MAX_H + PIXCOL_P_PER_WORD - 1) / PIXCOL_P_PER_WORD)
44
45 typedef struct { Pixcolv w[PIXCOL_WORDS]; } Pixcol;
46
47 static inline int pixcol_p_word(int y) {
48   return y / PIXCOL_P_PER_WORD;
49 }
50 static inline int pixcol_p_shift(int y) {
51   return (y % PIXCOL_P_PER_WORD)*AADEPTH;
52 }
53 static inline void pixcol_p_add(Pixcol *pixcol, int y, unsigned pixval) {
54   pixcol->w[pixcol_p_word(y)] |= pixval << pixcol_p_shift(y);
55 }
56 static inline unsigned int pixcol_p_get(const Pixcol *pixcol, int y) {
57   return (pixcol->w[pixcol_p_word(y)] >> pixcol_p_shift(y)) & AAMAXVAL;
58 }
59 static inline int pixcol_cmp(const Pixcol *pixcol1, const Pixcol *pixcol2) {
60   return memcmp(pixcol1, pixcol2, sizeof(*pixcol1));
61 }
62 static inline int pixcol_nonzero(const Pixcol *pixcol) {
63   static const Pixcol zero;
64   return pixcol_cmp(pixcol, &zero);
65 }
66
67 #if AADEPTH==3
68 # define PRPIXCOL1 "%0*" PRIo32
69 #else
70 # error need to implement PRPIXCOL1 for this AADEPTH
71 #endif
72
73 #if PIXCOL_WORDS==3
74 # define PIXCOL_PRFMT                           \
75                               PRPIXCOL1 "-"     \
76                               PRPIXCOL1 "-"     \
77                               PRPIXCOL1
78 # define PIXCOL_PRVAL(pixcol)                                   \
79                               PIXCOL_P_PER_WORD, (pixcol).w[2], \
80                               PIXCOL_P_PER_WORD, (pixcol).w[1], \
81                               PIXCOL_P_PER_WORD, (pixcol).w[0]
82 #else
83 # error need to implement PIXCOL_PR{FMT,VAL} for this PIXCOL_WORDS
84 #endif
85
86 typedef struct {
87   const char *s; /* valid until next call to ocr() */
88   int l,r; /* column numbers */
89   int match; /* match context index */
90   unsigned ctxmap; /* possible match contexts */
91 } OcrResultGlyph;
92
93
94 typedef const struct OcrCellTypeInfo *OcrCellType;
95 extern const struct OcrCellTypeInfo ocr_celltype_text;
96 extern const struct OcrCellTypeInfo ocr_celltype_number;
97 const char *ocr_celltype_name(OcrCellType ct);
98
99
100 typedef struct OcrReader OcrReader;
101 OcrReader *ocr_init(int h);
102 void ocr_showcharsets(void);
103
104 OcrResultGlyph *ocr(OcrReader *rd, OcrCellType, int w, const Pixcol cols[]);
105   /* return value is array terminated by {0,-1,-1}
106    * array is valid until next call to ocr()
107    */
108
109
110 extern const char *o_resolver;
111
112 FILE *resolve_start(void);
113 void resolve_finish(void);
114
115
116 #endif /*OCR_H*/