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,
32 #include "imgformat.h"
34 static int InterlacedOffset
[] = { 0, 4, 2, 1 };
35 static int InterlacedJumps
[] = { 8, 8, 4, 2 };
38 * Partially based on code in gif2rgb from giflib, by Gershon Elber.
40 RImage
*RLoadGIF(const char *file
, int index
)
44 GifFileType
*gif
= NULL
;
45 GifPixelType
*buffer
= NULL
;
48 GifRecordType recType
;
49 ColorMapObject
*colormap
;
50 unsigned char rmap
[256], gmap
[256], bmap
[256];
56 /* default error message */
57 RErrorCode
= RERR_BADINDEX
;
60 gif
= DGifOpenFileName(file
);
61 #else /* USE_GIF == 5 */
62 gif
= DGifOpenFileName(file
, &gif_error
);
67 gif_error
= GifLastError();
70 case D_GIF_ERR_OPEN_FAILED
:
71 RErrorCode
= RERR_OPEN
;
73 case D_GIF_ERR_READ_FAILED
:
74 RErrorCode
= RERR_READ
;
77 RErrorCode
= RERR_BADIMAGEFILE
;
83 if (gif
->SWidth
< 1 || gif
->SHeight
< 1) {
85 RErrorCode
= RERR_BADIMAGEFILE
;
89 colormap
= gif
->SColorMap
;
94 GifByteType
*extension
;
96 if (DGifGetRecordType(gif
, &recType
) == GIF_ERROR
)
100 case IMAGE_DESC_RECORD_TYPE
:
104 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 for (j
= 0; j
< colormap
->ColorCount
; j
++) {
118 rmap
[j
] = colormap
->Colors
[j
].Red
;
119 gmap
[j
] = colormap
->Colors
[j
].Green
;
120 bmap
[j
] = colormap
->Colors
[j
].Blue
;
124 buffer
= malloc(width
* sizeof(GifPixelType
));
126 RErrorCode
= RERR_NOMEMORY
;
130 image
= RCreateImage(width
, height
, False
);
134 if (gif
->Image
.Interlace
) {
137 if (RRGBAFormat
== image
->format
)
138 pelsPerLine
= width
* 4;
140 pelsPerLine
= width
* 3;
142 for (j
= 0; j
< 4; j
++) {
143 for (k
= InterlacedOffset
[j
]; k
< height
; k
+= InterlacedJumps
[j
]) {
144 if (DGifGetLine(gif
, buffer
, width
) == GIF_ERROR
)
147 cptr
= image
->data
+ (k
* pelsPerLine
);
148 for (l
= 0; l
< width
; l
++) {
149 int pixel
= buffer
[l
];
150 *cptr
++ = rmap
[pixel
];
151 *cptr
++ = gmap
[pixel
];
152 *cptr
++ = bmap
[pixel
];
158 for (j
= 0; j
< height
; j
++) {
159 if (DGifGetLine(gif
, buffer
, width
) == GIF_ERROR
)
162 for (k
= 0; k
< width
; k
++) {
163 int pixel
= buffer
[k
];
164 *cptr
++ = rmap
[pixel
];
165 *cptr
++ = gmap
[pixel
];
166 *cptr
++ = bmap
[pixel
];
167 if (RRGBAFormat
== image
->format
)
174 case EXTENSION_RECORD_TYPE
:
175 /* skip all extension blocks */
176 if (DGifGetExtension(gif
, &extCode
, &extension
) == GIF_ERROR
)
180 if (DGifGetExtensionNext(gif
, &extension
) == GIF_ERROR
)
188 } while (recType
!= TERMINATE_RECORD_TYPE
&& i
<= index
);
191 goto did_not_get_any_errors
;
194 switch (GifLastError()) {
195 case D_GIF_ERR_OPEN_FAILED
:
196 RErrorCode
= RERR_OPEN
;
198 case D_GIF_ERR_READ_FAILED
:
199 RErrorCode
= RERR_READ
;
202 RErrorCode
= RERR_BADIMAGEFILE
;
206 /* With gif_lib v5 there's no way to know what went wrong */
207 RErrorCode
= RERR_BADIMAGEFILE
;
211 RReleaseImage(image
);
213 did_not_get_any_errors
: