home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / unix / libtiff / lbtif3_3.tar / tools / tiffsplit.c < prev    next >
C/C++ Source or Header  |  1993-08-26  |  6KB  |  231 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/tools/RCS/tiffsplit.c,v 1.3 93/08/26 15:13:26 sam Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1992 Sam Leffler
  7.  * Copyright (c) 1992 Silicon Graphics, Inc.
  8.  *
  9.  * Permission to use, copy, modify, distribute, and sell this software and 
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that (i) the above copyright notices and this permission notice appear in
  12.  * all copies of the software and related documentation, and (ii) the names of
  13.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Sam Leffler and Silicon Graphics.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  20.  * 
  21.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  22.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  23.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  25.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  26.  * OF THIS SOFTWARE.
  27.  */
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32.  
  33. #include "tiffio.h"
  34.  
  35. #define    streq(a,b)    (strcmp(a,b) == 0)
  36. #define    CopyField(tag, v) \
  37.     if (TIFFGetField(in, tag, &v)) TIFFSetField(out, tag, v)
  38. #define    CopyField2(tag, v1, v2) \
  39.     if (TIFFGetField(in, tag, &v1, &v2)) TIFFSetField(out, tag, v1, v2)
  40. #define    CopyField3(tag, v1, v2, v3) \
  41.     if (TIFFGetField(in, tag, &v1, &v2, &v3)) TIFFSetField(out, tag, v1, v2, v3)
  42.  
  43. static    int ignore = 1;            /* if true, ignore read errors */
  44. static    char *filename;
  45. static    char fname[1024+1];
  46.  
  47. static    int tiffcp(TIFF*, TIFF*);
  48. static    void newfilename(void);
  49. static    int cpStrips(TIFF*, TIFF*);
  50. static    int cpTiles(TIFF*, TIFF*);
  51.  
  52. void
  53. main(int argc, char* argv[])
  54. {
  55.     TIFF *in, *out;
  56.  
  57.     if (argc < 2) {
  58.         fprintf(stderr, "usage: tiffsplit input.tif [prefix]\n");
  59.         exit(-3);
  60.     }
  61.     if (argc > 2)
  62.         strcpy(fname, argv[2]);
  63.     in = TIFFOpen(filename = argv[1], "r");
  64.     if (in != NULL) {
  65.         do {
  66.             char path[1024+1];
  67.             newfilename();
  68.             strcpy(path, fname);
  69.             strcat(path, ".tif");
  70.             out = TIFFOpen(path, "w");
  71.             if (out == NULL)
  72.                 exit(-2);
  73.             if (!tiffcp(in, out))
  74.                 exit(-1);
  75.             TIFFClose(out);
  76.         } while (TIFFReadDirectory(in));
  77.         (void) TIFFClose(in);
  78.     }
  79.     exit(0);
  80. }
  81.  
  82. static void
  83. newfilename(void)
  84. {
  85.     static int first = 1;
  86.     static long fnum;
  87.     static short defname;
  88.     static char *fpnt;
  89.  
  90.     if (first) {
  91.         if (fname[0]) {
  92.             fpnt = fname + strlen(fname);
  93.             defname = 0;
  94.         } else {
  95.             fname[0] = 'x';
  96.             fpnt = fname + 1;
  97.             defname = 1;
  98.         }
  99.         first = 0;
  100.     }
  101. #define    MAXFILES    676
  102.     if (fnum == MAXFILES) {
  103.         if (!defname || fname[0] == 'z') {
  104.             fprintf(stderr, "tiffsplit: too many files.\n");
  105.             exit(1);
  106.         }
  107.         fname[0]++;
  108.         fnum = 0;
  109.     }
  110.     fpnt[0] = fnum / 26 + 'a';
  111.     fpnt[1] = fnum % 26 + 'a';
  112.     fnum++;
  113. }
  114.  
  115. static int
  116. tiffcp(TIFF* in, TIFF* out)
  117. {
  118.     short bitspersample, samplesperpixel, shortv, *shortav;
  119.     uint32 w, l;
  120.     float floatv;
  121.     char *stringv;
  122.     uint32 longv;
  123.  
  124.     CopyField(TIFFTAG_SUBFILETYPE, longv);
  125.     CopyField(TIFFTAG_TILEWIDTH, w);
  126.     CopyField(TIFFTAG_TILELENGTH, l);
  127.     CopyField(TIFFTAG_IMAGEWIDTH, w);
  128.     CopyField(TIFFTAG_IMAGELENGTH, l);
  129.     CopyField(TIFFTAG_BITSPERSAMPLE, bitspersample);
  130.     CopyField(TIFFTAG_COMPRESSION, shortv);
  131.     CopyField(TIFFTAG_PREDICTOR, shortv);
  132.     CopyField(TIFFTAG_PHOTOMETRIC, shortv);
  133.     CopyField(TIFFTAG_THRESHHOLDING, shortv);
  134.     CopyField(TIFFTAG_FILLORDER, shortv);
  135.     CopyField(TIFFTAG_ORIENTATION, shortv);
  136.     CopyField(TIFFTAG_SAMPLESPERPIXEL, samplesperpixel);
  137.     CopyField(TIFFTAG_MINSAMPLEVALUE, shortv);
  138.     CopyField(TIFFTAG_MAXSAMPLEVALUE, shortv);
  139.     CopyField(TIFFTAG_XRESOLUTION, floatv);
  140.     CopyField(TIFFTAG_YRESOLUTION, floatv);
  141.     CopyField(TIFFTAG_GROUP3OPTIONS, longv);
  142.     CopyField(TIFFTAG_GROUP4OPTIONS, longv);
  143.     CopyField(TIFFTAG_RESOLUTIONUNIT, shortv);
  144.     CopyField(TIFFTAG_PLANARCONFIG, shortv);
  145.     CopyField(TIFFTAG_ROWSPERSTRIP, longv);
  146.     CopyField(TIFFTAG_XPOSITION, floatv);
  147.     CopyField(TIFFTAG_YPOSITION, floatv);
  148.     CopyField(TIFFTAG_IMAGEDEPTH, longv);
  149.     CopyField(TIFFTAG_TILEDEPTH, longv);
  150.     CopyField2(TIFFTAG_EXTRASAMPLES, shortv, shortav);
  151.     { uint16 *red, *green, *blue;
  152.       CopyField3(TIFFTAG_COLORMAP, red, green, blue);
  153.     }
  154.     { uint16 shortv2;
  155.       CopyField2(TIFFTAG_PAGENUMBER, shortv, shortv2);
  156.     }
  157.     CopyField(TIFFTAG_ARTIST, stringv);
  158.     CopyField(TIFFTAG_IMAGEDESCRIPTION, stringv);
  159.     CopyField(TIFFTAG_MAKE, stringv);
  160.     CopyField(TIFFTAG_MODEL, stringv);
  161.     CopyField(TIFFTAG_SOFTWARE, stringv);
  162.     CopyField(TIFFTAG_DATETIME, stringv);
  163.     CopyField(TIFFTAG_HOSTCOMPUTER, stringv);
  164.     CopyField(TIFFTAG_PAGENAME, stringv);
  165.     CopyField(TIFFTAG_DOCUMENTNAME, stringv);
  166.     if (TIFFIsTiled(in))
  167.         return (cpTiles(in, out));
  168.     else
  169.         return (cpStrips(in, out));
  170. }
  171.  
  172. static int
  173. cpStrips(TIFF* in, TIFF* out)
  174. {
  175.     tsize_t bufsize  = TIFFStripSize(in);
  176.     unsigned char *buf = (unsigned char *)malloc(bufsize);
  177.  
  178.     if (buf) {
  179.         tstrip_t s, ns = TIFFNumberOfStrips(in);
  180.         uint32 *bytecounts;
  181.  
  182.         TIFFGetField(in, TIFFTAG_STRIPBYTECOUNTS, &bytecounts);
  183.         for (s = 0; s < ns; s++) {
  184.             if (bytecounts[s] > bufsize) {
  185.                 buf = (unsigned char *)realloc(buf, bytecounts[s]);
  186.                 if (!buf)
  187.                     return (0);
  188.                 bufsize = bytecounts[s];
  189.             }
  190.             if (TIFFReadRawStrip(in, s, buf, bytecounts[s]) < 0 ||
  191.                 TIFFWriteRawStrip(out, s, buf, bytecounts[s]) < 0) {
  192.                 free(buf);
  193.                 return (0);
  194.             }
  195.         }
  196.         free(buf);
  197.         return (1);
  198.     }
  199.     return (0);
  200. }
  201.  
  202. static int
  203. cpTiles(TIFF* in, TIFF* out)
  204. {
  205.     tsize_t bufsize = TIFFTileSize(in);
  206.     unsigned char *buf = (unsigned char *)malloc(bufsize);
  207.  
  208.     if (buf) {
  209.         ttile_t t, nt = TIFFNumberOfTiles(in);
  210.         uint32 *bytecounts;
  211.  
  212.         TIFFGetField(in, TIFFTAG_TILEBYTECOUNTS, &bytecounts);
  213.         for (t = 0; t < nt; t++) {
  214.             if (bytecounts[t] > bufsize) {
  215.                 buf = (unsigned char *)realloc(buf, bytecounts[t]);
  216.                 if (!buf)
  217.                     return (0);
  218.                 bufsize = bytecounts[t];
  219.             }
  220.             if (TIFFReadRawTile(in, t, buf, bytecounts[t]) < 0 ||
  221.                 TIFFWriteRawTile(out, t, buf, bytecounts[t]) < 0) {
  222.                 free(buf);
  223.                 return (0);
  224.             }
  225.         }
  226.         free(buf);
  227.         return (1);
  228.     }
  229.     return (0);
  230. }
  231.