Skip to content

Commit

Permalink
fix: importing via magick++ for version >= 7.0.1
Browse files Browse the repository at this point in the history
There was an undefined behavior due to multiple increment
of same variable in multiple function arguments.

fix #3331
  • Loading branch information
Your Name authored and rodolforg committed Feb 12, 2024
1 parent 4348d65 commit 9cda2ed
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions synfig-core/src/modules/mod_magickpp/mptr_magickpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,33 +128,43 @@ magickpp_mptr::get_frame(synfig::Surface& surface, const synfig::RendDesc& /*ren
const auto height = image.size().height();
surface.set_wh(width, height);

image.type(Magick::TrueColorType);
// Sadly Magick++ API is a mess with namespaces...
{
using namespace Magick;
using namespace MagickCore;
#if MagickLibVersion >= 0x701 // MAGICKCORE_CHECK_VERSION(7,0,0) does not work!
const bool has_alpha = image.alpha();
#else
const bool has_alpha = image.matte();
#endif

if (has_alpha)
#if MagickLibVersion >= 0x701 // MAGICKCORE_CHECK_VERSION(7,0,0) does not work!
bool has_alpha = image.alpha();
image.type(Magick::TrueColorAlphaType);
#else
bool has_alpha = image.matte();
image.type(Magick::TrueColorMatteType);
#endif
else
image.type(Magick::TrueColorType);

const auto packet = image.getConstPixels(0, 0, width, height);

if (!packet) {
synfig::error(_("Magick++ importer: couldn't get pixel packet"));
return false;
}
// Sadly Magick++ API is a mess with namespaces...
{
using namespace Magick;
using namespace MagickCore;


constexpr synfig::Color::value_type factor = QuantumRange;

for (size_t y = 0, i = 0; y < height; ++y) {
for (size_t x = 0; x < width; ++x, ++i) {
#if MagickLibVersion >= 0x701 // MAGICKCORE_CHECK_VERSION(7,0,0) does not work!
if (has_alpha)
surface[y][x] = synfig::Color(packet[i++] / factor, packet[i++] / factor, packet[i++] / factor, packet[i] / factor);
surface[y][x] = synfig::Color(packet[i] / factor, packet[i+1] / factor, packet[i+2] / factor, packet[i+3] / factor);
else
surface[y][x] = synfig::Color(packet[i++] / factor, packet[i++] / factor, packet[i] / factor, 1.);
surface[y][x] = synfig::Color(packet[i] / factor, packet[i+1] / factor, packet[i+2] / factor, 1.);
i += has_alpha ? 3 : 2;
#else
const auto& color = packet[i];
if (has_alpha)
Expand Down

0 comments on commit 9cda2ed

Please sign in to comment.