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
)
47 GifFileType
*gif
= NULL
;
48 GifPixelType
*buffer
= NULL
;
51 GifRecordType recType
;
52 ColorMapObject
*colormap
;
53 unsigned char rmap
[256];
54 unsigned char gmap
[256];
55 unsigned char bmap
[256];
60 /* default error message */
61 RErrorCode
= RERR_BADINDEX
;
63 gif
= DGifOpenFileName(file
);
66 switch (GifLastError()) {
67 case D_GIF_ERR_OPEN_FAILED
:
68 RErrorCode
= RERR_OPEN
;
70 case D_GIF_ERR_READ_FAILED
:
71 RErrorCode
= RERR_READ
;
74 RErrorCode
= RERR_BADIMAGEFILE
;
80 colormap
= gif
->SColorMap
;
86 GifByteType
*extension
;
88 if (DGifGetRecordType(gif
, &recType
) == GIF_ERROR
) {
92 case IMAGE_DESC_RECORD_TYPE
:
96 if (DGifGetImageDesc(gif
)==GIF_ERROR
) {
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 */
110 * Well, since the spec says the colormap can be anything,
111 * lets just render it with whatever garbage the stack
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
));
127 RErrorCode
= RERR_NOMEMORY
;
131 image
= RCreateImage(width
, height
, False
);
136 if (gif
->Image
.Interlace
) {
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
) {
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
];
155 for (j
= 0; j
< height
; j
++) {
156 if (DGifGetLine(gif
, buffer
, width
)==GIF_ERROR
) {
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
];
169 case EXTENSION_RECORD_TYPE
:
170 /* skip all extension blocks */
171 if (DGifGetExtension(gif
, &extCode
, &extension
)==GIF_ERROR
) {
175 if (DGifGetExtensionNext(gif
, &extension
)==GIF_ERROR
) {
184 } while (recType
!= TERMINATE_RECORD_TYPE
&& i
<= index
);
187 goto did_not_get_any_errors
;
189 switch (GifLastError()) {
190 case D_GIF_ERR_OPEN_FAILED
:
191 RErrorCode
= RERR_OPEN
;
193 case D_GIF_ERR_READ_FAILED
:
194 RErrorCode
= RERR_READ
;
197 RErrorCode
= RERR_BADIMAGEFILE
;
202 RDestroyImage(image
);
204 did_not_get_any_errors
: