I am trying to convert an ImageMagick command for a blurring mask to Magick++ API.
ImageMagick :
convert -size 720x478 xc: -sparse-color Barycentric '0,0 black 0,%h white' -function polynomial 4,-4,1 -level 0,50% mask.jpg
Magick++ :
Magick::Image mask(Magick::Geometry(720,478), Magick::Color("white"));
double args[6];
args[0] = 0;
args[1] = 0;
args[2] = 0;
args[3] = 0;
args[4] = mask.rows();
args[5] = MaxRGB;
mask.sparseColor(Magick::DefaultChannels, Magick::BarycentricColorInterpolate, 6, args);
args[0] = 4;
args[1] = -4;
args[2] = 1;
args[3] = 0;
args[4] = 0;
args[5] = 0;
mask.quantumOperator(Magick::DefaultChannels, Magick::PolynomialFunction,
3,args);
parseLevel(image, "0,50%", args); // contains code from mogrify.c for parsing the leveling string
mask.level(args[0], args[1], args[2], ' ');
The result I get is just a white image, whereas the correct mask image should be like this:
Can someone please show me my mistake?
Mhm, I have no experience with imagemagick, but quickly looking into the docs and at your example, I have a hunch: Maybe the default image type is rgb, and you need three float/double parameters for each color in your sparse-color call. Like that:
Magick::Image mask(Magick::Geometry(720,478), Magick::Color("white"));
double args[10];
// -sparse-color Barycentric '0,0 black 0,%h white'
args[0] = 0; // x = 0
args[1] = 0; // y = 0
args[2] = 0; // black (R)
args[3] = 0; // black (G)
args[4] = 0; // black (B)
args[5] = 0; // x = 0
args[6] = mask.rows(); // y = %h
args[7] = MaxRGB; // white (R)
args[8] = MaxRGB; // white (G)
args[9] = MaxRGB; // white (B)
mask.sparseColor(Magick::DefaultChannels, Magick::BarycentricColorInterpolate, 10, args);
So it turns out that I was giving the wrong ChannelType
in sparseColor()
function. The DefaultChannels
enum contains RGBChannels
with OpacityChannel
and IndexChannel
. I had to exclude the latter two from the DefaultChannels enum by bitwise operation. As @ThorngardSO pointed out, the args size also needs to be 10.
code :
double args[10];
// -sparse-color Barycentric '0,0 black 0,%h white'
args[0] = 0; // x = 0
args[1] = 0; // y = 0
args[2] = 0; // black (R)
args[3] = 0; // black (G)
args[4] = 0; // black (B)
args[5] = 0; // x = 0
args[6] = mask.rows(); // y = %h
args[7] = MaxRGB; // white (R)
args[8] = MaxRGB; // white (G)
args[9] = MaxRGB; // white (B)
mask.sparseColor((Magick::DefaultChannels & ~OpacityChannel) & ~IndexChannel, Magick::BarycentricColorInterpolate, 10, args);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.