|
roentgen |
b75cab |
/* $Id: ras2tiff.c,v 1.18 2010-03-10 18:56:49 bfriesen Exp $ */
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
/*
|
|
roentgen |
b75cab |
* Copyright (c) 1988-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 <stdlib.h></stdlib.h>
|
|
roentgen |
b75cab |
#include <string.h></string.h>
|
|
roentgen |
b75cab |
#include <ctype.h></ctype.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 "rasterfile.h"
|
|
roentgen |
b75cab |
#include "tiffio.h"
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
#ifndef howmany
|
|
roentgen |
b75cab |
#define howmany(x, y) (((x)+((y)-1))/(y))
|
|
roentgen |
b75cab |
#endif
|
|
roentgen |
b75cab |
#define streq(a,b) (strcmp(a,b) == 0)
|
|
roentgen |
b75cab |
#define strneq(a,b,n) (strncmp(a,b,n) == 0)
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
static uint16 compression = (uint16) -1;
|
|
roentgen |
b75cab |
static int jpegcolormode = JPEGCOLORMODE_RGB;
|
|
roentgen |
b75cab |
static int quality = 75; /* JPEG quality */
|
|
roentgen |
b75cab |
static uint16 predictor = 0;
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
static void usage(void);
|
|
roentgen |
b75cab |
static int processCompressOptions(char*);
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
int
|
|
roentgen |
b75cab |
main(int argc, char* argv[])
|
|
roentgen |
b75cab |
{
|
|
roentgen |
b75cab |
unsigned char* buf;
|
|
roentgen |
b75cab |
long row;
|
|
roentgen |
b75cab |
tsize_t linebytes, scanline;
|
|
roentgen |
b75cab |
TIFF *out;
|
|
roentgen |
b75cab |
FILE *in;
|
|
roentgen |
b75cab |
struct rasterfile h;
|
|
roentgen |
b75cab |
uint16 photometric;
|
|
roentgen |
b75cab |
uint16 config = PLANARCONFIG_CONTIG;
|
|
roentgen |
b75cab |
uint32 rowsperstrip = (uint32) -1;
|
|
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:h")) != -1)
|
|
roentgen |
b75cab |
switch (c) {
|
|
roentgen |
b75cab |
case 'c': /* compression scheme */
|
|
roentgen |
b75cab |
if (!processCompressOptions(optarg))
|
|
roentgen |
b75cab |
usage();
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case 'r': /* rows/strip */
|
|
roentgen |
b75cab |
rowsperstrip = atoi(optarg);
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case 'h':
|
|
roentgen |
b75cab |
usage();
|
|
roentgen |
b75cab |
/*NOTREACHED*/
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
if (argc - optind != 2)
|
|
roentgen |
b75cab |
usage();
|
|
roentgen |
b75cab |
in = fopen(argv[optind], "rb");
|
|
roentgen |
b75cab |
if (in == NULL) {
|
|
roentgen |
b75cab |
fprintf(stderr, "%s: Can not open.\n", argv[optind]);
|
|
roentgen |
b75cab |
return (-1);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
if (fread(&h, sizeof (h), 1, in) != 1) {
|
|
roentgen |
b75cab |
fprintf(stderr, "%s: Can not read header.\n", argv[optind]);
|
|
roentgen |
b75cab |
fclose(in);
|
|
roentgen |
b75cab |
return (-2);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
if (strcmp(h.ras_magic, RAS_MAGIC) == 0) {
|
|
roentgen |
b75cab |
#ifndef WORDS_BIGENDIAN
|
|
roentgen |
b75cab |
TIFFSwabLong((uint32 *)&h.ras_width);
|
|
roentgen |
b75cab |
TIFFSwabLong((uint32 *)&h.ras_height);
|
|
roentgen |
b75cab |
TIFFSwabLong((uint32 *)&h.ras_depth);
|
|
roentgen |
b75cab |
TIFFSwabLong((uint32 *)&h.ras_length);
|
|
roentgen |
b75cab |
TIFFSwabLong((uint32 *)&h.ras_type);
|
|
roentgen |
b75cab |
TIFFSwabLong((uint32 *)&h.ras_maptype);
|
|
roentgen |
b75cab |
TIFFSwabLong((uint32 *)&h.ras_maplength);
|
|
roentgen |
b75cab |
#endif
|
|
roentgen |
b75cab |
} else if (strcmp(h.ras_magic, RAS_MAGIC_INV) == 0) {
|
|
roentgen |
b75cab |
#ifdef WORDS_BIGENDIAN
|
|
roentgen |
b75cab |
TIFFSwabLong((uint32 *)&h.ras_width);
|
|
roentgen |
b75cab |
TIFFSwabLong((uint32 *)&h.ras_height);
|
|
roentgen |
b75cab |
TIFFSwabLong((uint32 *)&h.ras_depth);
|
|
roentgen |
b75cab |
TIFFSwabLong((uint32 *)&h.ras_length);
|
|
roentgen |
b75cab |
TIFFSwabLong((uint32 *)&h.ras_type);
|
|
roentgen |
b75cab |
TIFFSwabLong((uint32 *)&h.ras_maptype);
|
|
roentgen |
b75cab |
TIFFSwabLong((uint32 *)&h.ras_maplength);
|
|
roentgen |
b75cab |
#endif
|
|
roentgen |
b75cab |
} else {
|
|
roentgen |
b75cab |
fprintf(stderr, "%s: Not a rasterfile.\n", argv[optind]);
|
|
roentgen |
b75cab |
fclose(in);
|
|
roentgen |
b75cab |
return (-3);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
out = TIFFOpen(argv[optind+1], "w");
|
|
roentgen |
b75cab |
if (out == NULL)
|
|
roentgen |
b75cab |
{
|
|
roentgen |
b75cab |
fclose(in);
|
|
roentgen |
b75cab |
return (-4);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
TIFFSetField(out, TIFFTAG_IMAGEWIDTH, (uint32) h.ras_width);
|
|
roentgen |
b75cab |
TIFFSetField(out, TIFFTAG_IMAGELENGTH, (uint32) h.ras_height);
|
|
roentgen |
b75cab |
TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
|
|
roentgen |
b75cab |
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, h.ras_depth > 8 ? 3 : 1);
|
|
roentgen |
b75cab |
TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, h.ras_depth > 1 ? 8 : 1);
|
|
roentgen |
b75cab |
TIFFSetField(out, TIFFTAG_PLANARCONFIG, config);
|
|
roentgen |
b75cab |
if (h.ras_maptype != RMT_NONE) {
|
|
roentgen |
b75cab |
uint16* red;
|
|
roentgen |
b75cab |
register uint16* map;
|
|
roentgen |
b75cab |
register int i, j;
|
|
roentgen |
b75cab |
int mapsize;
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
buf = (unsigned char *)_TIFFmalloc(h.ras_maplength);
|
|
roentgen |
b75cab |
if (buf == NULL) {
|
|
roentgen |
b75cab |
fprintf(stderr, "No space to read in colormap.\n");
|
|
roentgen |
b75cab |
return (-5);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
if (fread(buf, h.ras_maplength, 1, in) != 1) {
|
|
roentgen |
b75cab |
fprintf(stderr, "%s: Read error on colormap.\n",
|
|
roentgen |
b75cab |
argv[optind]);
|
|
roentgen |
b75cab |
return (-6);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
mapsize = 1<
|
|
roentgen |
b75cab |
if (h.ras_maplength > mapsize*3) {
|
|
roentgen |
b75cab |
fprintf(stderr,
|
|
roentgen |
b75cab |
"%s: Huh, %ld colormap entries, should be %d?\n",
|
|
roentgen |
b75cab |
argv[optind], h.ras_maplength, mapsize*3);
|
|
roentgen |
b75cab |
return (-7);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
red = (uint16*)_TIFFmalloc(mapsize * 3 * sizeof (uint16));
|
|
roentgen |
b75cab |
if (red == NULL) {
|
|
roentgen |
b75cab |
fprintf(stderr, "No space for colormap.\n");
|
|
roentgen |
b75cab |
return (-8);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
map = red;
|
|
roentgen |
b75cab |
for (j = 0; j < 3; j++) {
|
|
roentgen |
b75cab |
#define SCALE(x) (((x)*((1L<<16)-1))/255)
|
|
roentgen |
b75cab |
for (i = h.ras_maplength/3; i-- > 0;)
|
|
roentgen |
b75cab |
*map++ = SCALE(*buf++);
|
|
roentgen |
b75cab |
if ((i = h.ras_maplength/3) < mapsize) {
|
|
roentgen |
b75cab |
i = mapsize - i;
|
|
roentgen |
b75cab |
_TIFFmemset(map, 0, i*sizeof (uint16));
|
|
roentgen |
b75cab |
map += i;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
TIFFSetField(out, TIFFTAG_COLORMAP,
|
|
roentgen |
b75cab |
red, red + mapsize, red + 2*mapsize);
|
|
roentgen |
b75cab |
photometric = PHOTOMETRIC_PALETTE;
|
|
roentgen |
b75cab |
if (compression == (uint16) -1)
|
|
roentgen |
b75cab |
compression = COMPRESSION_PACKBITS;
|
|
roentgen |
b75cab |
TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
|
|
roentgen |
b75cab |
} else {
|
|
roentgen |
b75cab |
/* XXX this is bogus... */
|
|
roentgen |
b75cab |
photometric = h.ras_depth == 24 ?
|
|
roentgen |
b75cab |
PHOTOMETRIC_RGB : PHOTOMETRIC_MINISBLACK;
|
|
roentgen |
b75cab |
if (compression == (uint16) -1)
|
|
roentgen |
b75cab |
compression = COMPRESSION_LZW;
|
|
roentgen |
b75cab |
TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
switch (compression) {
|
|
roentgen |
b75cab |
case COMPRESSION_JPEG:
|
|
roentgen |
b75cab |
if (photometric == PHOTOMETRIC_RGB && jpegcolormode == JPEGCOLORMODE_RGB)
|
|
roentgen |
b75cab |
photometric = PHOTOMETRIC_YCBCR;
|
|
roentgen |
b75cab |
TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality);
|
|
roentgen |
b75cab |
TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, jpegcolormode);
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case COMPRESSION_LZW:
|
|
roentgen |
b75cab |
case COMPRESSION_DEFLATE:
|
|
roentgen |
b75cab |
if (predictor != 0)
|
|
roentgen |
b75cab |
TIFFSetField(out, TIFFTAG_PREDICTOR, predictor);
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric);
|
|
roentgen |
b75cab |
linebytes = ((h.ras_depth*h.ras_width+15) >> 3) &~ 1;
|
|
roentgen |
b75cab |
scanline = TIFFScanlineSize(out);
|
|
roentgen |
b75cab |
if (scanline > linebytes) {
|
|
roentgen |
b75cab |
buf = (unsigned char *)_TIFFmalloc(scanline);
|
|
roentgen |
b75cab |
_TIFFmemset(buf+linebytes, 0, scanline-linebytes);
|
|
roentgen |
b75cab |
} else
|
|
roentgen |
b75cab |
buf = (unsigned char *)_TIFFmalloc(linebytes);
|
|
roentgen |
b75cab |
TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
|
|
roentgen |
b75cab |
TIFFDefaultStripSize(out, rowsperstrip));
|
|
roentgen |
b75cab |
for (row = 0; row < h.ras_height; row++) {
|
|
roentgen |
b75cab |
if (fread(buf, linebytes, 1, in) != 1) {
|
|
roentgen |
b75cab |
fprintf(stderr, "%s: scanline %ld: Read error.\n",
|
|
roentgen |
b75cab |
argv[optind], row);
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
if (h.ras_type == RT_STANDARD && h.ras_depth == 24) {
|
|
roentgen |
b75cab |
tsize_t cc = h.ras_width;
|
|
roentgen |
b75cab |
unsigned char* cp = buf;
|
|
roentgen |
b75cab |
#define SWAP(a,b) { unsigned char t = (a); (a) = (b); (b) = t; }
|
|
roentgen |
b75cab |
do {
|
|
roentgen |
b75cab |
SWAP(cp[0], cp[2]);
|
|
roentgen |
b75cab |
cp += 3;
|
|
roentgen |
b75cab |
} while (--cc);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
if (TIFFWriteScanline(out, buf, row, 0) < 0)
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
(void) TIFFClose(out);
|
|
roentgen |
b75cab |
fclose(in);
|
|
roentgen |
b75cab |
return (0);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
static int
|
|
roentgen |
b75cab |
processCompressOptions(char* opt)
|
|
roentgen |
b75cab |
{
|
|
roentgen |
b75cab |
if (streq(opt, "none"))
|
|
roentgen |
b75cab |
compression = COMPRESSION_NONE;
|
|
roentgen |
b75cab |
else if (streq(opt, "packbits"))
|
|
roentgen |
b75cab |
compression = COMPRESSION_PACKBITS;
|
|
roentgen |
b75cab |
else if (strneq(opt, "jpeg", 4)) {
|
|
roentgen |
b75cab |
char* cp = strchr(opt, ':');
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
compression = COMPRESSION_JPEG;
|
|
roentgen |
b75cab |
while( cp )
|
|
roentgen |
b75cab |
{
|
|
roentgen |
b75cab |
if (isdigit((int)cp[1]))
|
|
roentgen |
b75cab |
quality = atoi(cp+1);
|
|
roentgen |
b75cab |
else if (cp[1] == 'r' )
|
|
roentgen |
b75cab |
jpegcolormode = JPEGCOLORMODE_RAW;
|
|
roentgen |
b75cab |
else
|
|
roentgen |
b75cab |
usage();
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
cp = strchr(cp+1,':');
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
} else if (strneq(opt, "lzw", 3)) {
|
|
roentgen |
b75cab |
char* cp = strchr(opt, ':');
|
|
roentgen |
b75cab |
if (cp)
|
|
roentgen |
b75cab |
predictor = atoi(cp+1);
|
|
roentgen |
b75cab |
compression = COMPRESSION_LZW;
|
|
roentgen |
b75cab |
} else if (strneq(opt, "zip", 3)) {
|
|
roentgen |
b75cab |
char* cp = strchr(opt, ':');
|
|
roentgen |
b75cab |
if (cp)
|
|
roentgen |
b75cab |
predictor = atoi(cp+1);
|
|
roentgen |
b75cab |
compression = COMPRESSION_DEFLATE;
|
|
roentgen |
b75cab |
} else
|
|
roentgen |
b75cab |
return (0);
|
|
roentgen |
b75cab |
return (1);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
char* stuff[] = {
|
|
roentgen |
b75cab |
"usage: ras2tiff [options] input.ras output.tif",
|
|
roentgen |
b75cab |
"where options are:",
|
|
roentgen |
b75cab |
" -r # make each strip have no more than # rows",
|
|
roentgen |
b75cab |
"",
|
|
roentgen |
b75cab |
" -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding",
|
|
roentgen |
b75cab |
" -c zip[:opts] compress output with deflate encoding",
|
|
roentgen |
b75cab |
" -c jpeg[:opts] compress output with JPEG encoding",
|
|
roentgen |
b75cab |
" -c packbits compress output with packbits encoding",
|
|
roentgen |
b75cab |
" -c none use no compression algorithm on output",
|
|
roentgen |
b75cab |
"",
|
|
roentgen |
b75cab |
"JPEG options:",
|
|
roentgen |
b75cab |
" # set compression quality level (0-100, default 75)",
|
|
roentgen |
b75cab |
" r output color image as RGB rather than YCbCr",
|
|
roentgen |
b75cab |
"For example, -c jpeg:r:50 to get JPEG-encoded RGB data with 50% comp. quality",
|
|
roentgen |
b75cab |
"",
|
|
roentgen |
b75cab |
"LZW and deflate options:",
|
|
roentgen |
b75cab |
" # set predictor value",
|
|
roentgen |
b75cab |
"For example, -c lzw:2 to get LZW-encoded data with horizontal differencing",
|
|
roentgen |
b75cab |
" -h this help message",
|
|
roentgen |
b75cab |
NULL
|
|
roentgen |
b75cab |
};
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
static void
|
|
roentgen |
b75cab |
usage(void)
|
|
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(-1);
|
|
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 |
*/
|