| 1 |
/* |
| 2 |
* Copyright (c) 1988-1997 Sam Leffler |
| 3 |
* Copyright (c) 1991-1997 Silicon Graphics, Inc. |
| 4 |
* |
| 5 |
* Permission to use, copy, modify, distribute, and sell this software and |
| 6 |
* its documentation for any purpose is hereby granted without fee, provided |
| 7 |
* that (i) the above copyright notices and this permission notice appear in |
| 8 |
* all copies of the software and related documentation, and (ii) the names of |
| 9 |
* Sam Leffler and Silicon Graphics may not be used in any advertising or |
| 10 |
* publicity relating to the software without the specific, prior written |
| 11 |
* permission of Sam Leffler and Silicon Graphics. |
| 12 |
* |
| 13 |
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
| 14 |
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
| 15 |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 16 |
* |
| 17 |
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR |
| 18 |
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
| 19 |
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 20 |
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
| 21 |
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 22 |
* OF THIS SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
#include "tif_config.h" |
| 26 |
|
| 27 |
#include <ctype.h> |
| 28 |
#include <limits.h> |
| 29 |
#include <stdio.h> |
| 30 |
#include <stdlib.h> |
| 31 |
#include <string.h> |
| 32 |
|
| 33 |
#ifdef HAVE_UNISTD_H |
| 34 |
#include <unistd.h> |
| 35 |
#endif |
| 36 |
|
| 37 |
#ifdef NEED_LIBPORT |
| 38 |
#include "libport.h" |
| 39 |
#endif |
| 40 |
|
| 41 |
#include "rasterfile.h" |
| 42 |
#include "tiffio.h" |
| 43 |
|
| 44 |
#ifndef howmany |
| 45 |
#define howmany(x, y) (((x) + ((y)-1)) / (y)) |
| 46 |
#endif |
| 47 |
#define streq(a, b) (strcmp(a, b) == 0) |
| 48 |
#define strneq(a, b, n) (strncmp(a, b, n) == 0) |
| 49 |
|
| 50 |
static uint16_t compression = (uint16_t)-1; |
| 51 |
static int jpegcolormode = JPEGCOLORMODE_RGB; |
| 52 |
static int quality = 75; /* JPEG quality */ |
| 53 |
static uint16_t predictor = 0; |
| 54 |
|
| 55 |
static void usage(void); |
| 56 |
static int processCompressOptions(char *); |
| 57 |
|
| 58 |
int main(int argc, char *argv[]) |
| 59 |
{ |
| 60 |
unsigned char *buf; |
| 61 |
long row; |
| 62 |
tsize_t linebytes, scanline; |
| 63 |
TIFF *out; |
| 64 |
FILE *in; |
| 65 |
struct rasterfile h; |
| 66 |
uint16_t photometric; |
| 67 |
uint16_t config = PLANARCONFIG_CONTIG; |
| 68 |
uint32_t rowsperstrip = (uint32_t)-1; |
| 69 |
int c; |
| 70 |
#if !HAVE_DECL_OPTARG |
| 71 |
extern int optind; |
| 72 |
extern char *optarg; |
| 73 |
#endif |
| 74 |
|
| 75 |
while ((c = getopt(argc, argv, "c:r:h")) != -1) |
| 76 |
switch (c) |
| 77 |
{ |
| 78 |
case 'c': /* compression scheme */ |
| 79 |
if (!processCompressOptions(optarg)) |
| 80 |
usage(); |
| 81 |
break; |
| 82 |
case 'r': /* rows/strip */ |
| 83 |
rowsperstrip = atoi(optarg); |
| 84 |
break; |
| 85 |
case 'h': |
| 86 |
usage(); |
| 87 |
/*NOTREACHED*/ |
| 88 |
} |
| 89 |
if (argc - optind != 2) |
| 90 |
usage(); |
| 91 |
in = fopen(argv[optind], "rb"); |
| 92 |
if (in == NULL) |
| 93 |
{ |
| 94 |
fprintf(stderr, "%s: Can not open.\n", argv[optind]); |
| 95 |
return (-1); |
| 96 |
} |
| 97 |
if (fread(&h, sizeof(h), 1, in) != 1) |
| 98 |
{ |
| 99 |
fprintf(stderr, "%s: Can not read header.\n", argv[optind]); |
| 100 |
fclose(in); |
| 101 |
return (-2); |
| 102 |
} |
| 103 |
if (strcmp(h.ras_magic, RAS_MAGIC) == 0) |
| 104 |
{ |
| 105 |
#ifndef WORDS_BIGENDIAN |
| 106 |
TIFFSwabLong((uint32_t *)&h.ras_width); |
| 107 |
TIFFSwabLong((uint32_t *)&h.ras_height); |
| 108 |
TIFFSwabLong((uint32_t *)&h.ras_depth); |
| 109 |
TIFFSwabLong((uint32_t *)&h.ras_length); |
| 110 |
TIFFSwabLong((uint32_t *)&h.ras_type); |
| 111 |
TIFFSwabLong((uint32_t *)&h.ras_maptype); |
| 112 |
TIFFSwabLong((uint32_t *)&h.ras_maplength); |
| 113 |
#endif |
| 114 |
} |
| 115 |
else if (strcmp(h.ras_magic, RAS_MAGIC_INV) == 0) |
| 116 |
{ |
| 117 |
#ifdef WORDS_BIGENDIAN |
| 118 |
TIFFSwabLong((uint32_t *)&h.ras_width); |
| 119 |
TIFFSwabLong((uint32_t *)&h.ras_height); |
| 120 |
TIFFSwabLong((uint32_t *)&h.ras_depth); |
| 121 |
TIFFSwabLong((uint32_t *)&h.ras_length); |
| 122 |
TIFFSwabLong((uint32_t *)&h.ras_type); |
| 123 |
TIFFSwabLong((uint32_t *)&h.ras_maptype); |
| 124 |
TIFFSwabLong((uint32_t *)&h.ras_maplength); |
| 125 |
#endif |
| 126 |
} |
| 127 |
else |
| 128 |
{ |
| 129 |
fprintf(stderr, "%s: Not a rasterfile.\n", argv[optind]); |
| 130 |
fclose(in); |
| 131 |
return (-3); |
| 132 |
} |
| 133 |
if ((h.ras_width <= 0) || (h.ras_width >= INT_MAX) || (h.ras_height <= 0) || |
| 134 |
(h.ras_height >= INT_MAX) || (h.ras_depth <= 0) || |
| 135 |
(h.ras_depth >= INT_MAX) || (h.ras_length <= 0) || |
| 136 |
(h.ras_length >= INT_MAX) || (h.ras_type <= 0) || |
| 137 |
(h.ras_maptype <= 0) || (h.ras_maplength <= 0) || |
| 138 |
(h.ras_maplength >= INT_MAX)) |
| 139 |
{ |
| 140 |
fprintf(stderr, "%s: Improper image header.\n", argv[optind]); |
| 141 |
fclose(in); |
| 142 |
return (-2); |
| 143 |
} |
| 144 |
if ((h.ras_depth != 1) && (h.ras_depth != 8) && (h.ras_depth != 24)) |
| 145 |
{ |
| 146 |
fprintf(stderr, "%s: Improper image depth (%d).\n", argv[optind], |
| 147 |
h.ras_depth); |
| 148 |
fclose(in); |
| 149 |
return (-2); |
| 150 |
} |
| 151 |
out = TIFFOpen(argv[optind + 1], "w"); |
| 152 |
if (out == NULL) |
| 153 |
{ |
| 154 |
fclose(in); |
| 155 |
return (-4); |
| 156 |
} |
| 157 |
TIFFSetField(out, TIFFTAG_IMAGEWIDTH, (uint32_t)h.ras_width); |
| 158 |
TIFFSetField(out, TIFFTAG_IMAGELENGTH, (uint32_t)h.ras_height); |
| 159 |
TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); |
| 160 |
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, h.ras_depth > 8 ? 3 : 1); |
| 161 |
TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, h.ras_depth > 1 ? 8 : 1); |
| 162 |
TIFFSetField(out, TIFFTAG_PLANARCONFIG, config); |
| 163 |
if (h.ras_maptype != RMT_NONE) |
| 164 |
{ |
| 165 |
uint16_t *red; |
| 166 |
register uint16_t *map; |
| 167 |
register int i, j; |
| 168 |
int mapsize; |
| 169 |
|
| 170 |
buf = (unsigned char *)_TIFFmalloc(h.ras_maplength); |
| 171 |
if (buf == NULL) |
| 172 |
{ |
| 173 |
fprintf(stderr, "No space to read in colormap.\n"); |
| 174 |
fclose(in); |
| 175 |
(void)TIFFClose(out); |
| 176 |
return (-5); |
| 177 |
} |
| 178 |
if (fread(buf, h.ras_maplength, 1, in) != 1) |
| 179 |
{ |
| 180 |
fprintf(stderr, "%s: Read error on colormap.\n", argv[optind]); |
| 181 |
fclose(in); |
| 182 |
(void)TIFFClose(out); |
| 183 |
return (-6); |
| 184 |
} |
| 185 |
mapsize = 1 << h.ras_depth; |
| 186 |
if (h.ras_maplength > mapsize * 3) |
| 187 |
{ |
| 188 |
fprintf(stderr, "%s: Huh, %d colormap entries, should be %d?\n", |
| 189 |
argv[optind], h.ras_maplength, mapsize * 3); |
| 190 |
fclose(in); |
| 191 |
(void)TIFFClose(out); |
| 192 |
return (-7); |
| 193 |
} |
| 194 |
red = (uint16_t *)_TIFFmalloc(mapsize * 3 * sizeof(uint16_t)); |
| 195 |
if (red == NULL) |
| 196 |
{ |
| 197 |
fprintf(stderr, "No space for colormap.\n"); |
| 198 |
fclose(in); |
| 199 |
(void)TIFFClose(out); |
| 200 |
return (-8); |
| 201 |
} |
| 202 |
map = red; |
| 203 |
for (j = 0; j < 3; j++) |
| 204 |
{ |
| 205 |
#define SCALE(x) (((x) * ((1L << 16) - 1)) / 255) |
| 206 |
for (i = h.ras_maplength / 3; i-- > 0;) |
| 207 |
*map++ = SCALE(*buf++); |
| 208 |
if ((i = h.ras_maplength / 3) < mapsize) |
| 209 |
{ |
| 210 |
i = mapsize - i; |
| 211 |
_TIFFmemset(map, 0, i * sizeof(uint16_t)); |
| 212 |
map += i; |
| 213 |
} |
| 214 |
} |
| 215 |
TIFFSetField(out, TIFFTAG_COLORMAP, red, red + mapsize, |
| 216 |
red + 2 * mapsize); |
| 217 |
photometric = PHOTOMETRIC_PALETTE; |
| 218 |
if (compression == (uint16_t)-1) |
| 219 |
compression = COMPRESSION_PACKBITS; |
| 220 |
TIFFSetField(out, TIFFTAG_COMPRESSION, compression); |
| 221 |
} |
| 222 |
else |
| 223 |
{ |
| 224 |
/* XXX this is bogus... */ |
| 225 |
photometric = |
| 226 |
h.ras_depth == 24 ? PHOTOMETRIC_RGB : PHOTOMETRIC_MINISBLACK; |
| 227 |
if (compression == (uint16_t)-1) |
| 228 |
compression = COMPRESSION_LZW; |
| 229 |
TIFFSetField(out, TIFFTAG_COMPRESSION, compression); |
| 230 |
} |
| 231 |
switch (compression) |
| 232 |
{ |
| 233 |
case COMPRESSION_JPEG: |
| 234 |
if (photometric == PHOTOMETRIC_RGB && |
| 235 |
jpegcolormode == JPEGCOLORMODE_RGB) |
| 236 |
photometric = PHOTOMETRIC_YCBCR; |
| 237 |
TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality); |
| 238 |
TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, jpegcolormode); |
| 239 |
break; |
| 240 |
case COMPRESSION_LZW: |
| 241 |
case COMPRESSION_ADOBE_DEFLATE: |
| 242 |
case COMPRESSION_DEFLATE: |
| 243 |
if (predictor != 0) |
| 244 |
TIFFSetField(out, TIFFTAG_PREDICTOR, predictor); |
| 245 |
break; |
| 246 |
} |
| 247 |
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric); |
| 248 |
linebytes = ((h.ras_depth * h.ras_width + 15) >> 3) & ~1; |
| 249 |
scanline = TIFFScanlineSize(out); |
| 250 |
if (scanline > linebytes) |
| 251 |
{ |
| 252 |
buf = (unsigned char *)_TIFFmalloc(scanline); |
| 253 |
_TIFFmemset(buf + linebytes, 0, scanline - linebytes); |
| 254 |
} |
| 255 |
else |
| 256 |
buf = (unsigned char *)_TIFFmalloc(linebytes); |
| 257 |
TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, |
| 258 |
TIFFDefaultStripSize(out, rowsperstrip)); |
| 259 |
for (row = 0; row < h.ras_height; row++) |
| 260 |
{ |
| 261 |
if (fread(buf, linebytes, 1, in) != 1) |
| 262 |
{ |
| 263 |
fprintf(stderr, "%s: scanline %ld: Read error.\n", argv[optind], |
| 264 |
row); |
| 265 |
break; |
| 266 |
} |
| 267 |
if (h.ras_type == RT_STANDARD && h.ras_depth == 24) |
| 268 |
{ |
| 269 |
tsize_t cc = h.ras_width; |
| 270 |
unsigned char *cp = buf; |
| 271 |
#define SWAP(a, b) \ |
| 272 |
{ \ |
| 273 |
unsigned char t = (a); \ |
| 274 |
(a) = (b); \ |
| 275 |
(b) = t; \ |
| 276 |
} |
| 277 |
do |
| 278 |
{ |
| 279 |
SWAP(cp[0], cp[2]); |
| 280 |
cp += 3; |
| 281 |
} while (--cc); |
| 282 |
} |
| 283 |
if (TIFFWriteScanline(out, buf, row, 0) < 0) |
| 284 |
break; |
| 285 |
} |
| 286 |
(void)TIFFClose(out); |
| 287 |
fclose(in); |
| 288 |
return (0); |
| 289 |
} |
| 290 |
|
| 291 |
static int processCompressOptions(char *opt) |
| 292 |
{ |
| 293 |
if (streq(opt, "none")) |
| 294 |
compression = COMPRESSION_NONE; |
| 295 |
else if (streq(opt, "packbits")) |
| 296 |
compression = COMPRESSION_PACKBITS; |
| 297 |
else if (strneq(opt, "jpeg", 4)) |
| 298 |
{ |
| 299 |
char *cp = strchr(opt, ':'); |
| 300 |
|
| 301 |
compression = COMPRESSION_JPEG; |
| 302 |
while (cp) |
| 303 |
{ |
| 304 |
if (isdigit((int)cp[1])) |
| 305 |
quality = atoi(cp + 1); |
| 306 |
else if (cp[1] == 'r') |
| 307 |
jpegcolormode = JPEGCOLORMODE_RAW; |
| 308 |
else |
| 309 |
usage(); |
| 310 |
|
| 311 |
cp = strchr(cp + 1, ':'); |
| 312 |
} |
| 313 |
} |
| 314 |
else if (strneq(opt, "lzw", 3)) |
| 315 |
{ |
| 316 |
char *cp = strchr(opt, ':'); |
| 317 |
if (cp) |
| 318 |
predictor = atoi(cp + 1); |
| 319 |
compression = COMPRESSION_LZW; |
| 320 |
} |
| 321 |
else if (strneq(opt, "zip", 3)) |
| 322 |
{ |
| 323 |
char *cp = strchr(opt, ':'); |
| 324 |
if (cp) |
| 325 |
predictor = atoi(cp + 1); |
| 326 |
compression = COMPRESSION_ADOBE_DEFLATE; |
| 327 |
} |
| 328 |
else |
| 329 |
return (0); |
| 330 |
return (1); |
| 331 |
} |
| 332 |
|
| 333 |
char *stuff[] = { |
| 334 |
"usage: ras2tiff [options] input.ras output.tif", |
| 335 |
"where options are:", |
| 336 |
" -r # make each strip have no more than # rows", |
| 337 |
"", |
| 338 |
" -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding", |
| 339 |
" -c zip[:opts] compress output with deflate encoding", |
| 340 |
" -c jpeg[:opts] compress output with JPEG encoding", |
| 341 |
" -c packbits compress output with packbits encoding", |
| 342 |
" -c none use no compression algorithm on output", |
| 343 |
"", |
| 344 |
"JPEG options:", |
| 345 |
" # set compression quality level (0-100, default 75)", |
| 346 |
" r output color image as RGB rather than YCbCr", |
| 347 |
"For example, -c jpeg:r:50 to get JPEG-encoded RGB data with 50% comp. " |
| 348 |
"quality", |
| 349 |
"", |
| 350 |
"LZW and deflate options:", |
| 351 |
" # set predictor value", |
| 352 |
"For example, -c lzw:2 to get LZW-encoded data with horizontal " |
| 353 |
"differencing", |
| 354 |
" -h this help message", |
| 355 |
NULL}; |
| 356 |
|
| 357 |
static void usage(void) |
| 358 |
{ |
| 359 |
char buf[BUFSIZ]; |
| 360 |
int i; |
| 361 |
|
| 362 |
setbuf(stderr, buf); |
| 363 |
fprintf(stderr, "%s\n\n", TIFFGetVersion()); |
| 364 |
for (i = 0; stuff[i] != NULL; i++) |
| 365 |
fprintf(stderr, "%s\n", stuff[i]); |
| 366 |
exit(-1); |
| 367 |
} |