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 DGifCloseFile(io->read_data);
41 libungif_read_header(struct image_io *io)
43 DBG("libungif_read_header()");
45 /* Create libungif structure */
47 if (unlikely(!(gif = DGifOpen(io->fastbuf, libungif_read_func))))
49 image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Cannot create libungif structure.");
53 struct libungif_read_data *rd = io->read_data = mp_alloc(io->internal_pool, sizeof(*rd));
56 DBG("executing DGifSlurp()");
57 if (unlikely(DGifSlurp(gif) != GIF_OK))
59 image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Gif read failed.");
64 DBG("ImageCount=%d ColorResolution=%d SBackGroundColor=%d SColorMap=%p", gif->ImageCount, gif->SColorResolution, gif->SBackGroundColor, gif->SColorMap);
65 if (unlikely(!gif->ImageCount))
67 image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "There are no images in gif file.");
72 /* Read image parameters */
73 SavedImage *image = gif->SavedImages;
74 if (unlikely(image->ImageDesc.Width <= 0 || image->ImageDesc.Height <= 0 ||
75 image->ImageDesc.Width > (int)IMAGE_MAX_SIZE || image->ImageDesc.Height > (int)IMAGE_MAX_SIZE))
77 image_thread_err(io->thread, IMAGE_ERR_INVALID_DIMENSIONS, "Invalid gif dimensions.");
81 ColorMapObject *color_map = image->ImageDesc.ColorMap ? : gif->SColorMap;
82 if (unlikely(!color_map))
84 image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Missing palette.");
88 io->cols = image->ImageDesc.Width;
89 io->rows = image->ImageDesc.Height;
90 if (unlikely((io->number_of_colors = color_map->ColorCount) > 256))
92 image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Too many gif colors.");
96 io->flags = COLOR_SPACE_RGB | IMAGE_IO_HAS_PALETTE;
98 /* Search extension blocks */
99 rd->transparent_index = -1;
100 for (int i = 0; i < image->ExtensionBlockCount; i++)
102 ExtensionBlock *e = image->ExtensionBlocks + i;
103 if (e->Function == 0xF9)
105 DBG("Found graphics control extension");
106 if (unlikely(e->ByteCount != 4))
108 image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Invalid graphics control extension.");
113 /* transparent color present */
116 rd->transparent_index = b[3];
117 io->flags |= IMAGE_ALPHA;
120 GifColorType *background = color_map->Colors + gif->SBackGroundColor;
121 color_make_rgb(&io->background_color, background->Red, background->Green, background->Blue);
124 /* We've got everything we need :-) */
128 DBG("Found unknown extension: type=%d size=%d", e->Function, e->ByteCount);
132 io->read_cancel = libungif_read_cancel;
137 libungif_read_data(struct image_io *io)
139 DBG("libungif_read_data()");
141 struct libungif_read_data *rd = io->read_data;
142 GifFileType *gif = rd->gif;
143 SavedImage *image = gif->SavedImages;
146 struct image_io_read_data_internals rdi;
147 if (unlikely(!image_io_read_data_prepare(&rdi, io, image->ImageDesc.Width, image->ImageDesc.Height, io->flags)))
153 /* Get pixels and palette */
154 byte *pixels = (byte *)image->RasterBits;
155 ColorMapObject *color_map = image->ImageDesc.ColorMap ? : gif->SColorMap;
156 GifColorType *palette = color_map->Colors;
157 byte *img_end = rdi.image->pixels + rdi.image->image_size;
159 /* Handle deinterlacing */
160 uns dein_step, dein_next;
161 if (image->ImageDesc.Interlace)
163 DBG("Deinterlaced image");
164 dein_step = dein_next = rdi.image->row_size << 3;
167 dein_step = dein_next = rdi.image->row_size;
170 switch (rdi.image->pixel_size)
174 byte pal[256], *pal_pos = pal, *pal_end = pal + 256;
175 for (uns i = 0; i < (uns)color_map->ColorCount; i++, pal_pos++, palette++)
176 *pal_pos = rgb_to_gray_func(palette->Red, palette->Green, palette->Blue);
177 if (pal_pos != pal_end)
178 bzero(pal_pos, pal_end - pal_pos);
179 if (rd->transparent_index >= 0 && (io->flags & IMAGE_IO_USE_BACKGROUND))
180 color_put_grayscale(pal + rd->transparent_index, &io->background_color);
181 # define DO_ROW_END do{ \
182 walk_row_start += dein_step; \
183 if (walk_row_start >= img_end) \
184 { uns n = dein_next >> 1; walk_row_start = rdi.image->pixels + n, dein_step = dein_next; dein_next = n; } \
186 # define IMAGE_WALK_PREFIX(x) walk_##x
187 # define IMAGE_WALK_INLINE
188 # define IMAGE_WALK_IMAGE (rdi.image)
189 # define IMAGE_WALK_UNROLL 4
190 # define IMAGE_WALK_COL_STEP 1
191 # define IMAGE_WALK_ROW_STEP 0
192 # define IMAGE_WALK_DO_STEP do{ *walk_pos = pal[*pixels++]; }while(0)
193 # define IMAGE_WALK_DO_ROW_END DO_ROW_END
194 # include "images/image-walk.h"
199 byte pal[256 * 2], *pal_pos = pal, *pal_end = pal + 256 * 2;
200 for (uns i = 0; i < (uns)color_map->ColorCount; i++, pal_pos += 2, palette++)
202 pal_pos[0] = rgb_to_gray_func(palette->Red, palette->Green, palette->Blue);
205 if (pal_pos != pal_end)
206 bzero(pal_pos, pal_end - pal_pos);
207 if (rd->transparent_index >= 0)
208 pal[rd->transparent_index * 2 + 1] = 0;
209 # define IMAGE_WALK_PREFIX(x) walk_##x
210 # define IMAGE_WALK_INLINE
211 # define IMAGE_WALK_IMAGE (rdi.image)
212 # define IMAGE_WALK_UNROLL 4
213 # define IMAGE_WALK_COL_STEP 2
214 # define IMAGE_WALK_ROW_STEP 0
215 # define IMAGE_WALK_DO_STEP do{ *(u16 *)walk_pos = ((u16 *)pal)[*pixels++]; }while(0)
216 # define IMAGE_WALK_DO_ROW_END DO_ROW_END
217 # include "images/image-walk.h"
222 byte pal[256 * 4], *pal_pos = pal, *pal_end = pal + 256 * 4;
223 for (uns i = 0; i < (uns)color_map->ColorCount; i++, pal_pos += 4, palette++)
225 pal_pos[0] = palette->Red;
226 pal_pos[1] = palette->Green;
227 pal_pos[2] = palette->Blue;
229 if (pal_pos != pal_end)
230 bzero(pal_pos, pal_end - pal_pos);
231 if (rd->transparent_index >= 0 && (io->flags & IMAGE_IO_USE_BACKGROUND))
232 color_put_rgb(pal + 4 * rd->transparent_index, &io->background_color);
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 3
238 # define IMAGE_WALK_ROW_STEP 0
239 # 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)
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 (uns i = 0; i < (uns)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;
254 if (pal_pos != pal_end)
255 bzero(pal_pos, pal_end - pal_pos);
256 if (rd->transparent_index >= 0)
257 pal[rd->transparent_index * 4 + 3] = 0;
258 # define IMAGE_WALK_PREFIX(x) walk_##x
259 # define IMAGE_WALK_INLINE
260 # define IMAGE_WALK_IMAGE (rdi.image)
261 # define IMAGE_WALK_UNROLL 4
262 # define IMAGE_WALK_COL_STEP 4
263 # define IMAGE_WALK_ROW_STEP 0
264 # define IMAGE_WALK_DO_STEP do{ *(u32 *)walk_pos = ((u32 *)pal)[*pixels++]; }while(0)
265 # define IMAGE_WALK_DO_ROW_END DO_ROW_END
266 # include "images/image-walk.h"
273 /* Destroy libungif structure */
277 return image_io_read_data_finish(&rdi, io);