4 * Copyright (c) 1992-1997 Sam Leffler
5 * Copyright (c) 1992-1997 Silicon Graphics, Inc.
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.
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.
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
27 #include "tif_config.h"
36 extern int getopt(int, char**, char*);
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)
46 #define PATH_LENGTH 8192
48 static const char TIFF_SUFFIX
[] = ".tif";
50 static char fname
[PATH_LENGTH
];
52 static int tiffcp(TIFF
*, TIFF
*);
53 static void newfilename(void);
54 static int cpStrips(TIFF
*, TIFF
*);
55 static int cpTiles(TIFF
*, TIFF
*);
58 main(int argc
, char* argv
[])
63 fprintf(stderr
, "%s\n\n", TIFFGetVersion());
64 fprintf(stderr
, "usage: tiffsplit input.tif [prefix]\n");
68 strncpy(fname
, argv
[2], sizeof(fname
));
69 fname
[sizeof(fname
) - 1] = '\0';
71 in
= TIFFOpen(argv
[1], "r");
79 path_len
= strlen(fname
) + sizeof(TIFF_SUFFIX
);
80 path
= (char *) _TIFFmalloc(path_len
);
81 strncpy(path
, fname
, path_len
);
82 path
[path_len
- 1] = '\0';
83 strncat(path
, TIFF_SUFFIX
, path_len
- strlen(path
) - 1);
84 out
= TIFFOpen(path
, TIFFIsBigEndian(in
)?"wb":"wl");
92 } while (TIFFReadDirectory(in
));
101 static int first
= 1;
102 static long lastTurn
;
104 static short defname
;
109 fpnt
= fname
+ strlen(fname
);
118 #define MAXFILES 17576
119 if (fnum
== MAXFILES
) {
120 if (!defname
|| fname
[0] == 'z') {
121 fprintf(stderr
, "tiffsplit: too many files.\n");
127 if (fnum
% 676 == 0) {
130 * advance to next letter every 676 pages
131 * condition for 'z'++ will be covered above
136 * set to 'a' if we are on the very first file
141 * set the value of the last turning point
146 * start from 0 every 676 times (provided by lastTurn)
147 * this keeps us within a-z boundaries
149 fpnt
[1] = (char)((fnum
- lastTurn
) / 26) + 'a';
151 * cycle last letter every file, from a-z, then repeat
153 fpnt
[2] = (char)(fnum
% 26) + 'a';
158 tiffcp(TIFF
* in
, TIFF
* out
)
160 uint16 bitspersample
, samplesperpixel
, compression
, shortv
, *shortav
;
166 CopyField(TIFFTAG_SUBFILETYPE
, longv
);
167 CopyField(TIFFTAG_TILEWIDTH
, w
);
168 CopyField(TIFFTAG_TILELENGTH
, l
);
169 CopyField(TIFFTAG_IMAGEWIDTH
, w
);
170 CopyField(TIFFTAG_IMAGELENGTH
, l
);
171 CopyField(TIFFTAG_BITSPERSAMPLE
, bitspersample
);
172 CopyField(TIFFTAG_SAMPLESPERPIXEL
, samplesperpixel
);
173 CopyField(TIFFTAG_COMPRESSION
, compression
);
174 if (compression
== COMPRESSION_JPEG
) {
177 if (TIFFGetField(in
, TIFFTAG_JPEGTABLES
, &count
, &table
)
178 && count
> 0 && table
) {
179 TIFFSetField(out
, TIFFTAG_JPEGTABLES
, count
, table
);
182 CopyField(TIFFTAG_PHOTOMETRIC
, shortv
);
183 CopyField(TIFFTAG_PREDICTOR
, shortv
);
184 CopyField(TIFFTAG_THRESHHOLDING
, shortv
);
185 CopyField(TIFFTAG_FILLORDER
, shortv
);
186 CopyField(TIFFTAG_ORIENTATION
, shortv
);
187 CopyField(TIFFTAG_MINSAMPLEVALUE
, shortv
);
188 CopyField(TIFFTAG_MAXSAMPLEVALUE
, shortv
);
189 CopyField(TIFFTAG_XRESOLUTION
, floatv
);
190 CopyField(TIFFTAG_YRESOLUTION
, floatv
);
191 CopyField(TIFFTAG_GROUP3OPTIONS
, longv
);
192 CopyField(TIFFTAG_GROUP4OPTIONS
, longv
);
193 CopyField(TIFFTAG_RESOLUTIONUNIT
, shortv
);
194 CopyField(TIFFTAG_PLANARCONFIG
, shortv
);
195 CopyField(TIFFTAG_ROWSPERSTRIP
, longv
);
196 CopyField(TIFFTAG_XPOSITION
, floatv
);
197 CopyField(TIFFTAG_YPOSITION
, floatv
);
198 CopyField(TIFFTAG_IMAGEDEPTH
, longv
);
199 CopyField(TIFFTAG_TILEDEPTH
, longv
);
200 CopyField(TIFFTAG_SAMPLEFORMAT
, shortv
);
201 CopyField2(TIFFTAG_EXTRASAMPLES
, shortv
, shortav
);
202 { uint16
*red
, *green
, *blue
;
203 CopyField3(TIFFTAG_COLORMAP
, red
, green
, blue
);
206 CopyField2(TIFFTAG_PAGENUMBER
, shortv
, shortv2
);
208 CopyField(TIFFTAG_ARTIST
, stringv
);
209 CopyField(TIFFTAG_IMAGEDESCRIPTION
, stringv
);
210 CopyField(TIFFTAG_MAKE
, stringv
);
211 CopyField(TIFFTAG_MODEL
, stringv
);
212 CopyField(TIFFTAG_SOFTWARE
, stringv
);
213 CopyField(TIFFTAG_DATETIME
, stringv
);
214 CopyField(TIFFTAG_HOSTCOMPUTER
, stringv
);
215 CopyField(TIFFTAG_PAGENAME
, stringv
);
216 CopyField(TIFFTAG_DOCUMENTNAME
, stringv
);
217 CopyField(TIFFTAG_BADFAXLINES
, longv
);
218 CopyField(TIFFTAG_CLEANFAXDATA
, longv
);
219 CopyField(TIFFTAG_CONSECUTIVEBADFAXLINES
, longv
);
220 CopyField(TIFFTAG_FAXRECVPARAMS
, longv
);
221 CopyField(TIFFTAG_FAXRECVTIME
, longv
);
222 CopyField(TIFFTAG_FAXSUBADDRESS
, stringv
);
223 CopyField(TIFFTAG_FAXDCS
, stringv
);
225 return (cpTiles(in
, out
));
227 return (cpStrips(in
, out
));
231 cpStrips(TIFF
* in
, TIFF
* out
)
233 tmsize_t bufsize
= TIFFStripSize(in
);
234 unsigned char *buf
= (unsigned char *)_TIFFmalloc(bufsize
);
237 tstrip_t s
, ns
= TIFFNumberOfStrips(in
);
240 if (!TIFFGetField(in
, TIFFTAG_STRIPBYTECOUNTS
, &bytecounts
)) {
241 fprintf(stderr
, "tiffsplit: strip byte counts are missing\n");
244 for (s
= 0; s
< ns
; s
++) {
245 if (bytecounts
[s
] > (uint64
)bufsize
) {
246 buf
= (unsigned char *)_TIFFrealloc(buf
, (tmsize_t
)bytecounts
[s
]);
249 bufsize
= (tmsize_t
)bytecounts
[s
];
251 if (TIFFReadRawStrip(in
, s
, buf
, (tmsize_t
)bytecounts
[s
]) < 0 ||
252 TIFFWriteRawStrip(out
, s
, buf
, (tmsize_t
)bytecounts
[s
]) < 0) {
264 cpTiles(TIFF
* in
, TIFF
* out
)
266 tmsize_t bufsize
= TIFFTileSize(in
);
267 unsigned char *buf
= (unsigned char *)_TIFFmalloc(bufsize
);
270 ttile_t t
, nt
= TIFFNumberOfTiles(in
);
273 if (!TIFFGetField(in
, TIFFTAG_TILEBYTECOUNTS
, &bytecounts
)) {
274 fprintf(stderr
, "tiffsplit: tile byte counts are missing\n");
277 for (t
= 0; t
< nt
; t
++) {
278 if (bytecounts
[t
] > (uint64
) bufsize
) {
279 buf
= (unsigned char *)_TIFFrealloc(buf
, (tmsize_t
)bytecounts
[t
]);
282 bufsize
= (tmsize_t
)bytecounts
[t
];
284 if (TIFFReadRawTile(in
, t
, buf
, (tmsize_t
)bytecounts
[t
]) < 0 ||
285 TIFFWriteRawTile(out
, t
, buf
, (tmsize_t
)bytecounts
[t
]) < 0) {
296 /* vim: set ts=8 sts=8 sw=8 noet: */