2 * Image Library -- GraphicsMagick (slow fallback library)
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 "lib/mempool.h"
14 #include "lib/fastbuf.h"
15 #include "images/images.h"
16 #include "images/error.h"
17 #include "images/color.h"
18 #include "images/io-main.h"
20 #include <sys/types.h>
23 #include <magick/api.h>
26 #define MAX_FILE_SIZE (1 << 30)
27 #define QUANTUM_SCALE (QuantumDepth - 8)
28 #define QUANTUM_TO_BYTE(x) ((uns)(x) >> QUANTUM_SCALE)
29 #define BYTE_TO_QUANTUM(x) ((uns)(x) << QUANTUM_SCALE)
30 #define ALPHA_TO_BYTE(x) (255 - QUANTUM_TO_BYTE(x))
31 #define BYTE_TO_ALPHA(x) (BYTE_TO_QUANTUM(255 - (x)))
33 static pthread_mutex_t libmagick_mutex = PTHREAD_MUTEX_INITIALIZER;
34 static uns libmagick_counter;
36 struct magick_read_data {
37 ExceptionInfo exception;
43 libmagick_init(struct image_io *io UNUSED)
45 pthread_mutex_lock(&libmagick_mutex);
46 if (!libmagick_counter++)
47 InitializeMagick(NULL);
48 pthread_mutex_unlock(&libmagick_mutex);
53 libmagick_cleanup(struct image_io *io UNUSED)
55 pthread_mutex_lock(&libmagick_mutex);
56 if (!--libmagick_counter)
58 pthread_mutex_unlock(&libmagick_mutex);
62 libmagick_destroy_read_data(struct magick_read_data *rd)
65 DestroyImage(rd->image);
66 DestroyImageInfo(rd->info);
67 DestroyExceptionInfo(&rd->exception);
71 libmagick_read_cancel(struct image_io *io)
73 DBG("libmagick_read_cancel()");
75 struct magick_read_data *rd = io->read_data;
76 libmagick_destroy_read_data(rd);
80 libmagick_read_header(struct image_io *io)
82 DBG("libmagick_read_header()");
84 /* Read entire stream */
85 sh_off_t file_size = bfilesize(io->fastbuf) - btell(io->fastbuf);
86 if (unlikely(file_size > MAX_FILE_SIZE))
88 IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "Too long stream.");
91 uns buf_size = file_size;
92 byte *buf = xmalloc(buf_size);
93 breadb(io->fastbuf, buf, buf_size);
95 /* Allocate read structure */
96 struct magick_read_data *rd = io->read_data = mp_alloc_zero(io->internal_pool, sizeof(*rd));
98 /* Initialize GraphicsMagick */
99 GetExceptionInfo(&rd->exception);
100 rd->info = CloneImageInfo(NULL);
101 rd->info->subrange = 1;
104 rd->image = BlobToImage(rd->info, buf, buf_size, &rd->exception);
106 if (unlikely(!rd->image))
108 IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "GraphicsMagick failed to read the image.");
111 if (unlikely(rd->image->columns > image_max_dim || rd->image->rows > image_max_dim))
113 IMAGE_ERROR(io->context, IMAGE_ERROR_INVALID_DIMENSIONS, "Image too large.");
117 /* Fill image parameters */
118 io->cols = rd->image->columns;
119 io->rows = rd->image->rows;
120 switch (rd->image->colorspace)
123 io->flags = COLOR_SPACE_GRAYSCALE;
126 io->flags = COLOR_SPACE_RGB;
129 if (rd->image->matte)
130 io->flags |= IMAGE_ALPHA;
131 io->number_of_colors = rd->image->colors;
132 if (rd->image->storage_class == PseudoClass && rd->image->compression != JPEGCompression)
133 io->flags |= IMAGE_IO_HAS_PALETTE;
135 io->read_cancel = libmagick_read_cancel;
139 libmagick_destroy_read_data(rd);
144 libmagick_pixel_to_gray(PixelPacket *pixel)
146 return rgb_to_gray_func(pixel->red, pixel->green, pixel->blue) >> QUANTUM_SCALE;
150 libmagick_read_data(struct image_io *io)
152 DBG("libmagick_read_data()");
154 struct magick_read_data *rd = io->read_data;
157 switch (rd->image->colorspace)
163 QuantizeInfo quantize;
164 GetQuantizeInfo(&quantize);
165 quantize.colorspace = RGBColorspace;
166 QuantizeImage(&quantize, rd->image);
170 /* Prepare the image */
171 struct image_io_read_data_internals rdi;
172 uns read_flags = io->flags;
173 uns cs = read_flags & IMAGE_COLOR_SPACE;
174 if (cs != COLOR_SPACE_GRAYSCALE && cs != COLOR_SPACE_RGB)
175 read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_PIXEL_FORMAT) | COLOR_SPACE_RGB;
176 if ((read_flags & IMAGE_IO_USE_BACKGROUND) && !(read_flags & IMAGE_ALPHA))
177 read_flags = (read_flags & IMAGE_CHANNELS_FORMAT) | IMAGE_ALPHA;
178 if (unlikely(!image_io_read_data_prepare(&rdi, io, rd->image->columns, rd->image->rows, read_flags)))
180 libmagick_destroy_read_data(rd);
185 PixelPacket *src = (PixelPacket *)AcquireImagePixels(rd->image, 0, 0, rd->image->columns, rd->image->rows, &rd->exception);
188 IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "Cannot acquire image pixels.");
189 libmagick_destroy_read_data(rd);
190 image_io_read_data_break(&rdi, io);
195 switch (rdi.image->pixel_size)
198 # define IMAGE_WALK_PREFIX(x) walk_##x
199 # define IMAGE_WALK_INLINE
200 # define IMAGE_WALK_IMAGE (rdi.image)
201 # define IMAGE_WALK_UNROLL 4
202 # define IMAGE_WALK_COL_STEP 1
203 # define IMAGE_WALK_DO_STEP do{ \
204 walk_pos[0] = libmagick_pixel_to_gray(src); \
206 # include "images/image-walk.h"
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_DO_STEP do{ \
216 walk_pos[0] = libmagick_pixel_to_gray(src); \
217 walk_pos[1] = ALPHA_TO_BYTE(src->opacity); \
219 # include "images/image-walk.h"
223 # define IMAGE_WALK_PREFIX(x) walk_##x
224 # define IMAGE_WALK_INLINE
225 # define IMAGE_WALK_IMAGE (rdi.image)
226 # define IMAGE_WALK_UNROLL 4
227 # define IMAGE_WALK_COL_STEP 3
228 # define IMAGE_WALK_DO_STEP do{ \
229 walk_pos[0] = QUANTUM_TO_BYTE(src->red); \
230 walk_pos[1] = QUANTUM_TO_BYTE(src->green); \
231 walk_pos[2] = QUANTUM_TO_BYTE(src->blue); \
233 # include "images/image-walk.h"
237 # define IMAGE_WALK_PREFIX(x) walk_##x
238 # define IMAGE_WALK_INLINE
239 # define IMAGE_WALK_IMAGE (rdi.image)
240 # define IMAGE_WALK_UNROLL 4
241 # define IMAGE_WALK_COL_STEP 4
242 # define IMAGE_WALK_DO_STEP do{ \
243 walk_pos[0] = QUANTUM_TO_BYTE(src->red); \
244 walk_pos[1] = QUANTUM_TO_BYTE(src->green); \
245 walk_pos[2] = QUANTUM_TO_BYTE(src->blue); \
246 walk_pos[3] = ALPHA_TO_BYTE(src->opacity); \
248 # include "images/image-walk.h"
255 /* Free GraphicsMagick structures */
256 libmagick_destroy_read_data(rd);
258 /* Finish the image */
259 return image_io_read_data_finish(&rdi, io);
263 libmagick_write(struct image_io *io)
265 DBG("libmagick_write()");
267 /* Initialize GraphicsMagick */
269 ExceptionInfo exception;
271 GetExceptionInfo(&exception);
272 info = CloneImageInfo(NULL);
274 /* Setup image parameters and allocate the image*/
275 struct image *img = io->image;
276 switch (img->flags & IMAGE_COLOR_SPACE)
278 case COLOR_SPACE_GRAYSCALE:
279 info->colorspace = GRAYColorspace;
281 case COLOR_SPACE_RGB:
282 info->colorspace = RGBColorspace;
285 IMAGE_ERROR(io->context, IMAGE_ERROR_WRITE_FAILED, "Unsupported color space.");
290 case IMAGE_FORMAT_JPEG:
291 strcpy(info->magick, "JPEG");
292 if (io->jpeg_quality)
293 info->quality = MIN(io->jpeg_quality, 100);
295 case IMAGE_FORMAT_PNG:
296 strcpy(info->magick, "PNG");
298 case IMAGE_FORMAT_GIF:
299 strcpy(info->magick, "GIF");
304 Image *image = AllocateImage(info);
305 if (unlikely(!image))
307 IMAGE_ERROR(io->context, IMAGE_ERROR_WRITE_FAILED, "GraphicsMagick failed to allocate the image.");
310 image->columns = img->cols;
311 image->rows = img->rows;
314 PixelPacket *pixels = SetImagePixels(image, 0, 0, img->cols, img->rows), *dest = pixels;
315 if (unlikely(!pixels))
317 IMAGE_ERROR(io->context, IMAGE_ERROR_WRITE_FAILED, "Cannot get GraphicsMagick pixels.");
322 switch (img->pixel_size)
325 # define IMAGE_WALK_PREFIX(x) walk_##x
326 # define IMAGE_WALK_INLINE
327 # define IMAGE_WALK_IMAGE img
328 # define IMAGE_WALK_UNROLL 4
329 # define IMAGE_WALK_COL_STEP 1
330 # define IMAGE_WALK_DO_STEP do{ \
331 dest->red = BYTE_TO_QUANTUM(walk_pos[0]); \
332 dest->green = BYTE_TO_QUANTUM(walk_pos[0]); \
333 dest->blue = BYTE_TO_QUANTUM(walk_pos[0]); \
336 # include "images/image-walk.h"
340 # define IMAGE_WALK_PREFIX(x) walk_##x
341 # define IMAGE_WALK_INLINE
342 # define IMAGE_WALK_IMAGE img
343 # define IMAGE_WALK_UNROLL 4
344 # define IMAGE_WALK_COL_STEP 2
345 # define IMAGE_WALK_DO_STEP do{ \
346 dest->red = BYTE_TO_QUANTUM(walk_pos[0]); \
347 dest->green = BYTE_TO_QUANTUM(walk_pos[0]); \
348 dest->blue = BYTE_TO_QUANTUM(walk_pos[0]); \
349 dest->opacity = BYTE_TO_ALPHA(walk_pos[1]); \
351 # include "images/image-walk.h"
355 # define IMAGE_WALK_PREFIX(x) walk_##x
356 # define IMAGE_WALK_INLINE
357 # define IMAGE_WALK_IMAGE img
358 # define IMAGE_WALK_UNROLL 4
359 # define IMAGE_WALK_COL_STEP 3
360 # define IMAGE_WALK_DO_STEP do{ \
361 dest->red = BYTE_TO_QUANTUM(walk_pos[0]); \
362 dest->green = BYTE_TO_QUANTUM(walk_pos[1]); \
363 dest->blue = BYTE_TO_QUANTUM(walk_pos[2]); \
366 # include "images/image-walk.h"
370 # define IMAGE_WALK_PREFIX(x) walk_##x
371 # define IMAGE_WALK_INLINE
372 # define IMAGE_WALK_IMAGE img
373 # define IMAGE_WALK_UNROLL 4
374 # define IMAGE_WALK_COL_STEP 4
375 # define IMAGE_WALK_DO_STEP do{ \
376 dest->red = BYTE_TO_QUANTUM(walk_pos[0]); \
377 dest->green = BYTE_TO_QUANTUM(walk_pos[1]); \
378 dest->blue = BYTE_TO_QUANTUM(walk_pos[2]); \
379 dest->opacity = BYTE_TO_ALPHA(walk_pos[3]); \
381 # include "images/image-walk.h"
389 if (unlikely(!SyncImagePixels(image)))
391 IMAGE_ERROR(io->context, IMAGE_ERROR_WRITE_FAILED, "Cannot sync GraphicsMagick pixels.");
397 void *buf = ImageToBlob(info, image, &buf_len, &exception);
400 IMAGE_ERROR(io->context, IMAGE_ERROR_WRITE_FAILED, "GraphicsMagick failed to compress the image.");
403 if (unlikely(buf_len > MAX_FILE_SIZE))
405 IMAGE_ERROR(io->context, IMAGE_ERROR_WRITE_FAILED, "Image too large.");
409 /* Write to stream */
410 bwrite(io->fastbuf, buf, buf_len);
418 DestroyImageInfo(info);
419 DestroyExceptionInfo(&exception);