summaryrefslogtreecommitdiff
blob: 2e01b8f67006b653092adc732651123b6d4e775e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*====================================================================*
 -  Copyright (C) 2001 Leptonica.  All rights reserved.
 -
 -  Redistribution and use in source and binary forms, with or without
 -  modification, are permitted provided that the following conditions
 -  are met:
 -  1. Redistributions of source code must retain the above copyright
 -     notice, this list of conditions and the following disclaimer.
 -  2. Redistributions in binary form must reproduce the above
 -     copyright notice, this list of conditions and the following
 -     disclaimer in the documentation and/or other materials
 -     provided with the distribution.
 -
 -  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 -  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 -  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 -  A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ANY
 -  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 -  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 -  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 -  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 -  OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 -  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 -  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *====================================================================*/

/*
 * ccbord_reg.c
 *
 *      Regression test for border-following representations of binary images.
 *      This uses the steps in ccbordtest.c to test specified images.
 */

#ifdef HAVE_CONFIG_H
#include <config_auto.h>
#endif  /* HAVE_CONFIG_H */

#include <string.h>
#include "allheaders.h"

void RunCCBordTest(const char *fname, L_REGPARAMS *rp);

int main(int    argc,
         char **argv)
{
L_REGPARAMS  *rp;

    if (regTestSetup(argc, argv, &rp))
        return 1;
    lept_mkdir("lept/ccbord");

    RunCCBordTest("feyn-fract.tif", rp);
    RunCCBordTest("dreyfus1.png", rp);
    return regTestCleanup(rp);
}

/* ----------------------------------------------- */
void RunCCBordTest(const char   *fname,
                   L_REGPARAMS  *rp)
{
char     *svgstr;
l_int32   count, disp;
CCBORDA  *ccba, *ccba2;
PIX      *pixs, *pixd, *pixd2, *pixd3;
PIX      *pixt, *pixc, *pixc2;

    pixs = pixRead(fname);
    disp = rp->display;

    /*------------------------------------------------------------------*
     *        Get border representation and verify border pixels        *
     *------------------------------------------------------------------*/
    if(disp) lept_stderr("Get border representation...");
    ccba = pixGetAllCCBorders(pixs);

        /* Get global locs directly and display borders */
    if (disp) lept_stderr("Convert from local to global locs...");
    ccbaGenerateGlobalLocs(ccba);
    if (disp) lept_stderr("display border representation...");
    pixd = ccbaDisplayBorder(ccba);
    regTestWritePixAndCheck(rp, pixd, IFF_PNG);  /* 0,7 */
    pixDisplayWithTitle(pixd, 0, 0, NULL, rp->display);
    pixDestroy(&pixd);

        /* Get step chain code, then global coords, and display borders */
    if (disp) lept_stderr("get step chain code...");
    ccbaGenerateStepChains(ccba);
    if (disp) lept_stderr("convert from step chain to global locs...");
    ccbaStepChainsToPixCoords(ccba, CCB_GLOBAL_COORDS);
    if (disp) lept_stderr("display border representation\n");
    pixd = ccbaDisplayBorder(ccba);
    regTestWritePixAndCheck(rp, pixd, IFF_PNG);  /* 1,8 */
    pixDisplayWithTitle(pixd, 200, 0, NULL, rp->display);

        /* Check if border pixels are in original set */
    lept_stderr("Check if border pixels are in original set\n");
    pixt = pixSubtract(NULL, pixd, pixs);
    pixCountPixels(pixt, &count, NULL);
    if (count == 0)
        lept_stderr(" ==> all border pixels are in original set\n");
    else
        lept_stderr(" ==> %d border pixels are not in original set\n", count);
    pixDestroy(&pixt);

        /* Display image */
    lept_stderr("Reconstruct image\n");
    pixc = ccbaDisplayImage2(ccba);
    regTestWritePixAndCheck(rp, pixc, IFF_PNG);  /* 2,9 */
    pixDisplayWithTitle(pixc, 400, 0, NULL, rp->display);

        /* Check with original to see if correct */
    pixXor(pixc, pixc, pixs);
    pixCountPixels(pixc, &count, NULL);
    if (count == 0)
        lept_stderr(" ==> perfect direct reconstruction\n");
    else {
        l_int32  w, h, i, j;
        l_uint32 val;
        lept_stderr(" ==> %d pixels in error in reconstruction\n", count);
#if 1
        w = pixGetWidth(pixc);
        h = pixGetHeight(pixc);
        for (i = 0; i < h; i++) {
            for (j = 0; j < w; j++) {
                pixGetPixel(pixc, j, i, &val);
                if (val == 1)
                    lept_stderr("bad pixel at (%d, %d)\n", j, i);
            }
        }
        pixWrite("/tmp/lept/ccbord/badpixels1.png", pixc, IFF_PNG);
#endif
    }

    /*----------------------------------------------------------*
     *        Write to file (compressed) and read back          *
     *----------------------------------------------------------*/
    if (disp) lept_stderr("Write serialized step data...");
    ccbaWrite("/tmp/lept/ccbord/stepdata.ccb", ccba);
    if (disp) lept_stderr("read serialized step data...");
    ccba2 = ccbaRead("/tmp/lept/ccbord/stepdata.ccb");

        /* Display the border pixels again */
    if (disp) lept_stderr("convert from step chain to global locs...");
    ccbaStepChainsToPixCoords(ccba2, CCB_GLOBAL_COORDS);
    if (disp) lept_stderr("display border representation\n");
    pixd2 = ccbaDisplayBorder(ccba2);
    regTestWritePixAndCheck(rp, pixd2, IFF_PNG);  /* 3,10 */
    pixDisplayWithTitle(pixd2, 600, 0, NULL, rp->display);

        /* Check if border pixels are same as first time */
    lept_stderr("Check border after write/read\n");
    pixXor(pixd2, pixd2, pixd);
    pixCountPixels(pixd2, &count, NULL);
    if (count == 0)
        lept_stderr(" ==> perfect w/r border recon\n");
    else
        lept_stderr(" ==> %d pixels in error in w/r recon\n", count);
    pixDestroy(&pixd2);

        /* Display image again */
    if (disp) lept_stderr("Convert from step chain to local coords...\n");
    ccbaStepChainsToPixCoords(ccba2, CCB_LOCAL_COORDS);
    lept_stderr("Reconstruct image from file\n");
    pixc2 = ccbaDisplayImage2(ccba2);
    regTestWritePixAndCheck(rp, pixc2, IFF_PNG);  /* 4,11 */
    pixDisplayWithTitle(pixc2, 800, 0, NULL, rp->display);

        /* Check with original to see if correct */
    pixXor(pixc2, pixc2, pixs);
    pixCountPixels(pixc2, &count, NULL);
    if (count == 0)
        lept_stderr(" ==> perfect image recon\n");
    else {
        l_int32  w, h, i, j;
        l_uint32 val;
        lept_stderr(" ==> %d pixels in error in image recon\n", count);
#if 1
        w = pixGetWidth(pixc2);
        h = pixGetHeight(pixc2);
        for (i = 0; i < h; i++) {
            for (j = 0; j < w; j++) {
                pixGetPixel(pixc2, j, i, &val);
                if (val == 1)
                    lept_stderr("bad pixel at (%d, %d)\n", j, i);
            }
        }
        pixWrite("/tmp/lept/ccbord/badpixels2.png", pixc, IFF_PNG);
#endif
    }

    /*----------------------------------------------------------*
     *     Make, display and check single path border for svg   *
     *----------------------------------------------------------*/
        /* Make local single path border for svg */
    if (disp) lept_stderr("Make local single path borders for svg ...");
    ccbaGenerateSinglePath(ccba);

        /* Generate global single path border */
    if (disp) lept_stderr("generate global single path borders ...");
    ccbaGenerateSPGlobalLocs(ccba, CCB_SAVE_TURNING_PTS);

        /* Display border pixels from single path */
    if (disp) lept_stderr("display border from single path\n");
    pixd3 = ccbaDisplaySPBorder(ccba);
    regTestWritePixAndCheck(rp, pixd3, IFF_PNG);  /* 5,12 */
    pixDisplayWithTitle(pixd3, 1000, 0, NULL, rp->display);

        /* Check if border pixels are in original set */
    lept_stderr("Check if border pixels are in original set\n");
    pixt = pixSubtract(NULL, pixd3, pixs);
    pixCountPixels(pixt, &count, NULL);
    if (count == 0)
        lept_stderr(" ==> all border pixels are in original set\n");
    else
        lept_stderr(" ==> %d border pixels are not in original set\n", count);
    lept_stderr("============================================\n");
    pixDestroy(&pixt);
    pixDestroy(&pixd3);

        /*  Output in svg file format */
    svgstr = ccbaWriteSVGString(ccba);
    regTestWriteDataAndCheck(rp, svgstr, strlen(svgstr), "ccb"); /* 6,13 */

    ccbaDestroy(&ccba2);
    ccbaDestroy(&ccba);
    pixDestroy(&pixs);
    pixDestroy(&pixd);
    pixDestroy(&pixc);
    pixDestroy(&pixc2);
    lept_free(svgstr);
}