summaryrefslogtreecommitdiff
blob: 1c938fadd87047af8bfbcf030b1ba4cfd3d2ff26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
From 5863f4aae667bce9242fe63f1afbd5b16ee23f1e Mon Sep 17 00:00:00 2001
From: Manuel Reimer <manuel.reimer@gmx.de>
Date: Sun, 17 Jul 2022 19:19:31 +0200
Subject: [PATCH] Update ImageMagick library name

ImageMagick++.pc was deleted back in 2012
https://github.com/ImageMagick/ImageMagick/commit/ae2846b1c8c016382febb54ddea432f1aba57296
---
 glcdgraphics/Makefile | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/glcdgraphics/Makefile b/glcdgraphics/Makefile
index 9d74477..cfb50de 100644
--- a/glcdgraphics/Makefile
+++ b/glcdgraphics/Makefile
@@ -49,8 +49,8 @@ endif
 # two ifdef/endif are used because older installations may not support 'else ifdef'
 ifeq ($(IMAGELIB), imagemagick)
 	DEFINES += -DHAVE_IMAGEMAGICK
-	INCLUDES += $(shell pkg-config --cflags ImageMagick++)
-	LIBS += $(shell pkg-config --libs ImageMagick++)
+	INCLUDES += $(shell pkg-config --cflags Magick++)
+	LIBS += $(shell pkg-config --libs Magick++)
 endif
 ifeq ($(IMAGELIB), graphicsmagick)
 	DEFINES += -DHAVE_IMAGEMAGICK # yep, really HAVE_IMAGEMAGICK here
From 93108089cc05ffabc18d81ebc97d39feb7f2c56e Mon Sep 17 00:00:00 2001
From: Manuel Reimer <manuel.reimer@gmx.de>
Date: Sun, 24 Jul 2022 19:20:30 +0200
Subject: [PATCH] Add ImageMagick 7 support. Fixes #3

---
 glcdgraphics/Makefile     |  13 ++-
 glcdgraphics/extformats.c | 186 ++++++++++++++++++--------------------
 2 files changed, 95 insertions(+), 104 deletions(-)

diff --git a/glcdgraphics/Makefile b/glcdgraphics/Makefile
index cfb50de..92e2847 100644
--- a/glcdgraphics/Makefile
+++ b/glcdgraphics/Makefile
@@ -12,6 +12,7 @@ ifdef HAVE_GRAPHICSMAGICK
 endif
 ifdef HAVE_IMAGEMAGICK
 	IMAGELIB = imagemagick
+	HAVE_IMAGEMAGICK_7 = $(shell pkg-config --atleast-version=7.0.0 MagickWand && echo true)
 endif
 
 
@@ -49,13 +50,17 @@ endif
 # two ifdef/endif are used because older installations may not support 'else ifdef'
 ifeq ($(IMAGELIB), imagemagick)
 	DEFINES += -DHAVE_IMAGEMAGICK
-	INCLUDES += $(shell pkg-config --cflags Magick++)
-	LIBS += $(shell pkg-config --libs Magick++)
+	INCLUDES += $(shell pkg-config --cflags MagickWand)
+	LIBS += $(shell pkg-config --libs MagickWand)
+	ifeq ($(HAVE_IMAGEMAGICK_7), true)
+		DEFINES += -DHAVE_IMAGEMAGICK_7
+	endif
 endif
+
 ifeq ($(IMAGELIB), graphicsmagick)
 	DEFINES += -DHAVE_IMAGEMAGICK # yep, really HAVE_IMAGEMAGICK here
-	INCLUDES += $(shell pkg-config --cflags GraphicsMagick++)
-	LIBS += $(shell pkg-config --libs GraphicsMagick++)
+	INCLUDES += $(shell pkg-config --cflags GraphicsMagickWand)
+	LIBS += $(shell pkg-config --libs GraphicsMagickWand)
 endif
 
 
diff --git a/glcdgraphics/extformats.c b/glcdgraphics/extformats.c
index 5734fce..90441e6 100644
--- a/glcdgraphics/extformats.c
+++ b/glcdgraphics/extformats.c
@@ -21,11 +21,10 @@
 #include "extformats.h"
 #include "image.h"
 
-#ifdef HAVE_IMAGEMAGICK
-#include <Magick++.h>
-//#elif defined(HAVE_IMLIB2)
-//#include "quantize.h"
-//#include <Imlib2.h>
+#ifdef HAVE_IMAGEMAGICK_7
+  #include <MagickWand/MagickWand.h>
+#elifdef HAVE_IMAGEMAGICK
+  #include <wand/magick_wand.h>
 #endif
 
 
@@ -37,9 +36,11 @@ using namespace std;
 
 cExtFormatFile::cExtFormatFile()
 {
-#ifdef HAVE_IMAGEMAGICK
-  Magick::InitializeMagick(NULL);
-#endif    
+#ifdef HAVE_IMAGEMAGICK_7
+  MagickWandGenesis();
+#elifdef HAVE_IMAGEMAGICK
+  InitializeMagick(NULL);
+#endif
 }
 
 cExtFormatFile::~cExtFormatFile()
@@ -56,118 +57,103 @@ bool cExtFormatFile::Load(cImage & image, const string & fileName)
 bool cExtFormatFile::LoadScaled(cImage & image, const string & fileName, uint16_t & scalew, uint16_t & scaleh)
 {
 #ifdef HAVE_IMAGEMAGICK
-  std::vector<Magick::Image> extimages;
-  try {
-    uint16_t width = 0;
-    uint16_t height = 0;
-    //uint16_t count;
-    uint32_t delay;
-
-    std::vector<Magick::Image>::iterator it;
-    readImages(&extimages, fileName);
-    if (extimages.size() == 0) {
-      syslog(LOG_ERR, "glcdgraphics: Couldn't load '%s' (cExtFormatFile::LoadScaled)", fileName.c_str());
-      return false;
-    }
+  MagickWand* mw = NewMagickWand();
 
-    delay = (uint32_t)(extimages[0].animationDelay() * 10);
+  uint16_t width = 0;
+  uint16_t height = 0;
+  uint32_t delay;
+
+  if (MagickReadImage(mw, fileName.c_str()) == MagickFalse) {
+    syslog(LOG_ERR, "glcdgraphics: Couldn't load '%s' (cExtFormatFile::LoadScaled)", fileName.c_str());
+    return false;
+  }
 
-    image.Clear();
-    image.SetDelay(delay);
+  delay = (uint32_t)(MagickGetImageDelay(mw) * 10);
 
-    bool firstImage = true;
+  image.Clear();
+  image.SetDelay(delay);
 
-    for (it = extimages.begin(); it != extimages.end(); ++it) {
-      bool ignoreImage = false;
+  for (unsigned long imageindex = 0; imageindex < MagickGetNumberImages(mw); imageindex++) {
 
-      //(*it).quantizeColorSpace( Magick::RGBColorspace );
-      //(*it).quantizeColors( 256*256*256 /*colors*/ );
-      //(*it).quantize();
+#ifdef HAVE_IMAGEMAGICK_7
+    MagickSetIteratorIndex(mw, imageindex);
+#else
+    MagickSetImageIndex(mw, imageindex);
+#endif
 
-      if (firstImage) {
-        width = (uint16_t)((*it).columns());
-        height = (uint16_t)((*it).rows());
-        firstImage = false;
+    bool ignoreImage = false;
 
-        // one out of scalew/h == 0 ? -> auto aspect ratio
-        if (scalew && ! scaleh) {
-          scaleh = (uint16_t)( ((uint32_t)scalew * (uint32_t)height) / (uint32_t)width );
-        } else if (!scalew && scaleh) {
-          scalew = (uint16_t)( ((uint32_t)scaleh * (uint32_t)width) / (uint32_t)height );
-        }
+    if (imageindex == 0) { // If first image
+      width = (uint16_t)MagickGetImageWidth(mw);
+      height = (uint16_t)MagickGetImageHeight(mw);
 
-        // scale image
-        if (scalew && ! (scalew == width && scaleh == height)) {
-          (*it).sample(Magick::Geometry(scalew, scaleh));
-          width = scalew;
-          height = scaleh;
-        } else {
-          // not scaled => reset to 0
-          scalew = 0;
-          scaleh = 0;
-        }
+      // one out of scalew/h == 0 ? -> auto aspect ratio
+      if (scalew && ! scaleh) {
+        scaleh = (uint16_t)( ((uint32_t)scalew * (uint32_t)height) / (uint32_t)width );
+      } else if (!scalew && scaleh) {
+        scalew = (uint16_t)( ((uint32_t)scaleh * (uint32_t)width) / (uint32_t)height );
+      }
 
-        image.SetWidth(width);
-        image.SetHeight(height);
+      // scale image
+      if (scalew && ! (scalew == width && scaleh == height)) {
+        MagickSampleImage(mw, scalew, scaleh);
+        width = scalew;
+        height = scaleh;
       } else {
-        if (scalew && scaleh) {
-          (*it).sample(Magick::Geometry(scalew, scaleh));
-        } else 
-        if ( (width != (uint16_t)((*it).columns())) || (height != (uint16_t)((*it).rows())) ) {
-          ignoreImage = true;
-        }
+        // not scaled => reset to 0
+        scalew = 0;
+        scaleh = 0;
       }
 
-      if (! ignoreImage) {
-        /*
-        if ((*it).depth() > 8) {
-          esyslog("ERROR: text2skin: More than 8bpp images are not supported");
-          return false;
-        }
-        */
-        uint32_t * bmpdata = new uint32_t[height * width];
-        //Dprintf("this image has %d colors\n", (*it).totalColors());
+      image.SetWidth(width);
+      image.SetHeight(height);
+    } else {
+      if (scalew && scaleh) {
+        MagickSampleImage(mw, scalew, scaleh);
+      } else 
+      if ( (width != (uint16_t)MagickGetImageWidth(mw)) || (height != (uint16_t)MagickGetImageHeight(mw)) ) {
+        ignoreImage = true;
+      }
+    }
 
-        bool isMatte = (*it).matte();
-        //bool isMonochrome = ((*it).totalColors() <= 2) ? true : false;
-        const Magick::PixelPacket *pix = (*it).getConstPixels(0, 0, (int)width, (int)height);
+    if (! ignoreImage) {
+      uint32_t * bmpdata = new uint32_t[height * width];
+
+#ifdef HAVE_IMAGEMAGICK_7
+      unsigned int status = MagickExportImagePixels(mw, 0, 0, width, height, "BGRA", CharPixel, (unsigned char*)bmpdata);
+#else
+      unsigned int status = MagickGetImagePixels(mw, 0, 0, width, height, "BGRA", CharPixel, (unsigned char*)bmpdata);
+#endif
+
+      if (status == MagickFalse) {
+        syslog(LOG_ERR, "glcdgraphics: Couldn't load '%s' (cExtFormatFile::LoadScaled): MagickGetImagePixels", fileName.c_str());
+        return false;
+      }
 
+#ifdef HAVE_IMAGEMAGICK_7
+      bool isMatte = (MagickGetImageAlphaChannel(mw) == MagickTrue);
+#else
+      bool isMatte = (MagickGetImageMatte(mw) == MagickTrue);
+#endif
+
+      // Give all transparent pixels our defined transparent color
+      if (isMatte) {
         for (int iy = 0; iy < (int)height; ++iy) {
           for (int ix = 0; ix < (int)width; ++ix) {
-            if ( isMatte && Magick::Color::scaleQuantumToDouble(pix->opacity) * 255 == 255 ) {
-                bmpdata[iy*width+ix] = cColor::Transparent;
-            } else {
-                bmpdata[iy*width+ix] = (uint32_t)(
-                                        (uint32_t(255 - (Magick::Color::scaleQuantumToDouble(pix->opacity) * 255)) << 24)  |
-                                        (uint32_t( Magick::Color::scaleQuantumToDouble(pix->red) * 255) << 16) |
-                                        (uint32_t( Magick::Color::scaleQuantumToDouble(pix->green) * 255) << 8) |
-                                         uint32_t( Magick::Color::scaleQuantumToDouble(pix->blue) * 255)
-                                       );
-                //if ( isMonochrome ) {  // if is monochrome: exchange black and white
-                //    uint32_t c = bmpdata[iy*width+ix];
-                //    switch(c) {
-                //        case cColor::White: c = cColor::Black; break;
-                //        case cColor::Black: c = cColor::White; break;
-                //    }
-                //    bmpdata[iy*width+ix] =  c;
-                //}
-            }
-            ++pix;
+            uint32_t* pixel = &bmpdata[ix+iy*width];
+            uint8_t alpha = *pixel >> 24;
+            if (alpha == 0)
+              *pixel = cColor::Transparent;
           }
         }
-        cBitmap * b = new cBitmap(width, height, bmpdata);
-        //b->SetMonochrome(isMonochrome);
-        image.AddBitmap(b);
-        delete[] bmpdata;
-        bmpdata = NULL;
       }
+
+      cBitmap * b = new cBitmap(width, height, bmpdata);
+      //b->SetMonochrome(isMonochrome);
+      image.AddBitmap(b);
+      delete[] bmpdata;
+      bmpdata = NULL;
     }
-  } catch (Magick::Exception &e) {
-    syslog(LOG_ERR, "glcdgraphics: Couldn't load '%s': %s (cExtFormatFile::LoadScaled)", fileName.c_str(), e.what());
-    return false;
-  } catch (...) {
-    syslog(LOG_ERR, "glcdgraphics: Couldn't load '%s': Unknown exception caught (cExtFormatFile::LoadScaled)", fileName.c_str());
-    return false;
   }
   return true;
 #else
From ad6496557a817a57cb29f2247ffaeaaa71168c72 Mon Sep 17 00:00:00 2001
From: Manuel Reimer <manuel.reimer@gmx.de>
Date: Sun, 24 Jul 2022 19:39:59 +0200
Subject: [PATCH] Add cleanup for MagickWand

---
 glcdgraphics/extformats.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/glcdgraphics/extformats.c b/glcdgraphics/extformats.c
index 90441e6..79d14da 100644
--- a/glcdgraphics/extformats.c
+++ b/glcdgraphics/extformats.c
@@ -65,6 +65,7 @@ bool cExtFormatFile::LoadScaled(cImage & image, const string & fileName, uint16_
 
   if (MagickReadImage(mw, fileName.c_str()) == MagickFalse) {
     syslog(LOG_ERR, "glcdgraphics: Couldn't load '%s' (cExtFormatFile::LoadScaled)", fileName.c_str());
+    DestroyMagickWand(mw);
     return false;
   }
 
@@ -127,6 +128,7 @@ bool cExtFormatFile::LoadScaled(cImage & image, const string & fileName, uint16_
 
       if (status == MagickFalse) {
         syslog(LOG_ERR, "glcdgraphics: Couldn't load '%s' (cExtFormatFile::LoadScaled): MagickGetImagePixels", fileName.c_str());
+        DestroyMagickWand(mw);
         return false;
       }
 
@@ -155,6 +157,7 @@ bool cExtFormatFile::LoadScaled(cImage & image, const string & fileName, uint16_
       bmpdata = NULL;
     }
   }
+  DestroyMagickWand(mw);
   return true;
 #else
   return false;