wmiv: align version with wm
[wmaker-crm.git] / wrlib / load_tiff.c
blob8e86baa130ed56ef5b1403952095f4997dadd411
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 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
29 #include <tiff.h>
30 #include <tiffio.h>
31 #include <tiffvers.h>
33 #include "wraster.h"
34 #include "imgformat.h"
35 #include "wr_i18n.h"
38 RImage *RLoadTIFF(const char *file, int index)
40 RImage *image = NULL;
41 TIFF *tif;
42 int i, ch;
43 unsigned char *r, *g, *b, *a;
44 #if TIFFLIB_VERSION < 20210416
45 uint16 alpha, amode, extrasamples;
46 uint16 *sampleinfo;
47 uint32 width, height;
48 uint32 *data, *ptr;
49 #else
50 uint16_t alpha, amode, extrasamples;;
51 uint16_t *sampleinfo;
52 uint32_t width, height;
53 uint32_t *data, *ptr;
54 #endif
56 tif = TIFFOpen(file, "r");
57 if (!tif)
58 return NULL;
60 /* seek index */
61 i = index;
62 while (i > 0) {
63 if (!TIFFReadDirectory(tif)) {
64 RErrorCode = RERR_BADINDEX;
65 TIFFClose(tif);
66 return NULL;
68 i--;
71 /* get info */
72 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
73 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
75 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES, &extrasamples, &sampleinfo);
77 alpha = (extrasamples == 1 &&
78 ((sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA) || (sampleinfo[0] == EXTRASAMPLE_UNASSALPHA)));
79 amode = (extrasamples == 1 && sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
81 if (width < 1 || height < 1) {
82 RErrorCode = RERR_BADIMAGEFILE;
83 TIFFClose(tif);
84 return NULL;
87 /* read data */
88 #if TIFFLIB_VERSION < 20210416
89 ptr = data = (uint32 *) _TIFFmalloc(width * height * sizeof(uint32));
90 #else
91 ptr = data = (uint32_t *) _TIFFmalloc(width * height * sizeof(uint32_t));
92 #endif
94 if (!data) {
95 RErrorCode = RERR_NOMEMORY;
96 } else {
97 if (!TIFFReadRGBAImage(tif, width, height, data, 0)) {
98 RErrorCode = RERR_BADIMAGEFILE;
99 } else {
101 /* convert data */
102 image = RCreateImage(width, height, alpha);
104 if (alpha)
105 ch = 4;
106 else
107 ch = 3;
109 if (image) {
110 int x, y;
112 r = image->data;
113 g = image->data + 1;
114 b = image->data + 2;
115 a = image->data + 3;
117 /* data seems to be stored upside down */
118 data += width * (height - 1);
119 for (y = 0; y < height; y++) {
120 for (x = 0; x < width; x++) {
122 *(r) = (*data) & 0xff;
123 *(g) = (*data >> 8) & 0xff;
124 *(b) = (*data >> 16) & 0xff;
126 if (alpha) {
127 *(a) = (*data >> 24) & 0xff;
129 if (amode && (*a > 0)) {
130 *r = (*r * 255) / *(a);
131 *g = (*g * 255) / *(a);
132 *b = (*b * 255) / *(a);
135 a += 4;
138 r += ch;
139 g += ch;
140 b += ch;
141 data++;
143 data -= 2 * width;
147 _TIFFfree(ptr);
150 TIFFClose(tif);
152 return image;