Page 1 of 1

[Magick++] pixelColor() transparency problem

Posted: 2010-01-23T05:59:08-07:00
by nicozzz
Hi everybody !

I am developping an application in C++ using Magick++ library. Basically, I would like to generate a black PNG file, with different trasparency levels depending on the pixel.

To do that, I compute for each pixel the value of the transparency chanel and I use the method pixelColor() on my image to set the color of each pixel.

In the Magick++ tutorial, the usage of this method is explained this way :

Code: Select all

// Example: setting pixels
Image my_image("640x480", "white"); // start with creating a white-background canvas
my_image.pixelColor(50,50,Color("red")); // set the pixel at position (50,50) to red
my_image.pixelColor(5,5,Color(MaxRGB,0,0,MaxRGB/2)); // set semitransparent red at (5,5)
Now, there is my code :

Code: Select all

int main()
{
  Image img = Image(Geometry(1000, 1000), "white");
  for (int i = 0; i < 1000; i++)
  {
  	for (int j = 0; j < 1000; j++)
  	{
       float d;
      if ((i/500)%2 == 0)
      {
        if ((j/500)%2 == 0) d = PerlinNoise_2D((i%500)/100.0, (j%500)/100.0);
        else d = PerlinNoise_2D((i%500)/100.0, (500-(j%500))/100.0);
      }
      else
      {
        if ((j/500)%2 == 0) d = PerlinNoise_2D((500-(i%500))/100.0, (j%500)/100.0);
        else d = PerlinNoise_2D((500-(i%500))/100.0, (500-(j%500))/100.0);
      }
      d = (d+1.0)/2.0;
      int res = (int) (d * MaxRGB);
      img.pixelColor(i, j, Color(0.0, 0.0, 0.0, res));
  	}
  }
  img.write("tst.png");
  return 0;
}
You just have to know that the float value d computed is in [0.0, 1.0]. This code produces a black image with no transparency. My d values are well computed : when I change the line

Code: Select all

img.pixelColor(i, j, Color(0.0, 0.0, 0.0, res));
to :

Code: Select all

img.pixelColor(i, j, Color(res, res, res, MaxRGB));
I obtain a greyscale image, in which I would like to set light pixels transparent.

It's been two days I am looking for an answer... No result... I really count on you !

Re: [Magick++] pixelColor() transparency problem

Posted: 2010-01-23T08:09:03-07:00
by el_supremo
The image you create doesn't have an alpha channel so the transparency part of the colour information is ignored.
I don't know how to do this with Magick++ but in C the MagickWand statement to set the alpha channel on is:

Code: Select all

	MagickSetImageAlphaChannel(magickwand,SetAlphaChannel);
Pete

Re: [Magick++] pixelColor() transparency problem

Posted: 2010-01-23T08:15:04-07:00
by nicozzz
Thanks a lot !

I'm going to try to find the equivalent function for Magick++.
I'll post it as soon as I find it.

However, if anybody already knows it...

Re: [Magick++] pixelColor() transparency problem

Posted: 2010-04-03T14:04:10-07:00
by hakiim35
Have you found the answer?
maybe

Code: Select all

void matte(const bool matteFlag_)
could help..

Re: [Magick++] pixelColor() transparency problem

Posted: 2010-04-06T13:54:11-07:00
by kyron
The following does give me a transparent canvas:

Code: Select all

Image canvas( Geometry(W,H), Color(1,1,1,TransparentOpacity) );
Note that the color values in this case are irrelevant as the are set to transparent. Subsequent modifications by calling pixelColor seem to work fine.