1
/****************************************************************************
2
tiff.c - read and write tiff images using libtiff routines.
3
Distributed with Xplanet.
4
Copyright (C) 2002 Hari Nair <hari@alumni.caltech.edu>
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
****************************************************************************/
27
read_tiff(const char *filename, int *width, int *height, unsigned char **rgb)
29
unsigned char *ptr = NULL;
32
TIFF* tif = TIFFOpen(filename, "r");
37
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
38
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
43
raster = _TIFFmalloc(w * h * sizeof(uint32));
46
rgb[0] = (unsigned char *) realloc(rgb[0], 3 * *width * *height);
49
fprintf(stderr, "Can't allocate memory for TIFF file.\n");
55
if (TIFFReadRGBAImage(tif, w, h, raster, 0))
57
for (j = h - 1; j >= 0; j--)
60
for (i = 0; i < w; i++)
62
*ptr++ = (unsigned char) TIFFGetR(raster[istart + i]);
63
*ptr++ = (unsigned char) TIFFGetG(raster[istart + i]);
64
*ptr++ = (unsigned char) TIFFGetB(raster[istart + i]);
83
write_tiff(const char *filename, int width, int height, unsigned char *rgb)
85
unsigned char *raster;
88
TIFF* tif = TIFFOpen(filename, "w");
92
fprintf(stderr, "Can't create TIFF file\n");
96
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
97
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32) width);
98
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32) height);
99
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
100
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
101
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(tif, -1));
102
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
103
for (j = 0; j < height; j++)
105
raster = rgb + (j * width * 3);
106
TIFFWriteScanline(tif, raster, j, 0);