]> mj.ucw.cz Git - libucw.git/blob - images/io-libungif.c
Merge with git+ssh://git.ucw.cz/projects/sherlock/GIT/sherlock.git
[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 "lib/lib.h"
13 #include "lib/mempool.h"
14 #include "lib/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   if (unlikely(!image_io_read_data_prepare(&rdi, io, image->ImageDesc.Width, image->ImageDesc.Height, io->flags)))
151     {
152       DGifCloseFile(gif);
153       return 0;
154     }
155
156   /* Get pixels and palette */
157   byte *pixels = (byte *)image->RasterBits;
158   ColorMapObject *color_map = image->ImageDesc.ColorMap ? : gif->SColorMap;
159   GifColorType *palette = color_map->Colors;
160   byte *img_end = rdi.image->pixels + rdi.image->image_size;
161
162   /* Handle deinterlacing */
163   uns dein_step, dein_next;
164   if (image->ImageDesc.Interlace)
165     {
166       DBG("Deinterlaced image");
167       dein_step = dein_next = rdi.image->row_size << 3;
168     }
169   else
170     dein_step = dein_next = rdi.image->row_size;
171
172   /* Convert pixels */
173   switch (rdi.image->pixel_size)
174     {
175       case 1:
176         {
177           byte pal[256], *pal_pos = pal, *pal_end = pal + 256;
178           for (uns i = 0; i < (uns)color_map->ColorCount; i++, pal_pos++, palette++)
179             *pal_pos = rgb_to_gray_func(palette->Red, palette->Green, palette->Blue);
180           if (pal_pos != pal_end)
181             bzero(pal_pos, pal_end - pal_pos);
182           if (rd->transparent_index >= 0 && (io->flags & IMAGE_IO_USE_BACKGROUND))
183             color_put_grayscale(pal + rd->transparent_index, &io->background_color);
184 #         define DO_ROW_END do{ \
185               walk_row_start += dein_step; \
186               if (walk_row_start >= img_end) \
187                 { uns n = dein_next >> 1; walk_row_start = rdi.image->pixels + n, dein_step = dein_next; dein_next = n; } \
188             }while(0)
189 #         define IMAGE_WALK_PREFIX(x) walk_##x
190 #         define IMAGE_WALK_INLINE
191 #         define IMAGE_WALK_IMAGE (rdi.image)
192 #         define IMAGE_WALK_UNROLL 4
193 #         define IMAGE_WALK_COL_STEP 1
194 #         define IMAGE_WALK_ROW_STEP 0
195 #         define IMAGE_WALK_DO_STEP do{ *walk_pos = pal[*pixels++]; }while(0)
196 #         define IMAGE_WALK_DO_ROW_END DO_ROW_END
197 #         include "images/image-walk.h"
198           break;
199         }
200       case 2:
201         {
202           byte pal[256 * 2], *pal_pos = pal, *pal_end = pal + 256 * 2;
203           for (uns i = 0; i < (uns)color_map->ColorCount; i++, pal_pos += 2, palette++)
204             {
205               pal_pos[0] = rgb_to_gray_func(palette->Red, palette->Green, palette->Blue);
206               pal_pos[1] = 255;
207             }
208           if (pal_pos != pal_end)
209             bzero(pal_pos, pal_end - pal_pos);
210           if (rd->transparent_index >= 0)
211             pal[rd->transparent_index * 2 + 1] = 0;
212 #         define IMAGE_WALK_PREFIX(x) walk_##x
213 #         define IMAGE_WALK_INLINE
214 #         define IMAGE_WALK_IMAGE (rdi.image)
215 #         define IMAGE_WALK_UNROLL 4
216 #         define IMAGE_WALK_COL_STEP 2
217 #         define IMAGE_WALK_ROW_STEP 0
218 #         define IMAGE_WALK_DO_STEP do{ *(u16 *)walk_pos = ((u16 *)pal)[*pixels++]; }while(0)
219 #         define IMAGE_WALK_DO_ROW_END DO_ROW_END
220 #         include "images/image-walk.h"
221           break;
222         }
223       case 3:
224         {
225           byte pal[256 * 4], *pal_pos = pal, *pal_end = pal + 256 * 4;
226           for (uns i = 0; i < (uns)color_map->ColorCount; i++, pal_pos += 4, palette++)
227             {
228               pal_pos[0] = palette->Red;
229               pal_pos[1] = palette->Green;
230               pal_pos[2] = palette->Blue;
231             }
232           if (pal_pos != pal_end)
233             bzero(pal_pos, pal_end - pal_pos);
234           if (rd->transparent_index >= 0 && (io->flags & IMAGE_IO_USE_BACKGROUND))
235             color_put_rgb(pal + 4 * rd->transparent_index, &io->background_color);
236 #         define IMAGE_WALK_PREFIX(x) walk_##x
237 #         define IMAGE_WALK_INLINE
238 #         define IMAGE_WALK_IMAGE (rdi.image)
239 #         define IMAGE_WALK_UNROLL 4
240 #         define IMAGE_WALK_COL_STEP 3
241 #         define IMAGE_WALK_ROW_STEP 0
242 #         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)
243 #         define IMAGE_WALK_DO_ROW_END DO_ROW_END
244 #         include "images/image-walk.h"
245           break;
246         }
247       case 4:
248         {
249           byte pal[256 * 4], *pal_pos = pal, *pal_end = pal + 256 * 4;
250           for (uns i = 0; i < (uns)color_map->ColorCount; i++, pal_pos += 4, palette++)
251             {
252               pal_pos[0] = palette->Red;
253               pal_pos[1] = palette->Green;
254               pal_pos[2] = palette->Blue;
255               pal_pos[3] = 255;
256             }
257           if (pal_pos != pal_end)
258             bzero(pal_pos, pal_end - pal_pos);
259           if (rd->transparent_index >= 0)
260             pal[rd->transparent_index * 4 + 3] = 0;
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 4
266 #         define IMAGE_WALK_ROW_STEP 0
267 #         define IMAGE_WALK_DO_STEP do{ *(u32 *)walk_pos = ((u32 *)pal)[*pixels++]; }while(0)
268 #         define IMAGE_WALK_DO_ROW_END DO_ROW_END
269 #         include "images/image-walk.h"
270           break;
271         }
272       default:
273         ASSERT(0);
274     }
275
276   /* Destroy libungif structure */
277   DGifCloseFile(gif);
278
279   /* Finish image */
280   return image_io_read_data_finish(&rdi, io);
281 }