Skip to content

Commit a47d5b5

Browse files
ferdnycjonoomph
authored andcommitted
Add backwards-compatible Imagemagick 7 support (#252)
* Add ImageMagick 7 compatibility A new header, `imclude/MagickUtilities.h`, is created to hold the compatibility `#define`s. The image-conversion code in `src/Frame.cpp` received the only major changes — instead of doing the export by hand (and having to account for changes in the underlying API), it uses the `MagickCore::ExportImagePixels()` function which does basically the same work, but accounts for all of the API changes for us. The API of that function is _unchanged_ from IM6 to IM7. TODO: `MagickCore::ExportImagePixels()` will return an `exception` struct if it encounters any problems. Currently the code ignores that, which it should not. * Add ImageMagick 7 compatibility A new header, `imclude/MagickUtilities.h`, is created to hold the compatibility `#define`s. The image-conversion code in `src/Frame.cpp` received the only major changes — instead of doing the export by hand (and having to account for changes in the underlying API), it uses the `MagickCore::ExportImagePixels()` function which does basically the same work, but accounts for all of the API changes for us. The API of that function is _unchanged_ from IM6 to IM7. TODO: `MagickCore::ExportImagePixels()` will return an `exception` struct if it encounters any problems. Currently the code ignores that, which it should not. Thanks @ferdnyc
1 parent faa3a34 commit a47d5b5

File tree

11 files changed

+113
-37
lines changed

11 files changed

+113
-37
lines changed

include/Frame.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@
5353
#include <memory>
5454
#include <unistd.h>
5555
#include "ZmqLogger.h"
56-
#ifdef USE_IMAGEMAGICK
57-
#include "Magick++.h"
58-
#endif
5956
#include "ChannelLayouts.h"
6057
#include "AudioBufferSource.h"
6158
#include "AudioResampler.h"
6259
#include "Fraction.h"
6360
#include "JuceHeader.h"
61+
#ifdef USE_IMAGEMAGICK
62+
#include "MagickUtilities.h"
63+
#endif
6464

6565
#pragma SWIG nowarn=362
6666
using namespace std;

include/ImageReader.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
#ifndef OPENSHOT_IMAGE_READER_H
2929
#define OPENSHOT_IMAGE_READER_H
3030

31+
// Require ImageMagick support
32+
#ifdef USE_IMAGEMAGICK
33+
3134
#include "ReaderBase.h"
3235

3336
#include <cmath>
@@ -36,9 +39,9 @@
3639
#include <omp.h>
3740
#include <stdio.h>
3841
#include <memory>
39-
#include "Magick++.h"
4042
#include "CacheMemory.h"
4143
#include "Exceptions.h"
44+
#include "MagickUtilities.h"
4245

4346
using namespace std;
4447

@@ -113,4 +116,5 @@ namespace openshot
113116

114117
}
115118

116-
#endif
119+
#endif //USE_IMAGEMAGICK
120+
#endif //OPENSHOT_IMAGE_READER_H

include/ImageWriter.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@
3232
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
3333
*/
3434

35-
3635
#ifndef OPENSHOT_IMAGE_WRITER_H
3736
#define OPENSHOT_IMAGE_WRITER_H
3837

38+
#ifdef USE_IMAGEMAGICK
39+
3940
#include "ReaderBase.h"
4041
#include "WriterBase.h"
4142

@@ -44,11 +45,10 @@
4445
#include <iostream>
4546
#include <stdio.h>
4647
#include <unistd.h>
47-
#include "Magick++.h"
4848
#include "CacheMemory.h"
4949
#include "Exceptions.h"
5050
#include "OpenMPUtilities.h"
51-
51+
#include "MagickUtilities.h"
5252

5353
using namespace std;
5454

@@ -145,4 +145,5 @@ namespace openshot
145145

146146
}
147147

148-
#endif
148+
#endif //USE_IMAGEMAGICK
149+
#endif //OPENSHOT_IMAGE_WRITER_H

include/MagickUtilities.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @file
3+
* @brief Header file for MagickUtilities (IM6/IM7 compatibility overlay)
4+
* @author Jonathan Thomas <[email protected]>
5+
* @author FeRD (Frank Dana) <[email protected]>
6+
*/
7+
8+
/* LICENSE
9+
*
10+
* Copyright (c) 2008-2019 OpenShot Studios, LLC
11+
* <http://www.openshotstudios.com/>. This file is part of
12+
* OpenShot Library (libopenshot), an open-source project dedicated to
13+
* delivering high quality video editing and animation solutions to the
14+
* world. For more information visit <http://www.openshot.org/>.
15+
*
16+
* OpenShot Library (libopenshot) is free software: you can redistribute it
17+
* and/or modify it under the terms of the GNU Lesser General Public License
18+
* as published by the Free Software Foundation, either version 3 of the
19+
* License, or (at your option) any later version.
20+
*
21+
* OpenShot Library (libopenshot) is distributed in the hope that it will be
22+
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
23+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24+
* GNU Lesser General Public License for more details.
25+
*
26+
* You should have received a copy of the GNU Lesser General Public License
27+
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
28+
*/
29+
30+
#ifndef OPENSHOT_MAGICK_UTILITIES_H
31+
#define OPENSHOT_MAGICK_UTILITIES_H
32+
33+
#ifdef USE_IMAGEMAGICK
34+
35+
#include "Magick++.h"
36+
37+
// Determine ImageMagick version, as IM7 isn't fully
38+
// backwards compatible
39+
#ifndef NEW_MAGICK
40+
#define NEW_MAGICK (MagickLibVersion >= 0x700)
41+
#endif
42+
43+
// IM7: <Magick::Image>->alpha(bool)
44+
// IM6: <Magick::Image>->matte(bool)
45+
#if NEW_MAGICK
46+
#define MAGICK_IMAGE_ALPHA(im, a) im->alpha((a))
47+
#else
48+
#define MAGICK_IMAGE_ALPHA(im, a) im->matte((a))
49+
#endif
50+
51+
// IM7: vector<Magick::Drawable>
52+
// IM6: list<Magick::Drawable>
53+
// (both have the push_back() method which is all we use)
54+
#if NEW_MAGICK
55+
#define MAGICK_DRAWABLE vector<Magick::Drawable>
56+
#else
57+
#define MAGICK_DRAWABLE list<Magick::Drawable>
58+
#endif
59+
60+
#endif
61+
#endif

include/TextReader.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
#ifndef OPENSHOT_TEXT_READER_H
2929
#define OPENSHOT_TEXT_READER_H
3030

31+
// Require ImageMagick support
32+
#ifdef USE_IMAGEMAGICK
33+
3134
#include "ReaderBase.h"
3235

3336
#include <cmath>
@@ -36,10 +39,10 @@
3639
#include <omp.h>
3740
#include <stdio.h>
3841
#include <memory>
39-
#include "Magick++.h"
4042
#include "CacheMemory.h"
4143
#include "Enums.h"
4244
#include "Exceptions.h"
45+
#include "MagickUtilities.h"
4346

4447
using namespace std;
4548

@@ -91,7 +94,7 @@ namespace openshot
9194
string text_color;
9295
string background_color;
9396
std::shared_ptr<Magick::Image> image;
94-
list<Magick::Drawable> lines;
97+
MAGICK_DRAWABLE lines;
9598
bool is_open;
9699
GravityType gravity;
97100

@@ -144,4 +147,5 @@ namespace openshot
144147

145148
}
146149

147-
#endif
150+
#endif //USE_IMAGEMAGICK
151+
#endif //OPENSHOT_TEXT_READER_H

include/effects/Mask.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
2626
*/
2727

28-
#ifndef OPENSHOT_WIPE_EFFECT_H
29-
#define OPENSHOT_WIPE_EFFECT_H
28+
#ifndef OPENSHOT_MASK_EFFECT_H
29+
#define OPENSHOT_MASK_EFFECT_H
3030

3131
#include "../EffectBase.h"
3232

@@ -45,6 +45,7 @@
4545
#include "../QtImageReader.h"
4646
#include "../ChunkReader.h"
4747
#ifdef USE_IMAGEMAGICK
48+
#include "../MagickUtilities.h"
4849
#include "../ImageReader.h"
4950
#endif
5051

@@ -54,7 +55,7 @@ namespace openshot
5455
{
5556

5657
/**
57-
* @brief This class uses the ImageMagick++ libraries, to apply alpha (or transparency) masks
58+
* @brief This class uses the image libraries to apply alpha (or transparency) masks
5859
* to any frame. It can also be animated, and used as a powerful Wipe transition.
5960
*
6061
* These masks / wipes can also be combined, such as a transparency mask on top of a clip, which

src/Frame.cpp

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ std::shared_ptr<Magick::Image> Frame::GetMagickImage()
926926
// Give image a transparent background color
927927
magick_image->backgroundColor(Magick::Color("none"));
928928
magick_image->virtualPixelMethod(Magick::TransparentVirtualPixelMethod);
929-
magick_image->matte(true);
929+
MAGICK_IMAGE_ALPHA(magick_image, true);
930930

931931
return magick_image;
932932
}
@@ -945,20 +945,12 @@ void Frame::AddMagickImage(std::shared_ptr<Magick::Image> new_image)
945945
qbuffer = new unsigned char[bufferSize]();
946946
unsigned char *buffer = (unsigned char*)qbuffer;
947947

948-
// Iterate through the pixel packets, and load our own buffer
949-
// Each color needs to be scaled to 8 bit (using the ImageMagick built-in ScaleQuantumToChar function)
950-
int numcopied = 0;
951-
Magick::PixelPacket *pixels = new_image->getPixels(0,0, new_image->columns(), new_image->rows());
952-
for (int n = 0, i = 0; n < new_image->columns() * new_image->rows(); n += 1, i += 4) {
953-
buffer[i+0] = MagickCore::ScaleQuantumToChar((Magick::Quantum) pixels[n].red);
954-
buffer[i+1] = MagickCore::ScaleQuantumToChar((Magick::Quantum) pixels[n].green);
955-
buffer[i+2] = MagickCore::ScaleQuantumToChar((Magick::Quantum) pixels[n].blue);
956-
buffer[i+3] = 255 - MagickCore::ScaleQuantumToChar((Magick::Quantum) pixels[n].opacity);
957-
numcopied+=4;
958-
}
959-
960-
// Create QImage of frame data
961-
image = std::shared_ptr<QImage>(new QImage(qbuffer, width, height, width * BPP, QImage::Format_RGBA8888, (QImageCleanupFunction) &cleanUpBuffer, (void*) qbuffer));
948+
MagickCore::ExceptionInfo exception;
949+
// TODO: Actually do something, if we get an exception here
950+
MagickCore::ExportImagePixels(new_image->constImage(), 0, 0, new_image->columns(), new_image->rows(), "RGBA", Magick::CharPixel, buffer, &exception);
951+
952+
// Create QImage of frame data
953+
image = std::shared_ptr<QImage>(new QImage(qbuffer, width, height, width * BPP, QImage::Format_RGBA8888, (QImageCleanupFunction) &cleanUpBuffer, (void*) qbuffer));
962954

963955
// Update height and width
964956
width = image->width();

src/ImageReader.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
2626
*/
2727

28+
// Require ImageMagick support
29+
#ifdef USE_IMAGEMAGICK
30+
2831
#include "../include/ImageReader.h"
2932

3033
using namespace openshot;
@@ -59,7 +62,7 @@ void ImageReader::Open()
5962

6063
// Give image a transparent background color
6164
image->backgroundColor(Magick::Color("none"));
62-
image->matte(true);
65+
MAGICK_IMAGE_ALPHA(image, true);
6366
}
6467
catch (Magick::Exception e) {
6568
// raise exception
@@ -192,3 +195,5 @@ void ImageReader::SetJsonValue(Json::Value root) {
192195
Open();
193196
}
194197
}
198+
199+
#endif //USE_IMAGEMAGICK

src/ImageWriter.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
2929
*/
3030

31+
//Require ImageMagick support
32+
#ifdef USE_IMAGEMAGICK
33+
3134
#include "../include/ImageWriter.h"
3235

3336
using namespace openshot;
@@ -97,7 +100,7 @@ void ImageWriter::WriteFrame(std::shared_ptr<Frame> frame)
97100
std::shared_ptr<Magick::Image> frame_image = frame->GetMagickImage();
98101
frame_image->magick( info.vcodec );
99102
frame_image->backgroundColor(Magick::Color("none"));
100-
frame_image->matte(true);
103+
MAGICK_IMAGE_ALPHA(frame_image, true);
101104
frame_image->quality(image_quality);
102105
frame_image->animationDelay(info.video_timebase.ToFloat() * 100);
103106
frame_image->animationIterations(number_of_loops);
@@ -153,3 +156,4 @@ void ImageWriter::Close()
153156
ZmqLogger::Instance()->AppendDebugMethod("ImageWriter::Close", "", -1, "", -1, "", -1, "", -1, "", -1, "", -1);
154157
}
155158

159+
#endif //USE_IMAGEMAGICK

src/TextReader.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
2626
*/
2727

28+
// Require ImageMagick support
29+
#ifdef USE_IMAGEMAGICK
30+
2831
#include "../include/TextReader.h"
2932

3033
using namespace openshot;
@@ -258,3 +261,5 @@ void TextReader::SetJsonValue(Json::Value root) {
258261
Open();
259262
}
260263
}
264+
265+
#endif //USE_IMAGEMAGICK

0 commit comments

Comments
 (0)