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., 675 Mass Ave, Cambridge, MA 02139, USA.
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.
44 RLoadGIF(RContext
*context
, char *file
, int index
)
48 GifFileType
*gif
= NULL
;
49 GifPixelType
*buffer
= NULL
;
52 GifRecordType recType
;
53 ColorMapObject
*colormap
;
54 unsigned char rmap
[256];
55 unsigned char gmap
[256];
56 unsigned char bmap
[256];
61 /* default error message */
62 RErrorCode
= RERR_BADINDEX
;
64 gif
= DGifOpenFileName(file
);
67 switch (GifLastError()) {
68 case D_GIF_ERR_OPEN_FAILED
:
69 RErrorCode
= RERR_OPEN
;
71 case D_GIF_ERR_READ_FAILED
:
72 RErrorCode
= RERR_READ
;
75 RErrorCode
= RERR_BADIMAGEFILE
;
81 if (gif
->SWidth
<1 || gif
->SHeight
<1) {
83 RErrorCode
= RERR_BADIMAGEFILE
;
87 colormap
= gif
->SColorMap
;
93 GifByteType
*extension
;
95 if (DGifGetRecordType(gif
, &recType
) == GIF_ERROR
) {
99 case IMAGE_DESC_RECORD_TYPE
:
103 if (DGifGetImageDesc(gif
)==GIF_ERROR
) {
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 */
117 * Well, since the spec says the colormap can be anything,
118 * lets just render it with whatever garbage the stack
125 for (j
= 0; j
< colormap
->ColorCount
; j
++) {
126 rmap
[j
] = colormap
->Colors
[j
].Red
;
127 gmap
[j
] = colormap
->Colors
[j
].Green
;
128 bmap
[j
] = colormap
->Colors
[j
].Blue
;
132 buffer
= malloc(width
* sizeof(GifColorType
));
134 RErrorCode
= RERR_NOMEMORY
;
138 image
= RCreateImage(width
, height
, False
);
143 if (gif
->Image
.Interlace
) {
147 if (RRGBAFormat
==image
->format
)
148 pelsPerLine
= width
* 4;
150 pelsPerLine
= width
* 3;
152 for (j
= 0; j
< 4; j
++) {
153 for (k
= InterlacedOffset
[j
]; k
< height
;
154 k
+= InterlacedJumps
[j
]) {
155 if (DGifGetLine(gif
, buffer
, width
)==GIF_ERROR
) {
158 cptr
= image
->data
+ (k
*pelsPerLine
);
159 for (l
= 0; l
< width
; l
++) {
160 int pixel
= buffer
[l
];
161 *cptr
++ = rmap
[pixel
];
162 *cptr
++ = gmap
[pixel
];
163 *cptr
++ = bmap
[pixel
];
169 for (j
= 0; j
< height
; j
++) {
170 if (DGifGetLine(gif
, buffer
, width
)==GIF_ERROR
) {
173 for (k
= 0; k
< width
; k
++) {
174 int pixel
= buffer
[k
];
175 *cptr
++ = rmap
[pixel
];
176 *cptr
++ = gmap
[pixel
];
177 *cptr
++ = bmap
[pixel
];
178 if (RRGBAFormat
==image
->format
)
185 case EXTENSION_RECORD_TYPE
:
186 /* skip all extension blocks */
187 if (DGifGetExtension(gif
, &extCode
, &extension
)==GIF_ERROR
) {
191 if (DGifGetExtensionNext(gif
, &extension
)==GIF_ERROR
) {
200 } while (recType
!= TERMINATE_RECORD_TYPE
&& i
<= index
);
203 goto did_not_get_any_errors
;
205 switch (GifLastError()) {
206 case D_GIF_ERR_OPEN_FAILED
:
207 RErrorCode
= RERR_OPEN
;
209 case D_GIF_ERR_READ_FAILED
:
210 RErrorCode
= RERR_READ
;
213 RErrorCode
= RERR_BADIMAGEFILE
;
218 RReleaseImage(image
);
220 did_not_get_any_errors
: