New function set_icon_image_from_database
[wmaker-crm.git] / wrlib / tiff.c
blobaccdfbe9bf5f9cc2358b4ed2c1279bd65221af85
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"
36 RImage *RLoadTIFF(char *file, int index)
38 RImage *image = NULL;
39 TIFF *tif;
40 int i;
41 unsigned char *r, *g, *b, *a;
42 uint16 alpha, amode;
43 uint32 width, height;
44 uint32 *data, *ptr;
45 uint16 extrasamples;
46 uint16 *sampleinfo;
47 int ch;
49 tif = TIFFOpen(file, "r");
50 if (!tif)
51 return NULL;
53 /* seek index */
54 i = index;
55 while (i > 0) {
56 if (!TIFFReadDirectory(tif)) {
57 RErrorCode = RERR_BADINDEX;
58 TIFFClose(tif);
59 return NULL;
61 i--;
64 /* get info */
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;
76 TIFFClose(tif);
77 return NULL;
80 /* read data */
81 ptr = data = (uint32 *) _TIFFmalloc(width * height * sizeof(uint32));
83 if (!data) {
84 RErrorCode = RERR_NOMEMORY;
85 } else {
86 if (!TIFFReadRGBAImage(tif, width, height, data, 0)) {
87 RErrorCode = RERR_BADIMAGEFILE;
88 } else {
90 /* convert data */
91 image = RCreateImage(width, height, alpha);
93 if (alpha)
94 ch = 4;
95 else
96 ch = 3;
98 if (image) {
99 int x, y;
101 r = image->data;
102 g = image->data + 1;
103 b = image->data + 2;
104 a = image->data + 3;
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;
115 if (alpha) {
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);
124 a += 4;
127 r += ch;
128 g += ch;
129 b += ch;
130 data++;
132 data -= 2 * width;
136 _TIFFfree(ptr);
139 TIFFClose(tif);
141 return image;
144 #endif /* USE_TIFF */