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
27 /*
28 * binmorph2_reg.c
29 *
30 * Thorough regression test for binary separable rasterops,
31 * using the sequence interpreters. This compares the
32 * results for 2-way composite Sels with unitary Sels,
33 * all invoked on the separable block morph ops.
34 */
35
36 #include "allheaders.h"
37
38 static const l_int32 MAX_SEL_SIZE = 120;
39
40 static void writeResult(char *sequence, l_int32 same);
41
42
main(int argc,char ** argv)43 int main(int argc,
44 char **argv)
45 {
46 char buffer1[256];
47 char buffer2[256];
48 l_int32 i, same, same2, factor1, factor2, diff, success;
49 PIX *pixs, *pixsd, *pixt1, *pixt2, *pixt3;
50 static char mainName[] = "binmorph2_reg";
51
52 if (argc != 1)
53 return ERROR_INT(" Syntax: binmorph2_reg", mainName, 1);
54
55 setLeptDebugOK(1);
56 pixs = pixRead("feyn-fract.tif");
57 pixsd = pixMorphCompSequence(pixs, "d5.5", 0);
58 success = TRUE;
59 for (i = 1; i < MAX_SEL_SIZE; i++) {
60
61 /* Check if the size is exactly decomposable */
62 selectComposableSizes(i, &factor1, &factor2);
63 diff = factor1 * factor2 - i;
64 fprintf(stderr, "%d: (%d, %d): %d\n", i, factor1, factor2, diff);
65
66 /* Carry out operations on identical sized Sels: dilation */
67 snprintf(buffer1, sizeof(buffer1), "d%d.%d", i + diff, i + diff);
68 snprintf(buffer2, sizeof(buffer2), "d%d.%d", i, i);
69 pixt1 = pixMorphSequence(pixsd, buffer1, 0);
70 pixt2 = pixMorphCompSequence(pixsd, buffer2, 0);
71 pixEqual(pixt1, pixt2, &same);
72 if (i < 64) {
73 pixt3 = pixMorphCompSequenceDwa(pixsd, buffer2, 0);
74 pixEqual(pixt1, pixt3, &same2);
75 } else {
76 pixt3 = NULL;
77 same2 = TRUE;
78 }
79 if (same && same2)
80 writeResult(buffer1, 1);
81 else {
82 writeResult(buffer1, 0);
83 success = FALSE;
84 }
85 pixDestroy(&pixt1);
86 pixDestroy(&pixt2);
87 pixDestroy(&pixt3);
88
89 /* ... erosion */
90 snprintf(buffer1, sizeof(buffer1), "e%d.%d", i + diff, i + diff);
91 snprintf(buffer2, sizeof(buffer2), "e%d.%d", i, i);
92 pixt1 = pixMorphSequence(pixsd, buffer1, 0);
93 pixt2 = pixMorphCompSequence(pixsd, buffer2, 0);
94 pixEqual(pixt1, pixt2, &same);
95 if (i < 64) {
96 pixt3 = pixMorphCompSequenceDwa(pixsd, buffer2, 0);
97 pixEqual(pixt1, pixt3, &same2);
98 } else {
99 pixt3 = NULL;
100 same2 = TRUE;
101 }
102 if (same && same2)
103 writeResult(buffer1, 1);
104 else {
105 writeResult(buffer1, 0);
106 success = FALSE;
107 }
108 pixDestroy(&pixt1);
109 pixDestroy(&pixt2);
110 pixDestroy(&pixt3);
111
112 /* ... opening */
113 snprintf(buffer1, sizeof(buffer1), "o%d.%d", i + diff, i + diff);
114 snprintf(buffer2, sizeof(buffer2), "o%d.%d", i, i);
115 pixt1 = pixMorphSequence(pixsd, buffer1, 0);
116 pixt2 = pixMorphCompSequence(pixsd, buffer2, 0);
117 pixEqual(pixt1, pixt2, &same);
118 if (i < 64) {
119 pixt3 = pixMorphCompSequenceDwa(pixsd, buffer2, 0);
120 pixEqual(pixt1, pixt3, &same2);
121 } else {
122 pixt3 = NULL;
123 same2 = TRUE;
124 }
125 if (same && same2)
126 writeResult(buffer1, 1);
127 else {
128 writeResult(buffer1, 0);
129 success = FALSE;
130 }
131 pixDestroy(&pixt1);
132 pixDestroy(&pixt2);
133 pixDestroy(&pixt3);
134
135 /* ... closing */
136 snprintf(buffer1, sizeof(buffer1), "c%d.%d", i + diff, i + diff);
137 snprintf(buffer2, sizeof(buffer2), "c%d.%d", i, i);
138 pixt1 = pixMorphSequence(pixsd, buffer1, 0);
139 pixt2 = pixMorphCompSequence(pixsd, buffer2, 0);
140 pixEqual(pixt1, pixt2, &same);
141 if (i < 64) {
142 pixt3 = pixMorphCompSequenceDwa(pixsd, buffer2, 0);
143 pixEqual(pixt1, pixt3, &same2);
144 } else {
145 pixt3 = NULL;
146 same2 = TRUE;
147 }
148 if (same && same2)
149 writeResult(buffer1, 1);
150 else {
151 writeResult(buffer1, 0);
152 success = FALSE;
153 }
154 pixDestroy(&pixt1);
155 pixDestroy(&pixt2);
156 pixDestroy(&pixt3);
157
158 }
159 pixDestroy(&pixs);
160 pixDestroy(&pixsd);
161
162 if (success)
163 fprintf(stderr, "\n---------- Success: no errors ----------\n");
164 else
165 fprintf(stderr, "\n---------- Failure: error(s) found -----------\n");
166 return 0;
167 }
168
169
writeResult(char * sequence,l_int32 same)170 static void writeResult(char *sequence,
171 l_int32 same)
172 {
173 if (same)
174 fprintf(stderr, "Sequence %s: SUCCESS\n", sequence);
175 else
176 fprintf(stderr, "Sequence %s: FAILURE\n", sequence);
177 }
178
179
180 #if 0
181 for (i = 1; i < 400; i++) {
182 selectComposableSizes(i, &factor1, &factor2);
183 diff = factor1 * factor2 - i;
184 fprintf(stderr, "%d: (%d, %d): %d\n",
185 i, factor1, factor2, diff);
186 selectComposableSels(i, L_HORIZ, &sel1, &sel2);
187 selDestroy(&sel1);
188 selDestroy(&sel2);
189 }
190 #endif
191
192 #if 0
193 selectComposableSels(68, L_HORIZ, &sel1, &sel2); /* 17, 4 */
194 str = selPrintToString(sel2);
195 fprintf(stderr, str);
196 selDestroy(&sel1);
197 selDestroy(&sel2);
198 lept_free(str);
199 selectComposableSels(70, L_HORIZ, &sel1, &sel2); /* 10, 7 */
200 str = selPrintToString(sel2);
201 selDestroy(&sel1);
202 selDestroy(&sel2);
203 fprintf(stderr, str);
204 lept_free(str);
205 selectComposableSels(85, L_HORIZ, &sel1, &sel2); /* 17, 5 */
206 str = selPrintToString(sel2);
207 selDestroy(&sel1);
208 selDestroy(&sel2);
209 fprintf(stderr, str);
210 lept_free(str);
211 selectComposableSels(96, L_HORIZ, &sel1, &sel2); /* 12, 8 */
212 str = selPrintToString(sel2);
213 selDestroy(&sel1);
214 selDestroy(&sel2);
215 fprintf(stderr, str);
216 lept_free(str);
217
218 { SELA *sela;
219 sela = selaAddBasic(NULL);
220 selaWrite("/tmp/junksela.sela", sela);
221 selaDestroy(&sela);
222 }
223 #endif
224