]> mj.ucw.cz Git - libucw.git/blob - images/io-main.c
9442c7e0170084a7cb106d4b67352e65b00f2c92
[libucw.git] / images / io-main.c
1 /*
2  *      Image Library -- Image compression/decompression interface
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 #define LOCAL_DEBUG
11
12 #include "lib/lib.h"
13 #include "images/images.h"
14 #include "images/io-main.h"
15 #include <string.h>
16
17 void
18 image_io_init(struct image_thread *it, struct image_io *io)
19 {
20   DBG("image_io_init()");
21   bzero(io, sizeof(*io));
22   io->thread = it;
23   io->internal_pool = mp_new(1024);
24 }
25
26 static inline void
27 image_io_read_cancel(struct image_io *io)
28 {
29   if (io->read_cancel)
30     {
31       io->read_cancel(io);
32       io->read_cancel = NULL;
33     }
34 }
35
36 static inline void
37 image_io_image_destroy(struct image_io *io)
38 {
39   if (io->image_destroy)
40     {
41       image_destroy(io->image);
42       io->image_destroy = 0;
43       io->image = NULL;
44     }
45 }
46
47 void
48 image_io_cleanup(struct image_io *io)
49 {
50   DBG("image_io_cleanup()");
51   image_io_read_cancel(io);
52   image_io_image_destroy(io);
53   mp_delete(io->internal_pool);
54 }
55
56 void
57 image_io_reset(struct image_io *io)
58 {
59   DBG("image_io_reset()");
60   image_io_read_cancel(io);
61   image_io_image_destroy(io);
62   struct mempool *pool = io->internal_pool;
63   mp_flush(pool);
64   bzero(io, sizeof(*io));
65   io->internal_pool = pool;
66 }
67
68 int
69 image_io_read_header(struct image_io *io)
70 {
71   DBG("image_io_read_header()");
72   image_io_read_cancel(io);
73   image_io_image_destroy(io);
74   switch (io->format) {
75     case IMAGE_FORMAT_JPEG:
76 #if defined(CONFIG_IMAGES_LIBJPEG)
77       return libjpeg_read_header(io);
78 #elif defined(CONFIG_IMAGES_LIBMAGICK)
79       return libmagick_read_header(io);
80 #endif
81       break;
82
83     case IMAGE_FORMAT_PNG:
84 #if defined(CONFIG_IMAGES_LIBPNG)
85       return libpng_read_header(io);
86 #elif defined(CONFIG_IMAGES_LIBMAGICK)
87       return libmagick_read_header(io);
88 #endif
89       break;
90
91     case IMAGE_FORMAT_GIF:
92 #if defined(CONFIG_IMAGES_LIBUNGIF) || defined(CONFIG_IMAGES_LIBGIF)
93       return libungif_read_header(io);
94 #elif defined(CONFIG_IMAGES_LIBMAGICK)
95       return libmagick_read_header(io);
96 #endif
97       break;
98
99     case IMAGE_FORMAT_UNDEFINED:
100       // FIXME: auto-detect
101       break;
102
103     default:
104       ASSERT(0);
105   }
106   image_thread_err(io->thread, IMAGE_ERR_INVALID_FILE_FORMAT, "Image format not supported.");
107   return 0;
108 }
109
110 struct image *
111 image_io_read_data(struct image_io *io, int ref)
112 {
113   DBG("image_io_read_data()");
114   ASSERT(io->read_cancel);
115   io->read_cancel = NULL;
116   int result;
117   switch (io->format) {
118     case IMAGE_FORMAT_JPEG:
119 #if defined(CONFIG_IMAGES_LIBJPEG)
120       result = libjpeg_read_data(io);
121 #elif defined(CONFIG_IMAGES_LIBMAGICK)
122       result = libmagick_read_data(io);
123 #else
124       ASSERT(0);
125 #endif
126       break;
127
128     case IMAGE_FORMAT_PNG:
129 #if defined(CONFIG_IMAGES_LIBPNG)
130       result = libpng_read_data(io);
131 #elif defined(CONFIG_IMAGES_LIBMAGICK)
132       result = libmagick_read_data(io);
133 #else
134       ASSERT(0);
135 #endif
136       break;
137
138     case IMAGE_FORMAT_GIF:
139 #if defined(CONFIG_IMAGES_LIBUNGIF) || defined(CONFIG_IMAGES_LIBGIF)
140       result = libungif_read_data(io);
141 #elif defined(CONFIG_IMAGES_LIBMAGICK)
142       result = libmagick_read_data(io);
143 #else
144       ASSERT(0);
145 #endif
146       break;
147
148     default:
149       ASSERT(0);
150   }
151   if (result)
152     {
153       if (ref)
154         io->image_destroy = 0;
155       return io->image;
156     }
157   else
158     return NULL;
159 }
160
161 struct image *
162 image_io_read(struct image_io *io, int ref)
163 {
164   if (!image_io_read_header(io))
165     return NULL;
166   return image_io_read_data(io, ref);
167 }
168
169 int
170 image_io_write(struct image_io *io)
171 {
172   DBG("image_io_write()");
173   image_io_read_cancel(io);
174   switch (io->format) {
175     case IMAGE_FORMAT_JPEG:
176 #if defined(CONFIG_IMAGES_LIBJPEG)
177       return libjpeg_write(io);
178 #elif defined(CONFIG_IMAGES_LIBMAGICK)
179       return libmagick_write(io);
180 #endif
181       break;
182
183     case IMAGE_FORMAT_PNG:
184 #if defined(CONFIG_IMAGES_LIBPNG)
185       return libpng_write(io);
186 #elif defined(CONFIG_IMAGES_LIBMAGICK)
187       return libmagick_write(io);
188 #endif
189       break;
190
191     case IMAGE_FORMAT_GIF:
192 #if defined(CONFIG_IMAGES_LIBMAGICK)
193       return libmagick_write(io);
194 #endif
195       break;
196
197     default:
198       break;
199   }
200   image_thread_err(io->thread, IMAGE_ERR_INVALID_FILE_FORMAT, "Image format not supported.");
201   return 0;
202 }
203
204 byte *
205 image_format_to_extension(enum image_format format)
206 {
207   switch (format)
208     {
209       case IMAGE_FORMAT_JPEG:
210         return "jpg";
211       case IMAGE_FORMAT_PNG:
212         return "png";
213       case IMAGE_FORMAT_GIF:
214         return "gif";
215       default:
216         return NULL;
217     }
218 }
219
220 enum image_format
221 image_extension_to_format(byte *extension)
222 {
223   if (!strcasecmp(extension, "jpg"))
224     return IMAGE_FORMAT_JPEG;
225   if (!strcasecmp(extension, "jpeg"))
226     return IMAGE_FORMAT_JPEG;
227   if (!strcasecmp(extension, "png"))
228     return IMAGE_FORMAT_PNG;
229   if (!strcasecmp(extension, "gif"))
230     return IMAGE_FORMAT_GIF;
231   return IMAGE_FORMAT_UNDEFINED;
232 }
233
234 enum image_format
235 image_file_name_to_format(byte *file_name)
236 {
237   byte *extension = strrchr(file_name, '.');
238   return extension ? image_extension_to_format(extension + 1) : IMAGE_FORMAT_UNDEFINED;
239 }
240
241 struct image *
242 image_io_read_data_prepare(struct image_io_read_data_internals *rdi, struct image_io *io, uns cols, uns rows, uns flags)
243 {
244   DBG("image_io_read_data_prepare()");
245   if (rdi->need_transformations = io->cols != cols || io->rows != rows || io->flags != flags)
246     {
247       rdi->need_destroy = 1;
248       return rdi->image = image_new(io->thread, cols, rows, flags & IMAGE_IO_IMAGE_FLAGS, NULL);
249     }
250   else
251     {
252       rdi->need_destroy = !io->pool;
253       return rdi->image = image_new(io->thread, io->cols, io->rows, io->flags & IMAGE_IO_IMAGE_FLAGS, io->pool);
254     }
255 }
256
257 int
258 image_io_read_data_finish(struct image_io_read_data_internals *rdi, struct image_io *io)
259 {
260   DBG("image_io_read_data_finish()");
261   if (rdi->need_transformations)
262     {
263       /* Scale the image */
264       if (io->cols != rdi->image->cols || io->rows != rdi->image->rows)
265         {
266           DBG("Scaling image");
267           rdi->need_transformations = io->flags != rdi->image->flags;
268           rdi->need_destroy = rdi->need_transformations || !io->pool;
269           struct image *img = image_new(io->thread, io->cols, io->rows, rdi->image->flags, rdi->need_transformations ? NULL : io->pool);
270           if (unlikely(!img))
271             {
272               image_destroy(rdi->image);
273               return 0;
274             }
275           if (unlikely(!image_scale(io->thread, img, rdi->image)))
276             {
277               image_destroy(rdi->image);
278               if (rdi->need_destroy)
279                 image_destroy(img);
280               return 0;
281             }
282           rdi->image = img;
283         }
284
285       /* Merge with background */
286       if ((io->flags ^ rdi->image->flags) & IMAGE_ALPHA)
287         {
288           DBG("Aplying background");
289           rdi->need_transformations = 0;
290           rdi->need_destroy = rdi->need_transformations || !io->pool;
291           struct image *img = image_new(io->thread, io->cols, io->rows, io->flags, rdi->need_transformations ? NULL : io->pool);
292           if (unlikely(!img))
293             {
294               image_destroy(rdi->image);
295               return 0;
296             }
297           if (unlikely(!image_apply_background(io->thread, img, rdi->image, &io->background_color)))
298             {
299               image_destroy(rdi->image);
300               if (rdi->need_destroy)
301                 image_destroy(img);
302               return 0;
303             }
304           rdi->image = img;
305         }
306
307       ASSERT(!rdi->need_transformations);
308     }
309
310   /* Success */
311   io->image = rdi->image;
312   io->image_destroy = rdi->need_destroy;
313   return 1;
314 }
315
316 void
317 image_io_read_data_break(struct image_io_read_data_internals *rdi, struct image_io *io UNUSED)
318 {
319   DBG("image_io_read_data_break()");
320   if (rdi->need_destroy)
321     image_destroy(rdi->image);
322 }