Include screen.h in dialog.h for definition of WScreen
[wmaker-crm.git] / wrlib / load_magick.c
blobd7ff694147a1565cda3f22f4716c8d8c2c2b5365
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 #ifdef USE_MAGICK
26 #if USE_MAGICK < 7
27 #include <wand/magick_wand.h>
28 #else
29 #include <MagickWand/MagickWand.h>
30 #endif
31 #endif
33 #include "wraster.h"
34 #include "imgformat.h"
35 #include "wr_i18n.h"
38 static int RInitMagickIfNeeded(void);
41 RImage *RLoadMagick(const char *file_name)
43 RImage *image = NULL;
44 unsigned char *ptr;
45 unsigned long w,h;
46 MagickWand *m_wand = NULL;
47 MagickBooleanType mrc;
48 MagickBooleanType hasAlfa;
49 PixelWand *bg_wand = NULL;
51 if (RInitMagickIfNeeded()) {
52 RErrorCode = RERR_BADFORMAT;
53 return NULL;
56 /* Create a wand */
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;
67 goto bye;
70 w = MagickGetImageWidth(m_wand);
71 h = MagickGetImageHeight(m_wand);
73 hasAlfa = MagickGetImageAlphaChannel(m_wand);
75 image = RCreateImage(w, h, (unsigned int) hasAlfa);
76 if (!image) {
77 RErrorCode = RERR_NOMEMORY;
78 goto bye;
81 ptr = image->data;
82 if (hasAlfa == MagickFalse)
83 mrc = MagickExportImagePixels(m_wand, 0, 0, (size_t)w, (size_t)h, "RGB", CharPixel, ptr);
84 else
85 mrc = MagickExportImagePixels(m_wand, 0, 0, (size_t)w, (size_t)h, "RGBA", CharPixel, ptr);
87 if (mrc == MagickFalse) {
88 RErrorCode = RERR_BADIMAGEFILE;
89 RReleaseImage(image);
90 image = NULL;
91 goto bye;
94 bye:
95 /* Tidy up */
96 DestroyPixelWand(bg_wand);
97 MagickClearException(m_wand);
98 DestroyMagickWand(m_wand);
100 return image;
103 /* Track the state of the library in memory */
104 static enum {
105 MW_NotReady,
106 MW_Ready
107 } magick_state;
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) {
117 MagickWandGenesis();
118 magick_state = MW_Ready;
121 return 0;
124 void RReleaseMagick(void)
126 if (magick_state == MW_Ready) {
127 MagickWandTerminus();
128 magick_state = MW_NotReady;