1
votes

I have an image, that has a sequence of two frames. The second frame is supposed to be an alpha mask for the first frame.

Here is an example:

http://i.imgur.com/c2M10u7.png

I've written the following code using Magick++ to split the image into two halves, and apply the alpha mask:

#include "stdafx.h"
#include <iostream>
#include <Magick++.h>

int main(int argc, char **argv)
{
    Magick::InitializeMagick(*argv);

    Magick::Image base, mask;
    std::string image;

    if (argc > 1)
    {
        image = argv[1];
    }
    else
        return EXIT_FAILURE;

    // Read image
    base.read(image);
    mask = base;

    // Crop out mask and sprite
    base.crop(Magick::Geometry(base.columns() / 2, base.rows(), 0, 0));
    mask.crop(Magick::Geometry(mask.columns() / 2, mask.rows(), mask.columns() / 2, 0));

    // Apply mask
    base.composite(mask, 0, 0, Magick::BlendCompositeOp);

    // Write
    base.write("output.png");

    return EXIT_SUCCESS;
}

However, I can't figure out how to actually apply this as an alpha mask, rather than just blending it. I cannot find a solution for it anywhere either, at least not for C++.

Any help would be greatly appreciated.

1
I think you need CopyOpacityCompositeOpMark Setchell

1 Answers

2
votes

You should disable the alpha channel on both images and use CopyOpacity instead of Blend. It also looks like your mask is inverted so you should probably negate it. Below is an example in C++:

// Crop out mask and sprite
base.crop(Magick::Geometry(base.columns() / 2, base.rows(), 0, 0));
mask.crop(Magick::Geometry(mask.columns() / 2, mask.rows(), mask.columns() / 2, 0));

// Disable alpha channel
base.matte(false);
mask.matte(false);

// Invert the mask
mask.negate();

// Apply mask
base.composite(mask, 0, 0, Magick::CopyOpacityCompositeOp);