~ubuntu-branches/ubuntu/karmic/tiff/karmic-security

« back to all changes in this revision

Viewing changes to tools/ppm2tiff.c

  • Committer: Bazaar Package Importer
  • Author(s): Fabio M. Di Nitto
  • Date: 2004-10-14 07:57:59 UTC
  • Revision ID: james.westby@ubuntu.com-20041014075759-a77e7zuaetya8cp0
Tags: upstream-3.6.1
ImportĀ upstreamĀ versionĀ 3.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Header: /cvsroot/osrs/libtiff/tools/ppm2tiff.c,v 1.6 2003/05/05 10:40:04 dron Exp $ */
 
2
 
 
3
/*
 
4
 * Copyright (c) 1991-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 <stdio.h>
 
28
#include <stdlib.h>
 
29
#include <string.h>
 
30
#include <ctype.h>
 
31
 
 
32
#include "tiffio.h"
 
33
 
 
34
#if defined(_WINDOWS) || defined(MSDOS)
 
35
#define BINMODE "b"
 
36
#else
 
37
#define BINMODE
 
38
#endif
 
39
 
 
40
#define streq(a,b)      (strcmp(a,b) == 0)
 
41
#define strneq(a,b,n)   (strncmp(a,b,n) == 0)
 
42
 
 
43
static  uint16 compression = COMPRESSION_PACKBITS;
 
44
static  uint16 predictor = 0;
 
45
static  int quality = 75;       /* JPEG quality */
 
46
static  int jpegcolormode = JPEGCOLORMODE_RGB;
 
47
 
 
48
static  void usage(void);
 
49
static  int processCompressOptions(char*);
 
50
 
 
51
static void
 
52
BadPPM(char* file)
 
53
{
 
54
        fprintf(stderr, "%s: Not a PPM file.\n", file);
 
55
        exit(-2);
 
56
}
 
57
 
 
58
int
 
59
main(int argc, char* argv[])
 
60
{
 
61
        uint16 photometric;
 
62
        uint32 rowsperstrip = (uint32) -1;
 
63
        double resolution = -1;
 
64
        unsigned char *buf = NULL;
 
65
        uint32 row;
 
66
        tsize_t linebytes;
 
67
        uint16 spp;
 
68
        TIFF *out;
 
69
        FILE *in;
 
70
        uint32 w, h;
 
71
        int prec;
 
72
        char *infile;
 
73
        int c;
 
74
        extern int optind;
 
75
        extern char* optarg;
 
76
 
 
77
        if ( argc < 2 ) {
 
78
            fprintf(stderr, "%s: Too few arguments\n", argv[0]);
 
79
            usage();
 
80
        }
 
81
        while ((c = getopt(argc, argv, "c:r:R:")) != -1)
 
82
                switch (c) {
 
83
                case 'c':               /* compression scheme */
 
84
                        if (!processCompressOptions(optarg))
 
85
                                usage();
 
86
                        break;
 
87
                case 'r':               /* rows/strip */
 
88
                        rowsperstrip = atoi(optarg);
 
89
                        break;
 
90
                case 'R':               /* resolution */
 
91
                        resolution = atof(optarg);
 
92
                        break;
 
93
                case '?':
 
94
                        usage();
 
95
                        /*NOTREACHED*/
 
96
                }
 
97
 
 
98
        if ( optind + 2 < argc ) {
 
99
            fprintf(stderr, "%s: Too many arguments\n", argv[0]);
 
100
            usage();
 
101
        }
 
102
 
 
103
        /*
 
104
         * If only one file is specified, read input from
 
105
         * stdin; otherwise usage is: ppm2tiff input output.
 
106
         */
 
107
        if (argc - optind > 1) {
 
108
                infile = argv[optind++];
 
109
                in = fopen(infile, "r" BINMODE);
 
110
                if (in == NULL) {
 
111
                        fprintf(stderr, "%s: Can not open.\n", infile);
 
112
                        return (-1);
 
113
                }
 
114
        } else {
 
115
                infile = "<stdin>";
 
116
                in = stdin;
 
117
        }
 
118
 
 
119
        if (fgetc(in) != 'P')
 
120
                BadPPM(infile);
 
121
        switch (fgetc(in)) {
 
122
        case '5':                       /* it's a PGM file */
 
123
                spp = 1;
 
124
                photometric = PHOTOMETRIC_MINISBLACK;
 
125
                break;
 
126
        case '6':                       /* it's a PPM file */
 
127
                spp = 3;
 
128
                photometric = PHOTOMETRIC_RGB;
 
129
                if (compression == COMPRESSION_JPEG &&
 
130
                    jpegcolormode == JPEGCOLORMODE_RGB)
 
131
                        photometric = PHOTOMETRIC_YCBCR;
 
132
                break;
 
133
        default:
 
134
                BadPPM(infile);
 
135
        }
 
136
 
 
137
        /* Parse header */
 
138
        while(1) {
 
139
                if (feof(in))
 
140
                        BadPPM(infile);
 
141
                c = fgetc(in);
 
142
                /* Skip whitespaces (blanks, TABs, CRs, LFs) */
 
143
                if (strchr(" \t\r\n", c))
 
144
                        continue;
 
145
 
 
146
                /* Check fo comment line */
 
147
                if (c == '#') {
 
148
                        do {
 
149
                            c = fgetc(in);
 
150
                        } while(!strchr("\r\n", c) || feof(in));
 
151
                        continue;
 
152
                }
 
153
 
 
154
                ungetc(c, in);
 
155
                break;
 
156
        }
 
157
        if (fscanf(in, " %ld %ld %d", &w, &h, &prec) != 3)
 
158
                BadPPM(infile);
 
159
        if (fgetc(in) != '\n' || w <= 0 || h <= 0 || prec != 255)
 
160
                BadPPM(infile);
 
161
 
 
162
        out = TIFFOpen(argv[optind], "w");
 
163
        if (out == NULL)
 
164
                return (-4);
 
165
        TIFFSetField(out, TIFFTAG_IMAGEWIDTH,  w);
 
166
        TIFFSetField(out, TIFFTAG_IMAGELENGTH, h);
 
167
        TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
 
168
        TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, spp);
 
169
        TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);
 
170
        TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
 
171
        TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric);
 
172
        TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
 
173
        switch (compression) {
 
174
        case COMPRESSION_JPEG:
 
175
                TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality);
 
176
                TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, jpegcolormode);
 
177
                break;
 
178
        case COMPRESSION_LZW:
 
179
        case COMPRESSION_DEFLATE:
 
180
                if (predictor != 0)
 
181
                        TIFFSetField(out, TIFFTAG_PREDICTOR, predictor);
 
182
                break;
 
183
        }
 
184
        linebytes = spp * w;
 
185
        if (TIFFScanlineSize(out) > linebytes)
 
186
                buf = (unsigned char *)_TIFFmalloc(linebytes);
 
187
        else
 
188
                buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(out));
 
189
        TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
 
190
            TIFFDefaultStripSize(out, rowsperstrip));
 
191
        if (resolution > 0) {
 
192
                TIFFSetField(out, TIFFTAG_XRESOLUTION, resolution);
 
193
                TIFFSetField(out, TIFFTAG_YRESOLUTION, resolution);
 
194
                TIFFSetField(out, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
 
195
        }
 
196
        for (row = 0; row < h; row++) {
 
197
                if (fread(buf, linebytes, 1, in) != 1) {
 
198
                        fprintf(stderr, "%s: scanline %lu: Read error.\n",
 
199
                            infile, (unsigned long) row);
 
200
                        break;
 
201
                }
 
202
                if (TIFFWriteScanline(out, buf, row, 0) < 0)
 
203
                        break;
 
204
        }
 
205
        (void) TIFFClose(out);
 
206
        if (buf)
 
207
                _TIFFfree(buf);
 
208
        return (0);
 
209
}
 
210
 
 
211
static int
 
212
processCompressOptions(char* opt)
 
213
{
 
214
        if (streq(opt, "none"))
 
215
                compression = COMPRESSION_NONE;
 
216
        else if (streq(opt, "packbits"))
 
217
                compression = COMPRESSION_PACKBITS;
 
218
        else if (strneq(opt, "jpeg", 4)) {
 
219
                char* cp = strchr(opt, ':');
 
220
                if (cp && isdigit(cp[1]))
 
221
                        quality = atoi(cp+1);
 
222
                if (cp && strchr(cp, 'r'))
 
223
                        jpegcolormode = JPEGCOLORMODE_RAW;
 
224
                compression = COMPRESSION_JPEG;
 
225
        } else if (strneq(opt, "lzw", 3)) {
 
226
                char* cp = strchr(opt, ':');
 
227
                if (cp)
 
228
                        predictor = atoi(cp+1);
 
229
                compression = COMPRESSION_LZW;
 
230
        } else if (strneq(opt, "zip", 3)) {
 
231
                char* cp = strchr(opt, ':');
 
232
                if (cp)
 
233
                        predictor = atoi(cp+1);
 
234
                compression = COMPRESSION_DEFLATE;
 
235
        } else
 
236
                return (0);
 
237
        return (1);
 
238
}
 
239
 
 
240
char* stuff[] = {
 
241
"usage: ppm2tiff [options] input.ppm output.tif",
 
242
"where options are:",
 
243
" -r #          make each strip have no more than # rows",
 
244
" -R #          set x&y resolution (dpi)",
 
245
"",
 
246
" -c jpeg[:opts]  compress output with JPEG encoding",
 
247
" -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding",
 
248
" -c zip[:opts] compress output with deflate encoding",
 
249
" -c packbits   compress output with packbits encoding (the default)",
 
250
" -c none       use no compression algorithm on output",
 
251
"",
 
252
"JPEG options:",
 
253
" #             set compression quality level (0-100, default 75)",
 
254
" r             output color image as RGB rather than YCbCr",
 
255
"LZW and deflate options:",
 
256
" #             set predictor value",
 
257
"For example, -c lzw:2 to get LZW-encoded data with horizontal differencing",
 
258
NULL
 
259
};
 
260
 
 
261
static void
 
262
usage(void)
 
263
{
 
264
        char buf[BUFSIZ];
 
265
        int i;
 
266
 
 
267
        setbuf(stderr, buf);
 
268
        fprintf(stderr, "%s\n\n", TIFFGetVersion());
 
269
        for (i = 0; stuff[i] != NULL; i++)
 
270
                fprintf(stderr, "%s\n", stuff[i]);
 
271
        exit(-1);
 
272
}