[Magick++] pixelColor() transparency problem

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
nicozzz

[Magick++] pixelColor() transparency problem

Post 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 !
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

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

Post 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
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
nicozzz

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

Post 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...
hakiim35
Posts: 19
Joined: 2010-02-18T07:04:01-07:00
Authentication code: 8675308

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

Post by hakiim35 »

Have you found the answer?
maybe

Code: Select all

void matte(const bool matteFlag_)
could help..
kyron

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

Post 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.
Post Reply