wrlib: return NULL if XImage could not be taken, for consistency
[wmaker-crm.git] / wrlib / load_gif.c
blob7a6cfc7f88f696d46cda55cf8d53fbb6822707e0
1 /* gif.c - load GIF image from file
3 * Raster graphics library
5 * Copyright (c) 1998-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 <gif_lib.h>
31 #include "wraster.h"
32 #include "imgformat.h"
34 static int InterlacedOffset[] = { 0, 4, 2, 1 };
35 static int InterlacedJumps[] = { 8, 8, 4, 2 };
38 * Partially based on code in gif2rgb from giflib, by Gershon Elber.
40 RImage *RLoadGIF(const char *file, int index)
42 RImage *image = NULL;
43 unsigned char *cptr;
44 GifFileType *gif = NULL;
45 GifPixelType *buffer = NULL;
46 int i, j, k;
47 int width, height;
48 GifRecordType recType;
49 ColorMapObject *colormap;
50 unsigned char rmap[256], gmap[256], bmap[256];
51 int gif_error;
53 if (index < 0)
54 index = 0;
56 /* default error message */
57 RErrorCode = RERR_BADINDEX;
59 #if USE_GIF == 4
60 gif = DGifOpenFileName(file);
61 #else /* USE_GIF == 5 */
62 gif = DGifOpenFileName(file, &gif_error);
63 #endif
65 if (!gif) {
66 #if USE_GIF == 4
67 gif_error = GifLastError();
68 #endif
69 switch (gif_error) {
70 case D_GIF_ERR_OPEN_FAILED:
71 RErrorCode = RERR_OPEN;
72 break;
73 case D_GIF_ERR_READ_FAILED:
74 RErrorCode = RERR_READ;
75 break;
76 default:
77 RErrorCode = RERR_BADIMAGEFILE;
78 break;
80 return NULL;
83 if (gif->SWidth < 1 || gif->SHeight < 1) {
84 DGifCloseFile(gif);
85 RErrorCode = RERR_BADIMAGEFILE;
86 return NULL;
89 colormap = gif->SColorMap;
90 i = 0;
92 do {
93 int extCode;
94 GifByteType *extension;
96 if (DGifGetRecordType(gif, &recType) == GIF_ERROR)
97 goto giferr;
99 switch (recType) {
100 case IMAGE_DESC_RECORD_TYPE:
101 if (i++ != index)
102 break;
104 if (DGifGetImageDesc(gif) == GIF_ERROR)
105 goto giferr;
107 width = gif->Image.Width;
108 height = gif->Image.Height;
110 if (gif->Image.ColorMap)
111 colormap = gif->Image.ColorMap;
113 /* the gif specs talk about a default colormap, but it
114 * doesnt say what the heck is this default colormap
115 * Render anything */
116 if (colormap) {
117 for (j = 0; j < colormap->ColorCount; j++) {
118 rmap[j] = colormap->Colors[j].Red;
119 gmap[j] = colormap->Colors[j].Green;
120 bmap[j] = colormap->Colors[j].Blue;
124 buffer = malloc(width * sizeof(GifPixelType));
125 if (!buffer) {
126 RErrorCode = RERR_NOMEMORY;
127 goto bye;
130 image = RCreateImage(width, height, False);
131 if (!image)
132 goto bye;
134 if (gif->Image.Interlace) {
135 int l, pelsPerLine;
137 if (RRGBAFormat == image->format)
138 pelsPerLine = width * 4;
139 else
140 pelsPerLine = width * 3;
142 for (j = 0; j < 4; j++) {
143 for (k = InterlacedOffset[j]; k < height; k += InterlacedJumps[j]) {
144 if (DGifGetLine(gif, buffer, width) == GIF_ERROR)
145 goto giferr;
147 cptr = image->data + (k * pelsPerLine);
148 for (l = 0; l < width; l++) {
149 int pixel = buffer[l];
150 *cptr++ = rmap[pixel];
151 *cptr++ = gmap[pixel];
152 *cptr++ = bmap[pixel];
156 } else {
157 cptr = image->data;
158 for (j = 0; j < height; j++) {
159 if (DGifGetLine(gif, buffer, width) == GIF_ERROR)
160 goto giferr;
162 for (k = 0; k < width; k++) {
163 int pixel = buffer[k];
164 *cptr++ = rmap[pixel];
165 *cptr++ = gmap[pixel];
166 *cptr++ = bmap[pixel];
167 if (RRGBAFormat == image->format)
168 cptr++;
172 break;
174 case EXTENSION_RECORD_TYPE:
175 /* skip all extension blocks */
176 if (DGifGetExtension(gif, &extCode, &extension) == GIF_ERROR)
177 goto giferr;
179 while (extension)
180 if (DGifGetExtensionNext(gif, &extension) == GIF_ERROR)
181 goto giferr;
183 break;
185 default:
186 break;
188 } while (recType != TERMINATE_RECORD_TYPE && i <= index);
190 /* yuck! */
191 goto did_not_get_any_errors;
192 giferr:
193 #if USE_GIF == 4
194 switch (GifLastError()) {
195 case D_GIF_ERR_OPEN_FAILED:
196 RErrorCode = RERR_OPEN;
197 break;
198 case D_GIF_ERR_READ_FAILED:
199 RErrorCode = RERR_READ;
200 break;
201 default:
202 RErrorCode = RERR_BADIMAGEFILE;
203 break;
205 #else
206 /* With gif_lib v5 there's no way to know what went wrong */
207 RErrorCode = RERR_BADIMAGEFILE;
208 #endif
209 bye:
210 if (image)
211 RReleaseImage(image);
212 image = NULL;
213 did_not_get_any_errors:
215 if (buffer)
216 free(buffer);
218 if (gif)
219 DGifCloseFile(gif);
221 return image;