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