]> mj.ucw.cz Git - libucw.git/blob - images/io-libungif.c
Images: Cope with API changes in recent versions of libgif
[libucw.git] / images / io-libungif.c
1 /*
2  *      Image Library -- libungif
3  *
4  *      (c) 2006 Pavel Charvat <pchar@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #undef LOCAL_DEBUG
11
12 #include <ucw/lib.h>
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>
19
20 #include <gif_lib.h>
21
22 // API of gif_lib has changed recenly
23 #ifndef GIFLIB_MAJOR
24 #define GIFLIB_MAJOR 0
25 #endif
26 #if GIFLIB_MAJOR > 5 || GIFLIB_MAJOR == 5 && GIFLIB_MINOR >= 1
27 static int dgif_error_code;     // Scratch pad only, no locking needed
28 #define DGIF_OPEN(_userptr, _readfunc) DGifOpen(_userptr, _readfunc, &dgif_error_code)
29 #define DGIF_CLOSE_FILE(_gif) DGifCloseFile(_gif, &dgif_error_code)
30 #else
31 #define DGIF_OPEN(_userptr, _readfunc) DGifOpen(_userptr, _readfunc)
32 #define DGIF_CLOSE_FILE(_gif) DGifCloseFile(_gif)
33 #endif
34
35 struct libungif_read_data {
36   GifFileType *gif;
37   int transparent_index;
38 };
39
40 static int
41 libungif_read_func(GifFileType *gif, GifByteType *ptr, int len)
42 {
43   DBG("libungif_read_func(len=%d)", len);
44   return bread((struct fastbuf *)gif->UserData, (byte *)ptr, len);
45 }
46
47 static void
48 libungif_read_cancel(struct image_io *io)
49 {
50   DBG("libungif_read_cancel()");
51
52   struct libungif_read_data *rd = io->read_data;
53   DGIF_CLOSE_FILE(rd->gif);
54 }
55
56 int
57 libungif_read_header(struct image_io *io)
58 {
59   DBG("libungif_read_header()");
60
61   /* Create libungif structure */
62   GifFileType *gif;
63   if (unlikely(!(gif = DGIF_OPEN(io->fastbuf, libungif_read_func))))
64     {
65       IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "Cannot create libungif structure.");
66       return 0;
67     }
68
69   struct libungif_read_data *rd = io->read_data = mp_alloc(io->internal_pool, sizeof(*rd));
70   rd->gif = gif;
71
72   DBG("executing DGifSlurp()");
73   if (unlikely(DGifSlurp(gif) != GIF_OK))
74     {
75       IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "Gif read failed.");
76       DGIF_CLOSE_FILE(gif);
77       return 0;
78     }
79
80   DBG("ImageCount=%d ColorResolution=%d SBackGroundColor=%d SColorMap=%p", gif->ImageCount, gif->SColorResolution, gif->SBackGroundColor, gif->SColorMap);
81   if (unlikely(!gif->ImageCount))
82     {
83       IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "There are no images in gif file.");
84       DGIF_CLOSE_FILE(gif);
85       return 0;
86     }
87
88   /* Read image parameters */
89   SavedImage *image = gif->SavedImages;
90   if (unlikely(image->ImageDesc.Width <= 0 || image->ImageDesc.Height <= 0 ||
91       image->ImageDesc.Width > (int)image_max_dim || image->ImageDesc.Height > (int)image_max_dim))
92     {
93       IMAGE_ERROR(io->context, IMAGE_ERROR_INVALID_DIMENSIONS, "Invalid gif dimensions.");
94       DGIF_CLOSE_FILE(gif);
95       return 0;
96     }
97   ColorMapObject *color_map = image->ImageDesc.ColorMap ? : gif->SColorMap;
98   if (unlikely(!color_map))
99     {
100       IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "Missing palette.");
101       DGIF_CLOSE_FILE(gif);
102       return 0;
103     }
104   io->cols = image->ImageDesc.Width;
105   io->rows = image->ImageDesc.Height;
106   if (unlikely((io->number_of_colors = color_map->ColorCount) > 256))
107     {
108       IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "Too many gif colors.");
109       DGIF_CLOSE_FILE(gif);
110       return 0;
111     }
112   io->flags = COLOR_SPACE_RGB | IMAGE_IO_HAS_PALETTE;
113
114   /* Search extension blocks */
115   rd->transparent_index = -1;
116   for (int i = 0; i < image->ExtensionBlockCount; i++)
117     {
118       ExtensionBlock *e = image->ExtensionBlocks + i;
119       if (e->Function == 0xF9)
120         {
121           DBG("Found graphics control extension");
122           if (unlikely(e->ByteCount != 4))
123             {
124               IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "Invalid graphics control extension.");
125               DGIF_CLOSE_FILE(gif);
126               return 0;
127             }
128           byte *b = e->Bytes;
129           /* transparent color present */
130           if (b[0] & 1)
131             {
132               rd->transparent_index = b[3];
133               io->flags |= IMAGE_ALPHA;
134               if (gif->SColorMap)
135                 {
136                   GifColorType *background = color_map->Colors + gif->SBackGroundColor;
137                   color_make_rgb(&io->background_color, background->Red, background->Green, background->Blue);
138                 }
139             }
140           /* We've got everything we need :-) */
141           break;
142         }
143       else
144         DBG("Found unknown extension: type=%d size=%d", e->Function, e->ByteCount);
145     }
146
147   /* Success */
148   io->read_cancel = libungif_read_cancel;
149   return 1;
150 }
151
152 int
153 libungif_read_data(struct image_io *io)
154 {
155   DBG("libungif_read_data()");
156
157   struct libungif_read_data *rd = io->read_data;
158   GifFileType *gif = rd->gif;
159   SavedImage *image = gif->SavedImages;
160
161   /* Prepare image */
162   struct image_io_read_data_internals rdi;
163   uint read_flags = io->flags;
164   uint cs = read_flags & IMAGE_COLOR_SPACE;
165   if (cs != COLOR_SPACE_GRAYSCALE && cs != COLOR_SPACE_RGB)
166     read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_RGB;
167   if (unlikely(!image_io_read_data_prepare(&rdi, io, image->ImageDesc.Width, image->ImageDesc.Height, read_flags)))
168     {
169       DGIF_CLOSE_FILE(gif);
170       return 0;
171     }
172
173   /* Get pixels and palette */
174   byte *pixels = (byte *)image->RasterBits;
175   ColorMapObject *color_map = image->ImageDesc.ColorMap ? : gif->SColorMap;
176   GifColorType *palette = color_map->Colors;
177   byte *img_end = rdi.image->pixels + rdi.image->image_size;
178
179   /* Handle deinterlacing */
180   uint dein_step, dein_next;
181   if (image->ImageDesc.Interlace)
182     {
183       DBG("Deinterlaced image");
184       dein_step = dein_next = rdi.image->row_size << 3;
185     }
186   else
187     dein_step = dein_next = rdi.image->row_size;
188
189   /* Convert pixels */
190   switch (rdi.image->pixel_size)
191     {
192       case 1:
193         {
194           byte pal[256], *pal_pos = pal, *pal_end = pal + 256;
195           for (uint i = 0; i < (uint)color_map->ColorCount; i++, pal_pos++, palette++)
196             *pal_pos = rgb_to_gray_func(palette->Red, palette->Green, palette->Blue);
197           if (pal_pos != pal_end)
198             bzero(pal_pos, pal_end - pal_pos);
199           if (rd->transparent_index >= 0 && (io->flags & IMAGE_IO_USE_BACKGROUND))
200             if (!color_put(io->context, &io->background_color, pal + rd->transparent_index, COLOR_SPACE_GRAYSCALE))
201               {
202                 DGIF_CLOSE_FILE(gif);
203                 return 0;
204               }
205 #         define DO_ROW_END do{ \
206               walk_row_start += dein_step; \
207               while (walk_row_start >= img_end) \
208                 { uint n = dein_next >> 1; walk_row_start = rdi.image->pixels + n, dein_step = dein_next; dein_next = n; } \
209             }while(0)
210 #         define IMAGE_WALK_PREFIX(x) walk_##x
211 #         define IMAGE_WALK_INLINE
212 #         define IMAGE_WALK_IMAGE (rdi.image)
213 #         define IMAGE_WALK_UNROLL 4
214 #         define IMAGE_WALK_COL_STEP 1
215 #         define IMAGE_WALK_ROW_STEP 0
216 #         define IMAGE_WALK_DO_STEP do{ *walk_pos = pal[*pixels++]; }while(0)
217 #         define IMAGE_WALK_DO_ROW_END DO_ROW_END
218 #         include <images/image-walk.h>
219           break;
220         }
221       case 2:
222         {
223           byte pal[256 * 2], *pal_pos = pal, *pal_end = pal + 256 * 2;
224           for (uint i = 0; i < (uint)color_map->ColorCount; i++, pal_pos += 2, palette++)
225             {
226               pal_pos[0] = rgb_to_gray_func(palette->Red, palette->Green, palette->Blue);
227               pal_pos[1] = 255;
228             }
229           if (pal_pos != pal_end)
230             bzero(pal_pos, pal_end - pal_pos);
231           if (rd->transparent_index >= 0)
232             pal[rd->transparent_index * 2 + 1] = 0;
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 2
238 #         define IMAGE_WALK_ROW_STEP 0
239 #         define IMAGE_WALK_DO_STEP do{ *(u16 *)walk_pos = ((u16 *)pal)[*pixels++]; }while(0)
240 #         define IMAGE_WALK_DO_ROW_END DO_ROW_END
241 #         include <images/image-walk.h>
242           break;
243         }
244       case 3:
245         {
246           byte pal[256 * 4], *pal_pos = pal, *pal_end = pal + 256 * 4;
247           for (uint i = 0; i < (uint)color_map->ColorCount; i++, pal_pos += 4, palette++)
248             {
249               pal_pos[0] = palette->Red;
250               pal_pos[1] = palette->Green;
251               pal_pos[2] = palette->Blue;
252             }
253           if (pal_pos != pal_end)
254             bzero(pal_pos, pal_end - pal_pos);
255           if (rd->transparent_index >= 0 && (io->flags & IMAGE_IO_USE_BACKGROUND))
256             if (!color_put(io->context, &io->background_color, pal + 4 * rd->transparent_index, COLOR_SPACE_RGB))
257               {
258                 DGIF_CLOSE_FILE(gif);
259                 return 0;
260               }
261 #         define IMAGE_WALK_PREFIX(x) walk_##x
262 #         define IMAGE_WALK_INLINE
263 #         define IMAGE_WALK_IMAGE (rdi.image)
264 #         define IMAGE_WALK_UNROLL 4
265 #         define IMAGE_WALK_COL_STEP 3
266 #         define IMAGE_WALK_ROW_STEP 0
267 #         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)
268 #         define IMAGE_WALK_DO_ROW_END DO_ROW_END
269 #         include <images/image-walk.h>
270           break;
271         }
272       case 4:
273         {
274           byte pal[256 * 4], *pal_pos = pal, *pal_end = pal + 256 * 4;
275           for (uint i = 0; i < (uint)color_map->ColorCount; i++, pal_pos += 4, palette++)
276             {
277               pal_pos[0] = palette->Red;
278               pal_pos[1] = palette->Green;
279               pal_pos[2] = palette->Blue;
280               pal_pos[3] = 255;
281             }
282           if (pal_pos != pal_end)
283             bzero(pal_pos, pal_end - pal_pos);
284           if (rd->transparent_index >= 0)
285             pal[rd->transparent_index * 4 + 3] = 0;
286 #         define IMAGE_WALK_PREFIX(x) walk_##x
287 #         define IMAGE_WALK_INLINE
288 #         define IMAGE_WALK_IMAGE (rdi.image)
289 #         define IMAGE_WALK_UNROLL 4
290 #         define IMAGE_WALK_COL_STEP 4
291 #         define IMAGE_WALK_ROW_STEP 0
292 #         define IMAGE_WALK_DO_STEP do{ *(u32 *)walk_pos = ((u32 *)pal)[*pixels++]; }while(0)
293 #         define IMAGE_WALK_DO_ROW_END DO_ROW_END
294 #         include <images/image-walk.h>
295           break;
296         }
297       default:
298         ASSERT(0);
299     }
300
301   /* Destroy libungif structure */
302   DGIF_CLOSE_FILE(gif);
303
304   /* Finish image */
305   return image_io_read_data_finish(&rdi, io);
306 }