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