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;
118 /* Handle deinterlacing */
119 uns dein_step, dein_next;
120 if (image->ImageDesc.Interlace)
121 dein_step = dein_next = img->row_size << 3;
123 dein_step = dein_next = img->row_size;
126 switch (img->pixel_size)
131 byte pal[256], *pal_pos = pal, *pal_end = pal + 256;
132 for (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 IMAGE_WALK_INLINE
137 # define IMAGE_WALK_UNROLL 4
138 # define IMAGE_WALK_COL_STEP 1
139 # define IMAGE_WALK_ROW_STEP 0
140 # define IMAGE_WALK_DO_STEP do{ *pos = pal[*pixels++]; }while(0)
141 # define IMAGE_WALK_DO_ROW_END do{ \
142 row_start += dein_step; \
143 if (row_start > img->pixels + img->image_size) \
144 row_start = img->pixels + (dein_next >>= 1), dein_step = dein_next << 1; \
146 # include "images/image-walk.h"
152 byte pal[256 * 2], *pal_pos = pal, *pal_end = pal + 256 * 2;
153 for (i = 0; i < (uns)color_map->ColorCount; i++, pal_pos += 2, palette++)
155 pal_pos[0] = libungif_pixel_to_gray(palette);
158 if (pal_pos != pal_end)
159 bzero(pal_pos, pal_end - pal_pos);
160 if (background < 256)
161 pal[background * 2 + 1] = 0;
162 # define IMAGE_WALK_INLINE
163 # define IMAGE_WALK_UNROLL 4
164 # define IMAGE_WALK_COL_STEP 2
165 # define IMAGE_WALK_ROW_STEP 0
166 # define IMAGE_WALK_DO_STEP do{ *(u16 *)pos = ((u16 *)pal)[*pixels++]; }while(0)
167 # define IMAGE_WALK_DO_ROW_END do{ \
168 row_start += dein_step; \
169 if (row_start > img->pixels + img->image_size) \
170 row_start = img->pixels + (dein_next >>= 1), dein_step = dein_next << 1; \
172 # include "images/image-walk.h"
178 byte pal[256 * 4], *pal_pos = pal, *pal_end = pal + 256 * 4;
179 for (i = 0; i < (uns)color_map->ColorCount; i++, pal_pos += 4, palette++)
181 pal_pos[0] = palette->Red;
182 pal_pos[1] = palette->Green;
183 pal_pos[2] = palette->Blue;
185 if (pal_pos != pal_end)
186 bzero(pal_pos, pal_end - pal_pos);
187 # define IMAGE_WALK_INLINE
188 # define IMAGE_WALK_UNROLL 4
189 # define IMAGE_WALK_COL_STEP 3
190 # define IMAGE_WALK_ROW_STEP 0
191 # define IMAGE_WALK_DO_STEP do{ byte *p = pal + 4 * (*pixels++); pos[0] = p[0]; pos[1] = p[1]; pos[2] = p[2]; }while(0)
192 # define IMAGE_WALK_DO_ROW_END do{ \
193 row_start += dein_step; \
194 if (row_start > img->pixels + img->image_size) \
195 row_start = img->pixels + (dein_next >>= 1), dein_step = dein_next << 1; \
197 # include "images/image-walk.h"
203 byte pal[256 * 4], *pal_pos = pal, *pal_end = pal + 256 * 4;
204 for (i = 0; i < (uns)color_map->ColorCount; i++, pal_pos += 4, palette++)
206 pal_pos[0] = palette->Red;
207 pal_pos[1] = palette->Green;
208 pal_pos[2] = palette->Blue;
211 if (pal_pos != pal_end)
212 bzero(pal_pos, pal_end - pal_pos);
213 if (background < 256)
214 pal[background * 4 + 3] = 0;
215 # define IMAGE_WALK_INLINE
216 # define IMAGE_WALK_UNROLL 4
217 # define IMAGE_WALK_COL_STEP 4
218 # define IMAGE_WALK_ROW_STEP 0
219 # define IMAGE_WALK_DO_STEP do{ *(u32 *)pos = ((u32 *)pal)[*pixels++]; }while(0)
220 # define IMAGE_WALK_DO_ROW_END do{ \
221 row_start += dein_step; \
222 if (row_start > img->pixels + img->image_size) \
223 row_start = img->pixels + (dein_next >>= 1), dein_step = dein_next << 1; \
225 # include "images/image-walk.h"
232 /* Destroy libungif structure */
238 struct image *img2 = image_new(io->thread, io->cols, io->rows, io->flags, io->pool);
241 int result = image_scale(io->thread, img2, img);
244 need_destroy = !io->pool;
245 if (unlikely(!result))
251 io->image_destroy = need_destroy;
254 /* Free structures */