]> mj.ucw.cz Git - libucw.git/blob - images/io-libungif.c
ABI: Tables used by ucw/chartype.h are prefixed
[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 struct libungif_read_data {
23   GifFileType *gif;
24   int transparent_index;
25 };
26
27 static int
28 libungif_read_func(GifFileType *gif, GifByteType *ptr, int len)
29 {
30   DBG("libungif_read_func(len=%d)", len);
31   return bread((struct fastbuf *)gif->UserData, (byte *)ptr, len);
32 }
33
34 static void
35 libungif_read_cancel(struct image_io *io)
36 {
37   DBG("libungif_read_cancel()");
38
39   struct libungif_read_data *rd = io->read_data;
40   DGifCloseFile(rd->gif);
41 }
42
43 int
44 libungif_read_header(struct image_io *io)
45 {
46   DBG("libungif_read_header()");
47
48   /* Create libungif structure */
49   GifFileType *gif;
50   if (unlikely(!(gif = DGifOpen(io->fastbuf, libungif_read_func))))
51     {
52       IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "Cannot create libungif structure.");
53       return 0;
54     }
55
56   struct libungif_read_data *rd = io->read_data = mp_alloc(io->internal_pool, sizeof(*rd));
57   rd->gif = gif;
58
59   DBG("executing DGifSlurp()");
60   if (unlikely(DGifSlurp(gif) != GIF_OK))
61     {
62       IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "Gif read failed.");
63       DGifCloseFile(gif);
64       return 0;
65     }
66
67   DBG("ImageCount=%d ColorResolution=%d SBackGroundColor=%d SColorMap=%p", gif->ImageCount, gif->SColorResolution, gif->SBackGroundColor, gif->SColorMap);
68   if (unlikely(!gif->ImageCount))
69     {
70       IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "There are no images in gif file.");
71       DGifCloseFile(gif);
72       return 0;
73     }
74
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))
79     {
80       IMAGE_ERROR(io->context, IMAGE_ERROR_INVALID_DIMENSIONS, "Invalid gif dimensions.");
81       DGifCloseFile(gif);
82       return 0;
83     }
84   ColorMapObject *color_map = image->ImageDesc.ColorMap ? : gif->SColorMap;
85   if (unlikely(!color_map))
86     {
87       IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "Missing palette.");
88       DGifCloseFile(gif);
89       return 0;
90     }
91   io->cols = image->ImageDesc.Width;
92   io->rows = image->ImageDesc.Height;
93   if (unlikely((io->number_of_colors = color_map->ColorCount) > 256))
94     {
95       IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "Too many gif colors.");
96       DGifCloseFile(gif);
97       return 0;
98     }
99   io->flags = COLOR_SPACE_RGB | IMAGE_IO_HAS_PALETTE;
100
101   /* Search extension blocks */
102   rd->transparent_index = -1;
103   for (int i = 0; i < image->ExtensionBlockCount; i++)
104     {
105       ExtensionBlock *e = image->ExtensionBlocks + i;
106       if (e->Function == 0xF9)
107         {
108           DBG("Found graphics control extension");
109           if (unlikely(e->ByteCount != 4))
110             {
111               IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "Invalid graphics control extension.");
112               DGifCloseFile(gif);
113               return 0;
114             }
115           byte *b = e->Bytes;
116           /* transparent color present */
117           if (b[0] & 1)
118             {
119               rd->transparent_index = b[3];
120               io->flags |= IMAGE_ALPHA;
121               if (gif->SColorMap)
122                 {
123                   GifColorType *background = color_map->Colors + gif->SBackGroundColor;
124                   color_make_rgb(&io->background_color, background->Red, background->Green, background->Blue);
125                 }
126             }
127           /* We've got everything we need :-) */
128           break;
129         }
130       else
131         DBG("Found unknown extension: type=%d size=%d", e->Function, e->ByteCount);
132     }
133
134   /* Success */
135   io->read_cancel = libungif_read_cancel;
136   return 1;
137 }
138
139 int
140 libungif_read_data(struct image_io *io)
141 {
142   DBG("libungif_read_data()");
143
144   struct libungif_read_data *rd = io->read_data;
145   GifFileType *gif = rd->gif;
146   SavedImage *image = gif->SavedImages;
147
148   /* Prepare image */
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)))
155     {
156       DGifCloseFile(gif);
157       return 0;
158     }
159
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;
165
166   /* Handle deinterlacing */
167   uns dein_step, dein_next;
168   if (image->ImageDesc.Interlace)
169     {
170       DBG("Deinterlaced image");
171       dein_step = dein_next = rdi.image->row_size << 3;
172     }
173   else
174     dein_step = dein_next = rdi.image->row_size;
175
176   /* Convert pixels */
177   switch (rdi.image->pixel_size)
178     {
179       case 1:
180         {
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))
188               {
189                 DGifCloseFile(gif);
190                 return 0;
191               }
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; } \
196             }while(0)
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>
206           break;
207         }
208       case 2:
209         {
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++)
212             {
213               pal_pos[0] = rgb_to_gray_func(palette->Red, palette->Green, palette->Blue);
214               pal_pos[1] = 255;
215             }
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>
229           break;
230         }
231       case 3:
232         {
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++)
235             {
236               pal_pos[0] = palette->Red;
237               pal_pos[1] = palette->Green;
238               pal_pos[2] = palette->Blue;
239             }
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))
244               {
245                 DGifCloseFile(gif);
246                 return 0;
247               }
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>
257           break;
258         }
259       case 4:
260         {
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++)
263             {
264               pal_pos[0] = palette->Red;
265               pal_pos[1] = palette->Green;
266               pal_pos[2] = palette->Blue;
267               pal_pos[3] = 255;
268             }
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>
282           break;
283         }
284       default:
285         ASSERT(0);
286     }
287
288   /* Destroy libungif structure */
289   DGifCloseFile(gif);
290
291   /* Finish image */
292   return image_io_read_data_finish(&rdi, io);
293 }