Page 1 of 1

MagickImage.SparseColors() how does it work?

Posted: 2014-07-03T06:53:13-07:00
by Paralon
Hello there, everyone!
I couldn't find anything in Magick.NET documentation about SparseColors() method and it get's me confused.
I'm trying to recreate the effect of convert -sparse-color shepards "..." command line parameter. As far as I understand it gets a list of {X,Y COLOR} objects.

MagickImage.SparseColors() asks for Channel, SparseColorMethod and an array of double coordinates in his parameters.
What I did is the following:

Code: Select all

var coords = GetCoords(points);
image.SparseColor(Channels.All, SparseColorMethod.Shepards, coords);

Code: Select all

private double[] GetCoords(Point[] points)
{
    var list = new List<double>();
    for (var i = 0; i < points.Length; i++)
    {
        var p = points[i];
        var c = colors[i];

        list.Add(p.X);
        list.Add(p.Y);
    }

    return list.ToArray();
}
And this code is not working (because colors are not specified, I guess). Question is how do I specify them?

Re: MagickImage.SparseColors() how does it work?

Posted: 2014-07-03T10:07:58-07:00
by fmw42
-sparse-color shepards takes a list of x,y,color and fills out the image using a shepards inverse distance squared interpolation see http://www.imagemagick.org/Usage/canvas/#sparse-color

sparse-color: outputs a list of x,y,color for every non-transparent (fully opaque) pixel in the image. see http://www.imagemagick.org/Usage/files/#sparse-color

I do not know if either are part of MagickNet, yet, as I am not a Windows user.

Re: MagickImage.SparseColors() how does it work?

Posted: 2014-07-03T13:48:19-07:00
by dlemstra
It is quite tricky to specify the arguments so I have changed this for the next release. With the next release of Magick.NET you will be able to do this:

Code: Select all

/*
  http://www.imagemagick.org/Usage/canvas/#diagonal_gradients
  convert -size 600x60 xc: -sparse-color barycentric '0,0 skyblue  -%w,%h skyblue  %w,%h black' diagonal_gradient.jpg
*/
MagickReadSettings settings = new MagickReadSettings();
settings.Width = 600;
settings.Height = 60;

using (MagickImage image = new MagickImage("xc:", settings))
{
  List<SparseColorArg> args = new List<SparseColorArg>();
  args.Add(new SparseColorArg(0, 0, new MagickColor("skyblue")));
  args.Add(new SparseColorArg(-image.Width, image.Height, new MagickColor("skyblue")));
  args.Add(new SparseColorArg(image.Width, image.Height, new MagickColor("black")));

  image.SparseColor(SparseColorMethod.Barycentric, args);
}