Leptonica 1.68
C Image Processing Library

mtifftest.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  * mtifftest.c
00018  *
00019  *   Tests tiff I/O for:
00020  *
00021  *       -- multipage tiff read/write
00022  *       -- writing special tiff tags to file
00023  */
00024 
00025 #include "allheaders.h"
00026 
00027 static const char *tempmtiff = "/tmp/junkmtiff";
00028 static const char *tempnewmtiff = "/tmp/junknewmtiff";
00029 
00030 
00031 main(int    argc,
00032      char **argv)
00033 {
00034 char        *filein, *fileout, *str, *fname, *filename;
00035 char         buffer[512];
00036 l_int32      i, count, npages, length;
00037 FILE        *fp;
00038 NUMA        *naflags, *nasizes;
00039 PIX         *pix, *pixd;
00040 PIXA        *pixa;
00041 PIXCMAP     *cmap;
00042 SARRAY      *savals, *satypes, *sa;
00043 static char  mainName[] = "mtifftest";
00044 
00045     if (argc != 3)
00046         exit(ERROR_INT(" Syntax:  mtifftest filein fileout", mainName, 1));
00047 
00048     filein = argv[1];
00049     fileout = argv[2];
00050 
00051 #if 1   /* ------------------  Test multipage I/O  -------------------*/
00052         /* This puts every image file in the directory with a string
00053          * match to "weasel" into a multipage tiff file.
00054          * Images with 1 bpp are coded as g4; the others as zip.
00055          * It then reads back into a pix and displays.  */
00056     writeMultipageTiff(".", "weasel", "/tmp/junkout.tif");
00057     pixa = pixaReadMultipageTiff("/tmp/junkout.tif");
00058     pixd = pixaDisplayTiledInRows(pixa, 1, 1200, 0.5, 0, 15, 4);
00059     pixDisplay(pixd, 100, 0);
00060     pixDestroy(&pixd);
00061     pixd = pixaDisplayTiledInRows(pixa, 8, 1200, 0.8, 0, 15, 4);
00062     pixDisplay(pixd, 100, 200);
00063     pixDestroy(&pixd);
00064     pixd = pixaDisplayTiledInRows(pixa, 32, 1200, 1.2, 0, 15, 4);
00065     pixDisplay(pixd, 100, 400);
00066     pixDestroy(&pixd);
00067     pixaDestroy(&pixa);
00068 #endif
00069 
00070 #if 0   /* ------------ Test single-to-multipage I/O  -------------------*/
00071         /* Use 'filein' to specify a directory of tiff files.
00072          * Read them in and generate a multipage tiff file.
00073          * Then convert that to a G4 compressed and ascii85 encoded
00074          * PS file. */
00075     sa = getFilenamesInDirectory(filein);
00076     sarrayWriteStream(stderr, sa);
00077     sarraySort(sa, sa, L_SORT_INCREASING);
00078     sarrayWriteStream(stderr, sa);
00079     npages = sarrayGetCount(sa);
00080     for (i = 0; i < npages; i++) {
00081         fname = sarrayGetString(sa, i, 0);
00082         filename = genPathname(filein, fname);
00083         pix = pixRead(filename);
00084         if (!pix) continue;
00085         if (i == 0)
00086             pixWriteTiff(tempmtiff, pix, IFF_TIFF_G4, "w+");
00087         else
00088             pixWriteTiff(tempmtiff, pix, IFF_TIFF_G4, "a");
00089         pixDestroy(&pix);
00090         lept_free(filename);
00091     }
00092 
00093         /* write it out as a PS file */
00094     convertTiffMultipageToPS(tempmtiff, fileout, NULL, 0.95);
00095     sarrayDestroy(&sa);
00096 #endif
00097 
00098 #if 0   /* ------------------  Test multipage I/O  -------------------*/
00099         /* read count of tiff multipage */
00100     fp = lept_fopen(filein, "rb");
00101     if (fileFormatIsTiff(fp)) {
00102         tiffGetCount(fp, &npages);
00103         fprintf(stderr, " Tiff: %d page\n", npages);
00104     }
00105     else 
00106         exit(ERROR_INT(" file not tiff", mainName, 1));
00107     lept_fclose(fp);
00108 
00109         /* split into separate page files */
00110     for (i = 0; i < npages + 1; i++) {   /* read one beyond to catch error */
00111         pix = pixReadTiff(filein, i);
00112         if (!pix) continue;
00113         sprintf(buffer, "/tmp/junkout.%d.tif", i);
00114         pixWrite(buffer, pix, IFF_TIFF_G4);
00115         pixDestroy(&pix);
00116     }
00117 
00118         /* read separate page files and write reversed file */
00119     for (i = npages - 1; i >= 0; i--) {
00120         sprintf(buffer, "/tmp/junkout.%d.tif", i);
00121         pix = pixRead(buffer);
00122         if (!pix) continue;
00123         if (i == npages - 1)
00124             pixWriteTiff(tempmtiff, pix, IFF_TIFF_G4, "w+");
00125         else
00126             pixWriteTiff(tempmtiff, pix, IFF_TIFF_G4, "a");
00127         pixDestroy(&pix);
00128     }
00129 
00130         /* read reversed file and reverse again */
00131     pixa = pixaCreate(npages);
00132     for (i = 0; i < 5; i++) {
00133         pix = pixReadTiff(tempmtiff, i);
00134         pixaAddPix(pixa, pix, L_INSERT);
00135     }
00136     for (i = npages - 1; i >= 0; i--) {
00137         pix = pixaGetPix(pixa, i, L_CLONE);
00138         if (i == npages - 1)
00139             pixWriteTiff(tempnewmtiff, pix, IFF_TIFF_G4, "w+");
00140         else
00141             pixWriteTiff(tempnewmtiff, pix, IFF_TIFF_G4, "a");
00142         pixDestroy(&pix);
00143     }
00144     pixaDestroy(&pixa);
00145 #endif
00146 
00147 
00148 #if 0    /* -----   test adding custom public tags to a tiff header ----- */
00149     pix = pixRead(filein);
00150     naflags = numaCreate(10);
00151     savals = sarrayCreate(10);
00152     satypes = sarrayCreate(10);
00153     nasizes = numaCreate(10);
00154     
00155 /*    numaAddNumber(naflags, TIFFTAG_XMLPACKET);  */ /* XMP:  700 */
00156     numaAddNumber(naflags, 700);
00157     str = "<xmp>This is a Fake XMP packet</xmp>\n<text>Guess what ...?</text>";
00158     length = strlen(str);
00159     sarrayAddString(savals, str, 1);
00160     sarrayAddString(satypes, "char*", 1);
00161     numaAddNumber(nasizes, length);  /* get it all */
00162 
00163     numaAddNumber(naflags, 269);  /* DOCUMENTNAME */
00164     sarrayAddString(savals, "One silly title", 1);
00165     sarrayAddString(satypes, "char*", 1);
00166     numaAddNumber(naflags, 270);  /* IMAGEDESCRIPTION */
00167     sarrayAddString(savals, "One page of text", 1);
00168     sarrayAddString(satypes, "char*", 1);
00169         /* the max sample is used by rendering programs 
00170          * to scale the dynamic range */
00171     numaAddNumber(naflags, 281);  /* MAXSAMPLEVALUE */
00172     sarrayAddString(savals, "4", 1);
00173     sarrayAddString(satypes, "l_uint16", 1);
00174         /* note that date is required to be a 20 byte string */
00175     numaAddNumber(naflags, 306);  /* DATETIME */
00176     sarrayAddString(savals, "2004:10:11 09:35:15", 1);
00177     sarrayAddString(satypes, "char*", 1);
00178         /* note that page number requires 2 l_uint16 input */
00179     numaAddNumber(naflags, 297);  /* PAGENUMBER */
00180     sarrayAddString(savals, "1-412", 1);
00181     sarrayAddString(satypes, "l_uint16-l_uint16", 1);
00182     pixWriteTiffCustom(fileout, pix, IFF_TIFF_G4, "w", naflags,
00183                        savals, satypes, nasizes);
00184     fprintTiffInfo(stderr, fileout);
00185     numaDestroy(&naflags);
00186     numaDestroy(&nasizes);
00187     sarrayDestroy(&savals);
00188     sarrayDestroy(&satypes);
00189     pixDestroy(&pix);
00190 #endif
00191 
00192     exit(0);
00193 }
00194 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines