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,
25 #include <wand/MagickWand.h>
28 #include "imgformat.h"
31 static int RInitMagickIfNeeded(void);
34 RImage
*RLoadMagick(const char *file_name
)
39 MagickWand
*m_wand
= NULL
;
40 MagickBooleanType mrc
;
41 MagickBooleanType hasAlfa
;
42 PixelWand
*bg_wand
= NULL
;
44 if (RInitMagickIfNeeded()) {
45 RErrorCode
= RERR_BADFORMAT
;
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
;
63 w
= MagickGetImageWidth(m_wand
);
64 h
= MagickGetImageHeight(m_wand
);
66 hasAlfa
= MagickGetImageAlphaChannel(m_wand
);
68 image
= RCreateImage(w
, h
, (unsigned int) hasAlfa
);
70 RErrorCode
= RERR_NOMEMORY
;
75 if (hasAlfa
== MagickFalse
)
76 mrc
= MagickExportImagePixels(m_wand
, 0, 0, (size_t)w
, (size_t)h
, "RGB", CharPixel
, ptr
);
78 mrc
= MagickExportImagePixels(m_wand
, 0, 0, (size_t)w
, (size_t)h
, "RGBA", CharPixel
, ptr
);
80 if (mrc
== MagickFalse
) {
81 RErrorCode
= RERR_BADIMAGEFILE
;
89 DestroyPixelWand(bg_wand
);
90 MagickClearException(m_wand
);
91 DestroyMagickWand(m_wand
);
96 /* Track the state of the library in memory */
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
) {
111 magick_state
= MW_Ready
;
117 void RReleaseMagick(void)
119 if (magick_state
== MW_Ready
) {
120 MagickWandTerminus();
121 magick_state
= MW_NotReady
;