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 * conncomp_reg.c
29 *
30 * Regression test for connected components (both 4 and 8
31 * connected), including regeneration of the original
32 * image from the components. This is also an implicit
33 * test of rasterop.
34 */
35
36 #include "allheaders.h"
37
main(int argc,char ** argv)38 int main(int argc,
39 char **argv)
40 {
41 l_uint8 *array1, *array2;
42 l_int32 n1, n2, n3;
43 size_t size1, size2;
44 FILE *fp;
45 BOXA *boxa1, *boxa2;
46 PIX *pixs, *pix1;
47 PIXA *pixa1;
48 PIXCMAP *cmap;
49 L_REGPARAMS *rp;
50
51 if (regTestSetup(argc, argv, &rp))
52 return 1;
53
54 pixs = pixRead("feyn.tif");
55
56 /* --------------------------------------------------------------- *
57 * Test pixConnComp() and pixCountConnComp(), *
58 * with output to both boxa and pixa *
59 * --------------------------------------------------------------- */
60 /* First, test with 4-cc */
61 boxa1= pixConnComp(pixs, &pixa1, 4);
62 n1 = boxaGetCount(boxa1);
63 boxa2= pixConnComp(pixs, NULL, 4);
64 n2 = boxaGetCount(boxa2);
65 pixCountConnComp(pixs, 4, &n3);
66 fprintf(stderr, "Number of 4 c.c.: n1 = %d; n2 = %d, n3 = %d\n",
67 n1, n2, n3);
68 regTestCompareValues(rp, n1, n2, 0); /* 0 */
69 regTestCompareValues(rp, n1, n3, 0); /* 1 */
70 regTestCompareValues(rp, n1, 4452, 0); /* 2 */
71 pix1 = pixaDisplay(pixa1, pixGetWidth(pixs), pixGetHeight(pixs));
72 regTestWritePixAndCheck(rp, pix1, IFF_PNG); /* 3 */
73 regTestComparePix(rp, pixs, pix1); /* 4 */
74 pixaDestroy(&pixa1);
75 boxaDestroy(&boxa1);
76 boxaDestroy(&boxa2);
77 pixDestroy(&pix1);
78
79 /* Test with 8-cc */
80 boxa1= pixConnComp(pixs, &pixa1, 8);
81 n1 = boxaGetCount(boxa1);
82 boxa2= pixConnComp(pixs, NULL, 8);
83 n2 = boxaGetCount(boxa2);
84 pixCountConnComp(pixs, 8, &n3);
85 fprintf(stderr, "Number of 8 c.c.: n1 = %d; n2 = %d, n3 = %d\n",
86 n1, n2, n3);
87 regTestCompareValues(rp, n1, n2, 0); /* 5 */
88 regTestCompareValues(rp, n1, n3, 0); /* 6 */
89 regTestCompareValues(rp, n1, 4305, 0); /* 7 */
90 pix1 = pixaDisplay(pixa1, pixGetWidth(pixs), pixGetHeight(pixs));
91 regTestWritePixAndCheck(rp, pix1, IFF_PNG); /* 8 */
92 regTestComparePix(rp, pixs, pix1); /* 9 */
93 pixaDestroy(&pixa1);
94 boxaDestroy(&boxa1);
95 boxaDestroy(&boxa2);
96 pixDestroy(&pix1);
97
98
99 /* --------------------------------------------------------------- *
100 * Test boxa I/O *
101 * --------------------------------------------------------------- */
102 lept_mkdir("lept/conn");
103 boxa1 = pixConnComp(pixs, NULL, 4);
104 fp = lept_fopen("/tmp/lept/conn/boxa1.ba", "wb+");
105 boxaWriteStream(fp, boxa1);
106 lept_fclose(fp);
107 fp = lept_fopen("/tmp/lept/conn/boxa1.ba", "rb");
108 boxa2 = boxaReadStream(fp);
109 lept_fclose(fp);
110 fp = lept_fopen("/tmp/lept/conn/boxa2.ba", "wb+");
111 boxaWriteStream(fp, boxa2);
112 lept_fclose(fp);
113 array1 = l_binaryRead("/tmp/lept/conn/boxa1.ba", &size1);
114 array2 = l_binaryRead("/tmp/lept/conn/boxa2.ba", &size2);
115 regTestCompareStrings(rp, array1, size1, array2, size2); /* 10 */
116 lept_free(array1);
117 lept_free(array2);
118 boxaDestroy(&boxa1);
119 boxaDestroy(&boxa2);
120
121
122 /* --------------------------------------------------------------- *
123 * Just for fun, display each component as a random color in *
124 * cmapped 8 bpp. Background is color 0; it is set to white. *
125 * --------------------------------------------------------------- */
126 boxa1 = pixConnComp(pixs, &pixa1, 4);
127 pix1 = pixaDisplayRandomCmap(pixa1, pixGetWidth(pixs), pixGetHeight(pixs));
128 cmap = pixGetColormap(pix1);
129 pixcmapResetColor(cmap, 0, 255, 255, 255); /* reset background to white */
130 regTestWritePixAndCheck(rp, pix1, IFF_PNG); /* 11 */
131 if (rp->display) pixDisplay(pix1, 100, 100);
132 boxaDestroy(&boxa1);
133 pixDestroy(&pix1);
134 pixaDestroy(&pixa1);
135
136 pixDestroy(&pixs);
137 return regTestCleanup(rp);
138 }
139
140
141