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,
35 static int InterlacedOffset
[] = { 0, 4, 2, 1 };
36 static int InterlacedJumps
[] = { 8, 8, 4, 2 };
39 * Partially based on code in gif2rgb from giflib, by Gershon Elber.
41 RImage
*RLoadGIF(char *file
, int index
)
45 GifFileType
*gif
= NULL
;
46 GifPixelType
*buffer
= NULL
;
49 GifRecordType recType
;
50 ColorMapObject
*colormap
;
51 unsigned char rmap
[256];
52 unsigned char gmap
[256];
53 unsigned char bmap
[256];
58 /* default error message */
59 RErrorCode
= RERR_BADINDEX
;
61 gif
= DGifOpenFileName(file
);
64 switch (GifLastError()) {
65 case D_GIF_ERR_OPEN_FAILED
:
66 RErrorCode
= RERR_OPEN
;
68 case D_GIF_ERR_READ_FAILED
:
69 RErrorCode
= RERR_READ
;
72 RErrorCode
= RERR_BADIMAGEFILE
;
78 if (gif
->SWidth
< 1 || gif
->SHeight
< 1) {
80 RErrorCode
= RERR_BADIMAGEFILE
;
84 colormap
= gif
->SColorMap
;
90 GifByteType
*extension
;
92 if (DGifGetRecordType(gif
, &recType
) == GIF_ERROR
) {
96 case IMAGE_DESC_RECORD_TYPE
:
100 if (DGifGetImageDesc(gif
) == GIF_ERROR
) {
104 width
= gif
->Image
.Width
;
105 height
= gif
->Image
.Height
;
107 if (gif
->Image
.ColorMap
)
108 colormap
= gif
->Image
.ColorMap
;
110 /* the gif specs talk about a default colormap, but it
111 * doesnt say what the heck is this default colormap */
114 * Well, since the spec says the colormap can be anything,
115 * lets just render it with whatever garbage the stack
122 for (j
= 0; j
< colormap
->ColorCount
; j
++) {
123 rmap
[j
] = colormap
->Colors
[j
].Red
;
124 gmap
[j
] = colormap
->Colors
[j
].Green
;
125 bmap
[j
] = colormap
->Colors
[j
].Blue
;
129 buffer
= malloc(width
* sizeof(GifColorType
));
131 RErrorCode
= RERR_NOMEMORY
;
135 image
= RCreateImage(width
, height
, False
);
140 if (gif
->Image
.Interlace
) {
144 if (RRGBAFormat
== image
->format
)
145 pelsPerLine
= width
* 4;
147 pelsPerLine
= width
* 3;
149 for (j
= 0; j
< 4; j
++) {
150 for (k
= InterlacedOffset
[j
]; k
< height
; k
+= InterlacedJumps
[j
]) {
151 if (DGifGetLine(gif
, buffer
, width
) == GIF_ERROR
) {
154 cptr
= image
->data
+ (k
* pelsPerLine
);
155 for (l
= 0; l
< width
; l
++) {
156 int pixel
= buffer
[l
];
157 *cptr
++ = rmap
[pixel
];
158 *cptr
++ = gmap
[pixel
];
159 *cptr
++ = bmap
[pixel
];
165 for (j
= 0; j
< height
; j
++) {
166 if (DGifGetLine(gif
, buffer
, width
) == GIF_ERROR
) {
169 for (k
= 0; k
< width
; k
++) {
170 int pixel
= buffer
[k
];
171 *cptr
++ = rmap
[pixel
];
172 *cptr
++ = gmap
[pixel
];
173 *cptr
++ = bmap
[pixel
];
174 if (RRGBAFormat
== image
->format
)
181 case EXTENSION_RECORD_TYPE
:
182 /* skip all extension blocks */
183 if (DGifGetExtension(gif
, &extCode
, &extension
) == GIF_ERROR
) {
187 if (DGifGetExtensionNext(gif
, &extension
) == GIF_ERROR
) {
196 } while (recType
!= TERMINATE_RECORD_TYPE
&& i
<= index
);
199 goto did_not_get_any_errors
;
201 switch (GifLastError()) {
202 case D_GIF_ERR_OPEN_FAILED
:
203 RErrorCode
= RERR_OPEN
;
205 case D_GIF_ERR_READ_FAILED
:
206 RErrorCode
= RERR_READ
;
209 RErrorCode
= RERR_BADIMAGEFILE
;
214 RReleaseImage(image
);
216 did_not_get_any_errors
: