~ubuntu-branches/ubuntu/dapper/tiff/dapper-updates

« back to all changes in this revision

Viewing changes to tools/tiff2rgba.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2005-11-09 18:21:15 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20051109182115-v0fd3zcbrq2sq6u4
Tags: 3.7.4-1ubuntu1
* Synchronize to Debian.
* Only change left: xlibmesa-gl-dev -> libgl1-mesa-dev build dependency
  change.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Header: /cvsroot/osrs/libtiff/tools/tiff2rgba.c,v 1.7 2003/10/03 11:22:29 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 <string.h>
29
 
#include <stdlib.h>
30
 
 
31
 
#include "tiffio.h"
32
 
 
33
 
#define streq(a,b)      (strcmp(a,b) == 0)
34
 
#define CopyField(tag, v) \
35
 
    if (TIFFGetField(in, tag, &v)) TIFFSetField(out, tag, v)
36
 
 
37
 
#ifndef howmany
38
 
#define howmany(x, y)   (((x)+((y)-1))/(y))
39
 
#endif
40
 
#define roundup(x, y)   (howmany(x,y)*((uint32)(y)))
41
 
 
42
 
uint16  compression = COMPRESSION_PACKBITS;
43
 
uint32  rowsperstrip = (uint32) -1;
44
 
int     process_by_block = 0; /* default is whole image at once */
45
 
int     no_alpha = 0;
46
 
 
47
 
 
48
 
static  int tiffcvt(TIFF* in, TIFF* out);
49
 
static  void usage(int code);
50
 
 
51
 
int
52
 
main(int argc, char* argv[])
53
 
{
54
 
    TIFF *in, *out;
55
 
    int c;
56
 
    extern int optind;
57
 
    extern char *optarg;
58
 
 
59
 
    while ((c = getopt(argc, argv, "c:r:t:bn")) != -1)
60
 
        switch (c) {
61
 
          case 'b':
62
 
            process_by_block = 1;
63
 
            break;
64
 
            
65
 
          case 'c':
66
 
            if (streq(optarg, "none"))
67
 
                compression = COMPRESSION_NONE;
68
 
            else if (streq(optarg, "packbits"))
69
 
                compression = COMPRESSION_PACKBITS;
70
 
            else if (streq(optarg, "lzw"))
71
 
                compression = COMPRESSION_LZW;
72
 
            else if (streq(optarg, "jpeg"))
73
 
                compression = COMPRESSION_JPEG;
74
 
            else if (streq(optarg, "zip"))
75
 
                compression = COMPRESSION_DEFLATE;
76
 
            else
77
 
                usage(-1);
78
 
            break;
79
 
 
80
 
          case 'r':
81
 
            rowsperstrip = atoi(optarg);
82
 
            break;
83
 
 
84
 
          case 't':
85
 
            rowsperstrip = atoi(optarg);
86
 
            break;
87
 
            
88
 
          case 'n':
89
 
            no_alpha = 1;
90
 
            break;
91
 
            
92
 
          case '?':
93
 
            usage(0);
94
 
            /*NOTREACHED*/
95
 
        }
96
 
 
97
 
    if (argc - optind < 2)
98
 
        usage(-1);
99
 
 
100
 
    out = TIFFOpen(argv[argc-1], "w");
101
 
    if (out == NULL)
102
 
        return (-2);
103
 
 
104
 
    for (; optind < argc-1; optind++) {
105
 
        in = TIFFOpen(argv[optind], "r");
106
 
        if (in != NULL) {
107
 
            do {
108
 
                if (!tiffcvt(in, out) ||
109
 
                    !TIFFWriteDirectory(out)) {
110
 
                    (void) TIFFClose(out);
111
 
                    return (1);
112
 
                }
113
 
            } while (TIFFReadDirectory(in));
114
 
            (void) TIFFClose(in);
115
 
        }
116
 
    }
117
 
    (void) TIFFClose(out);
118
 
    return (0);
119
 
}
120
 
 
121
 
static int
122
 
cvt_by_tile( TIFF *in, TIFF *out )
123
 
 
124
 
{
125
 
    uint32* raster;                     /* retrieve RGBA image */
126
 
    uint32  width, height;              /* image width & height */
127
 
    uint32  tile_width, tile_height;
128
 
    uint32  row, col;
129
 
    uint32  *wrk_line;
130
 
    int     ok = 1;
131
 
 
132
 
    TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &width);
133
 
    TIFFGetField(in, TIFFTAG_IMAGELENGTH, &height);
134
 
 
135
 
    if( !TIFFGetField(in, TIFFTAG_TILEWIDTH, &tile_width)
136
 
        || !TIFFGetField(in, TIFFTAG_TILELENGTH, &tile_height) ) {
137
 
        TIFFError(TIFFFileName(in), "Source image not tiled");
138
 
        return (0);
139
 
    }
140
 
    
141
 
    TIFFSetField(out, TIFFTAG_TILEWIDTH, tile_width );
142
 
    TIFFSetField(out, TIFFTAG_TILELENGTH, tile_height );
143
 
 
144
 
    /*
145
 
     * Allocate tile buffer
146
 
     */
147
 
    raster = (uint32*)_TIFFmalloc(tile_width * tile_height * sizeof (uint32));
148
 
    if (raster == 0) {
149
 
        TIFFError(TIFFFileName(in), "No space for raster buffer");
150
 
        return (0);
151
 
    }
152
 
 
153
 
    /*
154
 
     * Allocate a scanline buffer for swapping during the vertical
155
 
     * mirroring pass.
156
 
     */
157
 
    wrk_line = (uint32*)_TIFFmalloc(tile_width * sizeof (uint32));
158
 
    if (wrk_line == 0) {
159
 
        TIFFError(TIFFFileName(in), "No space for raster scanline buffer");
160
 
        ok = 0;
161
 
    }
162
 
    
163
 
    /*
164
 
     * Loop over the tiles.
165
 
     */
166
 
    for( row = 0; ok && row < height; row += tile_height )
167
 
    {
168
 
        for( col = 0; ok && col < width; col += tile_width )
169
 
        {
170
 
            int         i_row;
171
 
 
172
 
            /* Read the tile into an RGBA array */
173
 
            if (!TIFFReadRGBATile(in, col, row, raster)) {
174
 
                ok = 0;
175
 
                break;
176
 
            }
177
 
 
178
 
            /*
179
 
             * For some reason the TIFFReadRGBATile() function chooses the
180
 
             * lower left corner as the origin.  Vertically mirror scanlines.
181
 
             */
182
 
            for( i_row = 0; i_row < tile_height / 2; i_row++ )
183
 
            {
184
 
                uint32  *top_line, *bottom_line;
185
 
 
186
 
                top_line = raster + tile_width * i_row;
187
 
                bottom_line = raster + tile_width * (tile_height-i_row-1);
188
 
 
189
 
                _TIFFmemcpy(wrk_line, top_line, 4*tile_width);
190
 
                _TIFFmemcpy(top_line, bottom_line, 4*tile_width);
191
 
                _TIFFmemcpy(bottom_line, wrk_line, 4*tile_width);
192
 
            }
193
 
 
194
 
            /*
195
 
             * Write out the result in a tile.
196
 
             */
197
 
 
198
 
            if( TIFFWriteEncodedTile( out,
199
 
                                      TIFFComputeTile( out, col, row, 0, 0),
200
 
                                      raster,
201
 
                                      4 * tile_width * tile_height ) == -1 )
202
 
            {
203
 
                ok = 0;
204
 
                break;
205
 
            }
206
 
        }
207
 
    }
208
 
 
209
 
    _TIFFfree( raster );
210
 
    _TIFFfree( wrk_line );
211
 
 
212
 
    return ok;
213
 
}
214
 
 
215
 
static int
216
 
cvt_by_strip( TIFF *in, TIFF *out )
217
 
 
218
 
{
219
 
    uint32* raster;                     /* retrieve RGBA image */
220
 
    uint32  width, height;              /* image width & height */
221
 
    uint32  row;
222
 
    uint32  *wrk_line;
223
 
    int     ok = 1;
224
 
 
225
 
    TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &width);
226
 
    TIFFGetField(in, TIFFTAG_IMAGELENGTH, &height);
227
 
 
228
 
    if( !TIFFGetField(in, TIFFTAG_ROWSPERSTRIP, &rowsperstrip) ) {
229
 
        TIFFError(TIFFFileName(in), "Source image not in strips");
230
 
        return (0);
231
 
    }
232
 
    
233
 
    TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
234
 
 
235
 
    /*
236
 
     * Allocate strip buffer
237
 
     */
238
 
    raster = (uint32*)_TIFFmalloc(width * rowsperstrip * sizeof (uint32));
239
 
    if (raster == 0) {
240
 
        TIFFError(TIFFFileName(in), "No space for raster buffer");
241
 
        return (0);
242
 
    }
243
 
 
244
 
    /*
245
 
     * Allocate a scanline buffer for swapping during the vertical
246
 
     * mirroring pass.
247
 
     */
248
 
    wrk_line = (uint32*)_TIFFmalloc(width * sizeof (uint32));
249
 
    if (wrk_line == 0) {
250
 
        TIFFError(TIFFFileName(in), "No space for raster scanline buffer");
251
 
        ok = 0;
252
 
    }
253
 
    
254
 
    /*
255
 
     * Loop over the strips.
256
 
     */
257
 
    for( row = 0; ok && row < height; row += rowsperstrip )
258
 
    {
259
 
        int     rows_to_write, i_row;
260
 
 
261
 
        /* Read the strip into an RGBA array */
262
 
        if (!TIFFReadRGBAStrip(in, row, raster)) {
263
 
            ok = 0;
264
 
            break;
265
 
        }
266
 
 
267
 
        /*
268
 
         * Figure out the number of scanlines actually in this strip.
269
 
         */
270
 
        if( row + rowsperstrip > height )
271
 
            rows_to_write = height - row;
272
 
        else
273
 
            rows_to_write = rowsperstrip;
274
 
 
275
 
        /*
276
 
         * For some reason the TIFFReadRGBAStrip() function chooses the
277
 
         * lower left corner as the origin.  Vertically mirror scanlines.
278
 
         */
279
 
 
280
 
        for( i_row = 0; i_row < rows_to_write / 2; i_row++ )
281
 
        {
282
 
            uint32      *top_line, *bottom_line;
283
 
 
284
 
            top_line = raster + width * i_row;
285
 
            bottom_line = raster + width * (rows_to_write-i_row-1);
286
 
 
287
 
            _TIFFmemcpy(wrk_line, top_line, 4*width);
288
 
            _TIFFmemcpy(top_line, bottom_line, 4*width);
289
 
            _TIFFmemcpy(bottom_line, wrk_line, 4*width);
290
 
        }
291
 
 
292
 
        /*
293
 
         * Write out the result in a strip
294
 
         */
295
 
 
296
 
        if( TIFFWriteEncodedStrip( out, row / rowsperstrip, raster,
297
 
                                   4 * rows_to_write * width ) == -1 )
298
 
        {
299
 
            ok = 0;
300
 
            break;
301
 
        }
302
 
    }
303
 
 
304
 
    _TIFFfree( raster );
305
 
    _TIFFfree( wrk_line );
306
 
 
307
 
    return ok;
308
 
}
309
 
 
310
 
/*
311
 
 * cvt_whole_image()
312
 
 *
313
 
 * read the whole image into one big RGBA buffer and then write out
314
 
 * strips from that.  This is using the traditional TIFFReadRGBAImage()
315
 
 * API that we trust.
316
 
 */
317
 
 
318
 
static int
319
 
cvt_whole_image( TIFF *in, TIFF *out )
320
 
 
321
 
{
322
 
    uint32* raster;                     /* retrieve RGBA image */
323
 
    uint32  width, height;              /* image width & height */
324
 
    uint32      row;
325
 
    uint32  *wrk_line;
326
 
        
327
 
        
328
 
    TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &width);
329
 
    TIFFGetField(in, TIFFTAG_IMAGELENGTH, &height);
330
 
 
331
 
    rowsperstrip = TIFFDefaultStripSize(out, rowsperstrip);
332
 
    TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
333
 
 
334
 
    raster = (uint32*)_TIFFmalloc(width * height * sizeof (uint32));
335
 
    if (raster == 0) {
336
 
        TIFFError(TIFFFileName(in), "No space for raster buffer");
337
 
        return (0);
338
 
    }
339
 
 
340
 
    /* Read the image in one chunk into an RGBA array */
341
 
    if (!TIFFReadRGBAImageOriented(in, width, height, raster,
342
 
                                   ORIENTATION_TOPLEFT, 0)) {
343
 
        _TIFFfree(raster);
344
 
        return (0);
345
 
    }
346
 
 
347
 
    /*
348
 
    ** Do we want to strip away alpha components?
349
 
    */
350
 
    if( no_alpha )
351
 
    {
352
 
        int     pixel_count = width * height;
353
 
        unsigned char *src, *dst;
354
 
 
355
 
        src = (unsigned char *) raster;
356
 
        dst = (unsigned char *) raster;
357
 
        while( pixel_count > 0 )
358
 
        {
359
 
            *(dst++) = *(src++);
360
 
            *(dst++) = *(src++);
361
 
            *(dst++) = *(src++);
362
 
            src++;
363
 
            pixel_count--;
364
 
        }
365
 
    }
366
 
 
367
 
    /* Write out the result in strips */
368
 
 
369
 
    for( row = 0; row < height; row += rowsperstrip )
370
 
    {
371
 
        unsigned char * raster_strip;
372
 
        int     rows_to_write;
373
 
        int     bytes_per_pixel;
374
 
 
375
 
        if( no_alpha )
376
 
        {
377
 
            raster_strip = ((unsigned char *) raster) + 3 * row * width;
378
 
            bytes_per_pixel = 3;
379
 
        }
380
 
        else
381
 
        {
382
 
            raster_strip = (unsigned char *) (raster + row * width);
383
 
            bytes_per_pixel = 4;
384
 
        }
385
 
 
386
 
        if( row + rowsperstrip > height )
387
 
            rows_to_write = height - row;
388
 
        else
389
 
            rows_to_write = rowsperstrip;
390
 
 
391
 
        if( TIFFWriteEncodedStrip( out, row / rowsperstrip, raster_strip,
392
 
                             bytes_per_pixel * rows_to_write * width ) == -1 )
393
 
        {
394
 
            _TIFFfree( raster );
395
 
            return 0;
396
 
        }
397
 
    }
398
 
 
399
 
    _TIFFfree( raster );
400
 
 
401
 
    return 1;
402
 
}
403
 
 
404
 
 
405
 
static int
406
 
tiffcvt(TIFF* in, TIFF* out)
407
 
{
408
 
        uint32 width, height;           /* image width & height */
409
 
        uint16 shortv;
410
 
        float floatv;
411
 
        char *stringv;
412
 
        uint32 longv;
413
 
        uint16 v[1];
414
 
 
415
 
        TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &width);
416
 
        TIFFGetField(in, TIFFTAG_IMAGELENGTH, &height);
417
 
 
418
 
        CopyField(TIFFTAG_SUBFILETYPE, longv);
419
 
        TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width);
420
 
        TIFFSetField(out, TIFFTAG_IMAGELENGTH, height);
421
 
        TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);
422
 
        TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
423
 
        TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
424
 
 
425
 
        CopyField(TIFFTAG_FILLORDER, shortv);
426
 
        TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
427
 
 
428
 
        if( no_alpha )
429
 
            TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 3);
430
 
        else
431
 
            TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 4);
432
 
 
433
 
        if( !no_alpha )
434
 
        {
435
 
            v[0] = EXTRASAMPLE_ASSOCALPHA;
436
 
            TIFFSetField(out, TIFFTAG_EXTRASAMPLES, 1, v);
437
 
        }
438
 
 
439
 
        CopyField(TIFFTAG_XRESOLUTION, floatv);
440
 
        CopyField(TIFFTAG_YRESOLUTION, floatv);
441
 
        CopyField(TIFFTAG_RESOLUTIONUNIT, shortv);
442
 
        TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
443
 
        TIFFSetField(out, TIFFTAG_SOFTWARE, TIFFGetVersion());
444
 
        CopyField(TIFFTAG_DOCUMENTNAME, stringv);
445
 
 
446
 
        if( process_by_block && TIFFIsTiled( in ) )
447
 
            return( cvt_by_tile( in, out ) );
448
 
        else if( process_by_block )
449
 
            return( cvt_by_strip( in, out ) );
450
 
        else
451
 
            return( cvt_whole_image( in, out ) );
452
 
}
453
 
 
454
 
static char* stuff[] = {
455
 
    "usage: tiff2rgba [-c comp] [-r rows] [-b] input... output\n",
456
 
    "where comp is one of the following compression algorithms:\n",
457
 
    " jpeg\t\tJPEG encoding\n",
458
 
    " zip\t\tLempel-Ziv & Welch encoding\n",
459
 
    " lzw\t\tLempel-Ziv & Welch encoding\n",
460
 
    " packbits\tPackBits encoding\n",
461
 
    " none\t\tno compression\n",
462
 
    "and the other options are:\n",
463
 
    " -r\trows/strip\n",
464
 
    " -b (progress by block rather than as a whole image)\n",
465
 
    " -n don't emit alpha component.\n",
466
 
    NULL
467
 
};
468
 
 
469
 
static void
470
 
usage(int code)
471
 
{
472
 
        char buf[BUFSIZ];
473
 
        int i;
474
 
 
475
 
        setbuf(stderr, buf);
476
 
        fprintf(stderr, "%s\n\n", TIFFGetVersion());
477
 
        for (i = 0; stuff[i] != NULL; i++)
478
 
                fprintf(stderr, "%s\n", stuff[i]);
479
 
        exit(code);
480
 
}