]> mj.ucw.cz Git - libucw.git/blob - images/io-libungif.c
- removed testing code from io-libungif
[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/color.h"
17 #include "images/io-main.h"
18 #include <gif_lib.h>
19
20 struct libungif_read_data {
21   GifFileType *gif;
22   int transparent_index;
23 };
24
25 static int
26 libungif_read_func(GifFileType *gif, GifByteType *ptr, int len)
27 {
28   DBG("libungif_read_func(len=%d)", len);
29   return bread((struct fastbuf *)gif->UserData, (byte *)ptr, len);
30 }
31
32 static void
33 libungif_read_cancel(struct image_io *io)
34 {
35   DBG("libungif_read_cancel()");
36
37   DGifCloseFile(io->read_data);
38 }
39
40 int
41 libungif_read_header(struct image_io *io)
42 {
43   DBG("libungif_read_header()");
44
45   /* Create libungif structure */
46   GifFileType *gif;
47   if (unlikely(!(gif = DGifOpen(io->fastbuf, libungif_read_func))))
48     {
49       image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Cannot create libungif structure.");
50       return 0;
51     }
52
53   struct libungif_read_data *rd = io->read_data = mp_alloc(io->internal_pool, sizeof(*rd));
54   rd->gif = gif;
55
56   DBG("executing DGifSlurp()");
57   if (unlikely(DGifSlurp(gif) != GIF_OK))
58     {
59       image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Gif read failed.");
60       DGifCloseFile(gif);
61       return 0;
62     }
63
64   DBG("ImageCount=%d ColorResolution=%d SBackGroundColor=%d SColorMap=%p", gif->ImageCount, gif->SColorResolution, gif->SBackGroundColor, gif->SColorMap);
65   if (unlikely(!gif->ImageCount))
66     {
67       image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "There are no images in gif file.");
68       DGifCloseFile(gif);
69       return 0;
70     }
71
72   /* Read image parameters */
73   SavedImage *image = gif->SavedImages;
74   if (unlikely(image->ImageDesc.Width <= 0 || image->ImageDesc.Height <= 0 ||
75       image->ImageDesc.Width > (int)IMAGE_MAX_SIZE || image->ImageDesc.Height > (int)IMAGE_MAX_SIZE))
76     {
77       image_thread_err(io->thread, IMAGE_ERR_INVALID_DIMENSIONS, "Invalid gif dimensions.");
78       DGifCloseFile(gif);
79       return 0;
80     }
81   ColorMapObject *color_map = image->ImageDesc.ColorMap ? : gif->SColorMap;
82   if (unlikely(!color_map))
83     {
84       image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Missing palette.");
85       DGifCloseFile(gif);
86       return 0;
87     }
88   io->cols = image->ImageDesc.Width;
89   io->rows = image->ImageDesc.Height;
90   if (unlikely((io->number_of_colors = color_map->ColorCount) > 256))
91     {
92       image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Too many gif colors.");
93       DGifCloseFile(gif);
94       return 0;
95     }
96   io->flags = COLOR_SPACE_RGB | IMAGE_IO_HAS_PALETTE;
97
98   /* Search extension blocks */
99   rd->transparent_index = -1;
100   for (int i = 0; i < image->ExtensionBlockCount; i++)
101     {
102       ExtensionBlock *e = image->ExtensionBlocks + i;
103       if (e->Function == 0xF9)
104         {
105           DBG("Found graphics control extension");
106           if (unlikely(e->ByteCount != 4))
107             {
108               image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Invalid graphics control extension.");
109               DGifCloseFile(gif);
110               return 0;
111             }
112           byte *b = e->Bytes;
113           /* transparent color present */
114           if (b[0] & 1)
115             {
116               rd->transparent_index = b[3];
117               io->flags |= IMAGE_ALPHA;
118               if (gif->SColorMap)
119                 {
120                   GifColorType *background = color_map->Colors + gif->SBackGroundColor;
121                   color_make_rgb(&io->background_color, background->Red, background->Green, background->Blue);
122                 }
123             }
124           /* We've got everything we need :-) */
125           break;
126         }
127       else
128         DBG("Found unknown extension: type=%d size=%d", e->Function, e->ByteCount);
129     }
130
131   /* Success */
132   io->read_cancel = libungif_read_cancel;
133   return 1;
134 }
135
136 int
137 libungif_read_data(struct image_io *io)
138 {
139   DBG("libungif_read_data()");
140
141   struct libungif_read_data *rd = io->read_data;
142   GifFileType *gif = rd->gif;
143   SavedImage *image = gif->SavedImages;
144
145   /* Prepare image */
146   struct image_io_read_data_internals rdi;
147   if (unlikely(!image_io_read_data_prepare(&rdi, io, image->ImageDesc.Width, image->ImageDesc.Height, io->flags)))
148     {
149       DGifCloseFile(gif);
150       return 0;
151     }
152
153   /* Get pixels and palette */
154   byte *pixels = (byte *)image->RasterBits;
155   ColorMapObject *color_map = image->ImageDesc.ColorMap ? : gif->SColorMap;
156   GifColorType *palette = color_map->Colors;
157   byte *img_end = rdi.image->pixels + rdi.image->image_size;
158
159   /* Handle deinterlacing */
160   uns dein_step, dein_next;
161   if (image->ImageDesc.Interlace)
162     {
163       DBG("Deinterlaced image");
164       dein_step = dein_next = rdi.image->row_size << 3;
165     }
166   else
167     dein_step = dein_next = rdi.image->row_size;
168
169   /* Convert pixels */
170   switch (rdi.image->pixel_size)
171     {
172       case 1:
173         {
174           byte pal[256], *pal_pos = pal, *pal_end = pal + 256;
175           for (uns i = 0; i < (uns)color_map->ColorCount; i++, pal_pos++, palette++)
176             *pal_pos = rgb_to_gray_func(palette->Red, palette->Green, palette->Blue);
177           if (pal_pos != pal_end)
178             bzero(pal_pos, pal_end - pal_pos);
179           if (rd->transparent_index >= 0 && (io->flags & IMAGE_IO_USE_BACKGROUND))
180             color_put_grayscale(pal + rd->transparent_index, &io->background_color);
181 #         define DO_ROW_END do{ \
182               walk_row_start += dein_step; \
183               if (walk_row_start >= img_end) \
184                 { uns n = dein_next >> 1; walk_row_start = rdi.image->pixels + n, dein_step = dein_next; dein_next = n; } \
185             }while(0)
186 #         define IMAGE_WALK_PREFIX(x) walk_##x
187 #         define IMAGE_WALK_INLINE
188 #         define IMAGE_WALK_IMAGE (rdi.image)
189 #         define IMAGE_WALK_UNROLL 4
190 #         define IMAGE_WALK_COL_STEP 1
191 #         define IMAGE_WALK_ROW_STEP 0
192 #         define IMAGE_WALK_DO_STEP do{ *walk_pos = pal[*pixels++]; }while(0)
193 #         define IMAGE_WALK_DO_ROW_END DO_ROW_END
194 #         include "images/image-walk.h"
195           break;
196         }
197       case 2:
198         {
199           byte pal[256 * 2], *pal_pos = pal, *pal_end = pal + 256 * 2;
200           for (uns i = 0; i < (uns)color_map->ColorCount; i++, pal_pos += 2, palette++)
201             {
202               pal_pos[0] = rgb_to_gray_func(palette->Red, palette->Green, palette->Blue);
203               pal_pos[1] = 255;
204             }
205           if (pal_pos != pal_end)
206             bzero(pal_pos, pal_end - pal_pos);
207           if (rd->transparent_index >= 0)
208             pal[rd->transparent_index * 2 + 1] = 0;
209 #         define IMAGE_WALK_PREFIX(x) walk_##x
210 #         define IMAGE_WALK_INLINE
211 #         define IMAGE_WALK_IMAGE (rdi.image)
212 #         define IMAGE_WALK_UNROLL 4
213 #         define IMAGE_WALK_COL_STEP 2
214 #         define IMAGE_WALK_ROW_STEP 0
215 #         define IMAGE_WALK_DO_STEP do{ *(u16 *)walk_pos = ((u16 *)pal)[*pixels++]; }while(0)
216 #         define IMAGE_WALK_DO_ROW_END DO_ROW_END
217 #         include "images/image-walk.h"
218           break;
219         }
220       case 3:
221         {
222           byte pal[256 * 4], *pal_pos = pal, *pal_end = pal + 256 * 4;
223           for (uns i = 0; i < (uns)color_map->ColorCount; i++, pal_pos += 4, palette++)
224             {
225               pal_pos[0] = palette->Red;
226               pal_pos[1] = palette->Green;
227               pal_pos[2] = palette->Blue;
228             }
229           if (pal_pos != pal_end)
230             bzero(pal_pos, pal_end - pal_pos);
231           if (rd->transparent_index >= 0 && (io->flags & IMAGE_IO_USE_BACKGROUND))
232             color_put_rgb(pal + 4 * rd->transparent_index, &io->background_color);
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 3
238 #         define IMAGE_WALK_ROW_STEP 0
239 #         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)
240 #         define IMAGE_WALK_DO_ROW_END DO_ROW_END
241 #         include "images/image-walk.h"
242           break;
243         }
244       case 4:
245         {
246           byte pal[256 * 4], *pal_pos = pal, *pal_end = pal + 256 * 4;
247           for (uns i = 0; i < (uns)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               pal_pos[3] = 255;
253             }
254           if (pal_pos != pal_end)
255             bzero(pal_pos, pal_end - pal_pos);
256           if (rd->transparent_index >= 0)
257             pal[rd->transparent_index * 4 + 3] = 0;
258 #         define IMAGE_WALK_PREFIX(x) walk_##x
259 #         define IMAGE_WALK_INLINE
260 #         define IMAGE_WALK_IMAGE (rdi.image)
261 #         define IMAGE_WALK_UNROLL 4
262 #         define IMAGE_WALK_COL_STEP 4
263 #         define IMAGE_WALK_ROW_STEP 0
264 #         define IMAGE_WALK_DO_STEP do{ *(u32 *)walk_pos = ((u32 *)pal)[*pixels++]; }while(0)
265 #         define IMAGE_WALK_DO_ROW_END DO_ROW_END
266 #         include "images/image-walk.h"
267           break;
268         }
269       default:
270         ASSERT(0);
271     }
272
273   /* Destroy libungif structure */
274   DGifCloseFile(gif);
275
276   /* Finish image */
277   return image_io_read_data_finish(&rdi, io);
278 }