]> mj.ucw.cz Git - libucw.git/blob - images/io-libmagick.c
d75f0a8938e7168856a5a73721ba507cf404a696
[libucw.git] / images / io-libmagick.c
1 /*
2  *      Image Library -- GraphicsMagick (slow fallback library)
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 <sys/types.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <magick/api.h>
22
23 #define MAX_FILE_SIZE (1 << 30)
24 #define QUANTUM_SCALE (QuantumDepth - 8)
25 #define QUANTUM_TO_BYTE(x) ((uns)(x) >> QUANTUM_SCALE)
26 #define BYTE_TO_QUANTUM(x) ((uns)(x) << QUANTUM_SCALE)
27 #define OPACITY_MAX ((1 << QuantumDepth) - 1)
28
29 static uns libmagick_counter;
30
31 struct magick_read_data {
32   ExceptionInfo exception;
33   ImageInfo *info;
34   Image *image;
35 };
36
37 int
38 libmagick_init(struct image_io *io UNUSED)
39 {
40   // FIXME: lock
41   if (!libmagick_counter++)
42     InitializeMagick(NULL);
43   return 1;
44 }
45
46 void
47 libmagick_cleanup(struct image_io *io UNUSED)
48 {
49   // FIXME: lock
50   if (!--libmagick_counter)
51     DestroyMagick();
52 }
53
54 static void
55 libmagick_destroy_read_data(struct magick_read_data *rd)
56 {
57   if (rd->image)
58     DestroyImage(rd->image);
59   DestroyImageInfo(rd->info);
60   DestroyExceptionInfo(&rd->exception);
61 }
62
63 static void
64 libmagick_read_cancel(struct image_io *io)
65 {
66   DBG("libmagick_read_cancel()");
67
68   struct magick_read_data *rd = io->read_data;
69   libmagick_destroy_read_data(rd);
70 }
71
72 int
73 libmagick_read_header(struct image_io *io)
74 {
75   DBG("libmagick_read_header()");
76
77   /* Read entire stream */
78   sh_off_t file_size = bfilesize(io->fastbuf) - btell(io->fastbuf);
79   if (unlikely(file_size > MAX_FILE_SIZE))
80     {
81       image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Too long stream.");
82       return 0;
83     }
84   uns buf_size = file_size;
85   byte *buf = xmalloc(buf_size);
86   breadb(io->fastbuf, buf, buf_size);
87
88   /* Allocate read structure */
89   struct magick_read_data *rd = io->read_data = mp_alloc_zero(io->internal_pool, sizeof(*rd));
90
91   /* Initialize GraphicsMagick */
92   GetExceptionInfo(&rd->exception);
93   rd->info = CloneImageInfo(NULL);
94   rd->info->subrange = 1;
95
96   /* Read the image */
97   rd->image = BlobToImage(rd->info, buf, buf_size, &rd->exception);
98   xfree(buf);
99   if (unlikely(!rd->image))
100     {
101       image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "GraphicsMagick failed to read the image.");
102       goto err;
103     }
104   if (unlikely(rd->image->columns > IMAGE_MAX_SIZE || rd->image->rows > IMAGE_MAX_SIZE))
105     {
106       image_thread_err(io->thread, IMAGE_ERR_INVALID_DIMENSIONS, "Image too large.");
107       goto err;
108     }
109
110   /* Fill image parameters */
111   io->cols = rd->image->columns;
112   io->rows = rd->image->rows;
113   switch (rd->image->colorspace)
114     {
115       case GRAYColorspace:
116         io->flags = COLOR_SPACE_GRAYSCALE;
117         break;
118       default:
119         io->flags = COLOR_SPACE_RGB;
120         break;
121     }
122   if (rd->image->matte)
123     io->flags |= IMAGE_ALPHA;
124   io->number_of_colors = rd->image->colors;
125   if (rd->image->storage_class == PseudoClass && rd->image->compression != JPEGCompression)
126     io->flags |= IMAGE_IO_HAS_PALETTE;
127
128   io->read_cancel = libmagick_read_cancel;
129   return 1;
130
131 err:
132   libmagick_destroy_read_data(rd);
133   return 0;
134 }
135
136 static inline byte
137 libmagick_pixel_to_gray(PixelPacket *pixel)
138 {
139   return rgb_to_gray_func(pixel->red, pixel->green, pixel->blue) >> QUANTUM_SCALE;
140 }
141
142 int
143 libmagick_read_data(struct image_io *io)
144 {
145   DBG("libmagick_read_data()");
146
147   struct magick_read_data *rd = io->read_data;
148
149   /* Quantize image */
150   switch (rd->image->colorspace)
151     {
152       case RGBColorspace:
153       case GRAYColorspace:
154         break;
155       default: ;
156         QuantizeInfo quantize;
157         GetQuantizeInfo(&quantize);
158         quantize.colorspace = RGBColorspace;
159         QuantizeImage(&quantize, rd->image);
160         break;
161     }
162
163   /* Prepare the image */
164   struct image_io_read_data_internals rdi;
165   uns read_flags = io->flags;
166   if ((read_flags & IMAGE_IO_USE_BACKGROUND) && !(read_flags & IMAGE_ALPHA))
167     read_flags = (read_flags | IMAGE_ALPHA) & IMAGE_CHANNELS_FORMAT;
168   if (unlikely(!image_io_read_data_prepare(&rdi, io, rd->image->columns, rd->image->rows, read_flags)))
169     {
170       libmagick_destroy_read_data(rd);
171       return 0;
172     }
173
174   /* Acquire pixels */
175   PixelPacket *src = (PixelPacket *)AcquireImagePixels(rd->image, 0, 0, rd->image->columns, rd->image->rows, &rd->exception);
176   if (unlikely(!src))
177     {
178       image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Cannot acquire image pixels.");
179       libmagick_destroy_read_data(rd);
180       image_io_read_data_break(&rdi, io);
181       return 0;
182     }
183
184   /* Convert pixels */
185   switch (rdi.image->pixel_size)
186     {
187       case 1:
188 #       define IMAGE_WALK_PREFIX(x) walk_##x
189 #       define IMAGE_WALK_INLINE
190 #       define IMAGE_WALK_IMAGE (rdi.image)
191 #       define IMAGE_WALK_UNROLL 4
192 #       define IMAGE_WALK_COL_STEP 1
193 #       define IMAGE_WALK_DO_STEP do{ \
194           walk_pos[0] = libmagick_pixel_to_gray(src); \
195           src++; }while(0)
196 #       include "images/image-walk.h"
197         break;
198
199       case 2:
200 #       define IMAGE_WALK_PREFIX(x) walk_##x
201 #       define IMAGE_WALK_INLINE
202 #       define IMAGE_WALK_IMAGE (rdi.image)
203 #       define IMAGE_WALK_UNROLL 4
204 #       define IMAGE_WALK_COL_STEP 2
205 #       define IMAGE_WALK_DO_STEP do{ \
206           walk_pos[0] = libmagick_pixel_to_gray(src); \
207           walk_pos[1] = QUANTUM_TO_BYTE(src->opacity); \
208           src++; }while(0)
209 #       include "images/image-walk.h"
210         break;
211
212       case 3:
213 #       define IMAGE_WALK_PREFIX(x) walk_##x
214 #       define IMAGE_WALK_INLINE
215 #       define IMAGE_WALK_IMAGE (rdi.image)
216 #       define IMAGE_WALK_UNROLL 4
217 #       define IMAGE_WALK_COL_STEP 3
218 #       define IMAGE_WALK_DO_STEP do{ \
219           walk_pos[0] = QUANTUM_TO_BYTE(src->red); \
220           walk_pos[1] = QUANTUM_TO_BYTE(src->green); \
221           walk_pos[2] = QUANTUM_TO_BYTE(src->blue); \
222           src++; }while(0)
223 #       include "images/image-walk.h"
224         break;
225
226       case 4:
227 #       define IMAGE_WALK_PREFIX(x) walk_##x
228 #       define IMAGE_WALK_INLINE
229 #       define IMAGE_WALK_IMAGE (rdi.image)
230 #       define IMAGE_WALK_UNROLL 4
231 #       define IMAGE_WALK_COL_STEP 4
232 #       define IMAGE_WALK_DO_STEP do{ \
233           walk_pos[0] = QUANTUM_TO_BYTE(src->red); \
234           walk_pos[1] = QUANTUM_TO_BYTE(src->green); \
235           walk_pos[2] = QUANTUM_TO_BYTE(src->blue); \
236           walk_pos[3] = QUANTUM_TO_BYTE(src->opacity); \
237           src++; }while(0)
238 #       include "images/image-walk.h"
239         break;
240
241       default:
242         ASSERT(0);
243     }
244
245   /* Free GraphicsMagick structures */
246   libmagick_destroy_read_data(rd);
247
248   /* Finish the image */
249   return image_io_read_data_finish(&rdi, io);
250 }
251
252 int
253 libmagick_write(struct image_io *io)
254 {
255   DBG("libmagick_write()");
256
257   /* Initialize GraphicsMagick */
258   int result = 0;
259   ExceptionInfo exception;
260   ImageInfo *info;
261   GetExceptionInfo(&exception);
262   info = CloneImageInfo(NULL);
263
264   /* Setup image parameters and allocate the image*/
265   struct image *img = io->image;
266   switch (img->flags & IMAGE_COLOR_SPACE)
267     {
268       case COLOR_SPACE_GRAYSCALE:
269         info->colorspace = GRAYColorspace;
270         break;
271       case COLOR_SPACE_RGB:
272         info->colorspace = RGBColorspace;
273         break;
274       default:
275         ASSERT(0);
276     }
277   switch (io->format)
278     {
279       case IMAGE_FORMAT_JPEG:
280         strcpy(info->magick, "JPEG");
281         if (io->jpeg_quality)
282           info->quality = MIN(io->jpeg_quality, 100);
283         break;
284       case IMAGE_FORMAT_PNG:
285         strcpy(info->magick, "PNG");
286         break;
287       case IMAGE_FORMAT_GIF:
288         strcpy(info->magick, "GIF");
289         break;
290       default:
291         ASSERT(0);
292     }
293   Image *image = AllocateImage(info);
294   if (unlikely(!image))
295     {
296       image_thread_err(io->thread, IMAGE_ERR_WRITE_FAILED, "GraphicsMagick failed to allocate the image.");
297       goto err;
298     }
299   image->columns = img->cols;
300   image->rows = img->rows;
301
302   /* Get pixels */
303   PixelPacket *pixels = SetImagePixels(image, 0, 0, img->cols, img->rows), *dest = pixels;
304   if (unlikely(!pixels))
305     {
306       image_thread_err(io->thread, IMAGE_ERR_WRITE_FAILED, "Cannot get GraphicsMagick pixels.");
307       goto err2;
308     }
309
310   /* Convert pixels */
311   switch (img->pixel_size)
312     {
313       case 1:
314 #       define IMAGE_WALK_PREFIX(x) walk_##x
315 #       define IMAGE_WALK_INLINE
316 #       define IMAGE_WALK_IMAGE img
317 #       define IMAGE_WALK_UNROLL 4
318 #       define IMAGE_WALK_COL_STEP 1
319 #       define IMAGE_WALK_DO_STEP do{ \
320           dest->red = BYTE_TO_QUANTUM(walk_pos[0]); \
321           dest->green = BYTE_TO_QUANTUM(walk_pos[0]); \
322           dest->blue = BYTE_TO_QUANTUM(walk_pos[0]); \
323           dest->opacity = OPACITY_MAX; \
324           dest++; }while(0)
325 #       include "images/image-walk.h"
326         break;
327
328       case 2:
329 #       define IMAGE_WALK_PREFIX(x) walk_##x
330 #       define IMAGE_WALK_INLINE
331 #       define IMAGE_WALK_IMAGE img
332 #       define IMAGE_WALK_UNROLL 4
333 #       define IMAGE_WALK_COL_STEP 2
334 #       define IMAGE_WALK_DO_STEP do{ \
335           dest->red = BYTE_TO_QUANTUM(walk_pos[0]); \
336           dest->green = BYTE_TO_QUANTUM(walk_pos[0]); \
337           dest->blue = BYTE_TO_QUANTUM(walk_pos[0]); \
338           dest->opacity = BYTE_TO_QUANTUM(walk_pos[1]); \
339           dest++; }while(0)
340 #       include "images/image-walk.h"
341         break;
342
343       case 3:
344 #       define IMAGE_WALK_PREFIX(x) walk_##x
345 #       define IMAGE_WALK_INLINE
346 #       define IMAGE_WALK_IMAGE img
347 #       define IMAGE_WALK_UNROLL 4
348 #       define IMAGE_WALK_COL_STEP 3
349 #       define IMAGE_WALK_DO_STEP do{ \
350           dest->red = BYTE_TO_QUANTUM(walk_pos[0]); \
351           dest->green = BYTE_TO_QUANTUM(walk_pos[1]); \
352           dest->blue = BYTE_TO_QUANTUM(walk_pos[2]); \
353           dest->opacity = OPACITY_MAX; \
354           dest++; }while(0)
355 #       include "images/image-walk.h"
356         break;
357
358       case 4:
359 #       define IMAGE_WALK_PREFIX(x) walk_##x
360 #       define IMAGE_WALK_INLINE
361 #       define IMAGE_WALK_IMAGE img
362 #       define IMAGE_WALK_UNROLL 4
363 #       define IMAGE_WALK_COL_STEP 4
364 #       define IMAGE_WALK_DO_STEP do{ \
365           dest->red = BYTE_TO_QUANTUM(walk_pos[0]); \
366           dest->green = BYTE_TO_QUANTUM(walk_pos[1]); \
367           dest->blue = BYTE_TO_QUANTUM(walk_pos[2]); \
368           dest->opacity = BYTE_TO_QUANTUM(walk_pos[3]); \
369           dest++; }while(0)
370 #       include "images/image-walk.h"
371         break;
372
373       default:
374         ASSERT(0);
375     }
376
377   /* Store pixels */
378   if (unlikely(!SyncImagePixels(image)))
379     {
380       image_thread_err(io->thread, IMAGE_ERR_WRITE_FAILED, "Cannot sync GraphicsMagick pixels.");
381       goto err2;
382     }
383
384   /* Write image */
385   size_t buf_len = 0;
386   void *buf = ImageToBlob(info, image, &buf_len, &exception);
387   if (unlikely(!buf))
388     {
389       image_thread_err(io->thread, IMAGE_ERR_WRITE_FAILED, "GraphicsMagick failed to compress the image.");
390       goto err2;
391     }
392   if (unlikely(buf_len > MAX_FILE_SIZE))
393     {
394       image_thread_err(io->thread, IMAGE_ERR_WRITE_FAILED, "Image too large.");
395       goto err2;
396     }
397
398   /* Write to stream */
399   bwrite(io->fastbuf, buf, buf_len);
400
401   /* Success */
402   result = 1;
403
404 err2:
405   DestroyImage(image);
406 err:
407   DestroyImageInfo(info);
408   DestroyExceptionInfo(&exception);
409   return result;
410 }