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