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 struct libungif_read_data {
24 int transparent_index;
28 libungif_read_func(GifFileType *gif, GifByteType *ptr, int len)
30 DBG("libungif_read_func(len=%d)", len);
31 return bread((struct fastbuf *)gif->UserData, (byte *)ptr, len);
35 libungif_read_cancel(struct image_io *io)
37 DBG("libungif_read_cancel()");
39 struct libungif_read_data *rd = io->read_data;
40 DGifCloseFile(rd->gif);
44 libungif_read_header(struct image_io *io)
46 DBG("libungif_read_header()");
48 /* Create libungif structure */
50 if (unlikely(!(gif = DGifOpen(io->fastbuf, libungif_read_func))))
52 IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "Cannot create libungif structure.");
56 struct libungif_read_data *rd = io->read_data = mp_alloc(io->internal_pool, sizeof(*rd));
59 DBG("executing DGifSlurp()");
60 if (unlikely(DGifSlurp(gif) != GIF_OK))
62 IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "Gif read failed.");
67 DBG("ImageCount=%d ColorResolution=%d SBackGroundColor=%d SColorMap=%p", gif->ImageCount, gif->SColorResolution, gif->SBackGroundColor, gif->SColorMap);
68 if (unlikely(!gif->ImageCount))
70 IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "There are no images in gif file.");
75 /* Read image parameters */
76 SavedImage *image = gif->SavedImages;
77 if (unlikely(image->ImageDesc.Width <= 0 || image->ImageDesc.Height <= 0 ||
78 image->ImageDesc.Width > (int)image_max_dim || image->ImageDesc.Height > (int)image_max_dim))
80 IMAGE_ERROR(io->context, IMAGE_ERROR_INVALID_DIMENSIONS, "Invalid gif dimensions.");
84 ColorMapObject *color_map = image->ImageDesc.ColorMap ? : gif->SColorMap;
85 if (unlikely(!color_map))
87 IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "Missing palette.");
91 io->cols = image->ImageDesc.Width;
92 io->rows = image->ImageDesc.Height;
93 if (unlikely((io->number_of_colors = color_map->ColorCount) > 256))
95 IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "Too many gif colors.");
99 io->flags = COLOR_SPACE_RGB | IMAGE_IO_HAS_PALETTE;
101 /* Search extension blocks */
102 rd->transparent_index = -1;
103 for (int i = 0; i < image->ExtensionBlockCount; i++)
105 ExtensionBlock *e = image->ExtensionBlocks + i;
106 if (e->Function == 0xF9)
108 DBG("Found graphics control extension");
109 if (unlikely(e->ByteCount != 4))
111 IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "Invalid graphics control extension.");
116 /* transparent color present */
119 rd->transparent_index = b[3];
120 io->flags |= IMAGE_ALPHA;
123 GifColorType *background = color_map->Colors + gif->SBackGroundColor;
124 color_make_rgb(&io->background_color, background->Red, background->Green, background->Blue);
127 /* We've got everything we need :-) */
131 DBG("Found unknown extension: type=%d size=%d", e->Function, e->ByteCount);
135 io->read_cancel = libungif_read_cancel;
140 libungif_read_data(struct image_io *io)
142 DBG("libungif_read_data()");
144 struct libungif_read_data *rd = io->read_data;
145 GifFileType *gif = rd->gif;
146 SavedImage *image = gif->SavedImages;
149 struct image_io_read_data_internals rdi;
150 uns read_flags = io->flags;
151 uns cs = read_flags & IMAGE_COLOR_SPACE;
152 if (cs != COLOR_SPACE_GRAYSCALE && cs != COLOR_SPACE_RGB)
153 read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_RGB;
154 if (unlikely(!image_io_read_data_prepare(&rdi, io, image->ImageDesc.Width, image->ImageDesc.Height, read_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 if (!color_put(io->context, &io->background_color, pal + rd->transparent_index, COLOR_SPACE_GRAYSCALE))
192 # define DO_ROW_END do{ \
193 walk_row_start += dein_step; \
194 while (walk_row_start >= img_end) \
195 { uns n = dein_next >> 1; walk_row_start = rdi.image->pixels + n, dein_step = dein_next; dein_next = n; } \
197 # define IMAGE_WALK_PREFIX(x) walk_##x
198 # define IMAGE_WALK_INLINE
199 # define IMAGE_WALK_IMAGE (rdi.image)
200 # define IMAGE_WALK_UNROLL 4
201 # define IMAGE_WALK_COL_STEP 1
202 # define IMAGE_WALK_ROW_STEP 0
203 # define IMAGE_WALK_DO_STEP do{ *walk_pos = pal[*pixels++]; }while(0)
204 # define IMAGE_WALK_DO_ROW_END DO_ROW_END
205 # include <images/image-walk.h>
210 byte pal[256 * 2], *pal_pos = pal, *pal_end = pal + 256 * 2;
211 for (uns i = 0; i < (uns)color_map->ColorCount; i++, pal_pos += 2, palette++)
213 pal_pos[0] = rgb_to_gray_func(palette->Red, palette->Green, palette->Blue);
216 if (pal_pos != pal_end)
217 bzero(pal_pos, pal_end - pal_pos);
218 if (rd->transparent_index >= 0)
219 pal[rd->transparent_index * 2 + 1] = 0;
220 # define IMAGE_WALK_PREFIX(x) walk_##x
221 # define IMAGE_WALK_INLINE
222 # define IMAGE_WALK_IMAGE (rdi.image)
223 # define IMAGE_WALK_UNROLL 4
224 # define IMAGE_WALK_COL_STEP 2
225 # define IMAGE_WALK_ROW_STEP 0
226 # define IMAGE_WALK_DO_STEP do{ *(u16 *)walk_pos = ((u16 *)pal)[*pixels++]; }while(0)
227 # define IMAGE_WALK_DO_ROW_END DO_ROW_END
228 # include <images/image-walk.h>
233 byte pal[256 * 4], *pal_pos = pal, *pal_end = pal + 256 * 4;
234 for (uns i = 0; i < (uns)color_map->ColorCount; i++, pal_pos += 4, palette++)
236 pal_pos[0] = palette->Red;
237 pal_pos[1] = palette->Green;
238 pal_pos[2] = palette->Blue;
240 if (pal_pos != pal_end)
241 bzero(pal_pos, pal_end - pal_pos);
242 if (rd->transparent_index >= 0 && (io->flags & IMAGE_IO_USE_BACKGROUND))
243 if (!color_put(io->context, &io->background_color, pal + 4 * rd->transparent_index, COLOR_SPACE_RGB))
248 # define IMAGE_WALK_PREFIX(x) walk_##x
249 # define IMAGE_WALK_INLINE
250 # define IMAGE_WALK_IMAGE (rdi.image)
251 # define IMAGE_WALK_UNROLL 4
252 # define IMAGE_WALK_COL_STEP 3
253 # define IMAGE_WALK_ROW_STEP 0
254 # 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)
255 # define IMAGE_WALK_DO_ROW_END DO_ROW_END
256 # include <images/image-walk.h>
261 byte pal[256 * 4], *pal_pos = pal, *pal_end = pal + 256 * 4;
262 for (uns i = 0; i < (uns)color_map->ColorCount; i++, pal_pos += 4, palette++)
264 pal_pos[0] = palette->Red;
265 pal_pos[1] = palette->Green;
266 pal_pos[2] = palette->Blue;
269 if (pal_pos != pal_end)
270 bzero(pal_pos, pal_end - pal_pos);
271 if (rd->transparent_index >= 0)
272 pal[rd->transparent_index * 4 + 3] = 0;
273 # define IMAGE_WALK_PREFIX(x) walk_##x
274 # define IMAGE_WALK_INLINE
275 # define IMAGE_WALK_IMAGE (rdi.image)
276 # define IMAGE_WALK_UNROLL 4
277 # define IMAGE_WALK_COL_STEP 4
278 # define IMAGE_WALK_ROW_STEP 0
279 # define IMAGE_WALK_DO_STEP do{ *(u32 *)walk_pos = ((u32 *)pal)[*pixels++]; }while(0)
280 # define IMAGE_WALK_DO_ROW_END DO_ROW_END
281 # include <images/image-walk.h>
288 /* Destroy libungif structure */
292 return image_io_read_data_finish(&rdi, io);