|
kusano |
7d535a |
/* $Id: tiff2rgba.c,v 1.19 2011-02-23 21:46:09 fwarmerdam Exp $ */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Copyright (c) 1991-1997 Sam Leffler
|
|
kusano |
7d535a |
* Copyright (c) 1991-1997 Silicon Graphics, Inc.
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* Permission to use, copy, modify, distribute, and sell this software and
|
|
kusano |
7d535a |
* its documentation for any purpose is hereby granted without fee, provided
|
|
kusano |
7d535a |
* that (i) the above copyright notices and this permission notice appear in
|
|
kusano |
7d535a |
* all copies of the software and related documentation, and (ii) the names of
|
|
kusano |
7d535a |
* Sam Leffler and Silicon Graphics may not be used in any advertising or
|
|
kusano |
7d535a |
* publicity relating to the software without the specific, prior written
|
|
kusano |
7d535a |
* permission of Sam Leffler and Silicon Graphics.
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
|
|
kusano |
7d535a |
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
|
|
kusano |
7d535a |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
|
|
kusano |
7d535a |
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
|
|
kusano |
7d535a |
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
|
kusano |
7d535a |
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
|
|
kusano |
7d535a |
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
|
|
kusano |
7d535a |
* OF THIS SOFTWARE.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#include "tif_config.h"
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#include <stdio.h></stdio.h>
|
|
kusano |
7d535a |
#include <string.h></string.h>
|
|
kusano |
7d535a |
#include <stdlib.h></stdlib.h>
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#ifdef HAVE_UNISTD_H
|
|
kusano |
7d535a |
# include <unistd.h></unistd.h>
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#ifdef NEED_LIBPORT
|
|
kusano |
7d535a |
# include "libport.h"
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#include "tiffiop.h"
|
|
kusano |
7d535a |
#include "tiffio.h"
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#define streq(a,b) (strcmp(a,b) == 0)
|
|
kusano |
7d535a |
#define CopyField(tag, v) \
|
|
kusano |
7d535a |
if (TIFFGetField(in, tag, &v)) TIFFSetField(out, tag, v)
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#ifndef howmany
|
|
kusano |
7d535a |
#define howmany(x, y) (((x)+((y)-1))/(y))
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
#define roundup(x, y) (howmany(x,y)*((uint32)(y)))
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
uint16 compression = COMPRESSION_PACKBITS;
|
|
kusano |
7d535a |
uint32 rowsperstrip = (uint32) -1;
|
|
kusano |
7d535a |
int process_by_block = 0; /* default is whole image at once */
|
|
kusano |
7d535a |
int no_alpha = 0;
|
|
kusano |
7d535a |
int bigtiff_output = 0;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static int tiffcvt(TIFF* in, TIFF* out);
|
|
kusano |
7d535a |
static void usage(int code);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
int
|
|
kusano |
7d535a |
main(int argc, char* argv[])
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
TIFF *in, *out;
|
|
kusano |
7d535a |
int c;
|
|
kusano |
7d535a |
extern int optind;
|
|
kusano |
7d535a |
extern char *optarg;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
while ((c = getopt(argc, argv, "c:r:t:bn8")) != -1)
|
|
kusano |
7d535a |
switch (c) {
|
|
kusano |
7d535a |
case 'b':
|
|
kusano |
7d535a |
process_by_block = 1;
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
case 'c':
|
|
kusano |
7d535a |
if (streq(optarg, "none"))
|
|
kusano |
7d535a |
compression = COMPRESSION_NONE;
|
|
kusano |
7d535a |
else if (streq(optarg, "packbits"))
|
|
kusano |
7d535a |
compression = COMPRESSION_PACKBITS;
|
|
kusano |
7d535a |
else if (streq(optarg, "lzw"))
|
|
kusano |
7d535a |
compression = COMPRESSION_LZW;
|
|
kusano |
7d535a |
else if (streq(optarg, "jpeg"))
|
|
kusano |
7d535a |
compression = COMPRESSION_JPEG;
|
|
kusano |
7d535a |
else if (streq(optarg, "zip"))
|
|
kusano |
7d535a |
compression = COMPRESSION_DEFLATE;
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
usage(-1);
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
case 'r':
|
|
kusano |
7d535a |
rowsperstrip = atoi(optarg);
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
case 't':
|
|
kusano |
7d535a |
rowsperstrip = atoi(optarg);
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
case 'n':
|
|
kusano |
7d535a |
no_alpha = 1;
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
case '8':
|
|
kusano |
7d535a |
bigtiff_output = 1;
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
case '?':
|
|
kusano |
7d535a |
usage(0);
|
|
kusano |
7d535a |
/*NOTREACHED*/
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (argc - optind < 2)
|
|
kusano |
7d535a |
usage(-1);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
out = TIFFOpen(argv[argc-1], bigtiff_output?"w8":"w");
|
|
kusano |
7d535a |
if (out == NULL)
|
|
kusano |
7d535a |
return (-2);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
for (; optind < argc-1; optind++) {
|
|
kusano |
7d535a |
in = TIFFOpen(argv[optind], "r");
|
|
kusano |
7d535a |
if (in != NULL) {
|
|
kusano |
7d535a |
do {
|
|
kusano |
7d535a |
if (!tiffcvt(in, out) ||
|
|
kusano |
7d535a |
!TIFFWriteDirectory(out)) {
|
|
kusano |
7d535a |
(void) TIFFClose(out);
|
|
kusano |
7d535a |
(void) TIFFClose(in);
|
|
kusano |
7d535a |
return (1);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
} while (TIFFReadDirectory(in));
|
|
kusano |
7d535a |
(void) TIFFClose(in);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
(void) TIFFClose(out);
|
|
kusano |
7d535a |
return (0);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static int
|
|
kusano |
7d535a |
cvt_by_tile( TIFF *in, TIFF *out )
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
uint32* raster; /* retrieve RGBA image */
|
|
kusano |
7d535a |
uint32 width, height; /* image width & height */
|
|
kusano |
7d535a |
uint32 tile_width, tile_height;
|
|
kusano |
7d535a |
uint32 row, col;
|
|
kusano |
7d535a |
uint32 *wrk_line;
|
|
kusano |
7d535a |
int ok = 1;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &width);
|
|
kusano |
7d535a |
TIFFGetField(in, TIFFTAG_IMAGELENGTH, &height);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if( !TIFFGetField(in, TIFFTAG_TILEWIDTH, &tile_width)
|
|
kusano |
7d535a |
|| !TIFFGetField(in, TIFFTAG_TILELENGTH, &tile_height) ) {
|
|
kusano |
7d535a |
TIFFError(TIFFFileName(in), "Source image not tiled");
|
|
kusano |
7d535a |
return (0);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
TIFFSetField(out, TIFFTAG_TILEWIDTH, tile_width );
|
|
kusano |
7d535a |
TIFFSetField(out, TIFFTAG_TILELENGTH, tile_height );
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Allocate tile buffer
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
raster = (uint32*)_TIFFmalloc(tile_width * tile_height * sizeof (uint32));
|
|
kusano |
7d535a |
if (raster == 0) {
|
|
kusano |
7d535a |
TIFFError(TIFFFileName(in), "No space for raster buffer");
|
|
kusano |
7d535a |
return (0);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Allocate a scanline buffer for swapping during the vertical
|
|
kusano |
7d535a |
* mirroring pass.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
wrk_line = (uint32*)_TIFFmalloc(tile_width * sizeof (uint32));
|
|
kusano |
7d535a |
if (!wrk_line) {
|
|
kusano |
7d535a |
TIFFError(TIFFFileName(in), "No space for raster scanline buffer");
|
|
kusano |
7d535a |
ok = 0;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Loop over the tiles.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
for( row = 0; ok && row < height; row += tile_height )
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
for( col = 0; ok && col < width; col += tile_width )
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
uint32 i_row;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Read the tile into an RGBA array */
|
|
kusano |
7d535a |
if (!TIFFReadRGBATile(in, col, row, raster)) {
|
|
kusano |
7d535a |
ok = 0;
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* XXX: raster array has 4-byte unsigned integer type, that is why
|
|
kusano |
7d535a |
* we should rearrange it here.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
#if HOST_BIGENDIAN
|
|
kusano |
7d535a |
TIFFSwabArrayOfLong(raster, tile_width * tile_height);
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* For some reason the TIFFReadRGBATile() function chooses the
|
|
kusano |
7d535a |
* lower left corner as the origin. Vertically mirror scanlines.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
for( i_row = 0; i_row < tile_height / 2; i_row++ )
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
uint32 *top_line, *bottom_line;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
top_line = raster + tile_width * i_row;
|
|
kusano |
7d535a |
bottom_line = raster + tile_width * (tile_height-i_row-1);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
_TIFFmemcpy(wrk_line, top_line, 4*tile_width);
|
|
kusano |
7d535a |
_TIFFmemcpy(top_line, bottom_line, 4*tile_width);
|
|
kusano |
7d535a |
_TIFFmemcpy(bottom_line, wrk_line, 4*tile_width);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Write out the result in a tile.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if( TIFFWriteEncodedTile( out,
|
|
kusano |
7d535a |
TIFFComputeTile( out, col, row, 0, 0),
|
|
kusano |
7d535a |
raster,
|
|
kusano |
7d535a |
4 * tile_width * tile_height ) == -1 )
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
ok = 0;
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
_TIFFfree( raster );
|
|
kusano |
7d535a |
_TIFFfree( wrk_line );
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
return ok;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static int
|
|
kusano |
7d535a |
cvt_by_strip( TIFF *in, TIFF *out )
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
uint32* raster; /* retrieve RGBA image */
|
|
kusano |
7d535a |
uint32 width, height; /* image width & height */
|
|
kusano |
7d535a |
uint32 row;
|
|
kusano |
7d535a |
uint32 *wrk_line;
|
|
kusano |
7d535a |
int ok = 1;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &width);
|
|
kusano |
7d535a |
TIFFGetField(in, TIFFTAG_IMAGELENGTH, &height);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if( !TIFFGetField(in, TIFFTAG_ROWSPERSTRIP, &rowsperstrip) ) {
|
|
kusano |
7d535a |
TIFFError(TIFFFileName(in), "Source image not in strips");
|
|
kusano |
7d535a |
return (0);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Allocate strip buffer
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
raster = (uint32*)_TIFFmalloc(width * rowsperstrip * sizeof (uint32));
|
|
kusano |
7d535a |
if (raster == 0) {
|
|
kusano |
7d535a |
TIFFError(TIFFFileName(in), "No space for raster buffer");
|
|
kusano |
7d535a |
return (0);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Allocate a scanline buffer for swapping during the vertical
|
|
kusano |
7d535a |
* mirroring pass.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
wrk_line = (uint32*)_TIFFmalloc(width * sizeof (uint32));
|
|
kusano |
7d535a |
if (!wrk_line) {
|
|
kusano |
7d535a |
TIFFError(TIFFFileName(in), "No space for raster scanline buffer");
|
|
kusano |
7d535a |
ok = 0;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Loop over the strips.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
for( row = 0; ok && row < height; row += rowsperstrip )
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
int rows_to_write, i_row;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Read the strip into an RGBA array */
|
|
kusano |
7d535a |
if (!TIFFReadRGBAStrip(in, row, raster)) {
|
|
kusano |
7d535a |
ok = 0;
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* XXX: raster array has 4-byte unsigned integer type, that is why
|
|
kusano |
7d535a |
* we should rearrange it here.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
#if HOST_BIGENDIAN
|
|
kusano |
7d535a |
TIFFSwabArrayOfLong(raster, width * rowsperstrip);
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Figure out the number of scanlines actually in this strip.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
if( row + rowsperstrip > height )
|
|
kusano |
7d535a |
rows_to_write = height - row;
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
rows_to_write = rowsperstrip;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* For some reason the TIFFReadRGBAStrip() function chooses the
|
|
kusano |
7d535a |
* lower left corner as the origin. Vertically mirror scanlines.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
for( i_row = 0; i_row < rows_to_write / 2; i_row++ )
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
uint32 *top_line, *bottom_line;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
top_line = raster + width * i_row;
|
|
kusano |
7d535a |
bottom_line = raster + width * (rows_to_write-i_row-1);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
_TIFFmemcpy(wrk_line, top_line, 4*width);
|
|
kusano |
7d535a |
_TIFFmemcpy(top_line, bottom_line, 4*width);
|
|
kusano |
7d535a |
_TIFFmemcpy(bottom_line, wrk_line, 4*width);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Write out the result in a strip
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if( TIFFWriteEncodedStrip( out, row / rowsperstrip, raster,
|
|
kusano |
7d535a |
4 * rows_to_write * width ) == -1 )
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
ok = 0;
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
_TIFFfree( raster );
|
|
kusano |
7d535a |
_TIFFfree( wrk_line );
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
return ok;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* cvt_whole_image()
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* read the whole image into one big RGBA buffer and then write out
|
|
kusano |
7d535a |
* strips from that. This is using the traditional TIFFReadRGBAImage()
|
|
kusano |
7d535a |
* API that we trust.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static int
|
|
kusano |
7d535a |
cvt_whole_image( TIFF *in, TIFF *out )
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
uint32* raster; /* retrieve RGBA image */
|
|
kusano |
7d535a |
uint32 width, height; /* image width & height */
|
|
kusano |
7d535a |
uint32 row;
|
|
kusano |
7d535a |
size_t pixel_count;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &width);
|
|
kusano |
7d535a |
TIFFGetField(in, TIFFTAG_IMAGELENGTH, &height);
|
|
kusano |
7d535a |
pixel_count = width * height;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* XXX: Check the integer overflow. */
|
|
kusano |
7d535a |
if (!width || !height || pixel_count / width != height) {
|
|
kusano |
7d535a |
TIFFError(TIFFFileName(in),
|
|
kusano |
7d535a |
"Malformed input file; can't allocate buffer for raster of %lux%lu size",
|
|
kusano |
7d535a |
(unsigned long)width, (unsigned long)height);
|
|
kusano |
7d535a |
return 0;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
rowsperstrip = TIFFDefaultStripSize(out, rowsperstrip);
|
|
kusano |
7d535a |
TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
raster = (uint32*)_TIFFCheckMalloc(in, pixel_count, sizeof(uint32), "raster buffer");
|
|
kusano |
7d535a |
if (raster == 0) {
|
|
kusano |
7d535a |
TIFFError(TIFFFileName(in), "Failed to allocate buffer (%lu elements of %lu each)",
|
|
kusano |
7d535a |
(unsigned long)pixel_count, (unsigned long)sizeof(uint32));
|
|
kusano |
7d535a |
return (0);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Read the image in one chunk into an RGBA array */
|
|
kusano |
7d535a |
if (!TIFFReadRGBAImageOriented(in, width, height, raster,
|
|
kusano |
7d535a |
ORIENTATION_TOPLEFT, 0)) {
|
|
kusano |
7d535a |
_TIFFfree(raster);
|
|
kusano |
7d535a |
return (0);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* XXX: raster array has 4-byte unsigned integer type, that is why
|
|
kusano |
7d535a |
* we should rearrange it here.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
#if HOST_BIGENDIAN
|
|
kusano |
7d535a |
TIFFSwabArrayOfLong(raster, width * height);
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Do we want to strip away alpha components?
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
if (no_alpha)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
size_t count = pixel_count;
|
|
kusano |
7d535a |
unsigned char *src, *dst;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
src = dst = (unsigned char *) raster;
|
|
kusano |
7d535a |
while (count > 0)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
*(dst++) = *(src++);
|
|
kusano |
7d535a |
*(dst++) = *(src++);
|
|
kusano |
7d535a |
*(dst++) = *(src++);
|
|
kusano |
7d535a |
src++;
|
|
kusano |
7d535a |
count--;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Write out the result in strips
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
for (row = 0; row < height; row += rowsperstrip)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
unsigned char * raster_strip;
|
|
kusano |
7d535a |
int rows_to_write;
|
|
kusano |
7d535a |
int bytes_per_pixel;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (no_alpha)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
raster_strip = ((unsigned char *) raster) + 3 * row * width;
|
|
kusano |
7d535a |
bytes_per_pixel = 3;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
raster_strip = (unsigned char *) (raster + row * width);
|
|
kusano |
7d535a |
bytes_per_pixel = 4;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if( row + rowsperstrip > height )
|
|
kusano |
7d535a |
rows_to_write = height - row;
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
rows_to_write = rowsperstrip;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if( TIFFWriteEncodedStrip( out, row / rowsperstrip, raster_strip,
|
|
kusano |
7d535a |
bytes_per_pixel * rows_to_write * width ) == -1 )
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
_TIFFfree( raster );
|
|
kusano |
7d535a |
return 0;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
_TIFFfree( raster );
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
return 1;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static int
|
|
kusano |
7d535a |
tiffcvt(TIFF* in, TIFF* out)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
uint32 width, height; /* image width & height */
|
|
kusano |
7d535a |
uint16 shortv;
|
|
kusano |
7d535a |
float floatv;
|
|
kusano |
7d535a |
char *stringv;
|
|
kusano |
7d535a |
uint32 longv;
|
|
kusano |
7d535a |
uint16 v[1];
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &width);
|
|
kusano |
7d535a |
TIFFGetField(in, TIFFTAG_IMAGELENGTH, &height);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
CopyField(TIFFTAG_SUBFILETYPE, longv);
|
|
kusano |
7d535a |
TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width);
|
|
kusano |
7d535a |
TIFFSetField(out, TIFFTAG_IMAGELENGTH, height);
|
|
kusano |
7d535a |
TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);
|
|
kusano |
7d535a |
TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
|
|
kusano |
7d535a |
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
CopyField(TIFFTAG_FILLORDER, shortv);
|
|
kusano |
7d535a |
TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if( no_alpha )
|
|
kusano |
7d535a |
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 3);
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 4);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if( !no_alpha )
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
v[0] = EXTRASAMPLE_ASSOCALPHA;
|
|
kusano |
7d535a |
TIFFSetField(out, TIFFTAG_EXTRASAMPLES, 1, v);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
CopyField(TIFFTAG_XRESOLUTION, floatv);
|
|
kusano |
7d535a |
CopyField(TIFFTAG_YRESOLUTION, floatv);
|
|
kusano |
7d535a |
CopyField(TIFFTAG_RESOLUTIONUNIT, shortv);
|
|
kusano |
7d535a |
TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
|
|
kusano |
7d535a |
TIFFSetField(out, TIFFTAG_SOFTWARE, TIFFGetVersion());
|
|
kusano |
7d535a |
CopyField(TIFFTAG_DOCUMENTNAME, stringv);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if( process_by_block && TIFFIsTiled( in ) )
|
|
kusano |
7d535a |
return( cvt_by_tile( in, out ) );
|
|
kusano |
7d535a |
else if( process_by_block )
|
|
kusano |
7d535a |
return( cvt_by_strip( in, out ) );
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
return( cvt_whole_image( in, out ) );
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static char* stuff[] = {
|
|
kusano |
7d535a |
"usage: tiff2rgba [-c comp] [-r rows] [-b] [-n] [-8] input... output",
|
|
kusano |
7d535a |
"where comp is one of the following compression algorithms:",
|
|
kusano |
7d535a |
" jpeg\t\tJPEG encoding",
|
|
kusano |
7d535a |
" zip\t\tLempel-Ziv & Welch encoding",
|
|
kusano |
7d535a |
" lzw\t\tLempel-Ziv & Welch encoding",
|
|
kusano |
7d535a |
" packbits\tPackBits encoding",
|
|
kusano |
7d535a |
" none\t\tno compression",
|
|
kusano |
7d535a |
"and the other options are:",
|
|
kusano |
7d535a |
" -r\trows/strip",
|
|
kusano |
7d535a |
" -b (progress by block rather than as a whole image)",
|
|
kusano |
7d535a |
" -n don't emit alpha component.",
|
|
kusano |
7d535a |
" -8 write BigTIFF file instead of ClassicTIFF",
|
|
kusano |
7d535a |
NULL
|
|
kusano |
7d535a |
};
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static void
|
|
kusano |
7d535a |
usage(int code)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
char buf[BUFSIZ];
|
|
kusano |
7d535a |
int i;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
setbuf(stderr, buf);
|
|
kusano |
7d535a |
fprintf(stderr, "%s\n\n", TIFFGetVersion());
|
|
kusano |
7d535a |
for (i = 0; stuff[i] != NULL; i++)
|
|
kusano |
7d535a |
fprintf(stderr, "%s\n", stuff[i]);
|
|
kusano |
7d535a |
exit(code);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* vim: set ts=8 sts=8 sw=8 noet: */
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Local Variables:
|
|
kusano |
7d535a |
* mode: c
|
|
kusano |
7d535a |
* c-basic-offset: 8
|
|
kusano |
7d535a |
* fill-column: 78
|
|
kusano |
7d535a |
* End:
|
|
kusano |
7d535a |
*/
|