2 * Image Library -- Image compression/decompression interface
4 * (c) 2006 Pavel Charvat <pchar@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
13 #include "images/images.h"
17 image_io_init(struct image_thread *it, struct image_io *io)
19 DBG("image_io_init()");
20 bzero(io, sizeof(*io));
22 io->internal_pool = mp_new(1024);
26 image_io_read_cancel(struct image_io *io)
31 io->read_cancel = NULL;
36 image_io_image_destroy(struct image_io *io)
38 if (io->image_destroy)
40 image_destroy(io->thread, io->image);
41 io->image_destroy = 0;
47 image_io_cleanup(struct image_io *io)
49 DBG("image_io_cleanup()");
50 image_io_read_cancel(io);
51 image_io_image_destroy(io);
52 mp_delete(io->internal_pool);
56 image_io_reset(struct image_io *io)
58 DBG("image_io_reset()");
59 image_io_read_cancel(io);
60 image_io_image_destroy(io);
61 struct mempool *pool = io->internal_pool;
63 bzero(io, sizeof(*io));
64 io->internal_pool = pool;
68 image_io_read_header(struct image_io *io)
70 DBG("image_io_read_header()");
71 image_io_read_cancel(io);
72 image_io_image_destroy(io);
74 case IMAGE_FORMAT_JPEG:
75 #if defined(CONFIG_LIBJPEG)
76 return libjpeg_read_header(io);
77 #elif defined(CONFIG_LIBMAGICK)
78 return libmagick_read_header(io);
82 case IMAGE_FORMAT_PNG:
83 #if defined(CONFIG_LIBPNG)
84 return libpng_read_header(io);
85 #elif defined(CONFIG_LIBMAGICK)
86 return libmagick_read_header(io);
90 case IMAGE_FORMAT_GIF:
91 #if defined(CONFIG_LIBUNGIG)
92 return libungif_read_header(io);
93 #elif defined(CONFIG_LIBMAGICK)
94 return libmagick_read_header(io);
98 case IMAGE_FORMAT_UNDEFINED:
105 image_thread_err(io->thread, IMAGE_ERR_INVALID_FILE_FORMAT, "Image format not supported.");
110 image_io_read_data(struct image_io *io, int ref)
112 DBG("image_io_read_data()");
113 ASSERT(io->read_cancel);
114 io->read_cancel = NULL;
116 switch (io->format) {
117 case IMAGE_FORMAT_JPEG:
118 #if defined(CONFIG_LIBJPEG)
119 result = libjpeg_read_data(io);
120 #elif defined(CONFIG_LIBMAGICK)
121 result = libmagick_read_data(io);
127 case IMAGE_FORMAT_PNG:
128 #if defined(CONFIG_LIBPNG)
129 result = libpng_read_data(io);
130 #elif defined(CONFIG_LIBMAGICK)
131 result = libmagick_read_data(io);
137 case IMAGE_FORMAT_GIF:
138 #if defined(CONFIG_LIBMAGICK)
139 result = libmagick_read_data(io);
151 io->image_destroy = 0;
159 image_io_read(struct image_io *io, int ref)
161 if (!image_io_read_header(io))
163 return image_io_read_data(io, ref);
167 image_io_write(struct image_io *io)
169 DBG("image_io_write()");
170 image_io_read_cancel(io);
171 switch (io->format) {
172 case IMAGE_FORMAT_JPEG:
173 #if defined(CONFIG_LIBJPEG)
174 return libjpeg_write(io);
175 #elif defined(CONFIG_LIBMAGICK)
176 return libmagick_write(io);
180 case IMAGE_FORMAT_PNG:
181 #if defined(CONFIG_LIBMAGICK)
182 return libmagick_write(io);
186 case IMAGE_FORMAT_GIF:
187 #if defined(CONFIG_LIBMAGICK)
188 return libmagick_write(io);
195 image_thread_err(io->thread, IMAGE_ERR_INVALID_FILE_FORMAT, "Image format not supported.");
200 image_format_to_extension(enum image_format format)
204 case IMAGE_FORMAT_JPEG:
206 case IMAGE_FORMAT_PNG:
208 case IMAGE_FORMAT_GIF:
216 image_extension_to_format(byte *extension)
218 if (!strcasecmp(extension, "jpg"))
219 return IMAGE_FORMAT_JPEG;
220 if (!strcasecmp(extension, "jpeg"))
221 return IMAGE_FORMAT_JPEG;
222 if (!strcasecmp(extension, "png"))
223 return IMAGE_FORMAT_PNG;
224 if (!strcasecmp(extension, "gif"))
225 return IMAGE_FORMAT_GIF;
226 return IMAGE_FORMAT_UNDEFINED;
230 image_file_name_to_format(byte *file_name)
232 byte *extension = strrchr(file_name, '.');
233 return extension ? image_extension_to_format(extension + 1) : IMAGE_FORMAT_UNDEFINED;