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  * translate_reg.c
29  *
30  *    Regression test for in-place translation
31  */
32 
33 #include "allheaders.h"
34 
35 #define   BINARY_IMAGE        "test1.png"
36 #define   GRAYSCALE_IMAGE     "test8.jpg"
37 #define   FOUR_BPP_IMAGE      "weasel4.8g.png"
38 #define   COLORMAP_IMAGE      "dreyfus8.png"
39 #define   RGB_IMAGE           "marge.jpg"
40 
41 void TranslateAndSave1(PIXA *pixa, l_int32 depth, PIX *pix,
42                        l_int32 xshift, l_int32 yshift);
43 
44 void TranslateAndSave2(PIXA *pixa, PIX *pix, l_int32 xshift, l_int32 yshift);
45 
46 
main(int argc,char ** argv)47 int main(int    argc,
48          char **argv)
49 {
50 BOX          *box;
51 PIX          *pixs, *pixd;
52 PIX          *pix1, *pix2, *pix3, *pix4, *pix5, *pix6, *pix7;
53 PIXA         *pixa;
54 L_REGPARAMS  *rp;
55 
56     if (regTestSetup(argc, argv, &rp))
57         return 1;
58 
59         /* Set up images */
60     pix1 = pixRead("weasel2.4c.png");
61     pix2 = pixScaleBySampling(pix1, 3.0, 3.0);
62     box = boxCreate(0, 0, 209, 214);
63     pixs = pixClipRectangle(pix2, box, NULL);
64     pixDestroy(&pix1);
65     pixDestroy(&pix2);
66     boxDestroy(&box);
67     pix1 = pixRemoveColormap(pixs, REMOVE_CMAP_TO_GRAYSCALE);
68     pix2 = pixRemoveColormap(pixs, REMOVE_CMAP_TO_FULL_COLOR);
69     pix3 = pixConvertTo1(pixs, 128);
70     pix4 = pixRotateAM(pix1, 0.25, L_BRING_IN_BLACK);
71     pix5 = pixRotateAM(pix1, -0.25, L_BRING_IN_WHITE);
72     pix6 = pixRotateAM(pix2, -0.15, L_BRING_IN_BLACK);
73     pix7 = pixRotateAM(pix2, +0.15, L_BRING_IN_WHITE);
74 
75     pixa = pixaCreate(0);
76     TranslateAndSave1(pixa, 32, pixs, 30, 30);
77     TranslateAndSave1(pixa, 32, pix1, 35, 20);
78     TranslateAndSave1(pixa, 32, pix2, 20, 35);
79     TranslateAndSave1(pixa, 32, pix3, 20, 35);
80     pixd = pixaDisplayOnColor(pixa, 0, 0, 0x44aaaa00);
81     regTestWritePixAndCheck(rp, pixd, IFF_PNG);  /* 0 */
82     pixDisplayWithTitle(pixd, 0, 0, "trans0", rp->display);
83     pixDestroy(&pixd);
84     pixaDestroy(&pixa);
85 
86     pixa = pixaCreate(0);
87     TranslateAndSave1(pixa, 8, pix1, 35, 20);
88     TranslateAndSave1(pixa, 8, pix4, 35, 20);
89     pixd = pixaDisplayOnColor(pixa, 0, 0, 0x44);
90     regTestWritePixAndCheck(rp, pixd, IFF_PNG);  /* 1 */
91     pixDisplayWithTitle(pixd, 250, 0, "trans1", rp->display);
92     pixDestroy(&pixd);
93     pixaDestroy(&pixa);
94 
95     pixa = pixaCreate(0);
96     TranslateAndSave2(pixa, pixs, 30, 30);
97     TranslateAndSave2(pixa, pix1, 30, 30);
98     TranslateAndSave2(pixa, pix2, 35, 20);
99     TranslateAndSave2(pixa, pix3, 20, 35);
100     TranslateAndSave2(pixa, pix4, 25, 25);
101     TranslateAndSave2(pixa, pix5, 25, 25);
102     TranslateAndSave2(pixa, pix6, 25, 25);
103     TranslateAndSave2(pixa, pix7, 25, 25);
104     pixd = pixaDisplayTiledInRows(pixa, 32, 1200, 1.0, 0, 30, 3);
105     regTestWritePixAndCheck(rp, pixd, IFF_PNG);  /* 2 */
106     pixDisplayWithTitle(pixd, 500, 0, "trans2", rp->display);
107     pixDestroy(&pixd);
108     pixaDestroy(&pixa);
109 
110     pixDestroy(&pixs);
111     pixDestroy(&pix1);
112     pixDestroy(&pix2);
113     pixDestroy(&pix3);
114     pixDestroy(&pix4);
115     pixDestroy(&pix5);
116     pixDestroy(&pix6);
117     pixDestroy(&pix7);
118     return regTestCleanup(rp);
119 }
120 
121 
122 void
TranslateAndSave1(PIXA * pixa,l_int32 depth,PIX * pix,l_int32 xshift,l_int32 yshift)123 TranslateAndSave1(PIXA    *pixa,
124                   l_int32  depth,
125                   PIX     *pix,
126                   l_int32  xshift,
127                   l_int32  yshift)
128 {
129 PIX  *pix1, *pix2, *pix3, *pix4;
130 
131     pix1 = pixTranslate(NULL, pix, xshift, yshift, L_BRING_IN_WHITE);
132     pix2 = pixTranslate(NULL, pix, xshift, yshift, L_BRING_IN_BLACK);
133     pix3 = pixTranslate(NULL, pix, -xshift, -yshift, L_BRING_IN_WHITE);
134     pix4 = pixTranslate(NULL, pix, -xshift, -yshift, L_BRING_IN_BLACK);
135     pixSaveTiled(pix1, pixa, 1.0, 1, 25, depth);
136     pixSaveTiled(pix2, pixa, 1.0, 0, 25, depth);
137     pixSaveTiled(pix3, pixa, 1.0, 0, 25, depth);
138     pixSaveTiled(pix4, pixa, 1.0, 0, 25, depth);
139     pixDestroy(&pix1);
140     pixDestroy(&pix2);
141     pixDestroy(&pix3);
142     pixDestroy(&pix4);
143     return;
144 }
145 
146 void
TranslateAndSave2(PIXA * pixa,PIX * pix,l_int32 xshift,l_int32 yshift)147 TranslateAndSave2(PIXA    *pixa,
148                   PIX     *pix,
149                   l_int32  xshift,
150                   l_int32  yshift)
151 {
152 PIX  *pix1, *pix2, *pix3, *pix4;
153 
154     pix1 = pixTranslate(NULL, pix, xshift, yshift, L_BRING_IN_WHITE);
155     pix2 = pixTranslate(NULL, pix, xshift, yshift, L_BRING_IN_BLACK);
156     pix3 = pixTranslate(NULL, pix, -xshift, -yshift, L_BRING_IN_WHITE);
157     pix4 = pixTranslate(NULL, pix, -xshift, -yshift, L_BRING_IN_BLACK);
158     pixaAddPix(pixa, pix1, L_INSERT);
159     pixaAddPix(pixa, pix2, L_INSERT);
160     pixaAddPix(pixa, pix3, L_INSERT);
161     pixaAddPix(pixa, pix4, L_INSERT);
162     return;
163 }
164