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 * splitcomp_reg.c
29 *
30 * Regression test for splittings of a single component and for an image
31 * composed of several components, using different components and
32 * parameters. Note that:
33 * (1) All coverings must cover the fg of the mask.
34 * (2) The first set of parameters is small and generates
35 * a proper tiling, covering ONLY the mask fg.
36 * (3) The tilings generated on 90 degree rotated components
37 * are identical (rotated) to those on un-rotated components.
38 */
39
40 #include "allheaders.h"
41
main(int argc,char ** argv)42 int main(int argc,
43 char **argv)
44 {
45 l_int32 i, j, w, h;
46 l_int32 minsum[5] = { 2, 40, 50, 50, 70};
47 l_int32 skipdist[5] = { 5, 5, 10, 10, 30};
48 l_int32 delta[5] = { 2, 10, 10, 25, 40};
49 l_int32 maxbg[5] = {10, 15, 10, 20, 40};
50 BOX *box1, *box2, *box3, *box4;
51 BOXA *boxa;
52 PIX *pixs, *pixc, *pixt, *pixd, *pix32;
53 PIXA *pixas, *pixad;
54 L_REGPARAMS *rp;
55
56 if (regTestSetup(argc, argv, &rp))
57 return 1;
58
59 /* Generate and save 1 bpp masks */
60 pixas = pixaCreate(0);
61 pixs = pixCreate(300, 250, 1);
62 pixSetAll(pixs);
63 box1 = boxCreate(50, 0, 140, 25);
64 box2 = boxCreate(120, 100, 100, 25);
65 box3 = boxCreate(75, 170, 80, 20);
66 box4 = boxCreate(150, 80, 25, 70);
67
68 pixClearInRect(pixs, box1);
69 pixaAddPix(pixas, pixs, L_COPY);
70 pixt = pixRotateOrth(pixs, 1);
71 pixaAddPix(pixas, pixt, L_INSERT);
72
73 pixClearInRect(pixs, box2);
74 pixaAddPix(pixas, pixs, L_COPY);
75 pixt = pixRotateOrth(pixs, 1);
76 pixaAddPix(pixas, pixt, L_INSERT);
77
78 pixClearInRect(pixs, box3);
79 pixaAddPix(pixas, pixs, L_COPY);
80 pixt = pixRotateOrth(pixs, 1);
81 pixaAddPix(pixas, pixt, L_INSERT);
82
83 pixClearInRect(pixs, box4);
84 pixaAddPix(pixas, pixs, L_COPY);
85 pixt = pixRotateOrth(pixs, 1);
86 pixaAddPix(pixas, pixt, L_INSERT);
87
88 boxDestroy(&box1);
89 boxDestroy(&box2);
90 boxDestroy(&box3);
91 boxDestroy(&box4);
92 pixDestroy(&pixs);
93
94 /* Do 5 splittings on each of the 8 masks */
95 pixad = pixaCreate(0);
96 for (j = 0; j < 8; j++) {
97 pixt = pixaGetPix(pixas, j, L_CLONE);
98 pixGetDimensions(pixt, &w, &h, NULL);
99 pix32 = pixCreate(w, h, 32);
100 pixSetAll(pix32);
101 pixPaintThroughMask(pix32, pixt, 0, 0, 0xc0c0c000);
102 pixSaveTiled(pix32, pixad, 1.0, 1, 30, 32);
103 for (i = 0; i < 5; i++) {
104 pixc = pixCopy(NULL, pix32);
105 boxa = pixSplitComponentIntoBoxa(pixt, NULL, minsum[i], skipdist[i],
106 delta[i], maxbg[i], 0, 1);
107 /* boxaWriteStream(stderr, boxa); */
108 pixd = pixBlendBoxaRandom(pixc, boxa, 0.4);
109 pixRenderBoxaArb(pixd, boxa, 2, 255, 0, 0);
110 pixSaveTiled(pixd, pixad, 1.0, 0, 30, 32);
111 pixDestroy(&pixd);
112 pixDestroy(&pixc);
113 boxaDestroy(&boxa);
114 }
115 pixDestroy(&pixt);
116 pixDestroy(&pix32);
117 }
118
119 /* Display results */
120 pixd = pixaDisplay(pixad, 0, 0);
121 regTestWritePixAndCheck(rp, pixd, IFF_PNG); /* 0 */
122 pixDisplayWithTitle(pixd, 100, 100, NULL, rp->display);
123 pixDestroy(&pixd);
124 pixaDestroy(&pixad);
125
126 /* Put the 8 masks all together, and split 5 ways */
127 pixad = pixaCreate(0);
128 pixs = pixaDisplayOnLattice(pixas, 325, 325, NULL, NULL);
129 pixGetDimensions(pixs, &w, &h, NULL);
130 pix32 = pixCreate(w, h, 32);
131 pixSetAll(pix32);
132 pixPaintThroughMask(pix32, pixs, 0, 0, 0xc0c0c000);
133 pixSaveTiled(pix32, pixad, 1.0, 1, 30, 32);
134 for (i = 0; i < 5; i++) {
135 pixc = pixCopy(NULL, pix32);
136 boxa = pixSplitIntoBoxa(pixs, minsum[i], skipdist[i],
137 delta[i], maxbg[i], 0, 1);
138 /* boxaWriteStream(stderr, boxa); */
139 pixd = pixBlendBoxaRandom(pixc, boxa, 0.4);
140 pixRenderBoxaArb(pixd, boxa, 2, 255, 0, 0);
141 pixSaveTiled(pixd, pixad, 1.0, 0, 30, 32);
142 pixDestroy(&pixd);
143 pixDestroy(&pixc);
144 boxaDestroy(&boxa);
145 }
146 pixDestroy(&pix32);
147 pixDestroy(&pixs);
148
149 /* Display results */
150 pixd = pixaDisplay(pixad, 0, 0);
151 regTestWritePixAndCheck(rp, pixd, IFF_PNG); /* 1 */
152 pixDisplayWithTitle(pixd, 600, 100, NULL, rp->display);
153 pixDestroy(&pixd);
154 pixaDestroy(&pixad);
155
156 pixaDestroy(&pixas);
157 return regTestCleanup(rp);
158 }
159