gif.c code style
[wmaker-crm.git] / wrlib / gif.c
blobebd87296bf5f97d24399a22b8843569bde6405c6
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 #ifdef USE_GIF
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
31 #include <gif_lib.h>
33 #include "wraster.h"
34 #include "imgformat.h"
36 static int InterlacedOffset[] = { 0, 4, 2, 1 };
37 static int InterlacedJumps[] = { 8, 8, 4, 2 };
40 * Partially based on code in gif2rgb from giflib, by Gershon Elber.
42 RImage *RLoadGIF(const char *file, int index)
44 RImage *image = NULL;
45 unsigned char *cptr;
46 GifFileType *gif = NULL;
47 GifPixelType *buffer = NULL;
48 int i, j, k;
49 int width, height;
50 GifRecordType recType;
51 ColorMapObject *colormap;
52 unsigned char rmap[256], gmap[256], bmap[256];
54 if (index < 0)
55 index = 0;
57 /* default error message */
58 RErrorCode = RERR_BADINDEX;
60 gif = DGifOpenFileName(file);
62 if (!gif) {
63 switch (GifLastError()) {
64 case D_GIF_ERR_OPEN_FAILED:
65 RErrorCode = RERR_OPEN;
66 break;
67 case D_GIF_ERR_READ_FAILED:
68 RErrorCode = RERR_READ;
69 break;
70 default:
71 RErrorCode = RERR_BADIMAGEFILE;
72 break;
74 return NULL;
77 if (gif->SWidth < 1 || gif->SHeight < 1) {
78 DGifCloseFile(gif);
79 RErrorCode = RERR_BADIMAGEFILE;
80 return NULL;
83 colormap = gif->SColorMap;
84 i = 0;
86 do {
87 int extCode;
88 GifByteType *extension;
90 if (DGifGetRecordType(gif, &recType) == GIF_ERROR)
91 goto giferr;
93 switch (recType) {
94 case IMAGE_DESC_RECORD_TYPE:
95 if (i++ != index)
96 break;
98 if (DGifGetImageDesc(gif) == GIF_ERROR)
99 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 * Render anything */
110 if (colormap) {
111 for (j = 0; j < colormap->ColorCount; j++) {
112 rmap[j] = colormap->Colors[j].Red;
113 gmap[j] = colormap->Colors[j].Green;
114 bmap[j] = colormap->Colors[j].Blue;
118 buffer = malloc(width * sizeof(GifColorType));
119 if (!buffer) {
120 RErrorCode = RERR_NOMEMORY;
121 goto bye;
124 image = RCreateImage(width, height, False);
125 if (!image)
126 goto bye;
128 if (gif->Image.Interlace) {
129 int l, pelsPerLine;
131 if (RRGBAFormat == image->format)
132 pelsPerLine = width * 4;
133 else
134 pelsPerLine = width * 3;
136 for (j = 0; j < 4; j++) {
137 for (k = InterlacedOffset[j]; k < height; k += InterlacedJumps[j]) {
138 if (DGifGetLine(gif, buffer, width) == GIF_ERROR)
139 goto giferr;
141 cptr = image->data + (k * pelsPerLine);
142 for (l = 0; l < width; l++) {
143 int pixel = buffer[l];
144 *cptr++ = rmap[pixel];
145 *cptr++ = gmap[pixel];
146 *cptr++ = bmap[pixel];
150 } else {
151 cptr = image->data;
152 for (j = 0; j < height; j++) {
153 if (DGifGetLine(gif, buffer, width) == GIF_ERROR)
154 goto giferr;
156 for (k = 0; k < width; k++) {
157 int pixel = buffer[k];
158 *cptr++ = rmap[pixel];
159 *cptr++ = gmap[pixel];
160 *cptr++ = bmap[pixel];
161 if (RRGBAFormat == image->format)
162 cptr++;
166 break;
168 case EXTENSION_RECORD_TYPE:
169 /* skip all extension blocks */
170 if (DGifGetExtension(gif, &extCode, &extension) == GIF_ERROR)
171 goto giferr;
173 while (extension)
174 if (DGifGetExtensionNext(gif, &extension) == GIF_ERROR)
175 goto giferr;
177 break;
179 default:
180 break;
182 } while (recType != TERMINATE_RECORD_TYPE && i <= index);
184 /* yuck! */
185 goto did_not_get_any_errors;
186 giferr:
187 switch (GifLastError()) {
188 case D_GIF_ERR_OPEN_FAILED:
189 RErrorCode = RERR_OPEN;
190 break;
191 case D_GIF_ERR_READ_FAILED:
192 RErrorCode = RERR_READ;
193 break;
194 default:
195 RErrorCode = RERR_BADIMAGEFILE;
196 break;
198 bye:
199 if (image)
200 RReleaseImage(image);
201 image = NULL;
202 did_not_get_any_errors:
204 if (buffer)
205 free(buffer);
207 if (gif)
208 DGifCloseFile(gif);
210 return image;
213 #endif /* USE_GIF */