OpenShot Library | libopenshot  0.4.0
MagickUtilities.cpp
Go to the documentation of this file.
1 
8 // Copyright (c) 2008-2021 OpenShot Studios, LLC
9 //
10 // SPDX-License-Identifier: LGPL-3.0-or-later
11 
12 #ifdef USE_IMAGEMAGICK
13 
14 #include "MagickUtilities.h"
15 #include "QtUtilities.h"
16 
17 #include <QImage>
18 
19 // Get pointer to Magick::Image conversion of a QImage
20 std::shared_ptr<Magick::Image>
21 openshot::QImage2Magick(std::shared_ptr<QImage> image)
22 {
23  // Check for blank image
24  if (!image || image->isNull())
25  return nullptr;
26 
27  // Get the pixels from the frame image
28  const QRgb *tmpBits = (const QRgb*)image->constBits();
29 
30  // Create new image object, and fill with pixel data
31  auto magick_image = std::make_shared<Magick::Image>(
32  image->width(), image->height(),
33  "RGBA", Magick::CharPixel, tmpBits);
34 
35  // Give image a transparent background color
36  magick_image->backgroundColor(Magick::Color("none"));
37  magick_image->virtualPixelMethod(
38  Magick::TransparentVirtualPixelMethod);
39  MAGICK_IMAGE_ALPHA(magick_image, true);
40 
41  return magick_image;
42 }
43 
44 // Get pointer to QImage conversion of a Magick::Image
45 std::shared_ptr<QImage>
46 openshot::Magick2QImage(std::shared_ptr<Magick::Image> image)
47 {
48  if (!image)
49  return nullptr;
50 
51  const int BPP = 4;
52  const std::size_t size = image->columns() * image->rows() * BPP;
53 
54  auto* qbuffer = new unsigned char[size]();
55 
56  MagickCore::ExceptionInfo exception;
57  // TODO: Actually do something, if we get an exception here
58  MagickCore::ExportImagePixels(
59  image->constImage(), 0, 0,
60  image->columns(), image->rows(),
61  "RGBA", Magick::CharPixel,
62  qbuffer, &exception);
63 
64  auto qimage = std::make_shared<QImage>(
65  qbuffer, image->columns(), image->rows(),
66  image->columns() * BPP,
67  QImage::Format_RGBA8888_Premultiplied,
68  (QImageCleanupFunction) &openshot::cleanUpBuffer,
69  (void*) qbuffer);
70  return qimage;
71 }
72 
73 #endif // USE_IMAGEMAGICK
QtUtilities.h
Header file for QtUtilities (compatibiity overlay)
openshot::QImage2Magick
std::shared_ptr< Magick::Image > QImage2Magick(std::shared_ptr< QImage >)
Convert QImage to Magick::Image.
Definition: MagickUtilities.cpp:21
openshot::Magick2QImage
std::shared_ptr< QImage > Magick2QImage(std::shared_ptr< Magick::Image >)
Convert Magick::Image to QImage.
Definition: MagickUtilities.cpp:46
MAGICK_IMAGE_ALPHA
#define MAGICK_IMAGE_ALPHA(im, a)
Definition: MagickUtilities.h:44
MagickUtilities.h
Header file for MagickUtilities (IM6/IM7 compatibility overlay)