1
/* $Header: /cvsroot/osrs/libtiff/tools/tiff2rgba.c,v 1.7 2003/10/03 11:22:29 dron Exp $ */
4
* Copyright (c) 1991-1997 Sam Leffler
5
* Copyright (c) 1991-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
33
#define streq(a,b) (strcmp(a,b) == 0)
34
#define CopyField(tag, v) \
35
if (TIFFGetField(in, tag, &v)) TIFFSetField(out, tag, v)
38
#define howmany(x, y) (((x)+((y)-1))/(y))
40
#define roundup(x, y) (howmany(x,y)*((uint32)(y)))
42
uint16 compression = COMPRESSION_PACKBITS;
43
uint32 rowsperstrip = (uint32) -1;
44
int process_by_block = 0; /* default is whole image at once */
48
static int tiffcvt(TIFF* in, TIFF* out);
49
static void usage(int code);
52
main(int argc, char* argv[])
59
while ((c = getopt(argc, argv, "c:r:t:bn")) != -1)
66
if (streq(optarg, "none"))
67
compression = COMPRESSION_NONE;
68
else if (streq(optarg, "packbits"))
69
compression = COMPRESSION_PACKBITS;
70
else if (streq(optarg, "lzw"))
71
compression = COMPRESSION_LZW;
72
else if (streq(optarg, "jpeg"))
73
compression = COMPRESSION_JPEG;
74
else if (streq(optarg, "zip"))
75
compression = COMPRESSION_DEFLATE;
81
rowsperstrip = atoi(optarg);
85
rowsperstrip = atoi(optarg);
97
if (argc - optind < 2)
100
out = TIFFOpen(argv[argc-1], "w");
104
for (; optind < argc-1; optind++) {
105
in = TIFFOpen(argv[optind], "r");
108
if (!tiffcvt(in, out) ||
109
!TIFFWriteDirectory(out)) {
110
(void) TIFFClose(out);
113
} while (TIFFReadDirectory(in));
114
(void) TIFFClose(in);
117
(void) TIFFClose(out);
122
cvt_by_tile( TIFF *in, TIFF *out )
125
uint32* raster; /* retrieve RGBA image */
126
uint32 width, height; /* image width & height */
127
uint32 tile_width, tile_height;
132
TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &width);
133
TIFFGetField(in, TIFFTAG_IMAGELENGTH, &height);
135
if( !TIFFGetField(in, TIFFTAG_TILEWIDTH, &tile_width)
136
|| !TIFFGetField(in, TIFFTAG_TILELENGTH, &tile_height) ) {
137
TIFFError(TIFFFileName(in), "Source image not tiled");
141
TIFFSetField(out, TIFFTAG_TILEWIDTH, tile_width );
142
TIFFSetField(out, TIFFTAG_TILELENGTH, tile_height );
145
* Allocate tile buffer
147
raster = (uint32*)_TIFFmalloc(tile_width * tile_height * sizeof (uint32));
149
TIFFError(TIFFFileName(in), "No space for raster buffer");
154
* Allocate a scanline buffer for swapping during the vertical
157
wrk_line = (uint32*)_TIFFmalloc(tile_width * sizeof (uint32));
159
TIFFError(TIFFFileName(in), "No space for raster scanline buffer");
164
* Loop over the tiles.
166
for( row = 0; ok && row < height; row += tile_height )
168
for( col = 0; ok && col < width; col += tile_width )
172
/* Read the tile into an RGBA array */
173
if (!TIFFReadRGBATile(in, col, row, raster)) {
179
* For some reason the TIFFReadRGBATile() function chooses the
180
* lower left corner as the origin. Vertically mirror scanlines.
182
for( i_row = 0; i_row < tile_height / 2; i_row++ )
184
uint32 *top_line, *bottom_line;
186
top_line = raster + tile_width * i_row;
187
bottom_line = raster + tile_width * (tile_height-i_row-1);
189
_TIFFmemcpy(wrk_line, top_line, 4*tile_width);
190
_TIFFmemcpy(top_line, bottom_line, 4*tile_width);
191
_TIFFmemcpy(bottom_line, wrk_line, 4*tile_width);
195
* Write out the result in a tile.
198
if( TIFFWriteEncodedTile( out,
199
TIFFComputeTile( out, col, row, 0, 0),
201
4 * tile_width * tile_height ) == -1 )
210
_TIFFfree( wrk_line );
216
cvt_by_strip( TIFF *in, TIFF *out )
219
uint32* raster; /* retrieve RGBA image */
220
uint32 width, height; /* image width & height */
225
TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &width);
226
TIFFGetField(in, TIFFTAG_IMAGELENGTH, &height);
228
if( !TIFFGetField(in, TIFFTAG_ROWSPERSTRIP, &rowsperstrip) ) {
229
TIFFError(TIFFFileName(in), "Source image not in strips");
233
TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
236
* Allocate strip buffer
238
raster = (uint32*)_TIFFmalloc(width * rowsperstrip * sizeof (uint32));
240
TIFFError(TIFFFileName(in), "No space for raster buffer");
245
* Allocate a scanline buffer for swapping during the vertical
248
wrk_line = (uint32*)_TIFFmalloc(width * sizeof (uint32));
250
TIFFError(TIFFFileName(in), "No space for raster scanline buffer");
255
* Loop over the strips.
257
for( row = 0; ok && row < height; row += rowsperstrip )
259
int rows_to_write, i_row;
261
/* Read the strip into an RGBA array */
262
if (!TIFFReadRGBAStrip(in, row, raster)) {
268
* Figure out the number of scanlines actually in this strip.
270
if( row + rowsperstrip > height )
271
rows_to_write = height - row;
273
rows_to_write = rowsperstrip;
276
* For some reason the TIFFReadRGBAStrip() function chooses the
277
* lower left corner as the origin. Vertically mirror scanlines.
280
for( i_row = 0; i_row < rows_to_write / 2; i_row++ )
282
uint32 *top_line, *bottom_line;
284
top_line = raster + width * i_row;
285
bottom_line = raster + width * (rows_to_write-i_row-1);
287
_TIFFmemcpy(wrk_line, top_line, 4*width);
288
_TIFFmemcpy(top_line, bottom_line, 4*width);
289
_TIFFmemcpy(bottom_line, wrk_line, 4*width);
293
* Write out the result in a strip
296
if( TIFFWriteEncodedStrip( out, row / rowsperstrip, raster,
297
4 * rows_to_write * width ) == -1 )
305
_TIFFfree( wrk_line );
313
* read the whole image into one big RGBA buffer and then write out
314
* strips from that. This is using the traditional TIFFReadRGBAImage()
319
cvt_whole_image( TIFF *in, TIFF *out )
322
uint32* raster; /* retrieve RGBA image */
323
uint32 width, height; /* image width & height */
328
TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &width);
329
TIFFGetField(in, TIFFTAG_IMAGELENGTH, &height);
331
rowsperstrip = TIFFDefaultStripSize(out, rowsperstrip);
332
TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
334
raster = (uint32*)_TIFFmalloc(width * height * sizeof (uint32));
336
TIFFError(TIFFFileName(in), "No space for raster buffer");
340
/* Read the image in one chunk into an RGBA array */
341
if (!TIFFReadRGBAImageOriented(in, width, height, raster,
342
ORIENTATION_TOPLEFT, 0)) {
348
** Do we want to strip away alpha components?
352
int pixel_count = width * height;
353
unsigned char *src, *dst;
355
src = (unsigned char *) raster;
356
dst = (unsigned char *) raster;
357
while( pixel_count > 0 )
367
/* Write out the result in strips */
369
for( row = 0; row < height; row += rowsperstrip )
371
unsigned char * raster_strip;
377
raster_strip = ((unsigned char *) raster) + 3 * row * width;
382
raster_strip = (unsigned char *) (raster + row * width);
386
if( row + rowsperstrip > height )
387
rows_to_write = height - row;
389
rows_to_write = rowsperstrip;
391
if( TIFFWriteEncodedStrip( out, row / rowsperstrip, raster_strip,
392
bytes_per_pixel * rows_to_write * width ) == -1 )
406
tiffcvt(TIFF* in, TIFF* out)
408
uint32 width, height; /* image width & height */
415
TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &width);
416
TIFFGetField(in, TIFFTAG_IMAGELENGTH, &height);
418
CopyField(TIFFTAG_SUBFILETYPE, longv);
419
TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width);
420
TIFFSetField(out, TIFFTAG_IMAGELENGTH, height);
421
TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);
422
TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
423
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
425
CopyField(TIFFTAG_FILLORDER, shortv);
426
TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
429
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 3);
431
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 4);
435
v[0] = EXTRASAMPLE_ASSOCALPHA;
436
TIFFSetField(out, TIFFTAG_EXTRASAMPLES, 1, v);
439
CopyField(TIFFTAG_XRESOLUTION, floatv);
440
CopyField(TIFFTAG_YRESOLUTION, floatv);
441
CopyField(TIFFTAG_RESOLUTIONUNIT, shortv);
442
TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
443
TIFFSetField(out, TIFFTAG_SOFTWARE, TIFFGetVersion());
444
CopyField(TIFFTAG_DOCUMENTNAME, stringv);
446
if( process_by_block && TIFFIsTiled( in ) )
447
return( cvt_by_tile( in, out ) );
448
else if( process_by_block )
449
return( cvt_by_strip( in, out ) );
451
return( cvt_whole_image( in, out ) );
454
static char* stuff[] = {
455
"usage: tiff2rgba [-c comp] [-r rows] [-b] input... output\n",
456
"where comp is one of the following compression algorithms:\n",
457
" jpeg\t\tJPEG encoding\n",
458
" zip\t\tLempel-Ziv & Welch encoding\n",
459
" lzw\t\tLempel-Ziv & Welch encoding\n",
460
" packbits\tPackBits encoding\n",
461
" none\t\tno compression\n",
462
"and the other options are:\n",
464
" -b (progress by block rather than as a whole image)\n",
465
" -n don't emit alpha component.\n",
476
fprintf(stderr, "%s\n\n", TIFFGetVersion());
477
for (i = 0; stuff[i] != NULL; i++)
478
fprintf(stderr, "%s\n", stuff[i]);