MagickImage.SparseColors() how does it work?

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
Paralon
Posts: 6
Joined: 2014-07-03T03:05:05-07:00
Authentication code: 6789

MagickImage.SparseColors() how does it work?

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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.
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

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

Post 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);
}
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
Post Reply