]>
git.saurik.com Git - wxWidgets.git/blob - src/tiff/contrib/ras/tif2ras.c
4 * tif2ras.c - Converts from a Tagged Image File Format image to a Sun Raster.
6 * Copyright (c) 1990 by Sun Microsystems, Inc.
8 * Author: Patrick J. Naughton
9 * naughton@wind.sun.com
11 * Permission to use, copy, modify, and distribute this software and its
12 * documentation for any purpose and without fee is hereby granted,
13 * provided that the above copyright notice appear in all copies and that
14 * both that copyright notice and this permission notice appear in
15 * supporting documentation.
17 * This file is provided AS IS with no warranties of any kind. The author
18 * shall have no liability with respect to the infringement of copyrights,
19 * trade secrets or any patents by this file or any part thereof. In no
20 * event will the author be liable for any lost revenue or profits or
21 * other special, indirect and consequential damages.
23 * Comments and additions should be sent to the author:
27 * 2550 Garcia Ave, MS 14-40
28 * Mountain View, CA 94043
33 * 06-Mar-90: Change to byte encoded rasterfiles.
34 * fix bug in call to ReadScanline().
35 * fix bug in CVT() macro.
36 * fix assignment of td, (missing &).
39 * This program takes a MicroSoft/Aldus "Tagged Image File Format" image or
40 * "TIFF" file as input and writes a Sun Rasterfile [see rasterfile(5)]. The
41 * output file may be standard output, but the input TIFF file must be a real
42 * file since seek(2) is used.
46 #include <pixrect/pixrect_hs.h>
52 #define CVT(x) (((x) * 255) / ((1L<<16)-1))
54 boolean Verbose
= False
;
55 char *pname
; /* program name (used for error messages) */
62 fprintf(stderr
, s1
, pname
, s2
);
69 error("usage: %s -[vq] TIFFfile [rasterfile]\n", NULL
);
94 short samplesperpixel
;
100 Pixrect
*pix
; /* The Sun Pixrect */
101 colormap_t Colormap
; /* The Pixrect Colormap */
106 setbuf(stderr
, NULL
);
110 if ((++argv
)[0][0] == '-')
111 switch (argv
[0][1]) {
119 fprintf(stderr
, "%s: illegal option -%c.\n", pname
,
123 else if (inf
== NULL
)
125 else if (outf
== NULL
)
133 error("%s: can't read input file from a stream.\n", NULL
);
136 fprintf(stderr
, "Reading %s...", inf
);
138 tif
= TIFFOpen(inf
, "r");
141 error("%s: error opening TIFF file %s", inf
);
144 TIFFPrintDirectory(tif
, stderr
, True
, False
, False
);
145 TIFFGetField(tif
, TIFFTAG_BITSPERSAMPLE
, &bitspersample
);
146 if (bitspersample
> 8)
147 error("%s: can't handle more than 8-bits per sample\n", NULL
);
149 TIFFGetField(tif
, TIFFTAG_SAMPLESPERPIXEL
, &samplesperpixel
);
150 switch (samplesperpixel
) {
152 if (bitspersample
== 1)
162 error("%s: only handle 1-channel gray scale or 3-channel color\n");
165 TIFFGetField(tif
, TIFFTAG_IMAGEWIDTH
, &width
);
166 TIFFGetField(tif
, TIFFTAG_IMAGELENGTH
, &height
);
169 fprintf(stderr
, "%dx%dx%d image, ", width
, height
, depth
);
171 fprintf(stderr
, "%d bits/sample, %d samples/pixel, ",
172 bitspersample
, samplesperpixel
);
174 pix
= mem_create(width
, height
, depth
);
175 if (pix
== (Pixrect
*) NULL
)
176 error("%s: can't allocate memory for output pixrect...\n", NULL
);
178 numcolors
= (1 << bitspersample
);
180 TIFFGetField(tif
, TIFFTAG_PHOTOMETRIC
, &photometric
);
181 if (numcolors
== 2) {
183 fprintf(stderr
, "monochrome ");
184 Colormap
.type
= RMT_NONE
;
186 Colormap
.map
[0] = Colormap
.map
[1] = Colormap
.map
[2] = NULL
;
188 switch (photometric
) {
189 case PHOTOMETRIC_MINISBLACK
:
191 fprintf(stderr
, "%d graylevels (min=black), ", numcolors
);
192 Map
= (u_char
*) malloc(numcolors
* sizeof(u_char
));
193 for (i
= 0; i
< numcolors
; i
++)
194 Map
[i
] = (255 * i
) / numcolors
;
195 Colormap
.type
= RMT_EQUAL_RGB
;
196 Colormap
.length
= numcolors
;
197 Colormap
.map
[0] = Colormap
.map
[1] = Colormap
.map
[2] = Map
;
199 case PHOTOMETRIC_MINISWHITE
:
201 fprintf(stderr
, "%d graylevels (min=white), ", numcolors
);
202 Map
= (u_char
*) malloc(numcolors
* sizeof(u_char
));
203 for (i
= 0; i
< numcolors
; i
++)
204 Map
[i
] = 255 - ((255 * i
) / numcolors
);
205 Colormap
.type
= RMT_EQUAL_RGB
;
206 Colormap
.length
= numcolors
;
207 Colormap
.map
[0] = Colormap
.map
[1] = Colormap
.map
[2] = Map
;
209 case PHOTOMETRIC_RGB
:
211 fprintf(stderr
, "truecolor ");
212 Colormap
.type
= RMT_NONE
;
214 Colormap
.map
[0] = Colormap
.map
[1] = Colormap
.map
[2] = NULL
;
216 case PHOTOMETRIC_PALETTE
:
218 fprintf(stderr
, "colormapped ");
219 Colormap
.type
= RMT_EQUAL_RGB
;
220 Colormap
.length
= numcolors
;
221 memset(red
, 0, sizeof(red
));
222 memset(green
, 0, sizeof(green
));
223 memset(blue
, 0, sizeof(blue
));
224 TIFFGetField(tif
, TIFFTAG_COLORMAP
,
225 &redcolormap
, &greencolormap
, &bluecolormap
);
226 for (i
= 0; i
< numcolors
; i
++) {
227 red
[i
] = (u_char
) CVT(redcolormap
[i
]);
228 green
[i
] = (u_char
) CVT(greencolormap
[i
]);
229 blue
[i
] = (u_char
) CVT(bluecolormap
[i
]);
231 Colormap
.map
[0] = red
;
232 Colormap
.map
[1] = green
;
233 Colormap
.map
[2] = blue
;
235 case PHOTOMETRIC_MASK
:
236 error("%s: Don't know how to handle PHOTOMETRIC_MASK\n");
238 case PHOTOMETRIC_DEPTH
:
239 error("%s: Don't know how to handle PHOTOMETRIC_DEPTH\n");
242 error("%s: unknown photometric (cmap): %d\n", photometric
);
246 buf
= (u_char
*) malloc(TIFFScanlineSize(tif
));
248 error("%s: can't allocate memory for scanline buffer...\n", NULL
);
250 for (row
= 0; row
< height
; row
++) {
251 if (TIFFReadScanline(tif
, buf
, row
, 0) < 0)
252 error("%s: bad data read on line: %d\n", row
);
254 outp
= (u_char
*) mprd_addr(mpr_d(pix
), 0, row
);
255 switch (photometric
) {
256 case PHOTOMETRIC_RGB
:
257 if (samplesperpixel
== 4)
258 for (col
= 0; col
< width
; col
++) {
259 *outp
++ = *inp
++; /* Blue */
260 *outp
++ = *inp
++; /* Green */
261 *outp
++ = *inp
++; /* Red */
262 inp
++; /* skip alpha channel */
265 for (col
= 0; col
< width
; col
++) {
266 *outp
++ = *inp
++; /* Blue */
267 *outp
++ = *inp
++; /* Green */
268 *outp
++ = *inp
++; /* Red */
271 case PHOTOMETRIC_MINISWHITE
:
272 case PHOTOMETRIC_MINISBLACK
:
273 switch (bitspersample
) {
275 for (col
= 0; col
< ((width
+ 7) / 8); col
++)
279 for (col
= 0; col
< ((width
+ 3) / 4); col
++) {
280 *outp
++ = (*inp
>> 6) & 3;
281 *outp
++ = (*inp
>> 4) & 3;
282 *outp
++ = (*inp
>> 2) & 3;
283 *outp
++ = *inp
++ & 3;
287 for (col
= 0; col
< width
/ 2; col
++) {
289 *outp
++ = *inp
++ & 0xf;
293 for (col
= 0; col
< width
; col
++)
297 error("%s: bad bits/sample: %d\n", bitspersample
);
300 case PHOTOMETRIC_PALETTE
:
301 memcpy(outp
, inp
, width
);
304 error("%s: unknown photometric (write): %d\n", photometric
);
311 fprintf(stderr
, "done.\n");
313 if (outf
== NULL
|| strcmp(outf
, "Standard Output") == 0) {
314 outf
= "Standard Output";
317 if (!(fp
= fopen(outf
, "w")))
318 error("%s: %s couldn't be opened for writing.\n", outf
);
322 fprintf(stderr
, "Writing rasterfile in %s...", outf
);
324 if (pr_dump(pix
, fp
, &Colormap
, RT_BYTE_ENCODED
, 0) == PIX_ERR
)
325 error("%s: error writing Sun Rasterfile: %s\n", outf
);
328 fprintf(stderr
, "done.\n");