c++imagemagickimagemagick-convertmagick++

Translate CLI ImageMagick to Magick++: threshold and depth


I am converting a system call to Magick++ in my code, but I am having some trouble converting the threshold and depth.

The original:

convert /foo/bar.ppm -crop WxH+X+Y -threshold 50% -depth 1 /foo/out.ppm

My current C++ version is:

Magick::InitializeMagick(*argv);
Magick::Image img;
img.read("/foo/bar.ppm");
Magick::Image temp_img(img);
temp_img.chop(Magick::Geometry(X,Y);
temp_img.crop(Magick::Geometry(W,H));
temp_img.threshold(.50);
temp_img.depth(1);
temp_img.write("/foo/out.ppm");

The chop and crop behaves like I expect, but the rest does not. The threshold and depth commands take a double and a size_t, respectively. So what I have written in there seems like it would work. However, if either one of these lines are enabled, the result image comes out nearly all white.

Is there a more correct way of doing this?


Solution

  • Mark Setchell's comment is correct. Maigck::Image.threshold's argument must be scaled by the QuantumRange (provided by a C macro definition).

    temp_img.threshold(QuantumRange * 0.5);
    

    This scaling is expected for most arguments that are a percent ratio.