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"
19 libungif_read_func(GifFileType *gif, GifByteType *ptr, int len)
21 DBG("libungif_read_func(len=%d)", len);
22 return bread((struct fastbuf *)gif->UserData, (byte *)ptr, len);
26 libungif_read_cancel(struct image_io *io)
28 DBG("libungif_read_cancel()");
30 DGifCloseFile(io->read_data);
34 libungif_read_header(struct image_io *io)
36 DBG("libungif_read_header()");
38 /* Create libungif structure */
40 if (unlikely(!(gif = io->read_data = DGifOpen(io->fastbuf, libungif_read_func))))
42 image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Cannot create libungif structure.");
46 if (unlikely(DGifSlurp(gif) != GIF_OK))
48 image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Gif read failed.");
53 if (unlikely(!gif->ImageCount))
55 image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "There are no images in gif file.");
60 /* Read image parameters */
61 SavedImage *image = gif->SavedImages;
62 if (unlikely(image->ImageDesc.Width <= 0 || image->ImageDesc.Height <= 0 ||
63 image->ImageDesc.Width > (int)IMAGE_MAX_SIZE || image->ImageDesc.Height > (int)IMAGE_MAX_SIZE))
65 image_thread_err(io->thread, IMAGE_ERR_INVALID_DIMENSIONS, "Invalid gif dimensions.");
69 ColorMapObject *color_map = image->ImageDesc.ColorMap ? : gif->SColorMap;
70 if (unlikely(!color_map))
72 image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Missing palette.");
76 io->cols = image->ImageDesc.Width;
77 io->rows = image->ImageDesc.Height;
79 io->number_of_colors = color_map->ColorCount;
80 io->flags = COLOR_SPACE_RGB;
81 if ((uns)gif->SBackGroundColor < (uns)color_map->ColorCount)
82 io->flags |= IMAGE_ALPHA;
85 io->read_cancel = libungif_read_cancel;
90 libungif_pixel_to_gray(GifColorType *pixel)
92 return ((uns)pixel->Red * 19660 + (uns)pixel->Green * 38666 + (uns)pixel->Blue * 7210) >> 16;
96 libungif_read_data(struct image_io *io)
98 DBG("libungif_read_data()");
100 GifFileType *gif = io->read_data;
101 SavedImage *image = gif->SavedImages;
104 int need_scale = io->cols != (uns)image->ImageDesc.Width || io->rows != (uns)image->ImageDesc.Height;
105 int need_destroy = need_scale || !io->pool;
106 struct image *img = need_scale ?
107 image_new(io->thread, image->ImageDesc.Width, image->ImageDesc.Height, io->flags & IMAGE_CHANNELS_FORMAT, NULL) :
108 image_new(io->thread, io->cols, io->rows, io->flags, io->pool);
112 /* Get pixels and palette */
113 byte *pixels = (byte *)image->RasterBits;
114 ColorMapObject *color_map = image->ImageDesc.ColorMap ? : gif->SColorMap;
115 GifColorType *palette = color_map->Colors;
116 uns background = gif->SBackGroundColor;
117 byte *img_end = img->pixels + img->image_size;
119 /* Handle deinterlacing */
120 uns dein_step, dein_next;
121 if (image->ImageDesc.Interlace)
122 dein_step = dein_next = img->row_size << 3;
124 dein_step = dein_next = img->row_size;
127 switch (img->pixel_size)
131 byte pal[256], *pal_pos = pal, *pal_end = pal + 256;
132 for (uns i = 0; i < (uns)color_map->ColorCount; i++, pal_pos++, palette++)
133 *pal_pos = libungif_pixel_to_gray(palette);
134 if (pal_pos != pal_end)
135 bzero(pal_pos, pal_end - pal_pos);
136 # define DO_ROW_END do{ \
137 walk_row_start += dein_step; \
138 if (walk_row_start > img_end) \
139 { uns n = dein_next >> 1; walk_row_start = img->pixels + n, dein_step = dein_next; dein_next = n; } \
141 # define IMAGE_WALK_PREFIX(x) walk_##x
142 # define IMAGE_WALK_INLINE
143 # define IMAGE_WALK_IMAGE img
144 # define IMAGE_WALK_UNROLL 4
145 # define IMAGE_WALK_COL_STEP 1
146 # define IMAGE_WALK_ROW_STEP 0
147 # define IMAGE_WALK_DO_STEP do{ *walk_pos = pal[*pixels++]; }while(0)
148 # define IMAGE_WALK_DO_ROW_END DO_ROW_END
149 # include "images/image-walk.h"
154 byte pal[256 * 2], *pal_pos = pal, *pal_end = pal + 256 * 2;
155 for (uns i = 0; i < (uns)color_map->ColorCount; i++, pal_pos += 2, palette++)
157 pal_pos[0] = libungif_pixel_to_gray(palette);
160 if (pal_pos != pal_end)
161 bzero(pal_pos, pal_end - pal_pos);
162 if (background < 256)
163 pal[background * 2 + 1] = 0;
164 # define IMAGE_WALK_PREFIX(x) walk_##x
165 # define IMAGE_WALK_INLINE
166 # define IMAGE_WALK_IMAGE img
167 # define IMAGE_WALK_UNROLL 4
168 # define IMAGE_WALK_COL_STEP 2
169 # define IMAGE_WALK_ROW_STEP 0
170 # define IMAGE_WALK_DO_STEP do{ *(u16 *)walk_pos = ((u16 *)pal)[*pixels++]; }while(0)
171 # define IMAGE_WALK_DO_ROW_END DO_ROW_END
172 # include "images/image-walk.h"
177 byte pal[256 * 4], *pal_pos = pal, *pal_end = pal + 256 * 4;
178 for (uns i = 0; i < (uns)color_map->ColorCount; i++, pal_pos += 4, palette++)
180 pal_pos[0] = palette->Red;
181 pal_pos[1] = palette->Green;
182 pal_pos[2] = palette->Blue;
184 if (pal_pos != pal_end)
185 bzero(pal_pos, pal_end - pal_pos);
186 # define IMAGE_WALK_PREFIX(x) walk_##x
187 # define IMAGE_WALK_INLINE
188 # define IMAGE_WALK_IMAGE img
189 # define IMAGE_WALK_UNROLL 4
190 # define IMAGE_WALK_COL_STEP 3
191 # define IMAGE_WALK_ROW_STEP 0
192 # 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)
193 # define IMAGE_WALK_DO_ROW_END DO_ROW_END
194 # include "images/image-walk.h"
199 byte pal[256 * 4], *pal_pos = pal, *pal_end = pal + 256 * 4;
200 for (uns i = 0; i < (uns)color_map->ColorCount; i++, pal_pos += 4, palette++)
202 pal_pos[0] = palette->Red;
203 pal_pos[1] = palette->Green;
204 pal_pos[2] = palette->Blue;
207 if (pal_pos != pal_end)
208 bzero(pal_pos, pal_end - pal_pos);
209 if (background < 256)
210 pal[background * 4 + 3] = 0;
211 # define IMAGE_WALK_PREFIX(x) walk_##x
212 # define IMAGE_WALK_INLINE
213 # define IMAGE_WALK_IMAGE img
214 # define IMAGE_WALK_UNROLL 4
215 # define IMAGE_WALK_COL_STEP 4
216 # define IMAGE_WALK_ROW_STEP 0
217 # define IMAGE_WALK_DO_STEP do{ *(u32 *)walk_pos = ((u32 *)pal)[*pixels++]; }while(0)
218 # define IMAGE_WALK_DO_ROW_END DO_ROW_END
219 # include "images/image-walk.h"
226 /* Destroy libungif structure */
232 struct image *img2 = image_new(io->thread, io->cols, io->rows, io->flags, io->pool);
235 int result = image_scale(io->thread, img2, img);
238 need_destroy = !io->pool;
239 if (unlikely(!result))
245 io->image_destroy = need_destroy;
248 /* Free structures */