wmaker: Replaced local 'extern' definition of wPreferences by proper header usage
[wmaker-crm.git] / wrlib / tiff.c
bloba54596dab667c002919471eef04432f21d0bc94a
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,
20 * MA 02110-1301, USA.
23 #include <config.h>
25 #ifdef USE_TIFF
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
31 #include <tiff.h>
32 #include <tiffio.h>
34 #include "wraster.h"
35 #include "imgformat.h"
37 RImage *RLoadTIFF(const char *file, int index)
39 RImage *image = NULL;
40 TIFF *tif;
41 int i;
42 unsigned char *r, *g, *b, *a;
43 uint16 alpha, amode;
44 uint32 width, height;
45 uint32 *data, *ptr;
46 uint16 extrasamples;
47 uint16 *sampleinfo;
48 int ch;
50 tif = TIFFOpen(file, "r");
51 if (!tif)
52 return NULL;
54 /* seek index */
55 i = index;
56 while (i > 0) {
57 if (!TIFFReadDirectory(tif)) {
58 RErrorCode = RERR_BADINDEX;
59 TIFFClose(tif);
60 return NULL;
62 i--;
65 /* get info */
66 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
67 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
69 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES, &extrasamples, &sampleinfo);
71 alpha = (extrasamples == 1 &&
72 ((sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA) || (sampleinfo[0] == EXTRASAMPLE_UNASSALPHA)));
73 amode = (extrasamples == 1 && sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
75 if (width < 1 || height < 1) {
76 RErrorCode = RERR_BADIMAGEFILE;
77 TIFFClose(tif);
78 return NULL;
81 /* read data */
82 ptr = data = (uint32 *) _TIFFmalloc(width * height * sizeof(uint32));
84 if (!data) {
85 RErrorCode = RERR_NOMEMORY;
86 } else {
87 if (!TIFFReadRGBAImage(tif, width, height, data, 0)) {
88 RErrorCode = RERR_BADIMAGEFILE;
89 } else {
91 /* convert data */
92 image = RCreateImage(width, height, alpha);
94 if (alpha)
95 ch = 4;
96 else
97 ch = 3;
99 if (image) {
100 int x, y;
102 r = image->data;
103 g = image->data + 1;
104 b = image->data + 2;
105 a = image->data + 3;
107 /* data seems to be stored upside down */
108 data += width * (height - 1);
109 for (y = 0; y < height; y++) {
110 for (x = 0; x < width; x++) {
112 *(r) = (*data) & 0xff;
113 *(g) = (*data >> 8) & 0xff;
114 *(b) = (*data >> 16) & 0xff;
116 if (alpha) {
117 *(a) = (*data >> 24) & 0xff;
119 if (amode && (*a > 0)) {
120 *r = (*r * 255) / *(a);
121 *g = (*g * 255) / *(a);
122 *b = (*b * 255) / *(a);
125 a += 4;
128 r += ch;
129 g += ch;
130 b += ch;
131 data++;
133 data -= 2 * width;
137 _TIFFfree(ptr);
140 TIFFClose(tif);
142 return image;
145 #endif /* USE_TIFF */