1 /* gif.c - load GIF image from file
3 * Raster graphics library
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.
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 colormap
= gif
->SColorMap
;
87 GifByteType
*extension
;
89 if (DGifGetRecordType(gif
, &recType
) == GIF_ERROR
) {
93 case IMAGE_DESC_RECORD_TYPE
:
97 if (DGifGetImageDesc(gif
)==GIF_ERROR
) {
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 */
111 * Well, since the spec says the colormap can be anything,
112 * lets just render it with whatever garbage the stack
119 for (j
= 0; j
< colormap
->ColorCount
; j
++) {
120 rmap
[j
] = colormap
->Colors
[j
].Red
;
121 gmap
[j
] = colormap
->Colors
[j
].Green
;
122 bmap
[j
] = colormap
->Colors
[j
].Blue
;
126 buffer
= malloc(width
* sizeof(GifColorType
));
128 RErrorCode
= RERR_NOMEMORY
;
132 image
= RCreateImage(width
, height
, False
);
137 if (gif
->Image
.Interlace
) {
141 if (RRGBAFormat
==image
->format
)
142 pelsPerLine
= width
* 4;
144 pelsPerLine
= width
* 3;
146 for (j
= 0; j
< 4; j
++) {
147 for (k
= InterlacedOffset
[j
]; k
< height
;
148 k
+= InterlacedJumps
[j
]) {
149 if (DGifGetLine(gif
, buffer
, width
)==GIF_ERROR
) {
152 cptr
= image
->data
+ (k
*pelsPerLine
);
153 for (l
= 0; l
< width
; l
++) {
154 int pixel
= buffer
[l
];
155 *cptr
++ = rmap
[pixel
];
156 *cptr
++ = gmap
[pixel
];
157 *cptr
++ = bmap
[pixel
];
163 for (j
= 0; j
< height
; j
++) {
164 if (DGifGetLine(gif
, buffer
, width
)==GIF_ERROR
) {
167 for (k
= 0; k
< width
; k
++) {
168 int pixel
= buffer
[k
];
169 *cptr
++ = rmap
[pixel
];
170 *cptr
++ = gmap
[pixel
];
171 *cptr
++ = bmap
[pixel
];
172 if (RRGBAFormat
==image
->format
)
179 case EXTENSION_RECORD_TYPE
:
180 /* skip all extension blocks */
181 if (DGifGetExtension(gif
, &extCode
, &extension
)==GIF_ERROR
) {
185 if (DGifGetExtensionNext(gif
, &extension
)==GIF_ERROR
) {
194 } while (recType
!= TERMINATE_RECORD_TYPE
&& i
<= index
);
197 goto did_not_get_any_errors
;
199 switch (GifLastError()) {
200 case D_GIF_ERR_OPEN_FAILED
:
201 RErrorCode
= RERR_OPEN
;
203 case D_GIF_ERR_READ_FAILED
:
204 RErrorCode
= RERR_READ
;
207 RErrorCode
= RERR_BADIMAGEFILE
;
212 RReleaseImage(image
);
214 did_not_get_any_errors
: