2 * Image Library -- libungif
4 * (c) 2006 Pavel Charvat <pchar@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
13 #include <ucw/mempool.h>
14 #include <ucw/fastbuf.h>
15 #include <images/images.h>
16 #include <images/error.h>
17 #include <images/color.h>
18 #include <images/io-main.h>
22 // API of gif_lib has changed recenly
24 #define GIFLIB_MAJOR 0
26 #if GIFLIB_MAJOR > 5 || GIFLIB_MAJOR == 5 && GIFLIB_MINOR >= 1
27 static int dgif_error_code; // Scratch pad only, no locking needed
28 #define DGIF_OPEN(_userptr, _readfunc) DGifOpen(_userptr, _readfunc, &dgif_error_code)
29 #define DGIF_CLOSE_FILE(_gif) DGifCloseFile(_gif, &dgif_error_code)
31 #define DGIF_OPEN(_userptr, _readfunc) DGifOpen(_userptr, _readfunc)
32 #define DGIF_CLOSE_FILE(_gif) DGifCloseFile(_gif)
35 struct libungif_read_data {
37 int transparent_index;
41 libungif_read_func(GifFileType *gif, GifByteType *ptr, int len)
43 DBG("libungif_read_func(len=%d)", len);
44 return bread((struct fastbuf *)gif->UserData, (byte *)ptr, len);
48 libungif_read_cancel(struct image_io *io)
50 DBG("libungif_read_cancel()");
52 struct libungif_read_data *rd = io->read_data;
53 DGIF_CLOSE_FILE(rd->gif);
57 libungif_read_header(struct image_io *io)
59 DBG("libungif_read_header()");
61 /* Create libungif structure */
63 if (unlikely(!(gif = DGIF_OPEN(io->fastbuf, libungif_read_func))))
65 IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "Cannot create libungif structure.");
69 struct libungif_read_data *rd = io->read_data = mp_alloc(io->internal_pool, sizeof(*rd));
72 DBG("executing DGifSlurp()");
73 if (unlikely(DGifSlurp(gif) != GIF_OK))
75 IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "Gif read failed.");
80 DBG("ImageCount=%d ColorResolution=%d SBackGroundColor=%d SColorMap=%p", gif->ImageCount, gif->SColorResolution, gif->SBackGroundColor, gif->SColorMap);
81 if (unlikely(!gif->ImageCount))
83 IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "There are no images in gif file.");
88 /* Read image parameters */
89 SavedImage *image = gif->SavedImages;
90 if (unlikely(image->ImageDesc.Width <= 0 || image->ImageDesc.Height <= 0 ||
91 image->ImageDesc.Width > (int)image_max_dim || image->ImageDesc.Height > (int)image_max_dim))
93 IMAGE_ERROR(io->context, IMAGE_ERROR_INVALID_DIMENSIONS, "Invalid gif dimensions.");
97 ColorMapObject *color_map = image->ImageDesc.ColorMap ? : gif->SColorMap;
98 if (unlikely(!color_map))
100 IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "Missing palette.");
101 DGIF_CLOSE_FILE(gif);
104 io->cols = image->ImageDesc.Width;
105 io->rows = image->ImageDesc.Height;
106 if (unlikely((io->number_of_colors = color_map->ColorCount) > 256))
108 IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "Too many gif colors.");
109 DGIF_CLOSE_FILE(gif);
112 io->flags = COLOR_SPACE_RGB | IMAGE_IO_HAS_PALETTE;
114 /* Search extension blocks */
115 rd->transparent_index = -1;
116 for (int i = 0; i < image->ExtensionBlockCount; i++)
118 ExtensionBlock *e = image->ExtensionBlocks + i;
119 if (e->Function == 0xF9)
121 DBG("Found graphics control extension");
122 if (unlikely(e->ByteCount != 4))
124 IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "Invalid graphics control extension.");
125 DGIF_CLOSE_FILE(gif);
129 /* transparent color present */
132 rd->transparent_index = b[3];
133 io->flags |= IMAGE_ALPHA;
136 GifColorType *background = color_map->Colors + gif->SBackGroundColor;
137 color_make_rgb(&io->background_color, background->Red, background->Green, background->Blue);
140 /* We've got everything we need :-) */
144 DBG("Found unknown extension: type=%d size=%d", e->Function, e->ByteCount);
148 io->read_cancel = libungif_read_cancel;
153 libungif_read_data(struct image_io *io)
155 DBG("libungif_read_data()");
157 struct libungif_read_data *rd = io->read_data;
158 GifFileType *gif = rd->gif;
159 SavedImage *image = gif->SavedImages;
162 struct image_io_read_data_internals rdi;
163 uint read_flags = io->flags;
164 uint cs = read_flags & IMAGE_COLOR_SPACE;
165 if (cs != COLOR_SPACE_GRAYSCALE && cs != COLOR_SPACE_RGB)
166 read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_RGB;
167 if (unlikely(!image_io_read_data_prepare(&rdi, io, image->ImageDesc.Width, image->ImageDesc.Height, read_flags)))
169 DGIF_CLOSE_FILE(gif);
173 /* Get pixels and palette */
174 byte *pixels = (byte *)image->RasterBits;
175 ColorMapObject *color_map = image->ImageDesc.ColorMap ? : gif->SColorMap;
176 GifColorType *palette = color_map->Colors;
177 byte *img_end = rdi.image->pixels + rdi.image->image_size;
179 /* Handle deinterlacing */
180 uint dein_step, dein_next;
181 if (image->ImageDesc.Interlace)
183 DBG("Deinterlaced image");
184 dein_step = dein_next = rdi.image->row_size << 3;
187 dein_step = dein_next = rdi.image->row_size;
190 switch (rdi.image->pixel_size)
194 byte pal[256], *pal_pos = pal, *pal_end = pal + 256;
195 for (uint i = 0; i < (uint)color_map->ColorCount; i++, pal_pos++, palette++)
196 *pal_pos = rgb_to_gray_func(palette->Red, palette->Green, palette->Blue);
197 if (pal_pos != pal_end)
198 bzero(pal_pos, pal_end - pal_pos);
199 if (rd->transparent_index >= 0 && (io->flags & IMAGE_IO_USE_BACKGROUND))
200 if (!color_put(io->context, &io->background_color, pal + rd->transparent_index, COLOR_SPACE_GRAYSCALE))
202 DGIF_CLOSE_FILE(gif);
205 # define DO_ROW_END do{ \
206 walk_row_start += dein_step; \
207 while (walk_row_start >= img_end) \
208 { uint n = dein_next >> 1; walk_row_start = rdi.image->pixels + n, dein_step = dein_next; dein_next = n; } \
210 # define IMAGE_WALK_PREFIX(x) walk_##x
211 # define IMAGE_WALK_INLINE
212 # define IMAGE_WALK_IMAGE (rdi.image)
213 # define IMAGE_WALK_UNROLL 4
214 # define IMAGE_WALK_COL_STEP 1
215 # define IMAGE_WALK_ROW_STEP 0
216 # define IMAGE_WALK_DO_STEP do{ *walk_pos = pal[*pixels++]; }while(0)
217 # define IMAGE_WALK_DO_ROW_END DO_ROW_END
218 # include <images/image-walk.h>
223 byte pal[256 * 2], *pal_pos = pal, *pal_end = pal + 256 * 2;
224 for (uint i = 0; i < (uint)color_map->ColorCount; i++, pal_pos += 2, palette++)
226 pal_pos[0] = rgb_to_gray_func(palette->Red, palette->Green, palette->Blue);
229 if (pal_pos != pal_end)
230 bzero(pal_pos, pal_end - pal_pos);
231 if (rd->transparent_index >= 0)
232 pal[rd->transparent_index * 2 + 1] = 0;
233 # define IMAGE_WALK_PREFIX(x) walk_##x
234 # define IMAGE_WALK_INLINE
235 # define IMAGE_WALK_IMAGE (rdi.image)
236 # define IMAGE_WALK_UNROLL 4
237 # define IMAGE_WALK_COL_STEP 2
238 # define IMAGE_WALK_ROW_STEP 0
239 # define IMAGE_WALK_DO_STEP do{ *(u16 *)walk_pos = ((u16 *)pal)[*pixels++]; }while(0)
240 # define IMAGE_WALK_DO_ROW_END DO_ROW_END
241 # include <images/image-walk.h>
246 byte pal[256 * 4], *pal_pos = pal, *pal_end = pal + 256 * 4;
247 for (uint i = 0; i < (uint)color_map->ColorCount; i++, pal_pos += 4, palette++)
249 pal_pos[0] = palette->Red;
250 pal_pos[1] = palette->Green;
251 pal_pos[2] = palette->Blue;
253 if (pal_pos != pal_end)
254 bzero(pal_pos, pal_end - pal_pos);
255 if (rd->transparent_index >= 0 && (io->flags & IMAGE_IO_USE_BACKGROUND))
256 if (!color_put(io->context, &io->background_color, pal + 4 * rd->transparent_index, COLOR_SPACE_RGB))
258 DGIF_CLOSE_FILE(gif);
261 # define IMAGE_WALK_PREFIX(x) walk_##x
262 # define IMAGE_WALK_INLINE
263 # define IMAGE_WALK_IMAGE (rdi.image)
264 # define IMAGE_WALK_UNROLL 4
265 # define IMAGE_WALK_COL_STEP 3
266 # define IMAGE_WALK_ROW_STEP 0
267 # define IMAGE_WALK_DO_STEP do{ byte *p = pal + 4 * (*pixels++); walk_pos[0] = p[0]; walk_pos[1] = p[1]; walk_pos[2] = p[2]; }while(0)
268 # define IMAGE_WALK_DO_ROW_END DO_ROW_END
269 # include <images/image-walk.h>
274 byte pal[256 * 4], *pal_pos = pal, *pal_end = pal + 256 * 4;
275 for (uint i = 0; i < (uint)color_map->ColorCount; i++, pal_pos += 4, palette++)
277 pal_pos[0] = palette->Red;
278 pal_pos[1] = palette->Green;
279 pal_pos[2] = palette->Blue;
282 if (pal_pos != pal_end)
283 bzero(pal_pos, pal_end - pal_pos);
284 if (rd->transparent_index >= 0)
285 pal[rd->transparent_index * 4 + 3] = 0;
286 # define IMAGE_WALK_PREFIX(x) walk_##x
287 # define IMAGE_WALK_INLINE
288 # define IMAGE_WALK_IMAGE (rdi.image)
289 # define IMAGE_WALK_UNROLL 4
290 # define IMAGE_WALK_COL_STEP 4
291 # define IMAGE_WALK_ROW_STEP 0
292 # define IMAGE_WALK_DO_STEP do{ *(u32 *)walk_pos = ((u32 *)pal)[*pixels++]; }while(0)
293 # define IMAGE_WALK_DO_ROW_END DO_ROW_END
294 # include <images/image-walk.h>
301 /* Destroy libungif structure */
302 DGIF_CLOSE_FILE(gif);
305 return image_io_read_data_finish(&rdi, io);