Latest fixes for released 0.51.0
[wmaker-crm.git] / wrlib / tiff.c
blobbd67d703b44d6938f6f95fef4ca2204053534343
1 /* tiff.c - load TIFF image from file
2 *
3 * Raster graphics library
4 *
5 * Copyright (c) 1997 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.
22 #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>
35 #include "wraster.h"
38 RImage*
39 RLoadTIFF(RContext *context, char *file, int index)
41 RImage *image = NULL;
42 TIFF *tif;
43 int i;
44 unsigned char *r, *g, *b, *a;
45 uint16 alpha, amode;
46 uint32 width, height;
47 uint32 *data, *ptr;
48 uint16 extrasamples;
49 uint16 *sampleinfo;
52 tif = TIFFOpen(file, "r");
53 if (!tif)
54 return NULL;
56 /* seek index */
57 i = index;
58 while (i>0) {
59 if (!TIFFReadDirectory(tif)) {
60 RErrorCode = RERR_BADINDEX;
61 TIFFClose(tif);
62 return NULL;
64 i--;
67 /* get info */
68 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
69 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
71 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
72 &extrasamples, &sampleinfo);
74 alpha = (extrasamples == 1 &&
75 ((sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA) || (sampleinfo[0] == EXTRASAMPLE_UNASSALPHA)));
76 amode = (extrasamples == 1 && sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
78 if (width<1 || height<1) {
79 RErrorCode = RERR_BADIMAGEFILE;
80 TIFFClose(tif);
81 return NULL;
84 /* read data */
85 ptr = data = (uint32*)_TIFFmalloc(width * height * sizeof(uint32));
87 if (!data) {
88 RErrorCode = RERR_NOMEMORY;
89 } else {
90 if (!TIFFReadRGBAImage(tif, width, height, data, 0)) {
91 RErrorCode = RERR_BADIMAGEFILE;
92 } else {
94 /* convert data */
95 image = RCreateImage(width, height, alpha);
97 if (image) {
98 int x, y;
100 r = image->data[0];
101 g = image->data[1];
102 b = image->data[2];
103 a = image->data[3];
105 /* data seems to be stored upside down */
106 data += width * (height-1);
107 for (y=0; y<height; y++) {
108 for (x=0; x<width; x++) {
110 *(r) = (*data) & 0xff;
111 *(g) = (*data >> 8) & 0xff;
112 *(b) = (*data >> 16) & 0xff;
114 if (alpha) {
115 *(a) = (*data >> 24) & 0xff;
117 if (amode && (*a > 0)) {
118 *r = (*r * 255) / *(a);
119 *g = (*g * 255) / *(a);
120 *b = (*b * 255) / *(a);
123 a++;
126 r++; g++; b++;
127 data++;
129 data -= 2*width;
133 _TIFFfree(ptr);
136 TIFFClose(tif);
138 return image;
141 #endif /* USE_TIFF */