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