1 /* load_magick.c - load image file using ImageMagick
3 * Raster graphics library
5 * Copyright (c) 2014-2021 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,
27 #include <wand/magick_wand.h>
29 #include <MagickWand/MagickWand.h>
34 #include "imgformat.h"
38 static int RInitMagickIfNeeded(void);
41 RImage
*RLoadMagick(const char *file_name
)
46 MagickWand
*m_wand
= NULL
;
47 MagickBooleanType mrc
;
48 MagickBooleanType hasAlfa
;
49 PixelWand
*bg_wand
= NULL
;
51 if (RInitMagickIfNeeded()) {
52 RErrorCode
= RERR_BADFORMAT
;
57 m_wand
= NewMagickWand();
59 /* set the default background as transparent */
60 bg_wand
= NewPixelWand();
61 PixelSetColor(bg_wand
, "none");
62 MagickSetBackgroundColor(m_wand
, bg_wand
);
64 /* Read the input image */
65 if (!MagickReadImage(m_wand
, file_name
)) {
66 RErrorCode
= RERR_BADIMAGEFILE
;
70 w
= MagickGetImageWidth(m_wand
);
71 h
= MagickGetImageHeight(m_wand
);
73 hasAlfa
= MagickGetImageAlphaChannel(m_wand
);
75 image
= RCreateImage(w
, h
, (unsigned int) hasAlfa
);
77 RErrorCode
= RERR_NOMEMORY
;
82 if (hasAlfa
== MagickFalse
)
83 mrc
= MagickExportImagePixels(m_wand
, 0, 0, (size_t)w
, (size_t)h
, "RGB", CharPixel
, ptr
);
85 mrc
= MagickExportImagePixels(m_wand
, 0, 0, (size_t)w
, (size_t)h
, "RGBA", CharPixel
, ptr
);
87 if (mrc
== MagickFalse
) {
88 RErrorCode
= RERR_BADIMAGEFILE
;
96 DestroyPixelWand(bg_wand
);
97 MagickClearException(m_wand
);
98 DestroyMagickWand(m_wand
);
103 /* Track the state of the library in memory */
110 * Initialise MagickWand, but only if it was not already done
112 * Return ok(0) when MagickWand is usable and fail(!0) if not usable
114 static int RInitMagickIfNeeded(void)
116 if (magick_state
== MW_NotReady
) {
118 magick_state
= MW_Ready
;
124 void RReleaseMagick(void)
126 if (magick_state
== MW_Ready
) {
127 MagickWandTerminus();
128 magick_state
= MW_NotReady
;