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 <sys/types.h>
19 #include <magick/api.h>
21 #define MAX_FILE_SIZE (1 << 30)
22 #define QUANTUM_SCALE (QuantumDepth - 8)
23 #define QUANTUM_TO_BYTE(x) ((uns)(x) >> QUANTUM_SCALE)
24 #define BYTE_TO_QUANTUM(x) ((uns)(x) << QUANTUM_SCALE)
25 #define OPACITY_MAX ((1 << QuantumDepth) - 1)
27 struct magick_read_data {
28 ExceptionInfo exception;
34 libmagick_destroy_read_data(struct magick_read_data *rd)
37 DestroyImage(rd->image);
38 DestroyImageInfo(rd->info);
39 DestroyExceptionInfo(&rd->exception);
44 libmagick_read_cancel(struct image_io *io)
46 DBG("libmagick_read_cancel()");
48 struct magick_read_data *rd = io->read_data;
50 DestroyImage(rd->image);
51 libmagick_destroy_read_data(rd);
55 libmagick_read_header(struct image_io *io)
57 DBG("libmagick_read_header()");
59 /* Read entire stream */
60 sh_off_t file_size = bfilesize(io->fastbuf) - btell(io->fastbuf);
61 if (unlikely(file_size > MAX_FILE_SIZE))
63 image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Too long stream.");
66 uns buf_size = file_size;
67 byte *buf = xmalloc(buf_size);
68 breadb(io->fastbuf, buf, buf_size);
70 /* Allocate read structure */
71 struct magick_read_data *rd = io->read_data = mp_alloc(io->internal_pool, sizeof(*rd));
73 /* Initialize GraphicsMagick */
74 InitializeMagick(NULL);
75 GetExceptionInfo(&rd->exception);
76 rd->info = CloneImageInfo(NULL);
77 rd->info->subrange = 1;
80 rd->image = BlobToImage(rd->info, buf, buf_size, &rd->exception);
82 if (unlikely(!rd->image))
84 image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "GraphicsMagick failed to read the image.");
87 if (unlikely(rd->image->columns > IMAGE_MAX_SIZE || rd->image->rows > IMAGE_MAX_SIZE))
89 image_thread_err(io->thread, IMAGE_ERR_INVALID_DIMENSIONS, "Image too large.");
93 /* Fill image parameters */
94 io->cols = rd->image->columns;
95 io->rows = rd->image->rows;
96 switch (rd->image->colorspace)
99 io->flags |= COLOR_SPACE_GRAYSCALE | IMAGE_ALPHA;
102 io->flags |= COLOR_SPACE_RGB | IMAGE_ALPHA;
105 io->number_of_colors = rd->image->colors;
106 if (rd->image->storage_class == PseudoClass && rd->image->compression != JPEGCompression)
109 io->read_cancel = libmagick_read_cancel;
113 libmagick_destroy_read_data(rd);
118 libmagick_pixel_to_gray(PixelPacket *pixel)
120 return ((uns)pixel->red * 19660 + (uns)pixel->green * 38666 + (uns)pixel->blue * 7210) >> (16 + QUANTUM_SCALE);
124 libmagick_read_data(struct image_io *io)
126 DBG("libmagick_read_data()");
128 struct magick_read_data *rd = io->read_data;
131 switch (rd->image->colorspace)
137 QuantizeInfo quantize;
138 GetQuantizeInfo(&quantize);
139 quantize.colorspace = RGBColorspace;
140 QuantizeImage(&quantize, rd->image);
144 /* Allocate image for conversion */
145 int need_scale = io->cols != rd->image->columns || io->rows != rd->image->rows;
146 int need_destroy = need_scale || !io->pool;
147 struct image *img = need_scale ?
148 image_new(io->thread, rd->image->columns, rd->image->rows, io->flags & IMAGE_CHANNELS_FORMAT, NULL) :
149 image_new(io->thread, io->cols, io->rows, io->flags, io->pool);
154 PixelPacket *src = (PixelPacket *)AcquireImagePixels(rd->image, 0, 0, rd->image->columns, rd->image->rows, &rd->exception);
157 image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Cannot acquire image pixels.");
162 switch (img->pixel_size)
165 # define IMAGE_WALK_INLINE
166 # define IMAGE_WALK_UNROLL 4
167 # define IMAGE_WALK_COL_STEP 1
168 # define IMAGE_WALK_DO_STEP do{ \
169 pos[0] = libmagick_pixel_to_gray(src); \
171 # include "images/image-walk.h"
175 # define IMAGE_WALK_INLINE
176 # define IMAGE_WALK_UNROLL 4
177 # define IMAGE_WALK_COL_STEP 2
178 # define IMAGE_WALK_DO_STEP do{ \
179 pos[0] = libmagick_pixel_to_gray(src); \
180 pos[1] = QUANTUM_TO_BYTE(src->opacity); \
182 # include "images/image-walk.h"
186 # define IMAGE_WALK_INLINE
187 # define IMAGE_WALK_UNROLL 4
188 # define IMAGE_WALK_COL_STEP 3
189 # define IMAGE_WALK_DO_STEP do{ \
190 pos[0] = QUANTUM_TO_BYTE(src->red); \
191 pos[1] = QUANTUM_TO_BYTE(src->green); \
192 pos[2] = QUANTUM_TO_BYTE(src->blue); \
194 # include "images/image-walk.h"
198 # define IMAGE_WALK_INLINE
199 # define IMAGE_WALK_UNROLL 4
200 # define IMAGE_WALK_COL_STEP 4
201 # define IMAGE_WALK_DO_STEP do{ \
202 pos[0] = QUANTUM_TO_BYTE(src->red); \
203 pos[1] = QUANTUM_TO_BYTE(src->green); \
204 pos[2] = QUANTUM_TO_BYTE(src->blue); \
205 pos[3] = QUANTUM_TO_BYTE(src->opacity); \
207 # include "images/image-walk.h"
214 /* Free GraphicsMagick structures */
215 libmagick_destroy_read_data(rd);
220 struct image *img2 = image_new(io->thread, io->cols, io->rows, io->flags, io->pool);
223 int result = image_scale(io->thread, img2, img);
226 need_destroy = !io->pool;
227 if (unlikely(!result))
233 io->image_destroy = need_destroy;
236 /* Free structures */
238 libmagick_destroy_read_data(rd);
246 libmagick_write(struct image_io *io)
248 DBG("libmagick_write()");
250 /* Initialize GraphicsMagick */
252 ExceptionInfo exception;
254 InitializeMagick(NULL);
255 GetExceptionInfo(&exception);
256 info = CloneImageInfo(NULL);
258 /* Setup image parameters and allocate the image*/
259 struct image *img = io->image;
260 switch (img->flags & IMAGE_COLOR_SPACE)
262 case COLOR_SPACE_GRAYSCALE:
263 info->colorspace = GRAYColorspace;
265 case COLOR_SPACE_RGB:
266 info->colorspace = RGBColorspace;
273 case IMAGE_FORMAT_JPEG:
274 strcpy(info->magick, "JPEG");
275 if (io->jpeg_quality)
276 info->quality = MIN(io->jpeg_quality, 100);
278 case IMAGE_FORMAT_PNG:
279 strcpy(info->magick, "PNG");
281 case IMAGE_FORMAT_GIF:
282 strcpy(info->magick, "GIF");
287 Image *image = AllocateImage(info);
288 if (unlikely(!image))
290 image_thread_err(io->thread, IMAGE_ERR_WRITE_FAILED, "GraphicsMagick failed to allocate the image.");
293 image->columns = img->cols;
294 image->rows = img->rows;
297 PixelPacket *pixels = SetImagePixels(image, 0, 0, img->cols, img->rows), *dest = pixels;
298 if (unlikely(!pixels))
300 image_thread_err(io->thread, IMAGE_ERR_WRITE_FAILED, "Cannot get GraphicsMagick pixels.");
305 switch (img->pixel_size)
308 # define IMAGE_WALK_INLINE
309 # define IMAGE_WALK_UNROLL 4
310 # define IMAGE_WALK_COL_STEP 1
311 # define IMAGE_WALK_DO_STEP do{ \
312 dest->red = BYTE_TO_QUANTUM(pos[0]); \
313 dest->green = BYTE_TO_QUANTUM(pos[0]); \
314 dest->blue = BYTE_TO_QUANTUM(pos[0]); \
315 dest->opacity = OPACITY_MAX; \
317 # include "images/image-walk.h"
321 # define IMAGE_WALK_INLINE
322 # define IMAGE_WALK_UNROLL 4
323 # define IMAGE_WALK_COL_STEP 2
324 # define IMAGE_WALK_DO_STEP do{ \
325 dest->red = BYTE_TO_QUANTUM(pos[0]); \
326 dest->green = BYTE_TO_QUANTUM(pos[0]); \
327 dest->blue = BYTE_TO_QUANTUM(pos[0]); \
328 dest->opacity = BYTE_TO_QUANTUM(pos[1]); \
330 # include "images/image-walk.h"
334 # define IMAGE_WALK_INLINE
335 # define IMAGE_WALK_UNROLL 4
336 # define IMAGE_WALK_COL_STEP 3
337 # define IMAGE_WALK_DO_STEP do{ \
338 dest->red = BYTE_TO_QUANTUM(pos[0]); \
339 dest->green = BYTE_TO_QUANTUM(pos[1]); \
340 dest->blue = BYTE_TO_QUANTUM(pos[2]); \
341 dest->opacity = OPACITY_MAX; \
343 # include "images/image-walk.h"
347 # define IMAGE_WALK_INLINE
348 # define IMAGE_WALK_UNROLL 4
349 # define IMAGE_WALK_COL_STEP 4
350 # define IMAGE_WALK_DO_STEP do{ \
351 dest->red = BYTE_TO_QUANTUM(pos[0]); \
352 dest->green = BYTE_TO_QUANTUM(pos[1]); \
353 dest->blue = BYTE_TO_QUANTUM(pos[2]); \
354 dest->opacity = BYTE_TO_QUANTUM(pos[3]); \
356 # include "images/image-walk.h"
364 if (unlikely(!SyncImagePixels(image)))
366 image_thread_err(io->thread, IMAGE_ERR_WRITE_FAILED, "Cannot sync GraphicsMagick pixels.");
372 void *buf = ImageToBlob(info, image, &buf_len, &exception);
375 image_thread_err(io->thread, IMAGE_ERR_WRITE_FAILED, "GraphicsMagick failed to compress the image.");
378 if (unlikely(buf_len > MAX_FILE_SIZE))
380 image_thread_err(io->thread, IMAGE_ERR_WRITE_FAILED, "Image too large.");
384 /* Write to stream */
385 bwrite(io->fastbuf, buf, buf_len);
393 DestroyImageInfo(info);
394 DestroyExceptionInfo(&exception);