2 static char sccsid[] = "@(#)ras2tif.c 1.2 90/03/06";
5 * ras2tif.c - Converts from a Sun Rasterfile to a Tagged Image File.
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: fix bug in SCALE() macro.
35 * got rid of xres and yres, (they weren't working anyways).
36 * fixed bpsl calculation.
37 * 25-Nov-99: y2k fix (year as 1900 + tm_year) <mike@onshore.com>
40 * This program takes a Sun Rasterfile [see rasterfile(5)] as input and
41 * writes a MicroSoft/Aldus "Tagged Image File Format" image or "TIFF" file.
42 * The input file may be standard input, but the output TIFF file must be a
43 * real file since seek(2) is used.
48 #include <pixrect/pixrect_hs.h>
54 #define SCALE(x) (((x)*((1L<<16)-1))/255)
56 boolean Verbose = False;
57 boolean dummyinput = False;
58 char *pname; /* program name (used for error messages) */
65 fprintf(stderr, s1, pname, s2);
72 error("usage: %s -[vq] [-|rasterfile] TIFFfile\n", NULL);
87 Pixrect *pix; /* The Sun Pixrect */
88 colormap_t Colormap; /* The Pixrect Colormap */
99 short samplesperpixel;
102 static char *version = "ras2tif 1.0";
103 static char *datetime = "1990:01:01 12:00:00";
105 gettimeofday(&tv, (struct timezone *) NULL);
106 ct = localtime(&tv.tv_sec);
107 year=1900 + ct->tm_year;
108 sprintf(datetime, "%04d:%02d:%02d %02d:%02d:%02d",
109 year, ct->tm_mon + 1, ct->tm_mday,
110 ct->tm_hour, ct->tm_min, ct->tm_sec);
112 setbuf(stderr, NULL);
116 if ((++argv)[0][0] == '-') {
117 switch (argv[0][1]) {
131 fprintf(stderr, "%s: illegal option -%c.\n", pname,
135 } else if (inf == NULL && !dummyinput) {
137 } else if (outf == NULL)
144 error("%s: can't write output file to a stream.\n", NULL);
146 if (dummyinput || inf == NULL) {
147 inf = "Standard Input";
149 } else if ((fp = fopen(inf, "r")) == NULL)
150 error("%s: %s couldn't be opened.\n", inf);
153 fprintf(stderr, "Reading rasterfile from %s...", inf);
155 pix = pr_load(fp, &Colormap);
157 error("%s: %s is not a raster file.\n", inf);
160 fprintf(stderr, "done.\n");
163 fprintf(stderr, "Writing %s...", outf);
165 tif = TIFFOpen(outf, "w");
168 error("%s: error opening TIFF file %s", outf);
170 width = pix->pr_width;
171 height = pix->pr_height;
172 depth = pix->pr_depth;
178 photometric = PHOTOMETRIC_MINISBLACK;
183 photometric = PHOTOMETRIC_PALETTE;
188 photometric = PHOTOMETRIC_RGB;
193 photometric = PHOTOMETRIC_RGB;
196 error("%s: bogus depth: %d\n", depth);
199 bpsl = ((depth * width + 15) >> 3) & ~1;
200 rowsperstrip = (8 * 1024) / bpsl;
202 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
203 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
204 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bitspersample);
205 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
206 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
207 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photometric);
208 TIFFSetField(tif, TIFFTAG_DOCUMENTNAME, inf);
209 TIFFSetField(tif, TIFFTAG_IMAGEDESCRIPTION, "converted Sun rasterfile");
210 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, samplesperpixel);
211 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
212 TIFFSetField(tif, TIFFTAG_STRIPBYTECOUNTS, height / rowsperstrip);
213 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
214 TIFFSetField(tif, TIFFTAG_SOFTWARE, version);
215 TIFFSetField(tif, TIFFTAG_DATETIME, datetime);
217 memset(red, 0, sizeof(red));
218 memset(green, 0, sizeof(green));
219 memset(blue, 0, sizeof(blue));
221 TIFFSetField(tif, TIFFTAG_COLORMAP, red, green, blue);
222 for (i = 0; i < Colormap.length; i++) {
223 red[i] = SCALE(Colormap.map[0][i]);
224 green[i] = SCALE(Colormap.map[1][i]);
225 blue[i] = SCALE(Colormap.map[2][i]);
229 fprintf(stderr, "%dx%dx%d image, ", width, height, depth);
231 for (row = 0; row < height; row++)
232 if (TIFFWriteScanline(tif,
233 (u_char *) mprd_addr(mpr_d(pix), 0, row),
235 fprintf("failed a scanline write (%d)\n", row);
242 fprintf(stderr, "done.\n");