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 uns readed, total = 0;
30 while (len && (readed = bread((struct fastbuf *)gif->UserData, (byte *)ptr, len)))
40 libungif_read_cancel(struct image_io *io)
42 DBG("libungif_read_cancel()");
44 DGifCloseFile(io->read_data);
48 libungif_read_header(struct image_io *io)
50 DBG("libungif_read_header()");
52 /* Create libungif structure */
54 if (unlikely(!(gif = DGifOpen(io->fastbuf, libungif_read_func))))
56 image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Cannot create libungif structure.");
60 struct libungif_read_data *rd = io->read_data = mp_alloc(io->internal_pool, sizeof(*rd));
63 DBG("executing DGifSlurp()");
64 if (unlikely(DGifSlurp(gif) != GIF_OK))
66 image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Gif read failed.");
71 DBG("ImageCount=%d ColorResolution=%d SBackGroundColor=%d SColorMap=%p", gif->ImageCount, gif->SColorResolution, gif->SBackGroundColor, gif->SColorMap);
72 if (unlikely(!gif->ImageCount))
74 image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "There are no images in gif file.");
79 /* Read image parameters */
80 SavedImage *image = gif->SavedImages;
81 if (unlikely(image->ImageDesc.Width <= 0 || image->ImageDesc.Height <= 0 ||
82 image->ImageDesc.Width > (int)IMAGE_MAX_SIZE || image->ImageDesc.Height > (int)IMAGE_MAX_SIZE))
84 image_thread_err(io->thread, IMAGE_ERR_INVALID_DIMENSIONS, "Invalid gif dimensions.");
88 ColorMapObject *color_map = image->ImageDesc.ColorMap ? : gif->SColorMap;
89 if (unlikely(!color_map))
91 image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Missing palette.");
95 io->cols = image->ImageDesc.Width;
96 io->rows = image->ImageDesc.Height;
97 if (unlikely((io->number_of_colors = color_map->ColorCount) > 256))
99 image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Too many gif colors.");
103 io->flags = COLOR_SPACE_RGB | IMAGE_IO_HAS_PALETTE;
105 /* Search extension blocks */
106 rd->transparent_index = -1;
107 for (int i = 0; i < image->ExtensionBlockCount; i++)
109 ExtensionBlock *e = image->ExtensionBlocks + i;
110 if (e->Function == 0xF9)
112 DBG("Found graphics control extension");
113 if (unlikely(e->ByteCount != 4))
115 image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Invalid graphics control extension.");
120 /* transparent color present */
123 rd->transparent_index = b[3];
124 io->flags |= IMAGE_ALPHA;
127 GifColorType *background = color_map->Colors + gif->SBackGroundColor;
128 color_make_rgb(&io->background_color, background->Red, background->Green, background->Blue);
131 /* We've got everything we need :-) */
135 DBG("Found unknown extension: type=%d size=%d", e->Function, e->ByteCount);
139 io->read_cancel = libungif_read_cancel;
144 libungif_read_data(struct image_io *io)
146 DBG("libungif_read_data()");
148 struct libungif_read_data *rd = io->read_data;
149 GifFileType *gif = rd->gif;
150 SavedImage *image = gif->SavedImages;
153 struct image_io_read_data_internals rdi;
154 if (unlikely(!image_io_read_data_prepare(&rdi, io, image->ImageDesc.Width, image->ImageDesc.Height, io->flags)))
160 /* Get pixels and palette */
161 byte *pixels = (byte *)image->RasterBits;
162 ColorMapObject *color_map = image->ImageDesc.ColorMap ? : gif->SColorMap;
163 GifColorType *palette = color_map->Colors;
164 byte *img_end = rdi.image->pixels + rdi.image->image_size;
166 /* Handle deinterlacing */
167 uns dein_step, dein_next;
168 if (image->ImageDesc.Interlace)
170 DBG("Deinterlaced image");
171 dein_step = dein_next = rdi.image->row_size << 3;
174 dein_step = dein_next = rdi.image->row_size;
177 switch (rdi.image->pixel_size)
181 byte pal[256], *pal_pos = pal, *pal_end = pal + 256;
182 for (uns i = 0; i < (uns)color_map->ColorCount; i++, pal_pos++, palette++)
183 *pal_pos = rgb_to_gray_func(palette->Red, palette->Green, palette->Blue);
184 if (pal_pos != pal_end)
185 bzero(pal_pos, pal_end - pal_pos);
186 if (rd->transparent_index >= 0 && (io->flags & IMAGE_IO_USE_BACKGROUND))
187 color_put_grayscale(pal + rd->transparent_index, &io->background_color);
188 # define DO_ROW_END do{ \
189 walk_row_start += dein_step; \
190 if (walk_row_start >= img_end) \
191 { uns n = dein_next >> 1; walk_row_start = rdi.image->pixels + n, dein_step = dein_next; dein_next = n; } \
193 # define IMAGE_WALK_PREFIX(x) walk_##x
194 # define IMAGE_WALK_INLINE
195 # define IMAGE_WALK_IMAGE (rdi.image)
196 # define IMAGE_WALK_UNROLL 4
197 # define IMAGE_WALK_COL_STEP 1
198 # define IMAGE_WALK_ROW_STEP 0
199 # define IMAGE_WALK_DO_STEP do{ *walk_pos = pal[*pixels++]; }while(0)
200 # define IMAGE_WALK_DO_ROW_END DO_ROW_END
201 # include "images/image-walk.h"
206 byte pal[256 * 2], *pal_pos = pal, *pal_end = pal + 256 * 2;
207 for (uns i = 0; i < (uns)color_map->ColorCount; i++, pal_pos += 2, palette++)
209 pal_pos[0] = rgb_to_gray_func(palette->Red, palette->Green, palette->Blue);
212 if (pal_pos != pal_end)
213 bzero(pal_pos, pal_end - pal_pos);
214 if (rd->transparent_index >= 0)
215 pal[rd->transparent_index * 2 + 1] = 0;
216 # define IMAGE_WALK_PREFIX(x) walk_##x
217 # define IMAGE_WALK_INLINE
218 # define IMAGE_WALK_IMAGE (rdi.image)
219 # define IMAGE_WALK_UNROLL 4
220 # define IMAGE_WALK_COL_STEP 2
221 # define IMAGE_WALK_ROW_STEP 0
222 # define IMAGE_WALK_DO_STEP do{ *(u16 *)walk_pos = ((u16 *)pal)[*pixels++]; }while(0)
223 # define IMAGE_WALK_DO_ROW_END DO_ROW_END
224 # include "images/image-walk.h"
229 byte pal[256 * 4], *pal_pos = pal, *pal_end = pal + 256 * 4;
230 for (uns i = 0; i < (uns)color_map->ColorCount; i++, pal_pos += 4, palette++)
232 pal_pos[0] = palette->Red;
233 pal_pos[1] = palette->Green;
234 pal_pos[2] = palette->Blue;
236 if (pal_pos != pal_end)
237 bzero(pal_pos, pal_end - pal_pos);
238 if (rd->transparent_index >= 0 && (io->flags & IMAGE_IO_USE_BACKGROUND))
239 color_put_rgb(pal + 4 * rd->transparent_index, &io->background_color);
240 # define IMAGE_WALK_PREFIX(x) walk_##x
241 # define IMAGE_WALK_INLINE
242 # define IMAGE_WALK_IMAGE (rdi.image)
243 # define IMAGE_WALK_UNROLL 4
244 # define IMAGE_WALK_COL_STEP 3
245 # define IMAGE_WALK_ROW_STEP 0
246 # 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)
247 # define IMAGE_WALK_DO_ROW_END DO_ROW_END
248 # include "images/image-walk.h"
253 byte pal[256 * 4], *pal_pos = pal, *pal_end = pal + 256 * 4;
254 for (uns i = 0; i < (uns)color_map->ColorCount; i++, pal_pos += 4, palette++)
256 pal_pos[0] = palette->Red;
257 pal_pos[1] = palette->Green;
258 pal_pos[2] = palette->Blue;
261 if (pal_pos != pal_end)
262 bzero(pal_pos, pal_end - pal_pos);
263 if (rd->transparent_index >= 0)
264 pal[rd->transparent_index * 4 + 3] = 0;
265 # define IMAGE_WALK_PREFIX(x) walk_##x
266 # define IMAGE_WALK_INLINE
267 # define IMAGE_WALK_IMAGE (rdi.image)
268 # define IMAGE_WALK_UNROLL 4
269 # define IMAGE_WALK_COL_STEP 4
270 # define IMAGE_WALK_ROW_STEP 0
271 # define IMAGE_WALK_DO_STEP do{ *(u32 *)walk_pos = ((u32 *)pal)[*pixels++]; }while(0)
272 # define IMAGE_WALK_DO_ROW_END DO_ROW_END
273 # include "images/image-walk.h"
280 /* Destroy libungif structure */
284 return image_io_read_data_finish(&rdi, io);