- Added a test in configure for the version of libPropList that is installed
[wmaker-crm.git] / wrlib / gif.c
blob3719c4d886cc085d6e19ed134e853c55f16b8c84
1 /* gif.c - load GIF image from file
2 *
3 * Raster graphics library
4 *
5 * Copyright (c) 1998 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_GIF
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
31 #include <gif_lib.h>
33 #include "wraster.h"
36 static int InterlacedOffset[] = { 0, 4, 2, 1 };
37 static int InterlacedJumps[] = { 8, 8, 4, 2 };
41 * Partially based on code in gif2rgb from giflib, by Gershon Elber.
43 RImage*
44 RLoadGIF(RContext *context, char *file, int index)
46 RImage *image = NULL;
47 #if 0
48 GifFileType *gif = NULL;
49 GifPixelType *buffer = NULL;
50 int i, j, k, ofs = 0;
51 int width, height;
52 GifRecordType recType;
53 ColorMapObject *colormap;
54 unsigned char rmap[256];
55 unsigned char gmap[256];
56 unsigned char bmap[256];
58 if (index < 0)
59 index = 0;
61 /* default error message */
62 RErrorCode = RERR_BADINDEX;
64 gif = DGifOpenFileName(file);
66 if (!gif) {
67 switch (GifLastError()) {
68 case D_GIF_ERR_OPEN_FAILED:
69 RErrorCode = RERR_OPEN;
70 break;
71 case D_GIF_ERR_READ_FAILED:
72 RErrorCode = RERR_READ;
73 break;
74 default:
75 RErrorCode = RERR_BADIMAGEFILE;
76 break;
78 return NULL;
81 colormap = gif->SColorMap;
83 i = 0;
85 do {
86 int extCode;
87 GifByteType *extension;
89 if (DGifGetRecordType(gif, &recType) == GIF_ERROR) {
90 goto giferr;
92 switch (recType) {
93 case IMAGE_DESC_RECORD_TYPE:
94 if (i++ != index)
95 break;
97 if (DGifGetImageDesc(gif)==GIF_ERROR) {
98 goto giferr;
101 width = gif->Image.Width;
102 height = gif->Image.Height;
104 if (gif->Image.ColorMap)
105 colormap = gif->Image.ColorMap;
107 /* the gif specs talk about a default colormap, but it
108 * doesnt say what the heck is this default colormap */
109 if (!colormap) {
111 * Well, since the spec says the colormap can be anything,
112 * lets just render it with whatever garbage the stack
113 * has :)
116 goto bye;
118 } else {
119 for (j = 0; j < colormap->ColorCount; j++) {
120 rmap[j] = colormap->Colors[j].Red;
121 gmap[j] = colormap->Colors[j].Green;
122 bmap[j] = colormap->Colors[j].Blue;
126 buffer = malloc(width * sizeof(GifColorType));
127 if (!buffer) {
128 RErrorCode = RERR_NOMEMORY;
129 goto bye;
132 image = RCreateImage(width, height, False);
133 if (!image) {
134 goto bye;
137 if (gif->Image.Interlace) {
138 int l;
140 for (j = 0; j < 4; j++) {
141 for (k = InterlacedOffset[j]; k < height;
142 k += InterlacedJumps[j]) {
143 if (DGifGetLine(gif, buffer, width)==GIF_ERROR) {
144 goto giferr;
146 ofs = k*width;
147 for (l = 0; l < width; l++, ofs++) {
148 int pixel = buffer[l];
149 image->data[0][ofs] = rmap[pixel];
150 image->data[1][ofs] = gmap[pixel];
151 image->data[2][ofs] = bmap[pixel];
155 } else {
156 for (j = 0; j < height; j++) {
157 if (DGifGetLine(gif, buffer, width)==GIF_ERROR) {
158 goto giferr;
160 for (k = 0; k < width; k++, ofs++) {
161 int pixel = buffer[k];
162 image->data[0][ofs] = rmap[pixel];
163 image->data[1][ofs] = gmap[pixel];
164 image->data[2][ofs] = bmap[pixel];
168 break;
170 case EXTENSION_RECORD_TYPE:
171 /* skip all extension blocks */
172 if (DGifGetExtension(gif, &extCode, &extension)==GIF_ERROR) {
173 goto giferr;
175 while (extension) {
176 if (DGifGetExtensionNext(gif, &extension)==GIF_ERROR) {
177 goto giferr;
180 break;
182 default:
183 break;
185 } while (recType != TERMINATE_RECORD_TYPE && i <= index);
187 /* yuck! */
188 goto did_not_get_any_errors;
189 giferr:
190 switch (GifLastError()) {
191 case D_GIF_ERR_OPEN_FAILED:
192 RErrorCode = RERR_OPEN;
193 break;
194 case D_GIF_ERR_READ_FAILED:
195 RErrorCode = RERR_READ;
196 break;
197 default:
198 RErrorCode = RERR_BADIMAGEFILE;
199 break;
201 bye:
202 if (image)
203 RDestroyImage(image);
204 image = NULL;
205 did_not_get_any_errors:
207 if (buffer)
208 free(buffer);
210 if (gif)
211 DGifCloseFile(gif);
212 #endif
213 return image;
216 #endif /* USE_GIF */