Leptonica 1.82.0
Image processing and image analysis suite
dwacomb.2.c
1/*====================================================================*
2 - Copyright (C) 2001 Leptonica. All rights reserved.
3 -
4 - Redistribution and use in source and binary forms, with or without
5 - modification, are permitted provided that the following conditions
6 - are met:
7 - 1. Redistributions of source code must retain the above copyright
8 - notice, this list of conditions and the following disclaimer.
9 - 2. Redistributions in binary form must reproduce the above
10 - copyright notice, this list of conditions and the following
11 - disclaimer in the documentation and/or other materials
12 - provided with the distribution.
13 -
14 - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
18 - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 - OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *====================================================================*/
26
34#ifdef HAVE_CONFIG_H
35#include <config_auto.h>
36#endif /* HAVE_CONFIG_H */
37
38#include <string.h>
39#include "allheaders.h"
40
41PIX *pixMorphDwa_2(PIX *pixd, PIX *pixs, l_int32 operation, char *selname);
42PIX *pixFMorphopGen_2(PIX *pixd, PIX *pixs, l_int32 operation, char *selname);
43l_int32 fmorphopgen_low_2(l_uint32 *datad, l_int32 w,
44 l_int32 h, l_int32 wpld,
45 l_uint32 *datas, l_int32 wpls,
46 l_int32 index);
47
48static l_int32 NUM_SELS_GENERATED = 76;
49static char SEL_NAMES[][80] = {
50 "sel_comb_4h",
51 "sel_comb_4v",
52 "sel_comb_5h",
53 "sel_comb_5v",
54 "sel_comb_6h",
55 "sel_comb_6v",
56 "sel_comb_7h",
57 "sel_comb_7v",
58 "sel_comb_8h",
59 "sel_comb_8v",
60 "sel_comb_9h",
61 "sel_comb_9v",
62 "sel_comb_10h",
63 "sel_comb_10v",
64 "sel_comb_12h",
65 "sel_comb_12v",
66 "sel_comb_14h",
67 "sel_comb_14v",
68 "sel_comb_15h",
69 "sel_comb_15v",
70 "sel_comb_16h",
71 "sel_comb_16v",
72 "sel_comb_18h",
73 "sel_comb_18v",
74 "sel_comb_20h",
75 "sel_comb_20v",
76 "sel_comb_21h",
77 "sel_comb_21v",
78 "sel_comb_22h",
79 "sel_comb_22v",
80 "sel_comb_24h",
81 "sel_comb_24v",
82 "sel_comb_25h",
83 "sel_comb_25v",
84 "sel_comb_27h",
85 "sel_comb_27v",
86 "sel_comb_28h",
87 "sel_comb_28v",
88 "sel_comb_30h",
89 "sel_comb_30v",
90 "sel_comb_32h",
91 "sel_comb_32v",
92 "sel_comb_33h",
93 "sel_comb_33v",
94 "sel_comb_35h",
95 "sel_comb_35v",
96 "sel_comb_36h",
97 "sel_comb_36v",
98 "sel_comb_39h",
99 "sel_comb_39v",
100 "sel_comb_40h",
101 "sel_comb_40v",
102 "sel_comb_42h",
103 "sel_comb_42v",
104 "sel_comb_44h",
105 "sel_comb_44v",
106 "sel_comb_45h",
107 "sel_comb_45v",
108 "sel_comb_48h",
109 "sel_comb_48v",
110 "sel_comb_49h",
111 "sel_comb_49v",
112 "sel_comb_50h",
113 "sel_comb_50v",
114 "sel_comb_52h",
115 "sel_comb_52v",
116 "sel_comb_54h",
117 "sel_comb_54v",
118 "sel_comb_55h",
119 "sel_comb_55v",
120 "sel_comb_56h",
121 "sel_comb_56v",
122 "sel_comb_60h",
123 "sel_comb_60v",
124 "sel_comb_63h",
125 "sel_comb_63v"};
126
146PIX *
147pixMorphDwa_2(PIX *pixd,
148 PIX *pixs,
149 l_int32 operation,
150 char *selname)
151{
152l_int32 bordercolor, bordersize;
153PIX *pixt1, *pixt2, *pixt3;
154
155 PROCNAME("pixMorphDwa_2");
156
157 if (!pixs)
158 return (PIX *)ERROR_PTR("pixs not defined", procName, pixd);
159 if (pixGetDepth(pixs) != 1)
160 return (PIX *)ERROR_PTR("pixs must be 1 bpp", procName, pixd);
161
162 /* Set the border size */
163 bordercolor = getMorphBorderPixelColor(L_MORPH_ERODE, 1);
164 bordersize = 32;
165 if (bordercolor == 0 && operation == L_MORPH_CLOSE)
166 bordersize += 32;
167
168 pixt1 = pixAddBorder(pixs, bordersize, 0);
169 pixt2 = pixFMorphopGen_2(NULL, pixt1, operation, selname);
170 pixt3 = pixRemoveBorder(pixt2, bordersize);
171 pixDestroy(&pixt1);
172 pixDestroy(&pixt2);
173
174 if (!pixd)
175 return pixt3;
176
177 pixCopy(pixd, pixt3);
178 pixDestroy(&pixt3);
179 return pixd;
180}
181
182
206PIX *
207pixFMorphopGen_2(PIX *pixd,
208 PIX *pixs,
209 l_int32 operation,
210 char *selname)
211{
212l_int32 i, index, found, w, h, wpls, wpld, bordercolor, erodeop, borderop;
213l_uint32 *datad, *datas, *datat;
214PIX *pixt;
215
216 PROCNAME("pixFMorphopGen_2");
217
218 if (!pixs)
219 return (PIX *)ERROR_PTR("pixs not defined", procName, pixd);
220 if (pixGetDepth(pixs) != 1)
221 return (PIX *)ERROR_PTR("pixs must be 1 bpp", procName, pixd);
222
223 /* Get boundary colors to use */
224 bordercolor = getMorphBorderPixelColor(L_MORPH_ERODE, 1);
225 if (bordercolor == 1)
226 erodeop = PIX_SET;
227 else
228 erodeop = PIX_CLR;
229
230 found = FALSE;
231 for (i = 0; i < NUM_SELS_GENERATED; i++) {
232 if (strcmp(selname, SEL_NAMES[i]) == 0) {
233 found = TRUE;
234 index = 2 * i;
235 break;
236 }
237 }
238 if (found == FALSE)
239 return (PIX *)ERROR_PTR("sel index not found", procName, pixd);
240
241 if (!pixd) {
242 if ((pixd = pixCreateTemplate(pixs)) == NULL)
243 return (PIX *)ERROR_PTR("pixd not made", procName, NULL);
244 }
245 else /* for in-place or pre-allocated */
246 pixResizeImageData(pixd, pixs);
247 wpls = pixGetWpl(pixs);
248 wpld = pixGetWpl(pixd);
249
250 /* The images must be surrounded, in advance, with a border of
251 * size 32 pixels (or 64, for closing), that we'll read from.
252 * Fabricate a "proper" image as the subimage within the 32
253 * pixel border, having the following parameters: */
254 w = pixGetWidth(pixs) - 64;
255 h = pixGetHeight(pixs) - 64;
256 datas = pixGetData(pixs) + 32 * wpls + 1;
257 datad = pixGetData(pixd) + 32 * wpld + 1;
258
259 if (operation == L_MORPH_DILATE || operation == L_MORPH_ERODE) {
260 borderop = PIX_CLR;
261 if (operation == L_MORPH_ERODE) {
262 borderop = erodeop;
263 index++;
264 }
265 if (pixd == pixs) { /* in-place; generate a temp image */
266 if ((pixt = pixCopy(NULL, pixs)) == NULL)
267 return (PIX *)ERROR_PTR("pixt not made", procName, pixd);
268 datat = pixGetData(pixt) + 32 * wpls + 1;
269 pixSetOrClearBorder(pixt, 32, 32, 32, 32, borderop);
270 fmorphopgen_low_2(datad, w, h, wpld, datat, wpls, index);
271 pixDestroy(&pixt);
272 }
273 else { /* not in-place */
274 pixSetOrClearBorder(pixs, 32, 32, 32, 32, borderop);
275 fmorphopgen_low_2(datad, w, h, wpld, datas, wpls, index);
276 }
277 }
278 else { /* opening or closing; generate a temp image */
279 if ((pixt = pixCreateTemplate(pixs)) == NULL)
280 return (PIX *)ERROR_PTR("pixt not made", procName, pixd);
281 datat = pixGetData(pixt) + 32 * wpls + 1;
282 if (operation == L_MORPH_OPEN) {
283 pixSetOrClearBorder(pixs, 32, 32, 32, 32, erodeop);
284 fmorphopgen_low_2(datat, w, h, wpls, datas, wpls, index+1);
285 pixSetOrClearBorder(pixt, 32, 32, 32, 32, PIX_CLR);
286 fmorphopgen_low_2(datad, w, h, wpld, datat, wpls, index);
287 }
288 else { /* closing */
289 pixSetOrClearBorder(pixs, 32, 32, 32, 32, PIX_CLR);
290 fmorphopgen_low_2(datat, w, h, wpls, datas, wpls, index);
291 pixSetOrClearBorder(pixt, 32, 32, 32, 32, erodeop);
292 fmorphopgen_low_2(datad, w, h, wpld, datat, wpls, index+1);
293 }
294 pixDestroy(&pixt);
295 }
296
297 return pixd;
298}
299
l_uint32 getMorphBorderPixelColor(l_int32 type, l_int32 depth)
getMorphBorderPixelColor()
Definition: morph.c:1803
l_ok pixResizeImageData(PIX *pixd, const PIX *pixs)
pixResizeImageData()
Definition: pix1.c:768
void pixDestroy(PIX **ppix)
pixDestroy()
Definition: pix1.c:621
PIX * pixCreateTemplate(const PIX *pixs)
pixCreateTemplate()
Definition: pix1.c:383
PIX * pixCopy(PIX *pixd, const PIX *pixs)
pixCopy()
Definition: pix1.c:705
l_uint32 * pixGetData(PIX *pix)
pixGetData()
Definition: pix1.c:1763
PIX * pixRemoveBorder(PIX *pixs, l_int32 npix)
pixRemoveBorder()
Definition: pix2.c:1972
l_ok pixSetOrClearBorder(PIX *pixs, l_int32 left, l_int32 right, l_int32 top, l_int32 bot, l_int32 op)
pixSetOrClearBorder()
Definition: pix2.c:1514
PIX * pixAddBorder(PIX *pixs, l_int32 npix, l_uint32 val)
pixAddBorder()
Definition: pix2.c:1823
#define PIX_CLR
Definition: pix.h:333
#define PIX_SET
Definition: pix.h:334
Definition: pix.h:139