Change to the linux kernel coding style
[wmaker-crm.git] / wrlib / tiff.c
1 /* tiff.c - load TIFF image from file
2  *
3  * Raster graphics library
4  *
5  * Copyright (c) 1997-2003 Alfredo K. Kojima
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include <config.h>
23
24 #ifdef USE_TIFF
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29
30 #include <tiff.h>
31 #include <tiffio.h>
32
33 #include "wraster.h"
34
35 RImage *RLoadTIFF(RContext * context, char *file, int index)
36 {
37         RImage *image = NULL;
38         TIFF *tif;
39         int i;
40         unsigned char *r, *g, *b, *a;
41         uint16 alpha, amode;
42         uint32 width, height;
43         uint32 *data, *ptr;
44         uint16 extrasamples;
45         uint16 *sampleinfo;
46         int ch;
47
48         tif = TIFFOpen(file, "r");
49         if (!tif)
50                 return NULL;
51
52         /* seek index */
53         i = index;
54         while (i > 0) {
55                 if (!TIFFReadDirectory(tif)) {
56                         RErrorCode = RERR_BADINDEX;
57                         TIFFClose(tif);
58                         return NULL;
59                 }
60                 i--;
61         }
62
63         /* get info */
64         TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
65         TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
66
67         TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES, &extrasamples, &sampleinfo);
68
69         alpha = (extrasamples == 1 &&
70                  ((sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA) || (sampleinfo[0] == EXTRASAMPLE_UNASSALPHA)));
71         amode = (extrasamples == 1 && sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
72
73         if (width < 1 || height < 1) {
74                 RErrorCode = RERR_BADIMAGEFILE;
75                 TIFFClose(tif);
76                 return NULL;
77         }
78
79         /* read data */
80         ptr = data = (uint32 *) _TIFFmalloc(width * height * sizeof(uint32));
81
82         if (!data) {
83                 RErrorCode = RERR_NOMEMORY;
84         } else {
85                 if (!TIFFReadRGBAImage(tif, width, height, data, 0)) {
86                         RErrorCode = RERR_BADIMAGEFILE;
87                 } else {
88
89                         /* convert data */
90                         image = RCreateImage(width, height, alpha);
91
92                         if (alpha)
93                                 ch = 4;
94                         else
95                                 ch = 3;
96
97                         if (image) {
98                                 int x, y;
99
100                                 r = image->data;
101                                 g = image->data + 1;
102                                 b = image->data + 2;
103                                 a = image->data + 3;
104
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++) {
109
110                                                 *(r) = (*data) & 0xff;
111                                                 *(g) = (*data >> 8) & 0xff;
112                                                 *(b) = (*data >> 16) & 0xff;
113
114                                                 if (alpha) {
115                                                         *(a) = (*data >> 24) & 0xff;
116
117                                                         if (amode && (*a > 0)) {
118                                                                 *r = (*r * 255) / *(a);
119                                                                 *g = (*g * 255) / *(a);
120                                                                 *b = (*b * 255) / *(a);
121                                                         }
122
123                                                         a += 4;
124                                                 }
125
126                                                 r += ch;
127                                                 g += ch;
128                                                 b += ch;
129                                                 data++;
130                                         }
131                                         data -= 2 * width;
132                                 }
133                         }
134                 }
135                 _TIFFfree(ptr);
136         }
137
138         TIFFClose(tif);
139
140         return image;
141 }
142
143 #endif                          /* USE_TIFF */