wrlib: return NULL if XImage could not be taken, for consistency
[wmaker-crm.git] / wrlib / load_magick.c
blob3e809ba27d30bcdf7f7afb119dd23e48193fb479
1 /* load_magick.c - load image file using ImageMagick
3 * Raster graphics library
5 * Copyright (c) 2014 Window Maker Team
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
23 #include "config.h"
25 #include <wand/MagickWand.h>
27 #include "wraster.h"
28 #include "imgformat.h"
31 static int RInitMagickIfNeeded(void);
34 RImage *RLoadMagick(const char *file_name)
36 RImage *image = NULL;
37 unsigned char *ptr;
38 unsigned long w,h;
39 MagickWand *m_wand = NULL;
40 MagickBooleanType mrc;
41 MagickBooleanType hasAlfa;
42 PixelWand *bg_wand = NULL;
44 if (RInitMagickIfNeeded()) {
45 RErrorCode = RERR_BADFORMAT;
46 return NULL;
49 /* Create a wand */
50 m_wand = NewMagickWand();
52 /* set the default background as transparent */
53 bg_wand = NewPixelWand();
54 PixelSetColor(bg_wand, "none");
55 MagickSetBackgroundColor(m_wand, bg_wand);
57 /* Read the input image */
58 if (!MagickReadImage(m_wand, file_name)) {
59 RErrorCode = RERR_BADIMAGEFILE;
60 goto bye;
63 w = MagickGetImageWidth(m_wand);
64 h = MagickGetImageHeight(m_wand);
66 hasAlfa = MagickGetImageAlphaChannel(m_wand);
68 image = RCreateImage(w, h, (unsigned int) hasAlfa);
69 if (!image) {
70 RErrorCode = RERR_NOMEMORY;
71 goto bye;
74 ptr = image->data;
75 if (hasAlfa == MagickFalse)
76 mrc = MagickExportImagePixels(m_wand, 0, 0, (size_t)w, (size_t)h, "RGB", CharPixel, ptr);
77 else
78 mrc = MagickExportImagePixels(m_wand, 0, 0, (size_t)w, (size_t)h, "RGBA", CharPixel, ptr);
80 if (mrc == MagickFalse) {
81 RErrorCode = RERR_BADIMAGEFILE;
82 RReleaseImage(image);
83 image = NULL;
84 goto bye;
87 bye:
88 /* Tidy up */
89 DestroyPixelWand(bg_wand);
90 MagickClearException(m_wand);
91 DestroyMagickWand(m_wand);
93 return image;
96 /* Track the state of the library in memory */
97 static enum {
98 MW_NotReady,
99 MW_Ready
100 } magick_state;
103 * Initialise MagickWand, but only if it was not already done
105 * Return ok(0) when MagickWand is usable and fail(!0) if not usable
107 static int RInitMagickIfNeeded(void)
109 if (magick_state == MW_NotReady) {
110 MagickWandGenesis();
111 magick_state = MW_Ready;
114 return 0;
117 void RReleaseMagick(void)
119 if (magick_state == MW_Ready) {
120 MagickWandTerminus();
121 magick_state = MW_NotReady;