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

« back to all changes in this revision

Viewing changes to tools/sgisv.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/sgisv.c,v 1.3 1999/12/27 17:35:01 mwelles Exp $ */
 
2
 
 
3
/*
 
4
 * Copyright (c) 1990-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
 
 
31
#include <gl.h>
 
32
#include <ctype.h>
 
33
 
 
34
#include "tiffio.h"
 
35
 
 
36
typedef unsigned char u_char;
 
37
typedef unsigned long u_long;
 
38
 
 
39
#define streq(a,b)      (strcmp(a,b) == 0)
 
40
#define strneq(a,b,n)   (strncmp(a,b,n) == 0)
 
41
 
 
42
uint32  rowsperstrip = (uint32) -1;
 
43
uint16  compression = COMPRESSION_PACKBITS;
 
44
uint16  config = PLANARCONFIG_CONTIG;
 
45
uint16  predictor = 0;
 
46
int     xmaxscreen;
 
47
int     ymaxscreen;
 
48
uint16  photometric = PHOTOMETRIC_RGB;
 
49
int     jpegcolormode = JPEGCOLORMODE_RGB;
 
50
int     quality = 75;           /* JPEG quality */
 
51
 
 
52
static  void usage(void);
 
53
static  void tiffsv(char*, int, int, int, int);
 
54
 
 
55
int
 
56
main(int argc, char* argv[])
 
57
{
 
58
        int c;
 
59
        extern int optind;
 
60
        extern char* optarg;
 
61
 
 
62
        while ((c = getopt(argc, argv, "c:p:r:")) != -1)
 
63
                switch (c) {
 
64
                case 'b':               /* save as b&w */
 
65
                        photometric = PHOTOMETRIC_MINISBLACK;
 
66
                        break;
 
67
                case 'c':               /* compression scheme */
 
68
                        if (streq(optarg, "none"))
 
69
                                compression = COMPRESSION_NONE;
 
70
                        else if (streq(optarg, "packbits"))
 
71
                                compression = COMPRESSION_PACKBITS;
 
72
                        else if (strneq(optarg, "jpeg", 4)) {
 
73
                                char* cp = strchr(optarg, ':');
 
74
                                if (cp && isdigit(cp[1]))
 
75
                                        quality = atoi(cp+1);
 
76
                                if (cp && strchr(cp, 'r'))
 
77
                                        jpegcolormode = JPEGCOLORMODE_RAW;
 
78
                                compression = COMPRESSION_JPEG;
 
79
                        } else if (strneq(optarg, "lzw", 3)) {
 
80
                                char* cp = strchr(optarg, ':');
 
81
                                if (cp)
 
82
                                        predictor = atoi(cp+1);
 
83
                                compression = COMPRESSION_LZW;
 
84
                        } else
 
85
                                usage();
 
86
                        break;
 
87
                case 'p':               /* planar configuration */
 
88
                        if (streq(optarg, "separate"))
 
89
                                config = PLANARCONFIG_SEPARATE;
 
90
                        else if (streq(optarg, "contig"))
 
91
                                config = PLANARCONFIG_CONTIG;
 
92
                        else
 
93
                                usage();
 
94
                        break;
 
95
                case 'r':               /* rows/strip */
 
96
                        rowsperstrip = atoi(optarg);
 
97
                        break;
 
98
                case '?':
 
99
                        usage();
 
100
                        /*NOTREACHED*/
 
101
                }
 
102
        if (argc - optind != 1 && argc - optind != 5)
 
103
                usage();
 
104
        xmaxscreen = getgdesc(GD_XPMAX)-1;
 
105
        ymaxscreen = getgdesc(GD_YPMAX)-1;
 
106
        foreground();
 
107
        noport();
 
108
        winopen("tiffsv");
 
109
        if (argc - optind == 5)
 
110
                tiffsv(argv[optind],
 
111
                    atoi(argv[optind+1]), atoi(argv[optind+2]),
 
112
                    atoi(argv[optind+3]), atoi(argv[optind+4]));
 
113
        else
 
114
                tiffsv(argv[optind], 0, xmaxscreen, 0, ymaxscreen);
 
115
        return (0);
 
116
}
 
117
 
 
118
char* stuff[] = {
 
119
"usage: tiffsv [options] outimage.tif [x1 x2 y1 y2] [-b]",
 
120
"where options are:",
 
121
" -p contig     pack samples contiguously (e.g. RGBRGB...)",
 
122
" -p separate   store samples separately (e.g. RRR...GGG...BBB...)",
 
123
"",
 
124
" -r #          make each strip have no more than # rows",
 
125
"",
 
126
" -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding",
 
127
"               (no longer supported by default due to Unisys patent enforcement)", 
 
128
" -c jpeg[:opts]compress output with JPEG encoding",
 
129
" -c packbits   compress output with packbits encoding",
 
130
" -c none       use no compression algorithm on output",
 
131
"",
 
132
"JPEG options:",
 
133
" #             set compression quality level (0-100, default 75)",
 
134
" r             output color image as RGB rather than YCbCr",
 
135
"",
 
136
"LZW options:",
 
137
" #             set predictor value for Lempel-Ziv & Welch encoding",
 
138
"For example, -c lzw:2 to get LZW-encoded data with horizontal differencing",
 
139
NULL
 
140
};
 
141
 
 
142
static void
 
143
usage(void)
 
144
{
 
145
        char buf[BUFSIZ];
 
146
        int i;
 
147
 
 
148
        setbuf(stderr, buf);
 
149
        for (i = 0; stuff[i] != NULL; i++)
 
150
                fprintf(stderr, "%s\n", stuff[i]);
 
151
        exit(-1);
 
152
}
 
153
 
 
154
static void
 
155
svRGBSeparate(TIFF* tif, u_long* ss, int xsize, int ysize)
 
156
{
 
157
        tsize_t stripsize = TIFFStripSize(tif);
 
158
        u_char *rbuf = (u_char *)_TIFFmalloc(3*stripsize);
 
159
        u_char *gbuf = rbuf + stripsize;
 
160
        u_char *bbuf = gbuf + stripsize;
 
161
        register int y;
 
162
 
 
163
        for (y = 0; y <= ysize; y += rowsperstrip) {
 
164
                u_char *rp, *gp, *bp;
 
165
                register int x;
 
166
                register uint32 n;
 
167
 
 
168
                n = rowsperstrip;
 
169
                if (n > ysize-y+1)
 
170
                        n = ysize-y+1;
 
171
                rp = rbuf; gp = gbuf; bp = bbuf;
 
172
                do {
 
173
                        for (x = 0; x <= xsize; x++) {
 
174
                                u_long v = ss[x];
 
175
                                rp[x] = v;
 
176
                                gp[x] = v >> 8;
 
177
                                bp[x] = v >> 16;
 
178
                        }
 
179
                        rp += xsize+1, gp += xsize+1, bp += xsize+1;
 
180
                        ss += xsize+1;
 
181
                } while (--n);
 
182
                if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif,y,0),
 
183
                    rbuf, stripsize) < 0)
 
184
                        break;
 
185
                if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif,y,1),
 
186
                    gbuf, stripsize) < 0)
 
187
                        break;
 
188
                if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif,y,2),
 
189
                    bbuf, stripsize) < 0)
 
190
                        break;
 
191
        }
 
192
        _TIFFfree(rbuf);
 
193
}
 
194
 
 
195
static void
 
196
svRGBContig(TIFF* tif, u_long* ss, int xsize, int ysize)
 
197
{
 
198
        register int x, y;
 
199
        tsize_t stripsize = TIFFStripSize(tif);
 
200
        u_char *strip = (u_char *)_TIFFmalloc(stripsize);
 
201
 
 
202
        for (y = 0; y <= ysize; y += rowsperstrip) {
 
203
                register u_char *pp = strip;
 
204
                register uint32 n;
 
205
 
 
206
                n = rowsperstrip;
 
207
                if (n > ysize-y+1)
 
208
                        n = ysize-y+1;
 
209
                do {
 
210
                        for (x = 0; x <= xsize; x++) {
 
211
                                u_long v = ss[x];
 
212
                                pp[0] = v;
 
213
                                pp[1] = v >> 8;
 
214
                                pp[2] = v >> 16;
 
215
                                pp += 3;
 
216
                        }
 
217
                        ss += xsize+1;
 
218
                } while (--n);
 
219
                if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif,y,0),
 
220
                    strip, stripsize) < 0)
 
221
                        break;
 
222
        }
 
223
        _TIFFfree(strip);
 
224
}
 
225
 
 
226
#undef RED
 
227
#undef GREEN
 
228
#undef BLUE
 
229
#define CVT(x)  (((x)*255)/100)
 
230
#define RED     CVT(28)         /* 28% */
 
231
#define GREEN   CVT(59)         /* 59% */
 
232
#define BLUE    CVT(11)         /* 11% */
 
233
 
 
234
static void
 
235
svGrey(TIFF* tif, u_long* ss, int xsize, int ysize)
 
236
{
 
237
        register int x, y;
 
238
        u_char *buf = (u_char *)_TIFFmalloc(TIFFScanlineSize(tif));
 
239
 
 
240
        for (y = 0; y <= ysize; y++) {
 
241
                for (x = 0; x <= xsize; x++) {
 
242
                        u_char *cp = (u_char *)&ss[x];
 
243
                        buf[x] = (RED*cp[3] + GREEN*cp[2] + BLUE*cp[1]) >> 8;
 
244
                }
 
245
                if (TIFFWriteScanline(tif, buf, (uint32) y, 0) < 0)
 
246
                        break;
 
247
                ss += xsize+1;
 
248
        }
 
249
        _TIFFfree(buf);
 
250
}
 
251
 
 
252
#define MIN(a,b)        ((a)<(b)?(a):(b))
 
253
#define ABS(x)          ((x)<0?-(x):(x))
 
254
 
 
255
static void
 
256
tiffsv(char* name, int x1, int x2, int y1, int y2)
 
257
{
 
258
        TIFF *tif;
 
259
        int xsize, ysize;
 
260
        int xorg, yorg;
 
261
        u_long *scrbuf;
 
262
 
 
263
        xorg = MIN(x1,x2);
 
264
        yorg = MIN(y1,y2);
 
265
        if (xorg<0)
 
266
                xorg = 0;
 
267
        if (yorg<0)
 
268
                yorg = 0;
 
269
        xsize = ABS(x2-x1);
 
270
        ysize = ABS(y2-y1);
 
271
        if (xorg+xsize > xmaxscreen)
 
272
                xsize = xmaxscreen-xorg;
 
273
        if (yorg+ysize > ymaxscreen)
 
274
                ysize = ymaxscreen-yorg;
 
275
        tif = TIFFOpen(name, "w");
 
276
        TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32) (xsize+1));
 
277
        TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32) (ysize+1));
 
278
        TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
 
279
        TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL,
 
280
            photometric == PHOTOMETRIC_RGB ? 3 : 1);
 
281
        TIFFSetField(tif, TIFFTAG_PLANARCONFIG, config);
 
282
        TIFFSetField(tif, TIFFTAG_COMPRESSION, compression);
 
283
        switch (compression) {
 
284
        case COMPRESSION_JPEG:
 
285
                if (photometric == PHOTOMETRIC_RGB && jpegcolormode == JPEGCOLORMODE_RGB)
 
286
                        photometric = PHOTOMETRIC_YCBCR;
 
287
                TIFFSetField(tif, TIFFTAG_JPEGQUALITY, quality);
 
288
                TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, jpegcolormode);
 
289
                break;
 
290
        case COMPRESSION_LZW:
 
291
                if (predictor != 0)
 
292
                        TIFFSetField(tif, TIFFTAG_PREDICTOR, predictor);
 
293
                break;
 
294
        }
 
295
        TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photometric);
 
296
        TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_BOTLEFT);
 
297
        rowsperstrip = TIFFDefaultStripSize(tif, rowsperstrip);
 
298
        TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
 
299
        scrbuf = (u_long *)_TIFFmalloc((xsize+1)*(ysize+1)*sizeof (u_long));
 
300
        readdisplay(xorg, yorg, xorg+xsize, yorg+ysize, scrbuf, RD_FREEZE);
 
301
        if (photometric == PHOTOMETRIC_RGB) {
 
302
                if (config == PLANARCONFIG_SEPARATE)
 
303
                        svRGBSeparate(tif, scrbuf, xsize, ysize);
 
304
                else
 
305
                        svRGBContig(tif, scrbuf, xsize, ysize);
 
306
        } else
 
307
                svGrey(tif, scrbuf, xsize, ysize);
 
308
        (void) TIFFClose(tif);
 
309
        _TIFFfree((char *)scrbuf);
 
310
}