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/color.h"
17 #include "images/io-main.h"
18 #include <sys/types.h>
21 #include <magick/api.h>
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)
29 static uns libmagick_counter;
31 struct magick_read_data {
32 ExceptionInfo exception;
38 libmagick_init(struct image_io *io UNUSED)
41 if (!libmagick_counter++)
42 InitializeMagick(NULL);
47 libmagick_cleanup(struct image_io *io UNUSED)
50 if (!--libmagick_counter)
55 libmagick_destroy_read_data(struct magick_read_data *rd)
58 DestroyImage(rd->image);
59 DestroyImageInfo(rd->info);
60 DestroyExceptionInfo(&rd->exception);
64 libmagick_read_cancel(struct image_io *io)
66 DBG("libmagick_read_cancel()");
68 struct magick_read_data *rd = io->read_data;
69 libmagick_destroy_read_data(rd);
73 libmagick_read_header(struct image_io *io)
75 DBG("libmagick_read_header()");
77 /* Read entire stream */
78 sh_off_t file_size = bfilesize(io->fastbuf) - btell(io->fastbuf);
79 if (unlikely(file_size > MAX_FILE_SIZE))
81 image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Too long stream.");
84 uns buf_size = file_size;
85 byte *buf = xmalloc(buf_size);
86 breadb(io->fastbuf, buf, buf_size);
88 /* Allocate read structure */
89 struct magick_read_data *rd = io->read_data = mp_alloc_zero(io->internal_pool, sizeof(*rd));
91 /* Initialize GraphicsMagick */
92 GetExceptionInfo(&rd->exception);
93 rd->info = CloneImageInfo(NULL);
94 rd->info->subrange = 1;
97 rd->image = BlobToImage(rd->info, buf, buf_size, &rd->exception);
99 if (unlikely(!rd->image))
101 image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "GraphicsMagick failed to read the image.");
104 if (unlikely(rd->image->columns > IMAGE_MAX_SIZE || rd->image->rows > IMAGE_MAX_SIZE))
106 image_thread_err(io->thread, IMAGE_ERR_INVALID_DIMENSIONS, "Image too large.");
110 /* Fill image parameters */
111 io->cols = rd->image->columns;
112 io->rows = rd->image->rows;
113 switch (rd->image->colorspace)
116 io->flags = COLOR_SPACE_GRAYSCALE;
119 io->flags = COLOR_SPACE_RGB;
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;
128 io->read_cancel = libmagick_read_cancel;
132 libmagick_destroy_read_data(rd);
137 libmagick_pixel_to_gray(PixelPacket *pixel)
139 return rgb_to_gray_func(pixel->red, pixel->green, pixel->blue) >> QUANTUM_SCALE;
143 libmagick_read_data(struct image_io *io)
145 DBG("libmagick_read_data()");
147 struct magick_read_data *rd = io->read_data;
150 switch (rd->image->colorspace)
156 QuantizeInfo quantize;
157 GetQuantizeInfo(&quantize);
158 quantize.colorspace = RGBColorspace;
159 QuantizeImage(&quantize, rd->image);
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)))
170 libmagick_destroy_read_data(rd);
175 PixelPacket *src = (PixelPacket *)AcquireImagePixels(rd->image, 0, 0, rd->image->columns, rd->image->rows, &rd->exception);
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);
185 switch (rdi.image->pixel_size)
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); \
196 # include "images/image-walk.h"
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); \
209 # include "images/image-walk.h"
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); \
223 # include "images/image-walk.h"
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); \
238 # include "images/image-walk.h"
245 /* Free GraphicsMagick structures */
246 libmagick_destroy_read_data(rd);
248 /* Finish the image */
249 return image_io_read_data_finish(&rdi, io);
253 libmagick_write(struct image_io *io)
255 DBG("libmagick_write()");
257 /* Initialize GraphicsMagick */
259 ExceptionInfo exception;
261 GetExceptionInfo(&exception);
262 info = CloneImageInfo(NULL);
264 /* Setup image parameters and allocate the image*/
265 struct image *img = io->image;
266 switch (img->flags & IMAGE_COLOR_SPACE)
268 case COLOR_SPACE_GRAYSCALE:
269 info->colorspace = GRAYColorspace;
271 case COLOR_SPACE_RGB:
272 info->colorspace = RGBColorspace;
279 case IMAGE_FORMAT_JPEG:
280 strcpy(info->magick, "JPEG");
281 if (io->jpeg_quality)
282 info->quality = MIN(io->jpeg_quality, 100);
284 case IMAGE_FORMAT_PNG:
285 strcpy(info->magick, "PNG");
287 case IMAGE_FORMAT_GIF:
288 strcpy(info->magick, "GIF");
293 Image *image = AllocateImage(info);
294 if (unlikely(!image))
296 image_thread_err(io->thread, IMAGE_ERR_WRITE_FAILED, "GraphicsMagick failed to allocate the image.");
299 image->columns = img->cols;
300 image->rows = img->rows;
303 PixelPacket *pixels = SetImagePixels(image, 0, 0, img->cols, img->rows), *dest = pixels;
304 if (unlikely(!pixels))
306 image_thread_err(io->thread, IMAGE_ERR_WRITE_FAILED, "Cannot get GraphicsMagick pixels.");
311 switch (img->pixel_size)
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; \
325 # include "images/image-walk.h"
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]); \
340 # include "images/image-walk.h"
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; \
355 # include "images/image-walk.h"
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]); \
370 # include "images/image-walk.h"
378 if (unlikely(!SyncImagePixels(image)))
380 image_thread_err(io->thread, IMAGE_ERR_WRITE_FAILED, "Cannot sync GraphicsMagick pixels.");
386 void *buf = ImageToBlob(info, image, &buf_len, &exception);
389 image_thread_err(io->thread, IMAGE_ERR_WRITE_FAILED, "GraphicsMagick failed to compress the image.");
392 if (unlikely(buf_len > MAX_FILE_SIZE))
394 image_thread_err(io->thread, IMAGE_ERR_WRITE_FAILED, "Image too large.");
398 /* Write to stream */
399 bwrite(io->fastbuf, buf, buf_len);
407 DestroyImageInfo(info);
408 DestroyExceptionInfo(&exception);