Leptonica 1.68
C Image Processing Library

ioformats_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 /*
00018  * ioformats_reg.c
00019  *
00020  *    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
00021  *    This is the primary Leptonica regression test for lossless
00022  *    read/write I/O to standard image files (png, tiff, bmp, etc.)
00023  *    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
00024  *
00025  *    This tests reading and writing of images in different formats
00026  *    It should work properly on input images of any depth, with
00027  *    and without colormaps.
00028  *
00029  *    The first part of the test works by doing a write/read and
00030  *    testing the result for equality.  We only test the lossless
00031  *    file formats, with pix of various depths, both with and
00032  *    without colormaps.  Because jpeg works fine on grayscale
00033  *    and rgb, there is no need for explicit tests on jpeg
00034  *    compression here.
00035  *
00036  *    The second part tests all different tiff compressions, for
00037  *    read/write that is backed both by file and by memory.
00038  *    For r/w to file, it is actually redundant with the first part.)
00039  *
00040  *    This test is dependent on the following external libraries:
00041  *        libjpeg, libtiff, libpng, libz
00042  *
00043  *    TODO (1/28/11): Set up a jpegio_reg.c set of tests
00044  *    and include the following:
00045  *      (1) different color spaces; e.g., CYYK
00046  *      (2) colormapped images, which jpeg can handle
00047  *      (3) no chroma subsampling
00048  */
00049 
00050 #include "allheaders.h"
00051 
00052     /* Needed for checking libraries and HAVE_FMEMOPEN */
00053 #ifdef HAVE_CONFIG_H
00054 #include <config_auto.h>
00055 #endif /* HAVE_CONFIG_H */
00056 
00057 #define   BMP_FILE      "test1.bmp"
00058 #define   FILE_1BPP     "feyn.tif"
00059 #define   FILE_2BPP     "speckle2.png"
00060 #define   FILE_2BPP_C   "weasel2.4g.png"
00061 #define   FILE_4BPP     "speckle4.png"
00062 #define   FILE_4BPP_C   "weasel4.16c.png"
00063 #define   FILE_8BPP_1   "dreyfus8.png"
00064 #define   FILE_8BPP_2   "weasel8.240c.png"
00065 #define   FILE_8BPP_3   "test8.jpg"
00066 #define   FILE_16BPP    "test16.tif"
00067 #define   FILE_32BPP    "marge.jpg"
00068 
00069 static l_int32 testcomp(const char *filename, PIX *pix, l_int32 comptype);
00070 static l_int32 testcomp_mem(PIX *pixs, PIX **ppixt, l_int32 index,
00071                             l_int32 format);
00072 static l_int32 test_writemem(PIX *pixs, l_int32 format, char *psfile);
00073 static PIX *make_24_bpp_pix(PIX *pixs);
00074 static l_int32 get_header_data(const char *filename, l_int32 true_format);
00075 static void get_tiff_compression_name(char *buf, l_int32 format);
00076 
00077 LEPT_DLL extern const char *ImageFileFormatExtensions[];
00078 
00079 main(int    argc,
00080      char **argv)
00081 {
00082 #if HAVE_FMEMOPEN
00083 char          psname[256];
00084 #endif  /* HAVE_FMEMOPEN */
00085 char         *tempname;
00086 l_uint8      *data;
00087 l_int32       i, d, n, success, failure, same;
00088 l_int32       w, h, bps, spp;
00089 size_t        size, nbytes;
00090 PIX          *pix1, *pix2, *pix4, *pix8, *pix16, *pix32;
00091 PIX          *pix, *pixt, *pixd;
00092 PIXA         *pixa;
00093 L_REGPARAMS  *rp;
00094 
00095 #if  !HAVE_LIBJPEG
00096     fprintf(stderr, "Omitting libjpeg tests in ioformats_reg\n");
00097 #endif  /* !HAVE_LIBJPEG */
00098 
00099 #if  !HAVE_LIBTIFF
00100     fprintf(stderr, "Omitting libtiff tests in ioformats_reg\n");
00101 #endif  /* !HAVE_LIBTIFF */
00102 
00103 #if  !HAVE_LIBPNG || !HAVE_LIBZ
00104     fprintf(stderr, "Omitting libpng tests in ioformats_reg\n");
00105 #endif  /* !HAVE_LIBPNG || !HAVE_LIBZ */
00106 
00107     if (regTestSetup(argc, argv, &rp))
00108         return 1;
00109 
00110     /* --------- Part 1: Test all lossless formats for r/w to file ---------*/
00111 
00112     failure = FALSE;
00113     success = TRUE;
00114     fprintf(stderr, "Test bmp 1 bpp file:\n");
00115     if (ioFormatTest(BMP_FILE)) success = FALSE;
00116 
00117 
00118 #if  HAVE_LIBTIFF
00119     fprintf(stderr, "\nTest other 1 bpp file:\n");
00120     if (ioFormatTest(FILE_1BPP)) success = FALSE;
00121 #endif  /* HAVE_LIBTIFF */
00122 
00123 #if  HAVE_LIBPNG
00124     fprintf(stderr, "\nTest 2 bpp file:\n");
00125     if (ioFormatTest(FILE_2BPP)) success = FALSE;
00126     fprintf(stderr, "\nTest 2 bpp file with cmap:\n");
00127     if (ioFormatTest(FILE_2BPP_C)) success = FALSE;
00128     fprintf(stderr, "\nTest 4 bpp file:\n");
00129     if (ioFormatTest(FILE_4BPP)) success = FALSE;
00130     fprintf(stderr, "\nTest 4 bpp file with cmap:\n");
00131     if (ioFormatTest(FILE_4BPP_C)) success = FALSE;
00132     fprintf(stderr, "\nTest 8 bpp grayscale file with cmap:\n");
00133     if (ioFormatTest(FILE_8BPP_1)) success = FALSE;
00134     fprintf(stderr, "\nTest 8 bpp color file with cmap:\n");
00135     if (ioFormatTest(FILE_8BPP_2)) success = FALSE;
00136 #endif  /* HAVE_LIBPNG */
00137 
00138 #if  HAVE_LIBJPEG
00139     fprintf(stderr, "\nTest 8 bpp file without cmap:\n");
00140     if (ioFormatTest(FILE_8BPP_3)) success = FALSE;
00141 #endif  /* HAVE_LIBJPEG */
00142 
00143 #if  HAVE_LIBTIFF
00144     fprintf(stderr, "\nTest 16 bpp file:\n");
00145     if (ioFormatTest(FILE_16BPP)) success = FALSE;
00146 #endif  /* HAVE_LIBTIFF */
00147 
00148 #if  HAVE_LIBJPEG
00149     fprintf(stderr, "\nTest 32 bpp file:\n");
00150     if (ioFormatTest(FILE_32BPP)) success = FALSE;
00151 #endif  /* HAVE_LIBJPEG */
00152 
00153     if (success)
00154         fprintf(stderr,
00155             "\n  ********** Success on all i/o format tests *********\n");
00156     else
00157         fprintf(stderr,
00158             "\n  ******* Failure on at least one i/o format test ******\n");
00159     if (!success) failure = TRUE;
00160 
00161 
00162     /* ------------------ Part 2: Test tiff r/w to file ------------------- */
00163 #if  !HAVE_LIBTIFF
00164     goto part6;
00165 #endif  /* !HAVE_LIBTIFF */
00166 
00167     fprintf(stderr, "\nTest tiff r/w and format extraction\n");
00168     pixa = pixaCreate(6);
00169     pix1 = pixRead(BMP_FILE);
00170     pix2 = pixConvert1To2(NULL, pix1, 3, 0);
00171     pix4 = pixConvert1To4(NULL, pix1, 15, 0);
00172     pix16 = pixRead(FILE_16BPP);
00173     fprintf(stderr, "Input format: %d\n", pixGetInputFormat(pix16));
00174     pix8 = pixConvert16To8(pix16, 1);
00175     pix32 = pixRead(FILE_32BPP);
00176     pixaAddPix(pixa, pix1, L_INSERT);
00177     pixaAddPix(pixa, pix2, L_INSERT);
00178     pixaAddPix(pixa, pix4, L_INSERT);
00179     pixaAddPix(pixa, pix8, L_INSERT);
00180     pixaAddPix(pixa, pix16, L_INSERT);
00181     pixaAddPix(pixa, pix32, L_INSERT);
00182     n = pixaGetCount(pixa);
00183 
00184     success = TRUE;
00185     for (i = 0; i < n; i++) {
00186         pix = pixaGetPix(pixa, i, L_CLONE);
00187         d = pixGetDepth(pix);
00188         fprintf(stderr, "%d bpp\n", d);
00189         if (i == 0) {   /* 1 bpp */
00190             pixWrite("/tmp/junkg3.tif", pix, IFF_TIFF_G3);
00191             pixWrite("/tmp/junkg4.tif", pix, IFF_TIFF_G4);
00192             pixWrite("/tmp/junkrle.tif", pix, IFF_TIFF_RLE);
00193             pixWrite("/tmp/junkpb.tif", pix, IFF_TIFF_PACKBITS);
00194             if (testcomp("/tmp/junkg3.tif", pix, IFF_TIFF_G3)) success = FALSE;
00195             if (testcomp("/tmp/junkg4.tif", pix, IFF_TIFF_G4)) success = FALSE;
00196             if (testcomp("/tmp/junkrle.tif", pix, IFF_TIFF_RLE))
00197                 success = FALSE;
00198             if (testcomp("/tmp/junkpb.tif", pix, IFF_TIFF_PACKBITS))
00199                 success = FALSE;
00200         }
00201         pixWrite("/tmp/junklzw.tif", pix, IFF_TIFF_LZW);
00202         pixWrite("/tmp/junkzip.tif", pix, IFF_TIFF_ZIP);
00203         pixWrite("/tmp/junknon.tif", pix, IFF_TIFF);
00204         if (testcomp("/tmp/junklzw.tif", pix, IFF_TIFF_LZW)) success = FALSE;
00205         if (testcomp("/tmp/junkzip.tif", pix, IFF_TIFF_ZIP)) success = FALSE;
00206         if (testcomp("/tmp/junknon.tif", pix, IFF_TIFF)) success = FALSE;
00207         pixDestroy(&pix);
00208     }
00209     if (success)
00210         fprintf(stderr,
00211             "\n  ********** Success on tiff r/w to file *********\n\n");
00212     else
00213         fprintf(stderr,
00214             "\n  ******* Failure on at least one tiff r/w to file ******\n\n");
00215     if (!success) failure = TRUE;
00216 
00217     /* ------------------ Part 3: Test tiff r/w to memory ----------------- */
00218 
00219     success = TRUE;
00220     for (i = 0; i < n; i++) {
00221         pix = pixaGetPix(pixa, i, L_CLONE);
00222         d = pixGetDepth(pix);
00223         fprintf(stderr, "%d bpp\n", d);
00224         if (i == 0) {   /* 1 bpp */
00225             pixWriteMemTiff(&data, &size, pix, IFF_TIFF_G3);
00226             nbytes = nbytesInFile("/tmp/junkg3.tif");
00227             fprintf(stderr, "nbytes = %ld, size = %ld\n", nbytes, size);
00228             pixt = pixReadMemTiff(data, size, 0);
00229             if (testcomp_mem(pix, &pixt, i, IFF_TIFF_G3)) success = FALSE;
00230             lept_free(data);
00231             pixWriteMemTiff(&data, &size, pix, IFF_TIFF_G4);
00232             nbytes = nbytesInFile("/tmp/junkg4.tif");
00233             fprintf(stderr, "nbytes = %ld, size = %ld\n", nbytes, size);
00234             pixt = pixReadMemTiff(data, size, 0);
00235             if (testcomp_mem(pix, &pixt, i, IFF_TIFF_G4)) success = FALSE;
00236             readHeaderMemTiff(data, size, 0, &w, &h, &bps, &spp,
00237                               NULL, NULL, NULL);
00238             fprintf(stderr, "(w,h,bps,spp) = (%d,%d,%d,%d)\n", w, h, bps, spp);
00239             lept_free(data);
00240             pixWriteMemTiff(&data, &size, pix, IFF_TIFF_RLE);
00241             nbytes = nbytesInFile("/tmp/junkrle.tif");
00242             fprintf(stderr, "nbytes = %ld, size = %ld\n", nbytes, size);
00243             pixt = pixReadMemTiff(data, size, 0);
00244             if (testcomp_mem(pix, &pixt, i, IFF_TIFF_RLE)) success = FALSE;
00245             lept_free(data);
00246             pixWriteMemTiff(&data, &size, pix, IFF_TIFF_PACKBITS);
00247             nbytes = nbytesInFile("/tmp/junkpb.tif");
00248             fprintf(stderr, "nbytes = %ld, size = %ld\n", nbytes, size);
00249             pixt = pixReadMemTiff(data, size, 0);
00250             if (testcomp_mem(pix, &pixt, i, IFF_TIFF_PACKBITS)) success = FALSE;
00251             lept_free(data);
00252         }
00253         pixWriteMemTiff(&data, &size, pix, IFF_TIFF_LZW);
00254         pixt = pixReadMemTiff(data, size, 0);
00255         if (testcomp_mem(pix, &pixt, i, IFF_TIFF_LZW)) success = FALSE;
00256         lept_free(data);
00257         pixWriteMemTiff(&data, &size, pix, IFF_TIFF_ZIP);
00258         pixt = pixReadMemTiff(data, size, 0);
00259         if (testcomp_mem(pix, &pixt, i, IFF_TIFF_ZIP)) success = FALSE;
00260         readHeaderMemTiff(data, size, 0, &w, &h, &bps, &spp, NULL, NULL, NULL);
00261         fprintf(stderr, "(w,h,bps,spp) = (%d,%d,%d,%d)\n", w, h, bps, spp);
00262         lept_free(data);
00263         pixWriteMemTiff(&data, &size, pix, IFF_TIFF);
00264         pixt = pixReadMemTiff(data, size, 0);
00265         if (testcomp_mem(pix, &pixt, i, IFF_TIFF)) success = FALSE;
00266         lept_free(data);
00267         pixDestroy(&pix);
00268     }
00269     if (success)
00270         fprintf(stderr,
00271             "\n  ********** Success on tiff r/w to memory *********\n\n");
00272     else
00273         fprintf(stderr,
00274             "\n  ******* Failure on at least one tiff r/w to memory ******\n\n");
00275     if (!success) failure = TRUE;
00276 
00277 
00278     /* ---------------- Part 4: Test non-tiff r/w to memory ---------------- */
00279 
00280 #if HAVE_FMEMOPEN
00281     pixDisplayWrite(NULL, -1);
00282     success = TRUE;
00283     for (i = 0; i < n; i++) {
00284         pix = pixaGetPix(pixa, i, L_CLONE);
00285         d = pixGetDepth(pix);
00286         sprintf(psname, "/tmp/junkps.%d", d);
00287         fprintf(stderr, "%d bpp\n", d);
00288         if (d != 16) {
00289             if (test_writemem(pix, IFF_PNG, NULL)) success = FALSE;
00290             if (test_writemem(pix, IFF_BMP, NULL)) success = FALSE;
00291         }
00292         if (test_writemem(pix, IFF_PNM, NULL)) success = FALSE;
00293         if (test_writemem(pix, IFF_PS, psname)) success = FALSE;
00294         if (d == 8 || d == 32)
00295             if (test_writemem(pix, IFF_JFIF_JPEG, NULL)) success = FALSE;
00296         pixDestroy(&pix);
00297     }
00298     if (success)
00299         fprintf(stderr,
00300             "\n  ********** Success on non-tiff r/w to memory *********\n\n");
00301     else
00302         fprintf(stderr,
00303             "\n  **** Failure on at least one non-tiff r/w to memory *****\n\n");
00304     if (!success) failure = TRUE;
00305 #else
00306         fprintf(stderr,
00307             "\n  ***** Non-tiff r/w to memory not enabled *****\n\n");
00308 #endif  /*  HAVE_FMEMOPEN  */
00309 
00310     pixaDestroy(&pixa);
00311 
00312     /* ------------ Part 5: Test multipage tiff r/w to memory ------------ */
00313 
00314         /* Make a multipage tiff file, and read it back into memory */
00315     success = TRUE;
00316     pix = pixRead("feyn.tif");
00317     pixa = pixaSplitPix(pix, 3, 3, 0, 0);
00318     for (i = 0; i < 9; i++) {
00319         pixt = pixaGetPix(pixa, i, L_CLONE);
00320         if (i == 0)
00321             pixWriteTiff("/tmp/junktiffmpage.tif", pixt, IFF_TIFF_G4, "w");
00322         else
00323             pixWriteTiff("/tmp/junktiffmpage.tif", pixt, IFF_TIFF_G4, "a");
00324         pixDestroy(&pixt);
00325     }
00326     data = l_binaryRead("/tmp/junktiffmpage.tif", &nbytes);
00327     pixaDestroy(&pixa);
00328 
00329         /* Read the individual pages from memory to a pix */
00330     pixa = pixaCreate(9);
00331     for (i = 0; i < 9; i++) {
00332         pixt = pixReadMemTiff(data, nbytes, i);
00333         pixaAddPix(pixa, pixt, L_INSERT);
00334     }
00335     lept_free(data);
00336 
00337         /* Un-tile the pix in the pixa back to the original image */
00338     pixt = pixaDisplayUnsplit(pixa, 3, 3, 0, 0);
00339     pixaDestroy(&pixa);
00340 
00341         /* Clip to foreground to remove any extra rows or columns */
00342     pixClipToForeground(pix, &pix1, NULL);
00343     pixClipToForeground(pixt, &pix2, NULL);
00344     pixEqual(pix1, pix2, &same); 
00345     if (same)
00346         fprintf(stderr,
00347             "\n  ******* Success on tiff multipage read from memory ******\n\n");
00348     else
00349         fprintf(stderr,
00350             "\n  ******* Failure on tiff multipage read from memory ******\n\n");
00351     if (!same) failure = TRUE;
00352 
00353     pixDestroy(&pix);
00354     pixDestroy(&pixt);
00355     pixDestroy(&pix1);
00356     pixDestroy(&pix2);
00357 
00358     /* ------------ Part 6: Test 24 bpp writing ------------ */
00359 #if  !HAVE_LIBTIFF
00360 part6:
00361 #endif  /* !HAVE_LIBTIFF */
00362 
00363 #if  !HAVE_LIBPNG || !HAVE_LIBJPEG || !HAVE_LIBTIFF
00364     goto finish;
00365 #endif  /* !HAVE_LIBPNG || !HAVE_LIBJPEG || !HAVE_LIBTIFF */
00366 
00367         /* Generate a 24 bpp (not 32 bpp !!) rgb pix and write it out */
00368     success = TRUE;
00369     pix = pixRead("marge.jpg");
00370     pixt = make_24_bpp_pix(pix);
00371     pixWrite("/tmp/junk24.png", pixt, IFF_PNG);
00372     pixWrite("/tmp/junk24.jpg", pixt, IFF_JFIF_JPEG);
00373     pixWrite("/tmp/junk24.tif", pixt, IFF_TIFF);
00374     pixd = pixRead("/tmp/junk24.png");
00375     pixEqual(pix, pixd, &same);
00376     if (!same) success = FALSE;
00377     pixDestroy(&pixd);
00378     pixd = pixRead("/tmp/junk24.jpg");
00379     regTestCompareSimilarPix(rp, pix, pixd, 10, 0.0002, 0);
00380     pixDestroy(&pixd);
00381     pixd = pixRead("/tmp/junk24.tif");
00382     pixEqual(pix, pixd, &same);
00383     if (!same) success = FALSE;
00384     pixDestroy(&pixd);
00385     if (success)
00386         fprintf(stderr,
00387             "\n  ******* Success on 24 bpp rgb writing *******\n\n");
00388     else
00389         fprintf(stderr,
00390             "\n  ******* Failure on 24 bpp rgb writing *******\n\n");
00391     if (!success) failure = TRUE;
00392     pixDestroy(&pix);
00393     pixDestroy(&pixt);
00394 
00395     /* -------------- Part 7: Read header information -------------- */
00396     success = TRUE;
00397     if (get_header_data(FILE_1BPP, IFF_TIFF_G4)) success = FALSE;
00398     if (get_header_data(FILE_2BPP, IFF_PNG)) success = FALSE;
00399     if (get_header_data(FILE_2BPP_C, IFF_PNG)) success = FALSE;
00400     if (get_header_data(FILE_4BPP, IFF_PNG)) success = FALSE;
00401     if (get_header_data(FILE_4BPP_C, IFF_PNG)) success = FALSE;
00402     if (get_header_data(FILE_8BPP_1, IFF_PNG)) success = FALSE;
00403     if (get_header_data(FILE_8BPP_2, IFF_PNG)) success = FALSE;
00404     if (get_header_data(FILE_8BPP_3, IFF_JFIF_JPEG)) success = FALSE;
00405     if (get_header_data(FILE_16BPP, IFF_TIFF_ZIP)) success = FALSE;
00406     if (get_header_data(FILE_32BPP, IFF_JFIF_JPEG)) success = FALSE;
00407 
00408 #if HAVE_FMEMOPEN
00409     pix = pixRead(FILE_8BPP_1);
00410     tempname = genTempFilename((const char *)"/tmp", (const char *)".pnm",
00411                                1, 1);
00412     pixWrite(tempname, pix, IFF_PNM);
00413     if (get_header_data(tempname, IFF_PNM)) success = FALSE;
00414     pixDestroy(&pix);
00415     lept_free(tempname);
00416 #endif  /* HAVE_FMEMOPEN */
00417 
00418     pix = pixRead(FILE_1BPP);
00419     tempname = genTempFilename((const char *)"/tmp", (const char *)".tif",
00420                                1, 1);
00421     pixWrite(tempname, pix, IFF_TIFF_G3);
00422     if (get_header_data(tempname, IFF_TIFF_G3)) success = FALSE;
00423     pixWrite(tempname, pix, IFF_TIFF_G4);
00424     if (get_header_data(tempname, IFF_TIFF_G4)) success = FALSE;
00425     pixWrite(tempname, pix, IFF_TIFF_PACKBITS);
00426     if (get_header_data(tempname, IFF_TIFF_PACKBITS)) success = FALSE;
00427     pixWrite(tempname, pix, IFF_TIFF_RLE);
00428     if (get_header_data(tempname, IFF_TIFF_RLE)) success = FALSE;
00429     pixWrite(tempname, pix, IFF_TIFF_LZW);
00430     if (get_header_data(tempname, IFF_TIFF_LZW)) success = FALSE;
00431     pixWrite(tempname, pix, IFF_TIFF_ZIP);
00432     if (get_header_data(tempname, IFF_TIFF_ZIP)) success = FALSE;
00433     pixWrite(tempname, pix, IFF_TIFF);
00434     if (get_header_data(tempname, IFF_TIFF)) success = FALSE;
00435     pixDestroy(&pix);
00436     lept_free(tempname);
00437 
00438     if (success)
00439         fprintf(stderr,
00440             "\n  ******* Success on reading headers *******\n\n");
00441     else
00442         fprintf(stderr,
00443             "\n  ******* Failure on reading headers *******\n\n");
00444     if (!success) failure = TRUE;
00445 
00446 #if  !HAVE_LIBPNG || !HAVE_LIBJPEG || !HAVE_LIBTIFF
00447 finish:
00448 #endif  /* !HAVE_LIBPNG || !HAVE_LIBJPEG || !HAVE_LIBTIFF */
00449 
00450     if (!failure)
00451         fprintf(stderr,
00452             "  ******* Success on all tests *******\n\n");
00453     else
00454         fprintf(stderr,
00455             "  ******* Failure on at least one test *******\n\n");
00456 
00457     regTestCleanup(rp);
00458     return 0;
00459 }
00460 
00461 
00462     /* Returns 1 on error */
00463 static l_int32
00464 testcomp(const char  *filename,
00465          PIX         *pix,
00466          l_int32      comptype)
00467 {
00468 l_int32  format, sameformat, sameimage;
00469 FILE    *fp;
00470 PIX     *pixt;
00471 
00472     fp = lept_fopen(filename, "rb");
00473     findFileFormatStream(fp, &format);
00474     sameformat = TRUE;
00475     if (format != comptype) {
00476         fprintf(stderr, "File %s has format %d, not comptype %d\n",
00477                 filename, format, comptype);
00478         sameformat = FALSE;
00479     }
00480     lept_fclose(fp);
00481     pixt = pixRead(filename);
00482     pixEqual(pix, pixt, &sameimage);
00483     pixDestroy(&pixt);
00484     if (!sameimage)
00485         fprintf(stderr, "Write/read fail for file %s with format %d\n",
00486                 filename, format);
00487     return (!sameformat || !sameimage);
00488 }
00489 
00490 
00491     /* Returns 1 on error */
00492 static l_int32
00493 testcomp_mem(PIX     *pixs,
00494              PIX    **ppixt,
00495              l_int32  index,
00496              l_int32  format)
00497 {
00498 l_int32  sameimage;
00499 PIX     *pixt;
00500 
00501     pixt = *ppixt;
00502     pixEqual(pixs, pixt, &sameimage);
00503     if (!sameimage)
00504         fprintf(stderr, "Mem Write/read fail for file %d with format %d\n",
00505                 index, format);
00506     pixDestroy(&pixt);
00507     *ppixt = NULL;
00508     return (!sameimage);
00509 }
00510 
00511 
00512     /* Returns 1 on error */
00513 static l_int32
00514 test_writemem(PIX      *pixs,
00515               l_int32   format,
00516               char     *psfile)
00517 {
00518 l_uint8  *data = NULL;
00519 l_int32   same;
00520 size_t    size = 0;
00521 PIX      *pixd = NULL;
00522 
00523     if (format == IFF_PS) {
00524         pixWriteMemPS(&data, &size, pixs, NULL, 0, 1.0);
00525         l_binaryWrite(psfile, "w", data, size);
00526         lept_free(data);
00527         return 0;
00528     }
00529 
00530     /* Fail silently if library is not available */
00531 #if !HAVE_LIBJPEG
00532     if (format == IFF_JFIF_JPEG)
00533         return 0;
00534 #endif  /* !HAVE_LIBJPEG */
00535 #if !HAVE_LIBPNG
00536     if (format == IFF_PNG)
00537         return 0;
00538 #endif  /* !HAVE_LIBPNG */
00539 #if !HAVE_LIBTIFF
00540     if (format == IFF_TIFF)
00541         return 0;
00542 #endif  /* !HAVE_LIBTIFF */
00543 
00544     if (pixWriteMem(&data, &size, pixs, format)) {
00545         fprintf(stderr, "Mem write fail for format %d\n", format);
00546         return 1;
00547     }
00548     if ((pixd = pixReadMem(data, size)) == NULL) {
00549         fprintf(stderr, "Mem read fail for format %d\n", format);
00550         lept_free(data);
00551         return 1;
00552     }
00553     if (format == IFF_JFIF_JPEG) {
00554         fprintf(stderr, "jpeg size = %ld\n", size);
00555         pixDisplayWrite(pixd, 1);
00556         same = TRUE;
00557     }
00558     else {
00559         pixEqual(pixs, pixd, &same);
00560         if (!same)
00561            fprintf(stderr, "Mem write/read fail for format %d\n", format);
00562     }
00563     pixDestroy(&pixd);
00564     lept_free(data);
00565     return (!same);
00566 }
00567 
00568 
00569     /* Composes 24 bpp rgb pix */
00570 static PIX *
00571 make_24_bpp_pix(PIX  *pixs)
00572 {
00573 l_int32    i, j, w, h, wpls, wpld, rval, gval, bval;
00574 l_uint32  *lines, *lined, *datas, *datad;
00575 PIX       *pixd;
00576 
00577     pixGetDimensions(pixs, &w, &h, NULL);
00578     pixd = pixCreate(w, h, 24);
00579     datas = pixGetData(pixs);
00580     wpls = pixGetWpl(pixs);
00581     datad = pixGetData(pixd);
00582     wpld = pixGetWpl(pixd);
00583     for (i = 0; i < h; i++) {
00584         lines = datas + i * wpls;
00585         lined = datad + i * wpld;
00586         for (j = 0; j < w; j++) {
00587             extractRGBValues(lines[j], &rval, &gval, &bval);
00588             *((l_uint8 *)lined + 3 * j) = rval;
00589             *((l_uint8 *)lined + 3 * j + 1) = gval;
00590             *((l_uint8 *)lined + 3 * j + 2) = bval;
00591         }
00592     }
00593 
00594     return pixd;
00595 }
00596 
00597 
00598     /* Retrieve header data from file */
00599 static l_int32
00600 get_header_data(const char  *filename,
00601                 l_int32      true_format)
00602 {
00603 char      buf[64];
00604 l_uint8  *data;
00605 l_int32   ret1, ret2, format1, format2;
00606 l_int32   w1, w2, h1, h2, d1, d2, bps1, bps2, spp1, spp2, iscmap1, iscmap2;
00607 size_t    size1, size2;
00608 
00609     /* Fail silently if library is not available */
00610 #if !HAVE_LIBJPEG
00611     if (true_format == IFF_JFIF_JPEG)
00612         return 0;
00613 #endif  /* !HAVE_LIBJPEG */
00614 #if !HAVE_LIBPNG
00615     if (true_format == IFF_PNG)
00616         return 0;
00617 #endif  /* !HAVE_LIBPNG */
00618 #if !HAVE_LIBTIFF
00619     if (true_format == IFF_TIFF_G3 || true_format == IFF_TIFF_G4 ||
00620         true_format == IFF_TIFF_ZIP || true_format == IFF_TIFF_LZW ||
00621         true_format == IFF_TIFF_PACKBITS || true_format == IFF_TIFF_RLE ||
00622         true_format == IFF_TIFF)
00623         return 0;
00624 #endif  /* !HAVE_LIBTIFF */
00625 
00626         /* Read header from file */
00627     size1 = nbytesInFile(filename);
00628     ret1 = pixReadHeader(filename, &format1, &w1, &h1, &bps1, &spp1, &iscmap1);
00629     d1 = bps1 * spp1;
00630     if (d1 == 24) d1 = 32;
00631     if (ret1)
00632         fprintf(stderr, "Error: couldn't read header data: %s\n", filename);
00633     else {
00634         if (format1 > IFF_PNG && format1 < IFF_PNM) {
00635             get_tiff_compression_name(buf, format1);
00636             fprintf(stderr, "Format data for image %s with format %s:\n"
00637                 "  nbytes = %ld, size (w, h, d) = (%d, %d, %d)\n"
00638                 "  bps = %d, spp = %d, iscmap = %d\n",
00639                 filename, buf, size1, w1, h1, d1, bps1, spp1, iscmap1);
00640         } else {
00641             fprintf(stderr, "Format data for image %s with format %s:\n"
00642                 "  nbytes = %ld, size (w, h, d) = (%d, %d, %d)\n"
00643                 "  bps = %d, spp = %d, iscmap = %d\n",
00644                 filename, ImageFileFormatExtensions[format1], size1,
00645                 w1, h1, d1, bps1, spp1, iscmap1);
00646         }
00647         if (format1 != true_format) {
00648             fprintf(stderr, "Error: format is %d; should be %d\n",
00649                     format1, true_format);
00650             ret1 = 1;
00651         }
00652     }
00653 
00654         /* Read header from array in memory */
00655     data = l_binaryRead(filename, &size2);
00656     ret2 = pixReadHeaderMem(data, size2, &format2, &w2, &h2, &bps2,
00657                             &spp2, &iscmap2);
00658     lept_free(data);
00659     d2 = bps2 * spp2;
00660     if (d2 == 24) d2 = 32;
00661     if (ret2)
00662         fprintf(stderr, "Error: couldn't mem-read header data: %s\n", filename);
00663     else {
00664         if (size1 != size2 || format1 != format2 || w1 != w2 ||
00665             h1 != h2 || d1 != d2 || bps1 != bps2 || spp1 != spp2 ||
00666             iscmap1 != iscmap2) {
00667             fprintf(stderr, "Incomsistency reading image %s with format %s\n",
00668                     filename, buf);
00669             ret2 = 1;
00670         }
00671     }
00672     return ret1 || ret2;
00673 }
00674 
00675 
00676 static void
00677 get_tiff_compression_name(char    *buf,
00678                           l_int32  format)
00679 {
00680     if (format == IFF_TIFF_G4)
00681         sprintf(buf, "tiff_g4");
00682     else if (format == IFF_TIFF_G3)
00683         sprintf(buf, "tiff_g3");
00684     else if (format == IFF_TIFF_ZIP)
00685         sprintf(buf, "tiff_zip");
00686     else if (format == IFF_TIFF_LZW)
00687         sprintf(buf, "tiff_lzw");
00688     else if (format == IFF_TIFF_RLE)
00689         sprintf(buf, "tiff_rle");
00690     else if (format == IFF_TIFF_PACKBITS)
00691         sprintf(buf, "tiff_packbits");
00692     else if (format == IFF_TIFF)
00693         sprintf(buf, "tiff_uncompressed");
00694     else
00695         fprintf(stderr, "format %d: not tiff\n", format);
00696     return;
00697 }
00698 
00699 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines