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 * digitprep1.c
29 *
30 * Extract barcode digits and put in a pixaa (a resource file for
31 * readnum.c).
32 */
33
34 #include "allheaders.h"
35
36 static const l_int32 HEIGHT = 32; /* pixels */
37
main(int argc,char ** argv)38 int main(int argc,
39 char **argv)
40 {
41 char buf[8];
42 l_int32 i, n, h;
43 l_float32 scalefact;
44 BOXA *boxa;
45 PIX *pixs, *pixt1, *pixt2;
46 PIXA *pixa, *pixas, *pixad;
47 PIXAA *pixaa;
48 static char mainName[] = "digitprep1";
49
50 if (argc != 1) {
51 ERROR_INT(" Syntax: digitprep1", mainName, 1);
52 return 1;
53 }
54
55 setLeptDebugOK(1);
56 if ((pixs = pixRead("barcode-digits.png")) == NULL)
57 return ERROR_INT("pixs not read", mainName, 1);
58
59 /* Extract the digits and scale to HEIGHT */
60 boxa = pixConnComp(pixs, &pixa, 8);
61 pixas = pixaSort(pixa, L_SORT_BY_X, L_SORT_INCREASING, NULL, L_CLONE);
62 n = pixaGetCount(pixas);
63
64 /* Move the last ("0") to the first position */
65 pixt1 = pixaGetPix(pixas, n - 1, L_CLONE);
66 pixaInsertPix(pixas, 0, pixt1, NULL);
67 pixaRemovePix(pixas, n);
68
69 /* Make the output scaled pixa */
70 pixad = pixaCreate(n);
71 for (i = 0; i < n; i++) {
72 pixt1 = pixaGetPix(pixas, i, L_CLONE);
73 pixGetDimensions(pixt1, NULL, &h, NULL);
74 scalefact = HEIGHT / (l_float32)h;
75 pixt2 = pixScale(pixt1, scalefact, scalefact);
76 if (pixGetHeight(pixt2) != 32)
77 return ERROR_INT("height not 32!", mainName, 1);
78 snprintf(buf, sizeof(buf), "%d", i);
79 pixSetText(pixt2, buf);
80 pixaAddPix(pixad, pixt2, L_INSERT);
81 pixDestroy(&pixt1);
82 }
83
84 /* Save in a pixaa, with 1 pix in each pixa */
85 pixaa = pixaaCreateFromPixa(pixad, 1, L_CHOOSE_CONSECUTIVE, L_CLONE);
86 pixaaWrite("junkdigits.pixaa", pixaa);
87
88 /* Show result */
89 pixt1 = pixaaDisplayByPixa(pixaa, 20, 20, 1000);
90 pixDisplay(pixt1, 100, 100);
91 pixDestroy(&pixt1);
92
93 boxaDestroy(&boxa);
94 pixaDestroy(&pixa);
95 pixaDestroy(&pixas);
96 pixaDestroy(&pixad);
97 pixaaDestroy(&pixaa);
98 return 0;
99 }
100
101
102