1 /* Seven Segment Optical Character Recognition Image Processing Functions */
2 
3 /*  This program is free software: you can redistribute it and/or modify
4  *  it under the terms of the GNU General Public License as published by
5  *  the Free Software Foundation, either version 3 of the License, or
6  *  (at your option) any later version.
7  *
8  *  This program is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *  GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License
14  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
15 */
16 
17 /* Copyright (C) 2004-2021 Erik Auerswald <auerswal@unix-ag.uni-kl.de> */
18 
19 #ifndef SSOCR2_IMGPROC_H
20 #define SSOCR2_IMGPROC_H
21 
22 /* parse luminance keyword */
23 luminance_t parse_lum(char *keyword);
24 
25 /* set foreground color */
26 void set_fg_color(int color);
27 
28 /* set background color */
29 void set_bg_color(int color);
30 
31 /* set imlib color */
32 void ssocr_set_color(fg_bg_t color);
33 
34 /* draw a fore- or background pixel */
35 void draw_pixel(Imlib_Image *image, int x, int y, fg_bg_t color);
36 
37 /* draw a foreground pixel */
38 void draw_fg_pixel(Imlib_Image *image, int x, int y);
39 
40 /* draw a background pixel */
41 void draw_bg_pixel(Imlib_Image *image, int x, int y);
42 
43 /* check if a pixel is set regarding current foreground/background colors */
44 int is_pixel_set(int value, double threshold);
45 
46 /* set pixel if at least mask neighboring pixels (including the pixel to be set)
47  * are set
48  * a pixel is set if its luminance value is less than thresh
49  * mask=1 is the standard dilation filter
50  * mask=9 is the standard erosion filter */
51 Imlib_Image set_pixels_filter(Imlib_Image *source_image, double thresh,
52                               luminance_t lt, int mask);
53 
54 /* perform set pixel filter operation iter times */
55 Imlib_Image set_pixels_filter_iter(Imlib_Image *source_image, double thresh,
56                                    luminance_t lt, int mask, int iter);
57 
58 /* shortcut for dilation */
59 Imlib_Image dilation(Imlib_Image *source_image, double thresh, luminance_t lt,
60                      int n);
61 
62 /* shortcut for erosion */
63 Imlib_Image erosion(Imlib_Image *source_image, double thresh, luminance_t lt,
64                     int n);
65 
66 /* shortcut for closing */
67 Imlib_Image closing(Imlib_Image *source_image, double thresh, luminance_t lt,
68                     int n);
69 
70 /* shortcut for opening */
71 Imlib_Image opening(Imlib_Image *source_image, double thresh, luminance_t lt,
72                     int n);
73 
74 /* keep only pixels that have at least mask-1 neighbors set */
75 Imlib_Image keep_pixels_filter(Imlib_Image *source_image, double thresh,
76                                luminance_t lt, int mask);
77 
78 /* remove isolated pixels (shortcut for keep_pixels_filter with mask = 2) */
79 Imlib_Image remove_isolated(Imlib_Image *source_image, double thresh,
80                             luminance_t lt);
81 
82 /* gray stretching, i.e. lum<t1 => lum=0, lum>t2 => lum=100,
83  * else lum=((lum-t1)*MAXRGB)/(t2-t1) */
84 Imlib_Image gray_stretch(Imlib_Image *source_image, double t1, double t2,
85                          luminance_t lt);
86 
87 /* use dynamic (aka adaptive) local thresholding to create monochrome image */
88 Imlib_Image dynamic_threshold(Imlib_Image *source_image, double t,
89                               luminance_t lt ,int ww, int wh);
90 
91 /* make black and white */
92 Imlib_Image make_mono(Imlib_Image *source_image, double thresh, luminance_t lt);
93 
94 /* set pixel to black (0,0,0) if R<T, T=thresh/100*255 */
95 Imlib_Image r_threshold(Imlib_Image *source_image, double thresh);
96 
97 /* set pixel to black (0,0,0) if G<T, T=thresh/100*255 */
98 Imlib_Image g_threshold(Imlib_Image *source_image, double thresh);
99 
100 /* set pixel to black (0,0,0) if B<T, T=thresh/100*255 */
101 Imlib_Image b_threshold(Imlib_Image *source_image, double thresh);
102 
103 /* make the border white */
104 Imlib_Image white_border(Imlib_Image *source_image, int width);
105 
106 /* invert image */
107 Imlib_Image invert(Imlib_Image *source_image, double thresh, luminance_t lt);
108 
109 /* shear the image
110  * the top line is unchanged
111  * the bottom line is moved offset pixels to the right
112  * the other lines are moved yPos*offset/(height-1) pixels to the right
113  * white pixels are inserted at the left side */
114 Imlib_Image shear(Imlib_Image *source_image, int offset);
115 
116 /* rotate the image */
117 Imlib_Image rotate(Imlib_Image *source_image, double theta);
118 
119 /* mirror image horizontally or vertically */
120 Imlib_Image mirror(Imlib_Image *source_image, direction_t direction);
121 
122 /* turn image to grayscale */
123 Imlib_Image grayscale(Imlib_Image *source_image, luminance_t lt);
124 
125 /* crop image */
126 Imlib_Image crop(Imlib_Image *source_image, int x, int y, int w, int h);
127 
128 /* adapt threshold to image values values */
129 double adapt_threshold(Imlib_Image *image, double thresh, luminance_t lt, int x,
130                        int y, int w, int h, unsigned int flags);
131 
132 /* compute dynamic threshold value from the rectangle (x,y),(x+w,y+h) of
133  * source_image */
134 double get_threshold(Imlib_Image *source_image, double fraction, luminance_t lt,
135                      int x, int y, int w, int h);
136 
137 /* determine threshold by an iterative method */
138 double iterative_threshold(Imlib_Image *source_image, double thresh,
139                            luminance_t lt, int x, int y, int w, int h);
140 
141 /* get minimum gray value */
142 double get_minval(Imlib_Image *source_image, int x, int y, int w, int h,
143                  luminance_t lt);
144 
145 /* get maximum gray value */
146 double get_maxval(Imlib_Image *source_image, int x, int y, int w, int h,
147                  luminance_t lt);
148 
149 /* compute luminance from RGB values */
150 int get_lum(Imlib_Color *color, luminance_t lt);
151 
152 /* compute luminance Y_709 from linear RGB values */
153 int get_lum_709(Imlib_Color *color);
154 
155 /* compute luminance Y_601 from gamma corrected (non-linear) RGB values */
156 int get_lum_601(Imlib_Color *color);
157 
158 /* compute luminance Y = (R+G+B)/3 */
159 int get_lum_lin(Imlib_Color *color);
160 
161 /* compute luminance Y = min(R,G,B) as used in GNU Ocrad 0.14 */
162 int get_lum_min(Imlib_Color *color);
163 
164 /* compute luminance Y = max(R,G,B) */
165 int get_lum_max(Imlib_Color *color);
166 
167 /* compute luminance Y = R */
168 int get_lum_red(Imlib_Color *color);
169 
170 /* compute luminance Y = G */
171 int get_lum_green(Imlib_Color *color);
172 
173 /* compute luminance Y = B */
174 int get_lum_blue(Imlib_Color *color);
175 
176 /* clip value thus that it is in the given interval [min,max] */
177 int clip(int value, int min, int max);
178 
179 /* save image to file */
180 void save_image(const char *image_type, Imlib_Image *image, const char *fmt,
181                 const char *filename, unsigned int flags);
182 
183 /* report Imlib2 load/save error to stderr */
184 void report_imlib_error(Imlib_Load_Error error);
185 
186 #endif /* SSOCR2_IMGPROC_H */
187