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 "lib/mempool.h"
14 #include "lib/fastbuf.h"
15 #include "images/images.h"
16 #include "images/color.h"
17 #include "images/io-main.h"
20 struct libungif_read_data {
22 int transparent_index;
26 libungif_read_func(GifFileType *gif, GifByteType *ptr, int len)
28 DBG("libungif_read_func(len=%d)", len);
29 return bread((struct fastbuf *)gif->UserData, (byte *)ptr, len);
33 libungif_read_cancel(struct image_io *io)
35 DBG("libungif_read_cancel()");
37 struct libungif_read_data *rd = io->read_data;
38 DGifCloseFile(rd->gif);
42 libungif_read_header(struct image_io *io)
44 DBG("libungif_read_header()");
46 /* Create libungif structure */
48 if (unlikely(!(gif = DGifOpen(io->fastbuf, libungif_read_func))))
50 image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Cannot create libungif structure.");
54 struct libungif_read_data *rd = io->read_data = mp_alloc(io->internal_pool, sizeof(*rd));
57 DBG("executing DGifSlurp()");
58 if (unlikely(DGifSlurp(gif) != GIF_OK))
60 image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Gif read failed.");
65 DBG("ImageCount=%d ColorResolution=%d SBackGroundColor=%d SColorMap=%p", gif->ImageCount, gif->SColorResolution, gif->SBackGroundColor, gif->SColorMap);
66 if (unlikely(!gif->ImageCount))
68 image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "There are no images in gif file.");
73 /* Read image parameters */
74 SavedImage *image = gif->SavedImages;
75 if (unlikely(image->ImageDesc.Width <= 0 || image->ImageDesc.Height <= 0 ||
76 image->ImageDesc.Width > (int)IMAGE_MAX_SIZE || image->ImageDesc.Height > (int)IMAGE_MAX_SIZE))
78 image_thread_err(io->thread, IMAGE_ERR_INVALID_DIMENSIONS, "Invalid gif dimensions.");
82 ColorMapObject *color_map = image->ImageDesc.ColorMap ? : gif->SColorMap;
83 if (unlikely(!color_map))
85 image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Missing palette.");
89 io->cols = image->ImageDesc.Width;
90 io->rows = image->ImageDesc.Height;
91 if (unlikely((io->number_of_colors = color_map->ColorCount) > 256))
93 image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Too many gif colors.");
97 io->flags = COLOR_SPACE_RGB | IMAGE_IO_HAS_PALETTE;
99 /* Search extension blocks */
100 rd->transparent_index = -1;
101 for (int i = 0; i < image->ExtensionBlockCount; i++)
103 ExtensionBlock *e = image->ExtensionBlocks + i;
104 if (e->Function == 0xF9)
106 DBG("Found graphics control extension");
107 if (unlikely(e->ByteCount != 4))
109 image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Invalid graphics control extension.");
114 /* transparent color present */
117 rd->transparent_index = b[3];
118 io->flags |= IMAGE_ALPHA;
121 GifColorType *background = color_map->Colors + gif->SBackGroundColor;
122 color_make_rgb(&io->background_color, background->Red, background->Green, background->Blue);
125 /* We've got everything we need :-) */
129 DBG("Found unknown extension: type=%d size=%d", e->Function, e->ByteCount);
133 io->read_cancel = libungif_read_cancel;
138 libungif_read_data(struct image_io *io)
140 DBG("libungif_read_data()");
142 struct libungif_read_data *rd = io->read_data;
143 GifFileType *gif = rd->gif;
144 SavedImage *image = gif->SavedImages;
147 struct image_io_read_data_internals rdi;
148 if (unlikely(!image_io_read_data_prepare(&rdi, io, image->ImageDesc.Width, image->ImageDesc.Height, io->flags)))
154 /* Get pixels and palette */
155 byte *pixels = (byte *)image->RasterBits;
156 ColorMapObject *color_map = image->ImageDesc.ColorMap ? : gif->SColorMap;
157 GifColorType *palette = color_map->Colors;
158 byte *img_end = rdi.image->pixels + rdi.image->image_size;
160 /* Handle deinterlacing */
161 uns dein_step, dein_next;
162 if (image->ImageDesc.Interlace)
164 DBG("Deinterlaced image");
165 dein_step = dein_next = rdi.image->row_size << 3;
168 dein_step = dein_next = rdi.image->row_size;
171 switch (rdi.image->pixel_size)
175 byte pal[256], *pal_pos = pal, *pal_end = pal + 256;
176 for (uns i = 0; i < (uns)color_map->ColorCount; i++, pal_pos++, palette++)
177 *pal_pos = rgb_to_gray_func(palette->Red, palette->Green, palette->Blue);
178 if (pal_pos != pal_end)
179 bzero(pal_pos, pal_end - pal_pos);
180 if (rd->transparent_index >= 0 && (io->flags & IMAGE_IO_USE_BACKGROUND))
181 color_put_grayscale(pal + rd->transparent_index, &io->background_color);
182 # define DO_ROW_END do{ \
183 walk_row_start += dein_step; \
184 if (walk_row_start >= img_end) \
185 { uns n = dein_next >> 1; walk_row_start = rdi.image->pixels + n, dein_step = dein_next; dein_next = n; } \
187 # define IMAGE_WALK_PREFIX(x) walk_##x
188 # define IMAGE_WALK_INLINE
189 # define IMAGE_WALK_IMAGE (rdi.image)
190 # define IMAGE_WALK_UNROLL 4
191 # define IMAGE_WALK_COL_STEP 1
192 # define IMAGE_WALK_ROW_STEP 0
193 # define IMAGE_WALK_DO_STEP do{ *walk_pos = pal[*pixels++]; }while(0)
194 # define IMAGE_WALK_DO_ROW_END DO_ROW_END
195 # include "images/image-walk.h"
200 byte pal[256 * 2], *pal_pos = pal, *pal_end = pal + 256 * 2;
201 for (uns i = 0; i < (uns)color_map->ColorCount; i++, pal_pos += 2, palette++)
203 pal_pos[0] = rgb_to_gray_func(palette->Red, palette->Green, palette->Blue);
206 if (pal_pos != pal_end)
207 bzero(pal_pos, pal_end - pal_pos);
208 if (rd->transparent_index >= 0)
209 pal[rd->transparent_index * 2 + 1] = 0;
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 2
215 # define IMAGE_WALK_ROW_STEP 0
216 # define IMAGE_WALK_DO_STEP do{ *(u16 *)walk_pos = ((u16 *)pal)[*pixels++]; }while(0)
217 # define IMAGE_WALK_DO_ROW_END DO_ROW_END
218 # include "images/image-walk.h"
223 byte pal[256 * 4], *pal_pos = pal, *pal_end = pal + 256 * 4;
224 for (uns i = 0; i < (uns)color_map->ColorCount; i++, pal_pos += 4, palette++)
226 pal_pos[0] = palette->Red;
227 pal_pos[1] = palette->Green;
228 pal_pos[2] = palette->Blue;
230 if (pal_pos != pal_end)
231 bzero(pal_pos, pal_end - pal_pos);
232 if (rd->transparent_index >= 0 && (io->flags & IMAGE_IO_USE_BACKGROUND))
233 color_put_rgb(pal + 4 * rd->transparent_index, &io->background_color);
234 # define IMAGE_WALK_PREFIX(x) walk_##x
235 # define IMAGE_WALK_INLINE
236 # define IMAGE_WALK_IMAGE (rdi.image)
237 # define IMAGE_WALK_UNROLL 4
238 # define IMAGE_WALK_COL_STEP 3
239 # define IMAGE_WALK_ROW_STEP 0
240 # 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)
241 # define IMAGE_WALK_DO_ROW_END DO_ROW_END
242 # include "images/image-walk.h"
247 byte pal[256 * 4], *pal_pos = pal, *pal_end = pal + 256 * 4;
248 for (uns i = 0; i < (uns)color_map->ColorCount; i++, pal_pos += 4, palette++)
250 pal_pos[0] = palette->Red;
251 pal_pos[1] = palette->Green;
252 pal_pos[2] = palette->Blue;
255 if (pal_pos != pal_end)
256 bzero(pal_pos, pal_end - pal_pos);
257 if (rd->transparent_index >= 0)
258 pal[rd->transparent_index * 4 + 3] = 0;
259 # define IMAGE_WALK_PREFIX(x) walk_##x
260 # define IMAGE_WALK_INLINE
261 # define IMAGE_WALK_IMAGE (rdi.image)
262 # define IMAGE_WALK_UNROLL 4
263 # define IMAGE_WALK_COL_STEP 4
264 # define IMAGE_WALK_ROW_STEP 0
265 # define IMAGE_WALK_DO_STEP do{ *(u32 *)walk_pos = ((u32 *)pal)[*pixels++]; }while(0)
266 # define IMAGE_WALK_DO_ROW_END DO_ROW_END
267 # include "images/image-walk.h"
274 /* Destroy libungif structure */
278 return image_io_read_data_finish(&rdi, io);