Leptonica 1.68
C Image Processing Library
|
00001 /*====================================================================* 00002 - Copyright (C) 2001 Leptonica. All rights reserved. 00003 - This software is distributed in the hope that it will be 00004 - useful, but with NO WARRANTY OF ANY KIND. 00005 - No author or distributor accepts responsibility to anyone for the 00006 - consequences of using this software, or for whether it serves any 00007 - particular purpose or works at all, unless he or she says so in 00008 - writing. Everyone is granted permission to copy, modify and 00009 - redistribute this source code, for commercial or non-commercial 00010 - purposes, with the following restrictions: (1) the origin of this 00011 - source code must not be misrepresented; (2) modified versions must 00012 - be plainly marked as such; and (3) this notice may not be removed 00013 - or altered from any source or modified source distribution. 00014 *====================================================================*/ 00015 00016 /* 00017 * gifio_reg.c 00018 * 00019 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 00020 * This is the Leptonica regression test for lossless read/write 00021 * I/O in gif format. 00022 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 00023 * 00024 * This tests reading and writing of images in gif format for 00025 * varioius depths. 00026 * 00027 * The relative times for writing of gif and png are interesting. 00028 * 00029 * For 1 bpp: 00030 * 00031 * png writing is about 2x faster than gif writing, using giflib. 00032 * 00033 * For 32 bpp, using a 1 Mpix rgb image: 00034 * 00035 * png: Lossless: 1.16 sec (2.0 MB output file) 00036 * Lossy: 0.43 sec, composed of: 00037 * 0.22 sec (octree quant with dithering) 00038 * 0.21 sec (to compress and write out) 00039 * 00040 * gif: Lossy: 0.34 sec, composed of: 00041 * 0.22 sec (octree quant with dithering) 00042 * 0.12 sec (to compress and write out) 00043 * (note: no lossless mode; gif can't write out rgb) 00044 */ 00045 00046 #include <math.h> 00047 #include "allheaders.h" 00048 00049 static void test_gif(const char *fname, L_REGPARAMS *rp); 00050 static l_int32 test_mem_gif(const char *fname, l_int32 index); 00051 00052 00053 /* Needed for HAVE_LIBGIF and or HAVE_LIBUNGIF */ 00054 #ifdef HAVE_CONFIG_H 00055 #include <config_auto.h> 00056 #endif /* HAVE_CONFIG_H */ 00057 00058 #define FILE_1BPP "feyn.tif" 00059 #define FILE_2BPP "weasel2.4g.png" 00060 #define FILE_4BPP "weasel4.16c.png" 00061 #define FILE_8BPP_1 "dreyfus8.png" 00062 #define FILE_8BPP_2 "weasel8.240c.png" 00063 #define FILE_8BPP_3 "test8.jpg" 00064 #define FILE_16BPP "test16.tif" 00065 #define FILE_32BPP "marge.jpg" 00066 00067 #define REDUCTION 1 00068 00069 main(int argc, 00070 char **argv) 00071 { 00072 l_int32 success; 00073 L_REGPARAMS *rp; 00074 00075 #if !HAVE_LIBGIF && !HAVE_LIBUNGIF 00076 fprintf(stderr, "gifio is not enabled\n" 00077 "libgif or libungif are required for gifio_reg\n" 00078 "See environ.h: #define HAVE_LIBGIF or HAVE_LIBUNGIF 1\n" 00079 "See prog/Makefile: link in -lgif or -lungif\n\n"); 00080 return 0; 00081 #endif /* abort */ 00082 00083 if (regTestSetup(argc, argv, &rp)) 00084 return 1; 00085 pixDisplayWrite(NULL, -1); 00086 00087 /* ------------ Part 1: Test lossless r/w to file ------------*/ 00088 test_gif(FILE_1BPP, rp); 00089 test_gif(FILE_2BPP, rp); 00090 test_gif(FILE_4BPP, rp); 00091 test_gif(FILE_8BPP_1, rp); 00092 test_gif(FILE_8BPP_2, rp); 00093 test_gif(FILE_8BPP_3, rp); 00094 test_gif(FILE_16BPP, rp); 00095 test_gif(FILE_32BPP, rp); 00096 if (rp->success) 00097 fprintf(stderr, 00098 "\n ****** Success on lossless r/w to file *****\n\n"); 00099 else 00100 fprintf(stderr, 00101 "\n ******* Failure on at least one r/w to file ******\n\n"); 00102 00103 if (rp->display) 00104 pixDisplayMultiple("/tmp/junk_write_display*"); 00105 00106 /* ------------ Part 2: Test lossless r/w to memory ------------ */ 00107 success = TRUE; 00108 #if HAVE_FMEMOPEN 00109 pixDisplayWrite(NULL, -1); 00110 if (test_mem_gif(FILE_1BPP, 0)) success = FALSE; 00111 if (test_mem_gif(FILE_2BPP, 1)) success = FALSE; 00112 if (test_mem_gif(FILE_4BPP, 2)) success = FALSE; 00113 if (test_mem_gif(FILE_8BPP_1, 3)) success = FALSE; 00114 if (test_mem_gif(FILE_8BPP_2, 4)) success = FALSE; 00115 if (test_mem_gif(FILE_8BPP_3, 5)) success = FALSE; 00116 if (test_mem_gif(FILE_16BPP, 6)) success = FALSE; 00117 if (test_mem_gif(FILE_32BPP, 7)) success = FALSE; 00118 if (success) 00119 fprintf(stderr, 00120 "\n ****** Success on lossless r/w to memory *****\n\n"); 00121 else 00122 fprintf(stderr, 00123 "\n ******* Failure on at least one r/w to memory ******\n\n"); 00124 00125 #else 00126 fprintf(stderr, 00127 "\n ***** r/w to memory not enabled *****\n\n"); 00128 #endif /* HAVE_FMEMOPEN */ 00129 00130 /* Success only if all tests are passed */ 00131 if (rp->success == TRUE) rp->success = success; 00132 00133 regTestCleanup(rp); 00134 return 0; 00135 } 00136 00137 00138 static void 00139 test_gif(const char *fname, 00140 L_REGPARAMS *rp) 00141 { 00142 char buf[256]; 00143 l_int32 same; 00144 PIX *pixs, *pix1, *pix2; 00145 00146 pixs = pixRead(fname); 00147 snprintf(buf, sizeof(buf), "/tmp/gifio-a.%d.gif", rp->index + 1); 00148 pixWrite(buf, pixs, IFF_GIF); 00149 pix1 = pixRead(buf); 00150 snprintf(buf, sizeof(buf), "/tmp/gifio-b.%d.gif", rp->index + 1); 00151 pixWrite(buf, pix1, IFF_GIF); 00152 pix2 = pixRead(buf); 00153 regTestWritePixAndCheck(rp, pix2, IFF_GIF); 00154 pixEqual(pixs, pix2, &same); 00155 if (!same && rp->index < 6) { 00156 fprintf(stderr, "Error for %s\n", fname); 00157 rp->success = FALSE; 00158 } 00159 if (rp->display) { 00160 fprintf(stderr, 00161 " depth: pixs = %d, pix1 = %d\n", pixGetDepth(pixs), 00162 pixGetDepth(pix1)); 00163 pixDisplayWrite(pix2, REDUCTION); 00164 } 00165 pixDestroy(&pixs); 00166 pixDestroy(&pix1); 00167 pixDestroy(&pix2); 00168 return; 00169 } 00170 00171 00172 /* Returns 1 on error */ 00173 static l_int32 00174 test_mem_gif(const char *fname, 00175 l_int32 index) 00176 { 00177 l_uint8 *data = NULL; 00178 l_int32 same; 00179 size_t size = 0; 00180 PIX *pixs; 00181 PIX *pixd = NULL; 00182 00183 if ((pixs = pixRead(fname)) == NULL) { 00184 fprintf(stderr, "Failure to read %s\n", fname); 00185 return 1; 00186 } 00187 if (pixWriteMem(&data, &size, pixs, IFF_GIF)) { 00188 fprintf(stderr, "Mem write fail for gif\n"); 00189 return 1; 00190 } 00191 if ((pixd = pixReadMem(data, size)) == NULL) { 00192 fprintf(stderr, "Mem read fail for gif\n"); 00193 lept_free(data); 00194 return 1; 00195 } 00196 00197 pixEqual(pixs, pixd, &same); 00198 pixDestroy(&pixs); 00199 pixDestroy(&pixd); 00200 lept_free(data); 00201 if (!same && index < 6) { 00202 fprintf(stderr, "Mem write/read fail for file %s\n", fname); 00203 return 1; 00204 } 00205 else 00206 return 0; 00207 } 00208