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 * jpegio_reg.c
29 *
30 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
31 * This is a Leptonica regression test for jpeg I/O
32 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
33 *
34 * This tests reading and writing of images and image
35 * metadata, between Pix and compressed data in jpeg format.
36 *
37 * This only tests properly written jpeg files. To test
38 * reading of corrupted jpeg files to insure that the
39 * reader does not crash, use prog/corrupttest.c.
40 *
41 * TODO (5/5/14): Add tests for
42 * (1) different color spaces
43 * (2) no chroma subsampling
44 * (3) luminance only reading
45 */
46
47 #include <string.h>
48 #include "allheaders.h"
49
50 /* Needed for HAVE_LIBJPEG */
51 #ifdef HAVE_CONFIG_H
52 #include <config_auto.h>
53 #endif /* HAVE_CONFIG_H */
54
55 void DoJpegTest1(L_REGPARAMS *rp, const char *fname);
56 void DoJpegTest2(L_REGPARAMS *rp, const char *fname);
57 void DoJpegTest3(L_REGPARAMS *rp, const char *fname);
58 void DoJpegTest4(L_REGPARAMS *rp, const char *fname);
59
60
main(int argc,char ** argv)61 int main(int argc,
62 char **argv)
63 {
64 L_REGPARAMS *rp;
65
66 #if !HAVE_LIBJPEG
67 fprintf(stderr, "jpegio is not enabled\n"
68 "See environ.h: #define HAVE_LIBJPEG\n"
69 "See prog/Makefile: link in -ljpeg\n\n");
70 return 0;
71 #endif /* abort */
72
73 if (regTestSetup(argc, argv, &rp))
74 return 1;
75
76 DoJpegTest1(rp, "test8.jpg");
77 DoJpegTest1(rp, "fish24.jpg");
78 DoJpegTest1(rp, "test24.jpg");
79 DoJpegTest2(rp, "weasel2.png");
80 DoJpegTest2(rp, "weasel2.4g.png");
81 DoJpegTest2(rp, "weasel4.png");
82 DoJpegTest2(rp, "weasel4.5g.png");
83 DoJpegTest2(rp, "weasel4.16c.png");
84 DoJpegTest2(rp, "weasel8.16g.png");
85 DoJpegTest2(rp, "weasel8.240c.png");
86 DoJpegTest3(rp, "lucasta.150.jpg");
87 DoJpegTest3(rp, "tetons.jpg");
88 DoJpegTest4(rp, "karen8.jpg");
89
90 return regTestCleanup(rp);
91 }
92
93
94 /* Use this for 8 bpp (no cmap), 24 bpp or 32 bpp pix */
DoJpegTest1(L_REGPARAMS * rp,const char * fname)95 void DoJpegTest1(L_REGPARAMS *rp,
96 const char *fname)
97 {
98 size_t size;
99 l_uint8 *data;
100 char buf[256];
101 PIX *pixs, *pix1, *pix2, *pix3, *pix4, *pix5;
102
103 /* Test file read/write (general functions) */
104 pixs = pixRead(fname);
105 snprintf(buf, sizeof(buf), "/tmp/lept/regout/jpegio.%d.jpg", rp->index + 1);
106 pixWrite(buf, pixs, IFF_JFIF_JPEG);
107 pix1 = pixRead(buf);
108 regTestCompareSimilarPix(rp, pixs, pix1, 6, 0.01, 0);
109 pixDisplayWithTitle(pix1, 500, 100, "pix1", rp->display);
110
111 /* Test memory read/write (general functions) */
112 pixWriteMemJpeg(&data, &size, pixs, 75, 0);
113 pix2 = pixReadMem(data, size);
114 regTestComparePix(rp, pix1, pix2);
115 lept_free(data);
116
117 /* Test file read/write (specialized jpeg functions) */
118 pix3 = pixReadJpeg(fname, 0, 1, NULL, 0);
119 regTestComparePix(rp, pixs, pix3);
120 snprintf(buf, sizeof(buf), "/tmp/lept/regout/jpegio.%d.jpg", rp->index + 1);
121 pixWriteJpeg(buf, pix3, 75, 0);
122 pix4 = pixReadJpeg(buf, 0, 1, NULL, 0);
123 regTestComparePix(rp, pix2, pix4);
124
125 /* Test memory read/write (specialized jpeg functions) */
126 pixWriteMemJpeg(&data, &size, pixs, 75, 0);
127 pix5 = pixReadMemJpeg(data, size, 0, 1, NULL, 0);
128 regTestComparePix(rp, pix4, pix5);
129 lept_free(data);
130
131 pixDestroy(&pixs);
132 pixDestroy(&pix1);
133 pixDestroy(&pix2);
134 pixDestroy(&pix3);
135 pixDestroy(&pix4);
136 pixDestroy(&pix5);
137 return;
138 }
139
140 /* Use this for colormapped pix and for pix with d < 8 */
DoJpegTest2(L_REGPARAMS * rp,const char * fname)141 void DoJpegTest2(L_REGPARAMS *rp,
142 const char *fname)
143 {
144 size_t size;
145 l_uint8 *data;
146 char buf[256];
147 PIX *pixs, *pix1, *pix2, *pix3, *pix4, *pix5, *pix6;
148
149 /* Test file read/write (general functions) */
150 pixs = pixRead(fname);
151 snprintf(buf, sizeof(buf), "/tmp/lept/regout/jpegio.%d.jpg", rp->index + 1);
152 pixWrite(buf, pixs, IFF_JFIF_JPEG);
153 pix1 = pixRead(buf);
154 if (pixGetColormap(pixs) != NULL)
155 pix2 = pixRemoveColormap(pixs, REMOVE_CMAP_BASED_ON_SRC);
156 else
157 pix2 = pixConvertTo8(pixs, 0);
158 regTestCompareSimilarPix(rp, pix1, pix2, 20, 0.2, 0);
159 pixDisplayWithTitle(pix1, 500, 100, "pix1", rp->display);
160
161 /* Test memory read/write (general functions) */
162 pixWriteMemJpeg(&data, &size, pixs, 75, 0);
163 pix3 = pixReadMem(data, size);
164 regTestComparePix(rp, pix1, pix3);
165 lept_free(data);
166
167 /* Test file write (specialized jpeg function) */
168 pix4 = pixRead(fname);
169 snprintf(buf, sizeof(buf), "/tmp/lept/regout/jpegio.%d.jpg", rp->index + 1);
170 pixWriteJpeg(buf, pix4, 75, 0);
171 pix5 = pixReadJpeg(buf, 0, 1, NULL, 0);
172 regTestComparePix(rp, pix5, pix5);
173
174 /* Test memory write (specialized jpeg function) */
175 pixWriteMemJpeg(&data, &size, pixs, 75, 0);
176 pix6 = pixReadMemJpeg(data, size, 0, 1, NULL, 0);
177 regTestComparePix(rp, pix5, pix6);
178 lept_free(data);
179
180 pixDestroy(&pixs);
181 pixDestroy(&pix1);
182 pixDestroy(&pix2);
183 pixDestroy(&pix3);
184 pixDestroy(&pix4);
185 pixDestroy(&pix5);
186 pixDestroy(&pix6);
187 return;
188 }
189
DoJpegTest3(L_REGPARAMS * rp,const char * fname)190 void DoJpegTest3(L_REGPARAMS *rp,
191 const char *fname)
192 {
193 l_int32 w1, h1, bps1, spp1, w2, h2, bps2, spp2, format1, format2;
194 size_t size;
195 l_uint8 *data;
196 PIX *pixs;
197
198 /* Test header reading (specialized jpeg functions) */
199 readHeaderJpeg(fname, &w1, &h1, &spp1, NULL, NULL);
200 pixs = pixRead(fname);
201 pixWriteMemJpeg(&data, &size, pixs, 75, 0);
202 readHeaderMemJpeg(data, size, &w2, &h2, &spp2, NULL, NULL);
203 regTestCompareValues(rp, w1, w2, 0.0);
204 regTestCompareValues(rp, h1, h2, 0.0);
205 regTestCompareValues(rp, spp1, spp2, 0.0);
206 lept_free(data);
207
208 /* Test header reading (general jpeg functions) */
209 pixReadHeader(fname, &format1, &w1, &h1, &bps1, &spp1, NULL);
210 pixWriteMem(&data, &size, pixs, IFF_JFIF_JPEG);
211 pixReadHeaderMem(data, size, &format2, &w2, &h2, &bps2, &spp2, NULL);
212 regTestCompareValues(rp, format1, format2, 0.0);
213 regTestCompareValues(rp, w1, w2, 0.0);
214 regTestCompareValues(rp, h1, h2, 0.0);
215 regTestCompareValues(rp, bps1, bps2, 0.0);
216 regTestCompareValues(rp, bps1, 8, 0.0);
217 regTestCompareValues(rp, spp1, spp2, 0.0);
218 fprintf(stderr, "w = %d, h = %d, bps = %d, spp = %d, format = %d\n",
219 w1, h1, bps1, spp1, format1);
220
221 pixDestroy(&pixs);
222 lept_free(data);
223 return;
224 }
225
DoJpegTest4(L_REGPARAMS * rp,const char * fname)226 void DoJpegTest4(L_REGPARAMS *rp,
227 const char *fname)
228 {
229 char buf[256];
230 char comment1[256];
231 l_uint8 *comment2;
232 l_int32 xres, yres;
233 FILE *fp;
234 PIX *pixs;
235
236 /* Test special comment and resolution readers */
237 pixs = pixRead(fname);
238 snprintf(comment1, sizeof(comment1), "Test %d", rp->index + 1);
239 pixSetText(pixs, comment1);
240 pixSetResolution(pixs, 137, 137);
241 snprintf(buf, sizeof(buf), "/tmp/lept/regout/jpegio.%d.jpg", rp->index + 1);
242 pixWrite(buf, pixs, IFF_JFIF_JPEG);
243 regTestCheckFile(rp, buf);
244 fp = lept_fopen(buf, "rb");
245 fgetJpegResolution(fp, &xres, &yres);
246 fgetJpegComment(fp, &comment2);
247 if (!comment2)
248 comment2 = (l_uint8 *)stringNew("");
249 lept_fclose(fp);
250 regTestCompareValues(rp, xres, 137, 0.0);
251 regTestCompareValues(rp, yres, 137, 0.0);
252 regTestCompareStrings(rp, (l_uint8 *)comment1, strlen(comment1),
253 comment2, strlen((char *)comment2));
254 fprintf(stderr, "xres = %d, yres = %d, comment = %s\n",
255 xres, yres, comment1);
256
257 lept_free(comment2);
258 pixDestroy(&pixs);
259 return;
260 }
261
262
263