2
static char id[] = "$Id: tif2ras.c,v 1.2 1999/11/28 20:15:36 mwelles Exp $";
5
* tif2ras.c - Converts from a Tagged Image File Format image to a Sun Raster.
7
* Copyright (c) 1990 by Sun Microsystems, Inc.
9
* Author: Patrick J. Naughton
10
* naughton@wind.sun.com
12
* Permission to use, copy, modify, and distribute this software and its
13
* documentation for any purpose and without fee is hereby granted,
14
* provided that the above copyright notice appear in all copies and that
15
* both that copyright notice and this permission notice appear in
16
* supporting documentation.
18
* This file is provided AS IS with no warranties of any kind. The author
19
* shall have no liability with respect to the infringement of copyrights,
20
* trade secrets or any patents by this file or any part thereof. In no
21
* event will the author be liable for any lost revenue or profits or
22
* other special, indirect and consequential damages.
24
* Comments and additions should be sent to the author:
28
* 2550 Garcia Ave, MS 14-40
29
* Mountain View, CA 94043
34
* 06-Mar-90: Change to byte encoded rasterfiles.
35
* fix bug in call to ReadScanline().
36
* fix bug in CVT() macro.
37
* fix assignment of td, (missing &).
40
* This program takes a MicroSoft/Aldus "Tagged Image File Format" image or
41
* "TIFF" file as input and writes a Sun Rasterfile [see rasterfile(5)]. The
42
* output file may be standard output, but the input TIFF file must be a real
43
* file since seek(2) is used.
47
#include <pixrect/pixrect_hs.h>
53
#define CVT(x) (((x) * 255) / ((1L<<16)-1))
55
boolean Verbose = False;
56
char *pname; /* program name (used for error messages) */
63
fprintf(stderr, s1, pname, s2);
70
error("usage: %s -[vq] TIFFfile [rasterfile]\n", NULL);
95
short samplesperpixel;
101
Pixrect *pix; /* The Sun Pixrect */
102
colormap_t Colormap; /* The Pixrect Colormap */
107
setbuf(stderr, NULL);
111
if ((++argv)[0][0] == '-')
112
switch (argv[0][1]) {
120
fprintf(stderr, "%s: illegal option -%c.\n", pname,
124
else if (inf == NULL)
126
else if (outf == NULL)
134
error("%s: can't read input file from a stream.\n", NULL);
137
fprintf(stderr, "Reading %s...", inf);
139
tif = TIFFOpen(inf, "r");
142
error("%s: error opening TIFF file %s", inf);
145
TIFFPrintDirectory(tif, stderr, True, False, False);
146
TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bitspersample);
147
if (bitspersample > 8)
148
error("%s: can't handle more than 8-bits per sample\n", NULL);
150
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
151
switch (samplesperpixel) {
153
if (bitspersample == 1)
163
error("%s: only handle 1-channel gray scale or 3-channel color\n");
166
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
167
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
170
fprintf(stderr, "%dx%dx%d image, ", width, height, depth);
172
fprintf(stderr, "%d bits/sample, %d samples/pixel, ",
173
bitspersample, samplesperpixel);
175
pix = mem_create(width, height, depth);
176
if (pix == (Pixrect *) NULL)
177
error("%s: can't allocate memory for output pixrect...\n", NULL);
179
numcolors = (1 << bitspersample);
181
TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric);
182
if (numcolors == 2) {
184
fprintf(stderr, "monochrome ");
185
Colormap.type = RMT_NONE;
187
Colormap.map[0] = Colormap.map[1] = Colormap.map[2] = NULL;
189
switch (photometric) {
190
case PHOTOMETRIC_MINISBLACK:
192
fprintf(stderr, "%d graylevels (min=black), ", numcolors);
193
Map = (u_char *) malloc(numcolors * sizeof(u_char));
194
for (i = 0; i < numcolors; i++)
195
Map[i] = (255 * i) / numcolors;
196
Colormap.type = RMT_EQUAL_RGB;
197
Colormap.length = numcolors;
198
Colormap.map[0] = Colormap.map[1] = Colormap.map[2] = Map;
200
case PHOTOMETRIC_MINISWHITE:
202
fprintf(stderr, "%d graylevels (min=white), ", numcolors);
203
Map = (u_char *) malloc(numcolors * sizeof(u_char));
204
for (i = 0; i < numcolors; i++)
205
Map[i] = 255 - ((255 * i) / numcolors);
206
Colormap.type = RMT_EQUAL_RGB;
207
Colormap.length = numcolors;
208
Colormap.map[0] = Colormap.map[1] = Colormap.map[2] = Map;
210
case PHOTOMETRIC_RGB:
212
fprintf(stderr, "truecolor ");
213
Colormap.type = RMT_NONE;
215
Colormap.map[0] = Colormap.map[1] = Colormap.map[2] = NULL;
217
case PHOTOMETRIC_PALETTE:
219
fprintf(stderr, "colormapped ");
220
Colormap.type = RMT_EQUAL_RGB;
221
Colormap.length = numcolors;
222
memset(red, 0, sizeof(red));
223
memset(green, 0, sizeof(green));
224
memset(blue, 0, sizeof(blue));
225
TIFFGetField(tif, TIFFTAG_COLORMAP,
226
&redcolormap, &greencolormap, &bluecolormap);
227
for (i = 0; i < numcolors; i++) {
228
red[i] = (u_char) CVT(redcolormap[i]);
229
green[i] = (u_char) CVT(greencolormap[i]);
230
blue[i] = (u_char) CVT(bluecolormap[i]);
232
Colormap.map[0] = red;
233
Colormap.map[1] = green;
234
Colormap.map[2] = blue;
236
case PHOTOMETRIC_MASK:
237
error("%s: Don't know how to handle PHOTOMETRIC_MASK\n");
239
case PHOTOMETRIC_DEPTH:
240
error("%s: Don't know how to handle PHOTOMETRIC_DEPTH\n");
243
error("%s: unknown photometric (cmap): %d\n", photometric);
247
buf = (u_char *) malloc(TIFFScanlineSize(tif));
249
error("%s: can't allocate memory for scanline buffer...\n", NULL);
251
for (row = 0; row < height; row++) {
252
if (TIFFReadScanline(tif, buf, row, 0) < 0)
253
error("%s: bad data read on line: %d\n", row);
255
outp = (u_char *) mprd_addr(mpr_d(pix), 0, row);
256
switch (photometric) {
257
case PHOTOMETRIC_RGB:
258
if (samplesperpixel == 4)
259
for (col = 0; col < width; col++) {
260
*outp++ = *inp++; /* Blue */
261
*outp++ = *inp++; /* Green */
262
*outp++ = *inp++; /* Red */
263
inp++; /* skip alpha channel */
266
for (col = 0; col < width; col++) {
267
*outp++ = *inp++; /* Blue */
268
*outp++ = *inp++; /* Green */
269
*outp++ = *inp++; /* Red */
272
case PHOTOMETRIC_MINISWHITE:
273
case PHOTOMETRIC_MINISBLACK:
274
switch (bitspersample) {
276
for (col = 0; col < ((width + 7) / 8); col++)
280
for (col = 0; col < ((width + 3) / 4); col++) {
281
*outp++ = (*inp >> 6) & 3;
282
*outp++ = (*inp >> 4) & 3;
283
*outp++ = (*inp >> 2) & 3;
284
*outp++ = *inp++ & 3;
288
for (col = 0; col < width / 2; col++) {
290
*outp++ = *inp++ & 0xf;
294
for (col = 0; col < width; col++)
298
error("%s: bad bits/sample: %d\n", bitspersample);
301
case PHOTOMETRIC_PALETTE:
302
memcpy(outp, inp, width);
305
error("%s: unknown photometric (write): %d\n", photometric);
312
fprintf(stderr, "done.\n");
314
if (outf == NULL || strcmp(outf, "Standard Output") == 0) {
315
outf = "Standard Output";
318
if (!(fp = fopen(outf, "w")))
319
error("%s: %s couldn't be opened for writing.\n", outf);
323
fprintf(stderr, "Writing rasterfile in %s...", outf);
325
if (pr_dump(pix, fp, &Colormap, RT_BYTE_ENCODED, 0) == PIX_ERR)
326
error("%s: error writing Sun Rasterfile: %s\n", outf);
329
fprintf(stderr, "done.\n");