Leptonica 1.68
C Image Processing Library

pixcomp_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  * pixcomp_reg.c
00018  *
00019  *    Regression test for compressed pix and compressed pix arrays
00020  *    in memory.
00021  *
00022  *    Most of the functions tested here requires the ability to write
00023  *    a pix to a compressed string in memory, and conversely to
00024  *    read a compressed image string from memory to generate a pix.
00025  *    This functionality is not enabled by default, because it requires
00026  *    the gnu runtime.  If we detect a failure, we bomb out early.
00027  */
00028 
00029 #include <math.h>
00030 #include "allheaders.h"
00031 
00032 
00033 LEPT_DLL extern const char *ImageFileFormatExtensions[];
00034 static void get_format_data(l_int32 i, l_uint8 *data, size_t size);
00035 
00036 #define  DO_PNG     1  /* set to 0 for valgrind to remove most png errors */
00037 
00038 main(int    argc,
00039      char **argv)
00040 {
00041 l_int32     i, n;
00042 BOX        *box;
00043 PIX        *pix, *pixs, *pixd, *pixd2;
00044 PIXA       *pixad, *pixa1, *pixa2, *pixa3;
00045 PIXC       *pixc;
00046 PIXAC      *pixac, *pixac1, *pixac2;
00047 static char     mainName[] = "pixcomp_reg";
00048 
00049     pixad = pixaCreate(0);
00050 
00051         /* --- Read in the images --- */
00052     pixac = pixacompCreate(1);
00053     pixs = pixRead("marge.jpg");
00054     pixc = pixcompCreateFromPix(pixs, IFF_JFIF_JPEG);
00055     if (!pixc) {
00056         L_ERROR("Exiting because jpeg write to memory is not enabled", mainName);
00057         return 1;
00058     }
00059     pixd = pixCreateFromPixcomp(pixc);
00060     pixSaveTiledOutline(pixd, pixad, 1, 1, 30, 2, 32);
00061     pixDestroy(&pixd);
00062     pixcompDestroy(&pixc);
00063     pixacompAddPix(pixac, pixs, IFF_DEFAULT);
00064     pixDestroy(&pixs);
00065 
00066     pix = pixRead("feyn.tif");
00067     pixs = pixScaleToGray6(pix);
00068     pixc = pixcompCreateFromPix(pixs, IFF_JFIF_JPEG);
00069     pixd = pixCreateFromPixcomp(pixc);
00070     pixSaveTiledOutline(pixd, pixad, 1, 0, 30, 2, 32);
00071     pixDestroy(&pixd);
00072     pixcompDestroy(&pixc);
00073     pixacompAddPix(pixac, pixs, IFF_DEFAULT);
00074     pixDestroy(&pixs);
00075 
00076     box = boxCreate(1144, 611, 690, 180);
00077     pixs = pixClipRectangle(pix, box, NULL);
00078     pixc = pixcompCreateFromPix(pixs, IFF_TIFF_G4);
00079     pixd = pixCreateFromPixcomp(pixc);
00080     pixSaveTiledOutline(pixd, pixad, 1, 0, 30, 2, 32);
00081     pixDestroy(&pixd);
00082     pixcompDestroy(&pixc);
00083     pixacompAddPix(pixac, pixs, IFF_DEFAULT);
00084     pixDestroy(&pixs);
00085     boxDestroy(&box);
00086     pixDestroy(&pix);
00087 
00088 #if DO_PNG
00089     pixs = pixRead("weasel4.11c.png");
00090     pixc = pixcompCreateFromPix(pixs, IFF_PNG);
00091     pixd = pixCreateFromPixcomp(pixc);
00092     pixSaveTiledOutline(pixd, pixad, 1, 0, 30, 2, 32);
00093     pixDestroy(&pixd);
00094     pixcompDestroy(&pixc);
00095     pixacompAddPix(pixac, pixs, IFF_DEFAULT);
00096     pixDestroy(&pixs);
00097 #endif
00098 
00099         /* --- Retrieve to pix --- */
00100     n = pixacompGetCount(pixac);
00101     for (i = 0; i < n; i++) {
00102         pixs = pixacompGetPix(pixac, i);
00103         pixSaveTiledOutline(pixs, pixad, 1, i == 0 ? 1 : 0, 30, 2, 32);
00104         pixDestroy(&pixs);
00105     }
00106 
00107         /* --- Retrieve to pixa --- */
00108     pixa1 = pixaCreateFromPixacomp(pixac, L_CLONE);
00109     for (i = 0; i < n; i++) {
00110         pixs = pixaGetPix(pixa1, i, L_CLONE);
00111         pixSaveTiledOutline(pixs, pixad, 1, i == 0 ? 1 : 0, 30, 2, 32);
00112         pixDestroy(&pixs);
00113     }
00114 
00115         /* --- Do (pixa <==> pixac) conversions --- */
00116     pixaWrite("/tmp/junkpixa1.pa", pixa1);
00117     pixac1 = pixacompCreateFromPixa(pixa1, IFF_DEFAULT, L_CLONE);
00118     pixa2 = pixaCreateFromPixacomp(pixac1, L_CLONE);
00119     pixaWrite("/tmp/junkpixa2.pa", pixa2);
00120     pixac2 = pixacompCreateFromPixa(pixa2, IFF_DEFAULT, L_CLONE);
00121     pixa3 = pixaCreateFromPixacomp(pixac2, L_CLONE);
00122     pixaWrite("/tmp/junkpixa3.pa", pixa3);
00123 
00124         /* --- Extract formatting info from compressed strings --- */
00125     for (i = 0; i < n; i++) {
00126         pixc = pixacompGetPixcomp(pixac1, i);
00127         get_format_data(i, pixc->data, pixc->size);
00128     }
00129 
00130         /* --- Display results --- */
00131     pixd = pixaDisplay(pixad, 0, 0);
00132     pixDisplay(pixd, 100, 100);
00133     pixWrite("/tmp/junkcomp.jpg", pixd, IFF_JFIF_JPEG);
00134     pixDestroy(&pixd);
00135     pixaDestroy(&pixad);
00136 
00137     pixaDestroy(&pixa1);
00138     pixaDestroy(&pixa2);
00139     pixaDestroy(&pixa3);
00140     pixacompDestroy(&pixac);
00141     pixacompDestroy(&pixac1);
00142     pixacompDestroy(&pixac2);
00143 
00144         /* --- Read all the 'weasel' files and display results --- */
00145     pixac = pixacompCreateFromFiles(".", "weasel", IFF_DEFAULT);
00146     fprintf(stderr, "found %d weasel files\n", pixacompGetCount(pixac));
00147     pixcompWriteStreamInfo(stderr, pixac->pixc[7], NULL);
00148     pixd = pixacompDisplayTiledAndScaled(pixac, 32, 100, 8, 0, 15, 2);
00149     pixWrite("/tmp/junkweasel.jpg", pixd, IFF_JFIF_JPEG);
00150     pixDisplay(pixd, 100, 100);
00151     pixacompDestroy(&pixac);
00152     pixDestroy(&pixd);
00153 
00154         /* --- Use serialized I/O on the pixacomp --- */
00155     pixac = pixacompCreateFromFiles(".", "hardlight", IFF_DEFAULT);
00156     fprintf(stderr, "found %d jpg files\n", pixacompGetCount(pixac));
00157     pixd = pixacompDisplayTiledAndScaled(pixac, 32, 200, 6, 0, 15, 2);
00158     pixWrite("/tmp/junkhardlight.png", pixd, IFF_PNG);
00159     pixDisplay(pixd, 100, 300);
00160     pixacompWrite("/tmp/junkpixac1.pa", pixac);
00161     pixac2 = pixacompRead("/tmp/junkpixac1.pa");
00162     pixacompWrite("/tmp/junkpixac2.pa", pixac2);
00163     pixd2 = pixacompDisplayTiledAndScaled(pixac2, 32, 1200, 4, 0, 30, 2);
00164     pixDisplay(pixd2, 500, 300);
00165     pixacompWriteStreamInfo(stderr, pixac2, NULL);
00166     pixacompDestroy(&pixac);
00167     pixacompDestroy(&pixac2);
00168     pixDestroy(&pixd);
00169     pixDestroy(&pixd2);
00170 
00171         /* --- Read all the 'tif' files and display results --- */
00172     pixac = pixacompCreateFromFiles(".", ".tif", IFF_DEFAULT);
00173     fprintf(stderr, "found %d tiff files\n", pixacompGetCount(pixac));
00174     pixcompWriteStreamInfo(stderr, pixac->pixc[0], NULL);
00175     pixd = pixacompDisplayTiledAndScaled(pixac, 32, 200, 6, 0, 15, 2);
00176     pixWrite("/tmp/junktiffs.png", pixd, IFF_PNG);
00177     pixDisplay(pixd, 100, 500);
00178     pixacompDestroy(&pixac);
00179     pixDestroy(&pixd);
00180 
00181     return 0;
00182 }
00183 
00184 
00185 static void
00186 get_format_data(l_int32   i,
00187                 l_uint8  *data,
00188                 size_t    size)
00189 {
00190 l_int32  ret, format, w, h, d, bps, spp, iscmap;
00191 
00192     ret = pixReadHeaderMem(data, size, &format, &w, &h, &bps, &spp, &iscmap);
00193     d = bps * spp;
00194     if (d == 24) d = 32;
00195     if (ret)
00196         fprintf(stderr, "Error: couldn't read data: size = %d\n",
00197                 (l_int32)size);
00198     else
00199         fprintf(stderr, "Format data for image %d:\n"
00200                 "  format: %s, size (w, h, d) = (%d, %d, %d)\n"
00201                 "  bps = %d, spp = %d, iscmap = %d\n",
00202                 i, ImageFileFormatExtensions[format], w, h, d,
00203                 bps, spp, iscmap);
00204     return;
00205 }
00206 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines