Leptonica 1.68
C Image Processing Library

pngio_reg.c

Go to the documentation of this file.
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  * pngio_reg.c
00018  *
00019  *    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
00020  *    This is the Leptonica regression test for lossless read/write
00021  *    I/O in png format.
00022  *    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
00023  *
00024  *    This tests reading and writing of images in png format for
00025  *    various depths, with and without colormaps.
00026  *
00027  *    This test is dependent on the following external libraries:
00028  *        libpng, libz
00029  */
00030 
00031 #include "allheaders.h"
00032 
00033     /* Needed for checking libraries and HAVE_FMEMOPEN */
00034 #ifdef HAVE_CONFIG_H
00035 #include <config_auto.h>
00036 #endif /* HAVE_CONFIG_H */
00037 
00038 #define   FILE_1BPP     "rabi.png"
00039 #define   FILE_2BPP     "speckle2.png"
00040 #define   FILE_2BPP_C   "weasel2.4g.png"
00041 #define   FILE_4BPP     "speckle4.png"
00042 #define   FILE_4BPP_C   "weasel4.16c.png"
00043 #define   FILE_8BPP     "dreyfus8.png"
00044 #define   FILE_8BPP_C   "weasel8.240c.png"
00045 #define   FILE_16BPP    "test16.png"
00046 #define   FILE_32BPP    "weasel32.png"
00047 
00048 static l_int32 test_mem_png(const char *fname);
00049 static l_int32 get_header_data(const char *filename);
00050 
00051 LEPT_DLL extern const char *ImageFileFormatExtensions[];
00052 
00053 main(int    argc,
00054      char **argv)
00055 {
00056 l_int32       success, failure;
00057 l_int32       w, h;
00058 L_REGPARAMS  *rp;
00059 
00060 #if !HAVE_LIBPNG || !HAVE_LIBZ
00061     fprintf(stderr, "libpng & libz are required for testing pngio_reg\n");
00062     return 1;
00063 #endif  /* abort */
00064 
00065     if (regTestSetup(argc, argv, &rp))
00066         return 1;
00067 
00068     /* --------- Part 1: Test lossless r/w to file ---------*/
00069 
00070     failure = FALSE;
00071     success = TRUE;
00072     fprintf(stderr, "Test bmp 1 bpp file:\n");
00073     if (ioFormatTest(FILE_1BPP)) success = FALSE;
00074     fprintf(stderr, "\nTest 2 bpp file:\n");
00075     if (ioFormatTest(FILE_2BPP)) success = FALSE;
00076     fprintf(stderr, "\nTest 2 bpp file with cmap:\n");
00077     if (ioFormatTest(FILE_2BPP_C)) success = FALSE;
00078     fprintf(stderr, "\nTest 4 bpp file:\n");
00079     if (ioFormatTest(FILE_4BPP)) success = FALSE;
00080     fprintf(stderr, "\nTest 4 bpp file with cmap:\n");
00081     if (ioFormatTest(FILE_4BPP_C)) success = FALSE;
00082     fprintf(stderr, "\nTest 8 bpp grayscale file with cmap:\n");
00083     if (ioFormatTest(FILE_8BPP)) success = FALSE;
00084     fprintf(stderr, "\nTest 8 bpp color file with cmap:\n");
00085     if (ioFormatTest(FILE_8BPP_C)) success = FALSE;
00086     fprintf(stderr, "\nTest 16 bpp file:\n");
00087     if (ioFormatTest(FILE_16BPP)) success = FALSE;
00088     fprintf(stderr, "\nTest 32 bpp file:\n");
00089     if (ioFormatTest(FILE_32BPP)) success = FALSE;
00090     if (success)
00091         fprintf(stderr,
00092             "\n  ********** Success on lossless r/w to file *********\n");
00093     else
00094         fprintf(stderr,
00095             "\n  ******* Failure on at least one r/w to file ******\n");
00096     if (!success) failure = TRUE;
00097 
00098 
00099     /* ------------ Part 2: Test lossless r/w to memory ------------ */
00100 #if HAVE_FMEMOPEN
00101     pixDisplayWrite(NULL, -1);
00102     success = TRUE;
00103     if (test_mem_png(FILE_1BPP)) success = FALSE;
00104     if (test_mem_png(FILE_2BPP)) success = FALSE;
00105     if (test_mem_png(FILE_2BPP_C)) success = FALSE;
00106     if (test_mem_png(FILE_4BPP)) success = FALSE;
00107     if (test_mem_png(FILE_4BPP_C)) success = FALSE;
00108     if (test_mem_png(FILE_8BPP)) success = FALSE;
00109     if (test_mem_png(FILE_8BPP_C)) success = FALSE;
00110     if (test_mem_png(FILE_16BPP)) success = FALSE;
00111     if (test_mem_png(FILE_32BPP)) success = FALSE;
00112     if (success)
00113         fprintf(stderr,
00114             "\n  ****** Success on lossless r/w to memory *****\n");
00115     else
00116         fprintf(stderr,
00117             "\n  ******* Failure on at least one r/w to memory ******\n");
00118     if (!success) failure = TRUE;
00119 
00120 #else
00121         fprintf(stderr,
00122             "\n  *****  r/w to memory not enabled *****\n\n");
00123 #endif  /*  HAVE_FMEMOPEN  */
00124 
00125 
00126     /* -------------- Part 3: Read header information -------------- */
00127     success = TRUE;
00128     if (get_header_data(FILE_1BPP)) success = FALSE;
00129     if (get_header_data(FILE_2BPP)) success = FALSE;
00130     if (get_header_data(FILE_2BPP_C)) success = FALSE;
00131     if (get_header_data(FILE_4BPP)) success = FALSE;
00132     if (get_header_data(FILE_4BPP_C)) success = FALSE;
00133     if (get_header_data(FILE_8BPP)) success = FALSE;
00134     if (get_header_data(FILE_8BPP_C)) success = FALSE;
00135     if (get_header_data(FILE_16BPP)) success = FALSE;
00136     if (get_header_data(FILE_32BPP)) success = FALSE;
00137 
00138     if (success)
00139         fprintf(stderr,
00140             "\n  ******* Success on reading headers *******\n\n");
00141     else
00142         fprintf(stderr,
00143             "\n  ******* Failure on reading headers *******\n\n");
00144     if (!success) failure = TRUE;
00145 
00146     if (!failure)
00147         fprintf(stderr,
00148             "  ******* Success on all tests *******\n\n");
00149     else
00150         fprintf(stderr,
00151             "  ******* Failure on at least one test *******\n\n");
00152 
00153     if (failure) rp->success = FALSE;
00154     regTestCleanup(rp);
00155     return 0;
00156 }
00157 
00158 
00159     /* Returns 1 on error */
00160 static l_int32
00161 test_mem_png(const char  *fname)
00162 {
00163 l_uint8  *data = NULL;
00164 l_int32   same;
00165 size_t    size = 0;
00166 PIX      *pixs;
00167 PIX      *pixd = NULL;
00168 
00169     if ((pixs = pixRead(fname)) == NULL) {
00170         fprintf(stderr, "Failure to read %s\n", fname);
00171         return 1;
00172     }
00173     if (pixWriteMem(&data, &size, pixs, IFF_PNG)) {
00174         fprintf(stderr, "Mem write fail for png\n");
00175         return 1;
00176     }
00177     if ((pixd = pixReadMem(data, size)) == NULL) {
00178         fprintf(stderr, "Mem read fail for png\n");
00179         lept_free(data);
00180         return 1;
00181     }
00182 
00183     pixEqual(pixs, pixd, &same);
00184     if (!same)
00185         fprintf(stderr, "Mem write/read fail for format %d\n", IFF_PNG);
00186     pixDestroy(&pixs);
00187     pixDestroy(&pixd);
00188     lept_free(data);
00189     return (!same);
00190 }
00191 
00192     /* Retrieve header data from file and from array in memory */
00193 static l_int32
00194 get_header_data(const char  *filename)
00195 {
00196 l_uint8  *data;
00197 l_int32   ret1, ret2, format1, format2;
00198 l_int32   w1, w2, h1, h2, d1, d2, bps1, bps2, spp1, spp2, iscmap1, iscmap2;
00199 size_t    nbytes1, nbytes2;
00200 
00201         /* Read header from file */
00202     nbytes1 = nbytesInFile(filename);
00203     ret1 = pixReadHeader(filename, &format1, &w1, &h1, &bps1, &spp1, &iscmap1);
00204     d1 = bps1 * spp1;
00205     if (d1 == 24) d1 = 32;
00206     if (ret1)
00207         fprintf(stderr, "Error: couldn't read header data from file: %s\n",
00208                 filename);
00209     else {
00210         fprintf(stderr, "Format data for image %s with format %s:\n"
00211             "  nbytes = %ld, size (w, h, d) = (%d, %d, %d)\n"
00212             "  bps = %d, spp = %d, iscmap = %d\n",
00213             filename, ImageFileFormatExtensions[format1], nbytes1,
00214             w1, h1, d1, bps1, spp1, iscmap1);
00215         if (format1 != IFF_PNG) {
00216             fprintf(stderr, "Error: format is %d; should be %d\n",
00217                     format1, IFF_PNG);
00218             ret1 = 1;
00219         }
00220     }
00221 
00222         /* Read header from array in memory */
00223     ret2 = 0;
00224 #if HAVE_FMEMOPEN
00225     data = l_binaryRead(filename, &nbytes2);
00226     ret2 = pixReadHeaderMem(data, nbytes2, &format2, &w2, &h2, &bps2,
00227                             &spp2, &iscmap2);
00228     lept_free(data);
00229     d2 = bps2 * spp2;
00230     if (d2 == 24) d2 = 32;
00231     if (ret2)
00232         fprintf(stderr, "Error: couldn't mem-read header data: %s\n", filename);
00233     else {
00234         if (nbytes1 != nbytes2 || format1 != format2 || w1 != w2 ||
00235             h1 != h2 || d1 != d2 || bps1 != bps2 || spp1 != spp2 ||
00236             iscmap1 != iscmap2) {
00237             fprintf(stderr, "Incomsistency reading image %s with format %s\n",
00238                     filename, ImageFileFormatExtensions[IFF_PNG]);
00239             ret2 = 1;
00240         }
00241     }
00242 #endif  /* HAVE_FMEMOPEN */
00243 
00244     return ret1 || ret2;
00245 }
00246 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines