1
/* tiff.c - load TIFF image from file
3
* Raster graphics library
5
* Copyright (c) 1997-2003 Alfredo K. Kojima
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Library General Public
9
* License as published by the Free Software Foundation; either
10
* version 2 of the License, or (at your option) any later version.
12
* This library is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Library General Public License for more details.
17
* You should have received a copy of the GNU Library General Public
18
* License along with this library; if not, write to the Free
19
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
39
RLoadTIFF(RContext *context, char *file, int index)
44
unsigned char *r, *g, *b, *a;
53
tif = TIFFOpen(file, "r");
60
if (!TIFFReadDirectory(tif)) {
61
RErrorCode = RERR_BADINDEX;
69
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
70
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
72
TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
73
&extrasamples, &sampleinfo);
75
alpha = (extrasamples == 1 &&
76
((sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA) || (sampleinfo[0] == EXTRASAMPLE_UNASSALPHA)));
77
amode = (extrasamples == 1 && sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
79
if (width<1 || height<1) {
80
RErrorCode = RERR_BADIMAGEFILE;
86
ptr = data = (uint32*)_TIFFmalloc(width * height * sizeof(uint32));
89
RErrorCode = RERR_NOMEMORY;
91
if (!TIFFReadRGBAImage(tif, width, height, data, 0)) {
92
RErrorCode = RERR_BADIMAGEFILE;
96
image = RCreateImage(width, height, alpha);
111
/* data seems to be stored upside down */
112
data += width * (height-1);
113
for (y=0; y<height; y++) {
114
for (x=0; x<width; x++) {
116
*(r) = (*data) & 0xff;
117
*(g) = (*data >> 8) & 0xff;
118
*(b) = (*data >> 16) & 0xff;
121
*(a) = (*data >> 24) & 0xff;
123
if (amode && (*a > 0)) {
124
*r = (*r * 255) / *(a);
125
*g = (*g * 255) / *(a);
126
*b = (*b * 255) / *(a);
147
#endif /* USE_TIFF */