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  * blackwhite_reg.c
29  *
30  *   Tests functions that handle black and white pixels in an image.
31  */
32 
33 #include "allheaders.h"
34 
35 const char *fnames[11] = {"test1.png", "speckle2.png", "weasel2.4g.png",
36                           "speckle4.png", "weasel4.11c.png",
37                           "dreyfus8.png", "weasel8.240c.png",
38                           "test16.tif", "marge.jpg",
39                           "test-cmap-alpha.png", "test-gray-alpha.png"};
40 const l_int32  setsize = 11;
41 
42 
main(int argc,char ** argv)43 int main(int    argc,
44          char **argv)
45 {
46 l_int32       i, spp;
47 l_uint32      bval, wval;
48 PIX          *pixs, *pix1, *pix2, *pix3, *pixd;
49 PIXA         *pixa;
50 L_REGPARAMS  *rp;
51 
52     if (regTestSetup(argc, argv, &rp))
53         return 1;
54 
55         /* Scale each image and add a white boundary */
56     pixa = pixaCreate(setsize);
57     for (i = 0; i < setsize; i++) {
58         pixs = pixRead(fnames[i]);
59         spp = pixGetSpp(pixs);
60         pixGetBlackOrWhiteVal(pixs, L_GET_WHITE_VAL, &wval);
61         pixGetBlackOrWhiteVal(pixs, L_GET_BLACK_VAL, &bval);
62         fprintf(stderr, "d = %d, spp = %d, bval = %x, wval = %x\n",
63                 pixGetDepth(pixs), spp, bval, wval);
64         if (spp == 4)  /* remove alpha, using white background */
65             pix1 = pixAlphaBlendUniform(pixs, wval);
66         else
67             pix1 = pixClone(pixs);
68         pix2 = pixScaleToSize(pix1, 150, 150);
69         pixGetBlackOrWhiteVal(pix2, L_GET_WHITE_VAL, &wval);
70         pix3 = pixAddBorderGeneral(pix2, 30, 30, 20, 20, wval);
71         pixaAddPix(pixa, pix3, L_INSERT);
72         pixDestroy(&pixs);
73         pixDestroy(&pix1);
74         pixDestroy(&pix2);
75     }
76     pixd = pixaDisplayTiledInRows(pixa, 32, 1200, 1.0, 1, 30, 0);
77     regTestWritePixAndCheck(rp, pixd, IFF_PNG);
78     pixDisplayWithTitle(pixd, 0, 100, NULL, rp->display);
79     pixDestroy(&pixd);
80     pixaDestroy(&pixa);
81 
82         /* Scale each image and add a black boundary */
83     pixa = pixaCreate(setsize);
84     for (i = 0; i < setsize; i++) {
85         pixs = pixRead(fnames[i]);
86         spp = pixGetSpp(pixs);
87         pixGetBlackOrWhiteVal(pixs, L_GET_WHITE_VAL, &wval);
88         pixGetBlackOrWhiteVal(pixs, L_GET_BLACK_VAL, &bval);
89         fprintf(stderr, "d = %d, spp = %d, bval = %x, wval = %x\n",
90                 pixGetDepth(pixs), spp, bval, wval);
91         if (spp == 4)  /* remove alpha, using white background */
92             pix1 = pixAlphaBlendUniform(pixs, wval);
93         else
94             pix1 = pixClone(pixs);
95         pix2 = pixScaleToSize(pix1, 150, 150);
96         pixGetBlackOrWhiteVal(pixs, L_GET_BLACK_VAL, &bval);
97         pix3 = pixAddBorderGeneral(pix2, 30, 30, 20, 20, bval);
98         pixaAddPix(pixa, pix3, L_INSERT);
99         pixDestroy(&pixs);
100         pixDestroy(&pix1);
101         pixDestroy(&pix2);
102     }
103     pixd = pixaDisplayTiledInRows(pixa, 32, 1200, 1.0, 0, 30, 0);
104     regTestWritePixAndCheck(rp, pixd, IFF_PNG);
105     pixDisplayWithTitle(pixd, 1000, 100, NULL, rp->display);
106     pixDestroy(&pixd);
107     pixaDestroy(&pixa);
108 
109     return regTestCleanup(rp);
110 }
111