I am trying to convert a simple ImageMagick command line to Magick++/c++ code. However it didn't work as expected. The offical website lack of API example. I can not figure out the problem even I dive into the related implement code. And Google doesn't give any hint.
ImageMagick command:
convert -size 543x114 xc: -sparse-color Barycentric '0,0 red 0,%h blue' gradient.png
I want to copy its effect by following code, but the output is inconsistent.
Magick++ code:
#include <Magick++.h>
int main()
{
int width = 543;
int height = 114;
Magick::Image gradImage(Magick::Geometry(width, height), Magick::Color("white"));
// installed magick++ is built --with-quantum-depth=8
double maxRGB = 255;
double args[] = {
0, 0,
maxRGB, 0, 0,
0, height,
0, 0, maxRGB
};
gradImage.sparseColor(
(Magick::ChannelType)((Magick::DefaultChannels & ~Magick::OpacityChannel) & ~Magick::IndexChannel),
Magick::BarycentricColorInterpolate,
sizeof(args) / sizeof(double),
args
);
gradImage.write("gradient.png");
}
CMakelist.txt:
cmake_minimum_required(VERSION 3.16)
project(demo)
find_package(ImageMagick COMPONENTS Magick++)
include_directories(
${ImageMagick_INCLUDE_DIRS}
)
add_executable(
demo
main.cpp
)
target_link_libraries(
demo
${ImageMagick_Magick++_LIBRARY}
)
Other Info:
- OS: Ubuntu 20.04
- ImageMagick: 7.1.1 (built from source: ./configure --with-quantum-depth=8 && make)
Any help is appreciated!
I want the magick++ code have same effect with the command line


You've almost got it! It might be worth peaking at the source code for
GenerateCoefficientsto read the expected arguments, andSparseColorOptionto see how the CLI options get parsed.R, G, Bvalues should be in a range between0.0&1.0.DefaultChannels.Try the following...