Debian Codegraph @

texk/xdvipsk/thirdparty/libtiff/contrib/ras/tif2ras.c

raw
1 /*-
2 * tif2ras.c - Converts from a Tagged Image File Format image to a Sun Raster.
3 *
4 * Copyright (c) 1990 by Sun Microsystems, Inc.
5 *
6 * Author: Patrick J. Naughton
7 * [email protected]
8 *
9 * Permission to use, copy, modify, and distribute this software and its
10 * documentation for any purpose and without fee is hereby granted,
11 * provided that the above copyright notice appear in all copies and that
12 * both that copyright notice and this permission notice appear in
13 * supporting documentation.
14 *
15 * This file is provided AS IS with no warranties of any kind. The author
16 * shall have no liability with respect to the infringement of copyrights,
17 * trade secrets or any patents by this file or any part thereof. In no
18 * event will the author be liable for any lost revenue or profits or
19 * other special, indirect and consequential damages.
20 *
21 * Comments and additions should be sent to the author:
22 *
23 * Patrick J. Naughton
24 * Sun Microsystems
25 * 2550 Garcia Ave, MS 14-40
26 * Mountain View, CA 94043
27 * (415) 336-1080
28 *
29 * Revision History:
30 * 10-Jan-89: Created.
31 * 06-Mar-90: Change to byte encoded rasterfiles.
32 * fix bug in call to ReadScanline().
33 * fix bug in CVT() macro.
34 * fix assignment of td, (missing &).
35 *
36 * Description:
37 * This program takes a MicroSoft/Aldus "Tagged Image File Format" image or
38 * "TIFF" file as input and writes a Sun Rasterfile [see rasterfile(5)]. The
39 * output file may be standard output, but the input TIFF file must be a real
40 * file since seek(2) is used.
41 */
42
43 #include "tiffio.h"
44 #include <pixrect/pixrect_hs.h>
45 #include <stdio.h>
46
47 typedef int boolean;
48 #define True (1)
49 #define False (0)
50 #define CVT(x) (((x)*255) / ((1L << 16) - 1))
51
52 boolean Verbose = False;
53 char *pname; /* program name (used for error messages) */
54
55 void error(s1, s2) char *s1, *s2;
56 {
57 fprintf(stderr, s1, pname, s2);
58 exit(1);
59 }
60
61 void usage() { error("usage: %s -[vq] TIFFfile [rasterfile]\n", NULL); }
62
63 main(argc, argv) int argc;
64 char *argv[];
65 {
66 char *inf = NULL;
67 char *outf = NULL;
68 FILE *fp;
69 long width, height;
70 int depth, numcolors;
71 register TIFF *tif;
72 TIFFDirectory *td;
73 register u_char *inp, *outp;
74 register int col, i;
75 register long row;
76 u_char *Map = NULL;
77 u_char *buf;
78 short bitspersample;
79 short samplesperpixel;
80 short photometric;
81 u_short *redcolormap, *bluecolormap, *greencolormap;
82
83 Pixrect *pix; /* The Sun Pixrect */
84 colormap_t Colormap; /* The Pixrect Colormap */
85 u_char red[256], green[256], blue[256];
86
87 setbuf(stderr, NULL);
88 pname = argv[0];
89
90 while (--argc)
91 {
92 if ((++argv)[0][0] == '-')
93 switch (argv[0][1])
94 {
95 case 'v':
96 Verbose = True;
97 break;
98 case 'q':
99 usage();
100 break;
101 default:
102 fprintf(stderr, "%s: illegal option -%c.\n", pname,
103 argv[0][1]);
104 exit(1);
105 }
106 else if (inf == NULL)
107 inf = argv[0];
108 else if (outf == NULL)
109 outf = argv[0];
110 else
111 usage();
112 }
113
114 if (inf == NULL)
115 error("%s: can't read input file from a stream.\n", NULL);
116
117 if (Verbose)
118 fprintf(stderr, "Reading %s...", inf);
119
120 tif = TIFFOpen(inf, "r");
121
122 if (tif == NULL)
123 error("%s: error opening TIFF file %s", inf);
124
125 if (Verbose)
126 TIFFPrintDirectory(tif, stderr, True, False, False);
127 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bitspersample);
128 if (bitspersample > 8)
129 error("%s: can't handle more than 8-bits per sample\n", NULL);
130
131 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
132 switch (samplesperpixel)
133 {
134 case 1:
135 if (bitspersample == 1)
136 depth = 1;
137 else
138 depth = 8;
139 break;
140 case 3:
141 case 4:
142 depth = 24;
143 break;
144 default:
145 error("%s: only handle 1-channel gray scale or 3-channel color\n");
146 }
147
148 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
149 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
150
151 if (Verbose)
152 fprintf(stderr, "%dx%dx%d image, ", width, height, depth);
153 if (Verbose)
154 fprintf(stderr, "%d bits/sample, %d samples/pixel, ", bitspersample,
155 samplesperpixel);
156
157 pix = mem_create(width, height, depth);
158 if (pix == (Pixrect *)NULL)
159 error("%s: can't allocate memory for output pixrect...\n", NULL);
160
161 numcolors = (1 << bitspersample);
162
163 TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric);
164 if (numcolors == 2)
165 {
166 if (Verbose)
167 fprintf(stderr, "monochrome ");
168 Colormap.type = RMT_NONE;
169 Colormap.length = 0;
170 Colormap.map[0] = Colormap.map[1] = Colormap.map[2] = NULL;
171 }
172 else
173 {
174 switch (photometric)
175 {
176 case PHOTOMETRIC_MINISBLACK:
177 if (Verbose)
178 fprintf(stderr, "%d graylevels (min=black), ", numcolors);
179 Map = (u_char *)malloc(numcolors * sizeof(u_char));
180 for (i = 0; i < numcolors; i++)
181 Map[i] = (255 * i) / numcolors;
182 Colormap.type = RMT_EQUAL_RGB;
183 Colormap.length = numcolors;
184 Colormap.map[0] = Colormap.map[1] = Colormap.map[2] = Map;
185 break;
186 case PHOTOMETRIC_MINISWHITE:
187 if (Verbose)
188 fprintf(stderr, "%d graylevels (min=white), ", numcolors);
189 Map = (u_char *)malloc(numcolors * sizeof(u_char));
190 for (i = 0; i < numcolors; i++)
191 Map[i] = 255 - ((255 * i) / numcolors);
192 Colormap.type = RMT_EQUAL_RGB;
193 Colormap.length = numcolors;
194 Colormap.map[0] = Colormap.map[1] = Colormap.map[2] = Map;
195 break;
196 case PHOTOMETRIC_RGB:
197 if (Verbose)
198 fprintf(stderr, "truecolor ");
199 Colormap.type = RMT_NONE;
200 Colormap.length = 0;
201 Colormap.map[0] = Colormap.map[1] = Colormap.map[2] = NULL;
202 break;
203 case PHOTOMETRIC_PALETTE:
204 if (Verbose)
205 fprintf(stderr, "colormapped ");
206 Colormap.type = RMT_EQUAL_RGB;
207 Colormap.length = numcolors;
208 memset(red, 0, sizeof(red));
209 memset(green, 0, sizeof(green));
210 memset(blue, 0, sizeof(blue));
211 TIFFGetField(tif, TIFFTAG_COLORMAP, &redcolormap,
212 &greencolormap, &bluecolormap);
213 for (i = 0; i < numcolors; i++)
214 {
215 red[i] = (u_char)CVT(redcolormap[i]);
216 green[i] = (u_char)CVT(greencolormap[i]);
217 blue[i] = (u_char)CVT(bluecolormap[i]);
218 }
219 Colormap.map[0] = red;
220 Colormap.map[1] = green;
221 Colormap.map[2] = blue;
222 break;
223 case PHOTOMETRIC_MASK:
224 error("%s: Don't know how to handle PHOTOMETRIC_MASK\n");
225 break;
226 case PHOTOMETRIC_DEPTH:
227 error("%s: Don't know how to handle PHOTOMETRIC_DEPTH\n");
228 break;
229 default:
230 error("%s: unknown photometric (cmap): %d\n", photometric);
231 }
232 }
233
234 buf = (u_char *)malloc(TIFFScanlineSize(tif));
235 if (buf == NULL)
236 error("%s: can't allocate memory for scanline buffer...\n", NULL);
237
238 for (row = 0; row < height; row++)
239 {
240 if (TIFFReadScanline(tif, buf, row, 0) < 0)
241 error("%s: bad data read on line: %d\n", row);
242 inp = buf;
243 outp = (u_char *)mprd_addr(mpr_d(pix), 0, row);
244 switch (photometric)
245 {
246 case PHOTOMETRIC_RGB:
247 if (samplesperpixel == 4)
248 for (col = 0; col < width; col++)
249 {
250 *outp++ = *inp++; /* Blue */
251 *outp++ = *inp++; /* Green */
252 *outp++ = *inp++; /* Red */
253 inp++; /* skip alpha channel */
254 }
255 else
256 for (col = 0; col < width; col++)
257 {
258 *outp++ = *inp++; /* Blue */
259 *outp++ = *inp++; /* Green */
260 *outp++ = *inp++; /* Red */
261 }
262 break;
263 case PHOTOMETRIC_MINISWHITE:
264 case PHOTOMETRIC_MINISBLACK:
265 switch (bitspersample)
266 {
267 case 1:
268 for (col = 0; col < ((width + 7) / 8); col++)
269 *outp++ = *inp++;
270 break;
271 case 2:
272 for (col = 0; col < ((width + 3) / 4); col++)
273 {
274 *outp++ = (*inp >> 6) & 3;
275 *outp++ = (*inp >> 4) & 3;
276 *outp++ = (*inp >> 2) & 3;
277 *outp++ = *inp++ & 3;
278 }
279 break;
280 case 4:
281 for (col = 0; col < width / 2; col++)
282 {
283 *outp++ = *inp >> 4;
284 *outp++ = *inp++ & 0xf;
285 }
286 break;
287 case 8:
288 for (col = 0; col < width; col++)
289 *outp++ = *inp++;
290 break;
291 default:
292 error("%s: bad bits/sample: %d\n", bitspersample);
293 }
294 break;
295 case PHOTOMETRIC_PALETTE:
296 memcpy(outp, inp, width);
297 break;
298 default:
299 error("%s: unknown photometric (write): %d\n", photometric);
300 }
301 }
302
303 free((char *)buf);
304
305 if (Verbose)
306 fprintf(stderr, "done.\n");
307
308 if (outf == NULL || strcmp(outf, "Standard Output") == 0)
309 {
310 outf = "Standard Output";
311 fp = stdout;
312 }
313 else
314 {
315 if (!(fp = fopen(outf, "w")))
316 error("%s: %s couldn't be opened for writing.\n", outf);
317 }
318
319 if (Verbose)
320 fprintf(stderr, "Writing rasterfile in %s...", outf);
321
322 if (pr_dump(pix, fp, &Colormap, RT_BYTE_ENCODED, 0) == PIX_ERR)
323 error("%s: error writing Sun Rasterfile: %s\n", outf);
324
325 if (Verbose)
326 fprintf(stderr, "done.\n");
327
328 pr_destroy(pix);
329
330 if (fp != stdout)
331 fclose(fp);
332
333 exit(0);
334 }