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., 51 Franklin St, Fifth Floor, Boston,
36 RImage
*RLoadTIFF(char *file
, int index
)
41 unsigned char *r
, *g
, *b
, *a
;
49 tif
= TIFFOpen(file
, "r");
56 if (!TIFFReadDirectory(tif
)) {
57 RErrorCode
= RERR_BADINDEX
;
65 TIFFGetField(tif
, TIFFTAG_IMAGEWIDTH
, &width
);
66 TIFFGetField(tif
, TIFFTAG_IMAGELENGTH
, &height
);
68 TIFFGetFieldDefaulted(tif
, TIFFTAG_EXTRASAMPLES
, &extrasamples
, &sampleinfo
);
70 alpha
= (extrasamples
== 1 &&
71 ((sampleinfo
[0] == EXTRASAMPLE_ASSOCALPHA
) || (sampleinfo
[0] == EXTRASAMPLE_UNASSALPHA
)));
72 amode
= (extrasamples
== 1 && sampleinfo
[0] == EXTRASAMPLE_ASSOCALPHA
);
74 if (width
< 1 || height
< 1) {
75 RErrorCode
= RERR_BADIMAGEFILE
;
81 ptr
= data
= (uint32
*) _TIFFmalloc(width
* height
* sizeof(uint32
));
84 RErrorCode
= RERR_NOMEMORY
;
86 if (!TIFFReadRGBAImage(tif
, width
, height
, data
, 0)) {
87 RErrorCode
= RERR_BADIMAGEFILE
;
91 image
= RCreateImage(width
, height
, alpha
);
106 /* data seems to be stored upside down */
107 data
+= width
* (height
- 1);
108 for (y
= 0; y
< height
; y
++) {
109 for (x
= 0; x
< width
; x
++) {
111 *(r
) = (*data
) & 0xff;
112 *(g
) = (*data
>> 8) & 0xff;
113 *(b
) = (*data
>> 16) & 0xff;
116 *(a
) = (*data
>> 24) & 0xff;
118 if (amode
&& (*a
> 0)) {
119 *r
= (*r
* 255) / *(a
);
120 *g
= (*g
* 255) / *(a
);
121 *b
= (*b
* 255) / *(a
);
144 #endif /* USE_TIFF */