Updating to version 0.20.2
[wmaker-crm.git] / wrlib / gif.c
blob0a98ebb8031b18059374689140a08171fb6f84cd
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 GifFileType *gif = NULL;
48 GifPixelType *buffer = NULL;
49 int i, j, k, ofs = 0;
50 int width, height;
51 GifRecordType recType;
52 ColorMapObject *colormap;
53 unsigned char rmap[256];
54 unsigned char gmap[256];
55 unsigned char bmap[256];
57 if (index < 0)
58 index = 0;
60 /* default error message */
61 RErrorCode = RERR_BADINDEX;
63 gif = DGifOpenFileName(file);
65 if (!gif) {
66 switch (GifLastError()) {
67 case D_GIF_ERR_OPEN_FAILED:
68 RErrorCode = RERR_OPEN;
69 break;
70 case D_GIF_ERR_READ_FAILED:
71 RErrorCode = RERR_READ;
72 break;
73 default:
74 RErrorCode = RERR_BADIMAGEFILE;
75 break;
77 return NULL;
80 colormap = gif->SColorMap;
82 i = 0;
84 do {
85 int extCode;
86 GifByteType *extension;
88 if (DGifGetRecordType(gif, &recType) == GIF_ERROR) {
89 goto giferr;
91 switch (recType) {
92 case IMAGE_DESC_RECORD_TYPE:
93 if (i++ != index)
94 break;
96 if (DGifGetImageDesc(gif)==GIF_ERROR) {
97 goto giferr;
100 width = gif->Image.Width;
101 height = gif->Image.Height;
103 if (gif->Image.ColorMap)
104 colormap = gif->Image.ColorMap;
106 /* the gif specs talk about a default colormap, but it
107 * doesnt say what the heck is this default colormap */
108 if (!colormap) {
110 * Well, since the spec says the colormap can be anything,
111 * lets just render it with whatever garbage the stack
112 * has :)
115 goto bye;
117 } else {
118 for (j = 0; j < colormap->ColorCount; j++) {
119 rmap[j] = colormap->Colors[j].Red;
120 gmap[j] = colormap->Colors[j].Green;
121 bmap[j] = colormap->Colors[j].Blue;
125 buffer = malloc(width * sizeof(GifColorType));
126 if (!buffer) {
127 RErrorCode = RERR_NOMEMORY;
128 goto bye;
131 image = RCreateImage(width, height, False);
132 if (!image) {
133 goto bye;
136 if (gif->Image.Interlace) {
137 int l;
139 for (j = 0; j < 4; j++) {
140 for (k = InterlacedOffset[j]; k < height;
141 k += InterlacedJumps[j]) {
142 if (DGifGetLine(gif, buffer, width)==GIF_ERROR) {
143 goto giferr;
145 ofs = k*width;
146 for (l = 0; l < width; l++, ofs++) {
147 int pixel = buffer[l];
148 image->data[0][ofs] = rmap[pixel];
149 image->data[1][ofs] = gmap[pixel];
150 image->data[2][ofs] = bmap[pixel];
154 } else {
155 for (j = 0; j < height; j++) {
156 if (DGifGetLine(gif, buffer, width)==GIF_ERROR) {
157 goto giferr;
159 for (k = 0; k < width; k++, ofs++) {
160 int pixel = buffer[k];
161 image->data[0][ofs] = rmap[pixel];
162 image->data[1][ofs] = gmap[pixel];
163 image->data[2][ofs] = bmap[pixel];
167 break;
169 case EXTENSION_RECORD_TYPE:
170 /* skip all extension blocks */
171 if (DGifGetExtension(gif, &extCode, &extension)==GIF_ERROR) {
172 goto giferr;
174 while (extension) {
175 if (DGifGetExtensionNext(gif, &extension)==GIF_ERROR) {
176 goto giferr;
179 break;
181 default:
182 break;
184 } while (recType != TERMINATE_RECORD_TYPE && i <= index);
186 /* yuck! */
187 goto did_not_get_any_errors;
188 giferr:
189 switch (GifLastError()) {
190 case D_GIF_ERR_OPEN_FAILED:
191 RErrorCode = RERR_OPEN;
192 break;
193 case D_GIF_ERR_READ_FAILED:
194 RErrorCode = RERR_READ;
195 break;
196 default:
197 RErrorCode = RERR_BADIMAGEFILE;
198 break;
200 bye:
201 if (image)
202 RDestroyImage(image);
203 image = NULL;
204 did_not_get_any_errors:
206 if (buffer)
207 free(buffer);
209 if (gif)
210 DGifCloseFile(gif);
212 return image;
215 #endif /* USE_GIF */