]> mj.ucw.cz Git - libucw.git/blob - images/io-main.c
common parts of all lib*_read_data moved to io-main.h
[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 #undef 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)
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)
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 }