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  *   dewarptest1.c
29  *
30  *   This exercise functions in dewarp.c for dewarping based on lines
31  *   of horizontal text.  It also creates a 24-image pdf of steps
32  *   in the process.
33  */
34 
35 #include "allheaders.h"
36 
37 #define   DO_QUAD     1
38 #define   DO_CUBIC    0
39 #define   DO_QUARTIC  0
40 
main(int argc,char ** argv)41 l_int32 main(int    argc,
42              char **argv)
43 {
44 L_DEWARP   *dew1, *dew2;
45 L_DEWARPA  *dewa;
46 PIX        *pixs, *pixn, *pixg, *pixb, *pixd, *pixt1, *pixt2;
47 PIX        *pixs2, *pixn2, *pixg2, *pixb2, *pixd2;
48 
49     setLeptDebugOK(1);
50     lept_mkdir("lept/model");
51 
52 /*    pixs = pixRead("1555.007.jpg"); */
53     pixs = pixRead("cat.035.jpg");
54 /*    pixs = pixRead("cat.010.jpg"); */
55 
56         /* Normalize for varying background and binarize */
57     pixn = pixBackgroundNormSimple(pixs, NULL, NULL);
58     pixg = pixConvertRGBToGray(pixn, 0.5, 0.3, 0.2);
59     pixb = pixThresholdToBinary(pixg, 130);
60 
61         /* Run the basic functions */
62     dewa = dewarpaCreate(2, 30, 1, 10, 30);
63     dewarpaUseBothArrays(dewa, 1);
64     dew1 = dewarpCreate(pixb, 35);
65     dewarpaInsertDewarp(dewa, dew1);
66     dewarpBuildPageModel(dew1, "/tmp/lept/model/dewarp_model1.pdf");
67     dewarpaApplyDisparity(dewa, 35, pixg, 200, 0, 0, &pixd,
68                           "/tmp/lept/model/dewarp_apply1.pdf");
69 
70          /* Write out some of the files to be imaged */
71     lept_rmdir("lept/dewtest");
72     lept_mkdir("lept/dewtest");
73     pixWrite("/tmp/lept/dewtest/001.jpg", pixs, IFF_JFIF_JPEG);
74     pixWrite("/tmp/lept/dewtest/002.jpg", pixn, IFF_JFIF_JPEG);
75     pixWrite("/tmp/lept/dewtest/003.jpg", pixg, IFF_JFIF_JPEG);
76     pixWrite("/tmp/lept/dewtest/004.png", pixb, IFF_TIFF_G4);
77     pixWrite("/tmp/lept/dewtest/005.jpg", pixd, IFF_JFIF_JPEG);
78     pixt1 = pixRead("/tmp/lept/dewmod/0020.png");
79     pixWrite("/tmp/lept/dewtest/006.png", pixt1, IFF_PNG);
80     pixDestroy(&pixt1);
81     pixt1 = pixRead("/tmp/lept/dewmod/0030.png");
82     pixWrite("/tmp/lept/dewtest/007.png", pixt1, IFF_PNG);
83     pixDestroy(&pixt1);
84     pixt1 = pixRead("/tmp/lept/dewmod/0060.png");
85     pixWrite("/tmp/lept/dewtest/008.png", pixt1, IFF_PNG);
86     pixDestroy(&pixt1);
87     pixt1 = pixRead("/tmp/lept/dewmod/0070.png");
88     pixWrite("/tmp/lept/dewtest/009.png", pixt1, IFF_PNG);
89     pixDestroy(&pixt1);
90     pixt1 = pixRead("/tmp/lept/dewapply/002.png");
91     pixWrite("/tmp/lept/dewtest/010.png", pixt1, IFF_PNG);
92     pixDestroy(&pixt1);
93     pixt1 = pixRead("/tmp/lept/dewapply/003.png");
94     pixWrite("/tmp/lept/dewtest/011.png", pixt1, IFF_PNG);
95     pixt2 = pixThresholdToBinary(pixt1, 130);
96     pixWrite("/tmp/lept/dewtest/012.png", pixt2, IFF_TIFF_G4);
97     pixDestroy(&pixt1);
98     pixDestroy(&pixt2);
99     pixt1 = pixRead("/tmp/lept/dewmod/0041.png");
100     pixWrite("/tmp/lept/dewtest/013.png", pixt1, IFF_PNG);
101     pixDestroy(&pixt1);
102     pixt1 = pixRead("/tmp/lept/dewmod/0042.png");
103     pixWrite("/tmp/lept/dewtest/014.png", pixt1, IFF_PNG);
104     pixDestroy(&pixt1);
105     pixt1 = pixRead("/tmp/lept/dewmod/0051.png");
106     pixWrite("/tmp/lept/dewtest/015.png", pixt1, IFF_PNG);
107     pixDestroy(&pixt1);
108     pixt1 = pixRead("/tmp/lept/dewmod/0052.png");
109     pixWrite("/tmp/lept/dewtest/016.png", pixt1, IFF_PNG);
110     pixDestroy(&pixt1);
111 
112         /* Normalize another image, that may not have enough textlines
113          * to build an accurate model */
114 /*    pixs2 = pixRead("1555.003.jpg");  */
115     pixs2 = pixRead("cat.007.jpg");
116 /*    pixs2 = pixRead("cat.014.jpg"); */
117     pixn2 = pixBackgroundNormSimple(pixs2, NULL, NULL);
118     pixg2 = pixConvertRGBToGray(pixn2, 0.5, 0.3, 0.2);
119     pixb2 = pixThresholdToBinary(pixg2, 130);
120 
121         /* Apply the previous disparity model to this image */
122     dew2 = dewarpCreate(pixb2, 7);
123     dewarpaInsertDewarp(dewa, dew2);
124     dewarpaInsertRefModels(dewa, 0, 1);
125     dewarpaInfo(stderr, dewa);
126     dewarpaApplyDisparity(dewa, 7, pixg2, 200, 0, 0, &pixd2,
127                           "/tmp/lept/model/dewarp_apply2.pdf");
128     dewarpaDestroy(&dewa);
129 
130         /* Write out files for the second image */
131     pixWrite("/tmp/lept/dewtest/017.jpg", pixs2, IFF_JFIF_JPEG);
132     pixWrite("/tmp/lept/dewtest/018.jpg", pixg2, IFF_JFIF_JPEG);
133     pixWrite("/tmp/lept/dewtest/019.png", pixb2, IFF_TIFF_G4);
134     pixWrite("/tmp/lept/dewtest/020.jpg", pixd2, IFF_JFIF_JPEG);
135     pixt1 = pixRead("/tmp/lept/dewmod/0060.png");
136     pixWrite("/tmp/lept/dewtest/021.png", pixt1, IFF_PNG);
137     pixDestroy(&pixt1);
138     pixt1 = pixRead("/tmp/lept/dewapply/002.png");
139     pixWrite("/tmp/lept/dewtest/022.png", pixt1, IFF_PNG);
140     pixt2 = pixThresholdToBinary(pixt1, 130);
141     pixWrite("/tmp/lept/dewtest/023.png", pixt2, IFF_TIFF_G4);
142     pixDestroy(&pixt1);
143     pixDestroy(&pixt2);
144     pixt1 = pixRead("/tmp/lept/dewmod/0070.png");
145     pixWrite("/tmp/lept/dewtest/024.png", pixt1, IFF_PNG);
146     pixDestroy(&pixt1);
147     pixt1 = pixRead("/tmp/lept/dewapply/003.png");
148     pixWrite("/tmp/lept/dewtest/025.png", pixt1, IFF_PNG);
149     pixt2 = pixThresholdToBinary(pixt1, 130);
150     pixWrite("/tmp/lept/dewtest/026.png", pixt2, IFF_TIFF_G4);
151     pixDestroy(&pixt1);
152     pixDestroy(&pixt2);
153 
154         /* Generate the big pdf file */
155     convertFilesToPdf("/tmp/lept/dewtest", NULL, 135, 1.0, 0, 0, "Dewarp Test",
156                       "/tmp/lept/dewarptest1.pdf");
157     fprintf(stderr, "pdf file made: /tmp/lept/model/dewarptest1.pdf\n");
158 
159     lept_rmdir("lept/dewmod");
160     lept_rmdir("lept/dewtest");
161     pixDestroy(&pixs);
162     pixDestroy(&pixn);
163     pixDestroy(&pixg);
164     pixDestroy(&pixb);
165     pixDestroy(&pixd);
166     pixDestroy(&pixs2);
167     pixDestroy(&pixn2);
168     pixDestroy(&pixg2);
169     pixDestroy(&pixb2);
170     pixDestroy(&pixd2);
171     return 0;
172 }
173