3 * Copyright (c) 1992-1997 Sam Leffler
4 * Copyright (c) 1992-1997 Silicon Graphics, Inc.
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that (i) the above copyright notices and this permission notice appear in
9 * all copies of the software and related documentation, and (ii) the names of
10 * Sam Leffler and Silicon Graphics may not be used in any advertising or
11 * publicity relating to the software without the specific, prior written
12 * permission of Sam Leffler and Silicon Graphics.
14 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
16 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
19 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
20 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
22 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
26 #include "tif_config.h"
35 extern int getopt(int, char**, char*);
38 #define CopyField(tag, v) \
39 if (TIFFGetField(in, tag, &v)) TIFFSetField(out, tag, v)
40 #define CopyField2(tag, v1, v2) \
41 if (TIFFGetField(in, tag, &v1, &v2)) TIFFSetField(out, tag, v1, v2)
42 #define CopyField3(tag, v1, v2, v3) \
43 if (TIFFGetField(in, tag, &v1, &v2, &v3)) TIFFSetField(out, tag, v1, v2, v3)
45 #define PATH_LENGTH 8192
47 static const char TIFF_SUFFIX
[] = ".tif";
49 static char fname
[PATH_LENGTH
];
51 static int tiffcp(TIFF
*, TIFF
*);
52 static void newfilename(void);
53 static int cpStrips(TIFF
*, TIFF
*);
54 static int cpTiles(TIFF
*, TIFF
*);
57 main(int argc
, char* argv
[])
62 fprintf(stderr
, "%s\n\n", TIFFGetVersion());
63 fprintf(stderr
, "usage: tiffsplit input.tif [prefix]\n");
67 strncpy(fname
, argv
[2], sizeof(fname
));
68 fname
[sizeof(fname
) - 1] = '\0';
70 in
= TIFFOpen(argv
[1], "r");
78 path_len
= strlen(fname
) + sizeof(TIFF_SUFFIX
);
79 path
= (char *) _TIFFmalloc(path_len
);
80 strncpy(path
, fname
, path_len
);
81 path
[path_len
- 1] = '\0';
82 strncat(path
, TIFF_SUFFIX
, path_len
- strlen(path
) - 1);
83 out
= TIFFOpen(path
, TIFFIsBigEndian(in
)?"wb":"wl");
91 } while (TIFFReadDirectory(in
));
100 static int first
= 1;
101 static long lastTurn
;
103 static short defname
;
108 fpnt
= fname
+ strlen(fname
);
117 #define MAXFILES 17576
118 if (fnum
== MAXFILES
) {
119 if (!defname
|| fname
[0] == 'z') {
120 fprintf(stderr
, "tiffsplit: too many files.\n");
126 if (fnum
% 676 == 0) {
129 * advance to next letter every 676 pages
130 * condition for 'z'++ will be covered above
135 * set to 'a' if we are on the very first file
140 * set the value of the last turning point
145 * start from 0 every 676 times (provided by lastTurn)
146 * this keeps us within a-z boundaries
148 fpnt
[1] = (char)((fnum
- lastTurn
) / 26) + 'a';
150 * cycle last letter every file, from a-z, then repeat
152 fpnt
[2] = (char)(fnum
% 26) + 'a';
157 tiffcp(TIFF
* in
, TIFF
* out
)
159 uint16 bitspersample
, samplesperpixel
, compression
, shortv
, *shortav
;
165 CopyField(TIFFTAG_SUBFILETYPE
, longv
);
166 CopyField(TIFFTAG_TILEWIDTH
, w
);
167 CopyField(TIFFTAG_TILELENGTH
, l
);
168 CopyField(TIFFTAG_IMAGEWIDTH
, w
);
169 CopyField(TIFFTAG_IMAGELENGTH
, l
);
170 CopyField(TIFFTAG_BITSPERSAMPLE
, bitspersample
);
171 CopyField(TIFFTAG_SAMPLESPERPIXEL
, samplesperpixel
);
172 CopyField(TIFFTAG_COMPRESSION
, compression
);
173 if (compression
== COMPRESSION_JPEG
) {
176 if (TIFFGetField(in
, TIFFTAG_JPEGTABLES
, &count
, &table
)
177 && count
> 0 && table
) {
178 TIFFSetField(out
, TIFFTAG_JPEGTABLES
, count
, table
);
181 CopyField(TIFFTAG_PHOTOMETRIC
, shortv
);
182 CopyField(TIFFTAG_PREDICTOR
, shortv
);
183 CopyField(TIFFTAG_THRESHHOLDING
, shortv
);
184 CopyField(TIFFTAG_FILLORDER
, shortv
);
185 CopyField(TIFFTAG_ORIENTATION
, shortv
);
186 CopyField(TIFFTAG_MINSAMPLEVALUE
, shortv
);
187 CopyField(TIFFTAG_MAXSAMPLEVALUE
, shortv
);
188 CopyField(TIFFTAG_XRESOLUTION
, floatv
);
189 CopyField(TIFFTAG_YRESOLUTION
, floatv
);
190 CopyField(TIFFTAG_GROUP3OPTIONS
, longv
);
191 CopyField(TIFFTAG_GROUP4OPTIONS
, longv
);
192 CopyField(TIFFTAG_RESOLUTIONUNIT
, shortv
);
193 CopyField(TIFFTAG_PLANARCONFIG
, shortv
);
194 CopyField(TIFFTAG_ROWSPERSTRIP
, longv
);
195 CopyField(TIFFTAG_XPOSITION
, floatv
);
196 CopyField(TIFFTAG_YPOSITION
, floatv
);
197 CopyField(TIFFTAG_IMAGEDEPTH
, longv
);
198 CopyField(TIFFTAG_TILEDEPTH
, longv
);
199 CopyField(TIFFTAG_SAMPLEFORMAT
, shortv
);
200 CopyField2(TIFFTAG_EXTRASAMPLES
, shortv
, shortav
);
201 { uint16
*red
, *green
, *blue
;
202 CopyField3(TIFFTAG_COLORMAP
, red
, green
, blue
);
205 CopyField2(TIFFTAG_PAGENUMBER
, shortv
, shortv2
);
207 CopyField(TIFFTAG_ARTIST
, stringv
);
208 CopyField(TIFFTAG_IMAGEDESCRIPTION
, stringv
);
209 CopyField(TIFFTAG_MAKE
, stringv
);
210 CopyField(TIFFTAG_MODEL
, stringv
);
211 CopyField(TIFFTAG_SOFTWARE
, stringv
);
212 CopyField(TIFFTAG_DATETIME
, stringv
);
213 CopyField(TIFFTAG_HOSTCOMPUTER
, stringv
);
214 CopyField(TIFFTAG_PAGENAME
, stringv
);
215 CopyField(TIFFTAG_DOCUMENTNAME
, stringv
);
216 CopyField(TIFFTAG_BADFAXLINES
, longv
);
217 CopyField(TIFFTAG_CLEANFAXDATA
, longv
);
218 CopyField(TIFFTAG_CONSECUTIVEBADFAXLINES
, longv
);
219 CopyField(TIFFTAG_FAXRECVPARAMS
, longv
);
220 CopyField(TIFFTAG_FAXRECVTIME
, longv
);
221 CopyField(TIFFTAG_FAXSUBADDRESS
, stringv
);
222 CopyField(TIFFTAG_FAXDCS
, stringv
);
224 return (cpTiles(in
, out
));
226 return (cpStrips(in
, out
));
230 cpStrips(TIFF
* in
, TIFF
* out
)
232 tmsize_t bufsize
= TIFFStripSize(in
);
233 unsigned char *buf
= (unsigned char *)_TIFFmalloc(bufsize
);
236 tstrip_t s
, ns
= TIFFNumberOfStrips(in
);
239 if (!TIFFGetField(in
, TIFFTAG_STRIPBYTECOUNTS
, &bytecounts
)) {
240 fprintf(stderr
, "tiffsplit: strip byte counts are missing\n");
243 for (s
= 0; s
< ns
; s
++) {
244 if (bytecounts
[s
] > (uint64
)bufsize
) {
245 buf
= (unsigned char *)_TIFFrealloc(buf
, (tmsize_t
)bytecounts
[s
]);
248 bufsize
= (tmsize_t
)bytecounts
[s
];
250 if (TIFFReadRawStrip(in
, s
, buf
, (tmsize_t
)bytecounts
[s
]) < 0 ||
251 TIFFWriteRawStrip(out
, s
, buf
, (tmsize_t
)bytecounts
[s
]) < 0) {
263 cpTiles(TIFF
* in
, TIFF
* out
)
265 tmsize_t bufsize
= TIFFTileSize(in
);
266 unsigned char *buf
= (unsigned char *)_TIFFmalloc(bufsize
);
269 ttile_t t
, nt
= TIFFNumberOfTiles(in
);
272 if (!TIFFGetField(in
, TIFFTAG_TILEBYTECOUNTS
, &bytecounts
)) {
273 fprintf(stderr
, "tiffsplit: tile byte counts are missing\n");
276 for (t
= 0; t
< nt
; t
++) {
277 if (bytecounts
[t
] > (uint64
) bufsize
) {
278 buf
= (unsigned char *)_TIFFrealloc(buf
, (tmsize_t
)bytecounts
[t
]);
281 bufsize
= (tmsize_t
)bytecounts
[t
];
283 if (TIFFReadRawTile(in
, t
, buf
, (tmsize_t
)bytecounts
[t
]) < 0 ||
284 TIFFWriteRawTile(out
, t
, buf
, (tmsize_t
)bytecounts
[t
]) < 0) {
295 /* vim: set ts=8 sts=8 sw=8 noet: */