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
) {
140 for (j
= 0; j
< 4; j
++) {
141 for (k
= InterlacedOffset
[j
]; k
< height
;
142 k
+= InterlacedJumps
[j
]) {
143 if (DGifGetLine(gif
, buffer
, width
)==GIF_ERROR
) {
147 for (l
= 0; l
< width
; l
++, ofs
++) {
148 int pixel
= buffer
[l
];
149 image
->data
[0][ofs
] = rmap
[pixel
];
150 image
->data
[1][ofs
] = gmap
[pixel
];
151 image
->data
[2][ofs
] = bmap
[pixel
];
156 for (j
= 0; j
< height
; j
++) {
157 if (DGifGetLine(gif
, buffer
, width
)==GIF_ERROR
) {
160 for (k
= 0; k
< width
; k
++, ofs
++) {
161 int pixel
= buffer
[k
];
162 image
->data
[0][ofs
] = rmap
[pixel
];
163 image
->data
[1][ofs
] = gmap
[pixel
];
164 image
->data
[2][ofs
] = bmap
[pixel
];
170 case EXTENSION_RECORD_TYPE
:
171 /* skip all extension blocks */
172 if (DGifGetExtension(gif
, &extCode
, &extension
)==GIF_ERROR
) {
176 if (DGifGetExtensionNext(gif
, &extension
)==GIF_ERROR
) {
185 } while (recType
!= TERMINATE_RECORD_TYPE
&& i
<= index
);
188 goto did_not_get_any_errors
;
190 switch (GifLastError()) {
191 case D_GIF_ERR_OPEN_FAILED
:
192 RErrorCode
= RERR_OPEN
;
194 case D_GIF_ERR_READ_FAILED
:
195 RErrorCode
= RERR_READ
;
198 RErrorCode
= RERR_BADIMAGEFILE
;
203 RDestroyImage(image
);
205 did_not_get_any_errors
: