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 static char fname
[1024+1];
48 static int tiffcp(TIFF
*, TIFF
*);
49 static void newfilename(void);
50 static int cpStrips(TIFF
*, TIFF
*);
51 static int cpTiles(TIFF
*, TIFF
*);
54 main(int argc
, char* argv
[])
59 fprintf(stderr
, "%s\n\n", TIFFGetVersion());
60 fprintf(stderr
, "usage: tiffsplit input.tif [prefix]\n");
64 strcpy(fname
, argv
[2]);
65 in
= TIFFOpen(argv
[1], "r");
72 out
= TIFFOpen(path
, TIFFIsBigEndian(in
)?"wb":"wl");
78 } while (TIFFReadDirectory(in
));
95 fpnt
= fname
+ strlen(fname
);
104 #define MAXFILES 17576
105 if (fnum
== MAXFILES
) {
106 if (!defname
|| fname
[0] == 'z') {
107 fprintf(stderr
, "tiffsplit: too many files.\n");
113 if (fnum
% 676 == 0) {
116 * advance to next letter every 676 pages
117 * condition for 'z'++ will be covered above
122 * set to 'a' if we are on the very first file
127 * set the value of the last turning point
132 * start from 0 every 676 times (provided by lastTurn)
133 * this keeps us within a-z boundaries
135 fpnt
[1] = (char)((fnum
- lastTurn
) / 26) + 'a';
137 * cycle last letter every file, from a-z, then repeat
139 fpnt
[2] = (char)(fnum
% 26) + 'a';
144 tiffcp(TIFF
* in
, TIFF
* out
)
146 uint16 bitspersample
, samplesperpixel
, compression
, shortv
, *shortav
;
152 CopyField(TIFFTAG_SUBFILETYPE
, longv
);
153 CopyField(TIFFTAG_TILEWIDTH
, w
);
154 CopyField(TIFFTAG_TILELENGTH
, l
);
155 CopyField(TIFFTAG_IMAGEWIDTH
, w
);
156 CopyField(TIFFTAG_IMAGELENGTH
, l
);
157 CopyField(TIFFTAG_BITSPERSAMPLE
, bitspersample
);
158 CopyField(TIFFTAG_SAMPLESPERPIXEL
, samplesperpixel
);
159 CopyField(TIFFTAG_COMPRESSION
, compression
);
160 if (compression
== COMPRESSION_JPEG
) {
163 if (TIFFGetField(in
, TIFFTAG_JPEGTABLES
, &count
, &table
)
164 && count
> 0 && table
) {
165 TIFFSetField(out
, TIFFTAG_JPEGTABLES
, count
, table
);
168 CopyField(TIFFTAG_PHOTOMETRIC
, shortv
);
169 CopyField(TIFFTAG_PREDICTOR
, shortv
);
170 CopyField(TIFFTAG_THRESHHOLDING
, shortv
);
171 CopyField(TIFFTAG_FILLORDER
, shortv
);
172 CopyField(TIFFTAG_ORIENTATION
, shortv
);
173 CopyField(TIFFTAG_MINSAMPLEVALUE
, shortv
);
174 CopyField(TIFFTAG_MAXSAMPLEVALUE
, shortv
);
175 CopyField(TIFFTAG_XRESOLUTION
, floatv
);
176 CopyField(TIFFTAG_YRESOLUTION
, floatv
);
177 CopyField(TIFFTAG_GROUP3OPTIONS
, longv
);
178 CopyField(TIFFTAG_GROUP4OPTIONS
, longv
);
179 CopyField(TIFFTAG_RESOLUTIONUNIT
, shortv
);
180 CopyField(TIFFTAG_PLANARCONFIG
, shortv
);
181 CopyField(TIFFTAG_ROWSPERSTRIP
, longv
);
182 CopyField(TIFFTAG_XPOSITION
, floatv
);
183 CopyField(TIFFTAG_YPOSITION
, floatv
);
184 CopyField(TIFFTAG_IMAGEDEPTH
, longv
);
185 CopyField(TIFFTAG_TILEDEPTH
, longv
);
186 CopyField(TIFFTAG_SAMPLEFORMAT
, longv
);
187 CopyField2(TIFFTAG_EXTRASAMPLES
, shortv
, shortav
);
188 { uint16
*red
, *green
, *blue
;
189 CopyField3(TIFFTAG_COLORMAP
, red
, green
, blue
);
192 CopyField2(TIFFTAG_PAGENUMBER
, shortv
, shortv2
);
194 CopyField(TIFFTAG_ARTIST
, stringv
);
195 CopyField(TIFFTAG_IMAGEDESCRIPTION
, stringv
);
196 CopyField(TIFFTAG_MAKE
, stringv
);
197 CopyField(TIFFTAG_MODEL
, stringv
);
198 CopyField(TIFFTAG_SOFTWARE
, stringv
);
199 CopyField(TIFFTAG_DATETIME
, stringv
);
200 CopyField(TIFFTAG_HOSTCOMPUTER
, stringv
);
201 CopyField(TIFFTAG_PAGENAME
, stringv
);
202 CopyField(TIFFTAG_DOCUMENTNAME
, stringv
);
203 CopyField(TIFFTAG_BADFAXLINES
, longv
);
204 CopyField(TIFFTAG_CLEANFAXDATA
, longv
);
205 CopyField(TIFFTAG_CONSECUTIVEBADFAXLINES
, longv
);
206 CopyField(TIFFTAG_FAXRECVPARAMS
, longv
);
207 CopyField(TIFFTAG_FAXRECVTIME
, longv
);
208 CopyField(TIFFTAG_FAXSUBADDRESS
, stringv
);
209 CopyField(TIFFTAG_FAXDCS
, stringv
);
211 return (cpTiles(in
, out
));
213 return (cpStrips(in
, out
));
217 cpStrips(TIFF
* in
, TIFF
* out
)
219 tsize_t bufsize
= TIFFStripSize(in
);
220 unsigned char *buf
= (unsigned char *)_TIFFmalloc(bufsize
);
223 tstrip_t s
, ns
= TIFFNumberOfStrips(in
);
226 TIFFGetField(in
, TIFFTAG_STRIPBYTECOUNTS
, &bytecounts
);
227 for (s
= 0; s
< ns
; s
++) {
228 if (bytecounts
[s
] > (uint32
)bufsize
) {
229 buf
= (unsigned char *)_TIFFrealloc(buf
, bytecounts
[s
]);
232 bufsize
= bytecounts
[s
];
234 if (TIFFReadRawStrip(in
, s
, buf
, bytecounts
[s
]) < 0 ||
235 TIFFWriteRawStrip(out
, s
, buf
, bytecounts
[s
]) < 0) {
247 cpTiles(TIFF
* in
, TIFF
* out
)
249 tsize_t bufsize
= TIFFTileSize(in
);
250 unsigned char *buf
= (unsigned char *)_TIFFmalloc(bufsize
);
253 ttile_t t
, nt
= TIFFNumberOfTiles(in
);
256 TIFFGetField(in
, TIFFTAG_TILEBYTECOUNTS
, &bytecounts
);
257 for (t
= 0; t
< nt
; t
++) {
258 if (bytecounts
[t
] > (uint32
) bufsize
) {
259 buf
= (unsigned char *)_TIFFrealloc(buf
, bytecounts
[t
]);
262 bufsize
= bytecounts
[t
];
264 if (TIFFReadRawTile(in
, t
, buf
, bytecounts
[t
]) < 0 ||
265 TIFFWriteRawTile(out
, t
, buf
, bytecounts
[t
]) < 0) {
276 /* vim: set ts=8 sts=8 sw=8 noet: */