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