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  *  insert_reg.c
29  *
30  *  This tests removal and insertion operations in numa, boxa and pixa.
31  */
32 
33 #include <math.h>
34 #include "allheaders.h"
35 
main(int argc,char ** argv)36 int main(int    argc,
37          char **argv)
38 {
39 l_int32       i, n;
40 l_float32     pi, angle, val;
41 BOX          *box;
42 BOXA         *boxa, *boxa1, *boxa2;
43 NUMA         *na1, *na2;
44 PIX          *pix, *pix1, *pix2;
45 PIXA         *pixa1, *pixa2, *pixa3, *pixa4;
46 L_REGPARAMS  *rp;
47 
48     if (regTestSetup(argc, argv, &rp))
49         return 1;
50 
51     lept_rmfile("/tmp/lept/regout/insert3.ba");
52     lept_rmfile("/tmp/lept/regout/insert4.ba");
53     lept_rmfile("/tmp/lept/regout/insert6.pa");
54     lept_rmfile("/tmp/lept/regout/insert7.pa");
55     lept_rmfile("/tmp/lept/regout/insert9.pa");
56     lept_rmfile("/tmp/lept/regout/insert10.pa");
57 
58     /* ----------------- Test numa operations -------------------- */
59     pi = 3.1415926535;
60     na1 = numaCreate(500);
61     for (i = 0; i < 500; i++) {
62         angle = 0.02293 * i * pi;
63         val = (l_float32)sin(angle);
64         numaAddNumber(na1, val);
65     }
66     numaWrite("/tmp/lept/regout/insert0.na", na1);
67     na2 = numaCopy(na1);
68     n = numaGetCount(na2);
69     for (i = 0; i < n; i++) {
70         numaGetFValue(na2, i, &val);
71         numaRemoveNumber(na2, i);
72         numaInsertNumber(na2, i, val);
73     }
74     numaWrite("/tmp/lept/regout/insert1.na", na2);
75     regTestCheckFile(rp, "/tmp/lept/regout/insert0.na");  /* 0 */
76     regTestCheckFile(rp, "/tmp/lept/regout/insert1.na");  /* 1 */
77     regTestCompareFiles(rp, 0, 1);  /* 2 */
78     numaDestroy(&na1);
79     numaDestroy(&na2);
80 
81     /* ----------------- Test boxa operations -------------------- */
82     pix1 = pixRead("feyn.tif");
83     box = boxCreate(1138, 1666, 1070, 380);
84     pix2 = pixClipRectangle(pix1, box, NULL);
85     boxDestroy(&box);
86     boxa1 = pixConnComp(pix2, NULL, 8);
87     boxaWrite("/tmp/lept/regout/insert3.ba", boxa1);
88     boxa2 = boxaCopy(boxa1, L_COPY);
89     n = boxaGetCount(boxa2);
90     for (i = 0; i < n; i++) {
91         boxaRemoveBoxAndSave(boxa2, i, &box);
92         boxaInsertBox(boxa2, i, box);
93     }
94     boxaWrite("/tmp/lept/regout/insert4.ba", boxa2);
95     regTestCheckFile(rp, "/tmp/lept/regout/insert3.ba");  /* 3 */
96     regTestCheckFile(rp, "/tmp/lept/regout/insert4.ba");  /* 4 */
97     regTestCompareFiles(rp, 3, 4);  /* 5 */
98     pixDestroy(&pix1);
99     pixDestroy(&pix2);
100     boxaDestroy(&boxa1);
101     boxaDestroy(&boxa2);
102 
103     /* ----------------- Test pixa operations -------------------- */
104     pix1 = pixRead("feyn.tif");
105     box = boxCreate(1138, 1666, 1070, 380);
106     pix2 = pixClipRectangle(pix1, box, NULL);
107     boxDestroy(&box);
108     boxa = pixConnComp(pix2, &pixa1, 8);
109     boxaDestroy(&boxa);
110     pixaWrite("/tmp/lept/regout/insert6.pa", pixa1);
111     regTestCheckFile(rp, "/tmp/lept/regout/insert6.pa");  /* 6 */
112     pixDestroy(&pix1);
113     pixDestroy(&pix2);
114 
115         /* Remove and insert each one */
116     pixa2 = pixaCopy(pixa1, L_COPY);
117     n = pixaGetCount(pixa2);
118     for (i = 0; i < n; i++) {
119         pixaRemovePixAndSave(pixa2, i, &pix, &box);
120         pixaInsertPix(pixa2, i, pix, box);
121     }
122     pixaWrite("/tmp/lept/regout/insert7.pa", pixa2);
123     regTestCheckFile(rp, "/tmp/lept/regout/insert7.pa");  /* 7 */
124     regTestCompareFiles(rp, 6, 7);  /* 8 */
125 
126         /* Move the last to the beginning; do it n times */
127     pixa3 = pixaCopy(pixa2, L_COPY);
128     for (i = 0; i < n; i++) {
129         pix = pixaGetPix(pixa3, n - 1, L_CLONE);
130         box = pixaGetBox(pixa3, n - 1, L_CLONE);
131         pixaInsertPix(pixa3, 0, pix, box);
132         pixaRemovePix(pixa3, n);
133     }
134     pixaWrite("/tmp/lept/regout/insert9.pa", pixa3);
135     regTestCheckFile(rp, "/tmp/lept/regout/insert9.pa");  /* 9 */
136 
137         /* Move the first one to the end; do it n times */
138     pixa4 = pixaCopy(pixa3, L_COPY);
139     for (i = 0; i < n; i++) {
140         pix = pixaGetPix(pixa4, 0, L_CLONE);
141         box = pixaGetBox(pixa4, 0, L_CLONE);
142         pixaInsertPix(pixa4, n, pix, box);  /* make sure insert works at end */
143         pixaRemovePix(pixa4, 0);
144     }
145     pixaWrite("/tmp/lept/regout/insert10.pa", pixa4);
146     regTestCheckFile(rp, "/tmp/lept/regout/insert10.pa");  /* 10 */
147     regTestCompareFiles(rp, 9, 10);  /* 11 */
148     pixaDestroy(&pixa1);
149     pixaDestroy(&pixa2);
150     pixaDestroy(&pixa3);
151     pixaDestroy(&pixa4);
152 
153     return regTestCleanup(rp);
154 }
155