Page 1 of 1

Cannot get floodFillOpacity to work

Posted: 2011-08-06T08:03:28-07:00
by Imaginatorium
I am new to (Image)Magick++, so please forgive blunders, impatience, etc etc

I am trying to do some low-level pixel twiddling; I took most of last weekend to get to the point where I can read and write individual pixel RGB values. Now I want to handle the alpha channel directly, but to save reinventing the wheel, I tried to use the floodfill methods. First, this works as expected:

Code: Select all

image.floodFillColor(3, 3, Color(40*Percent, 90*Percent, 30*Percent));
(3, 3) is on the white background, which gets filled with (whatever) colour.
The documentation is incredibly inexplicit about how the flood fill replaces pixel values, but I discovered by experiment that any opacity value in the colour parameter is ignored. So next I try:

Code: Select all

	// arguments: x, y, opacity, "paint method"
	image.floodFillOpacity(3, 3, 692, FloodfillMethod);
The manual says: "Floodfill pixels matching color (within fuzz factor) of target pixel(x,y) with replacement opacity value using method." See: http://www.imagemagick.org/Magick++/Ima ... %20Methods

PaintMethod is an enumeration: http://www.imagemagick.org/Magick++/Enu ... aintMethod
(I expected that PaintMethod would include things like: write alpha value, write value if less than existing value, etc etc, being pixel combination methods) But actually these are things like "replace just the pixel in question" which make no sense at all (that I can see) in the context of a function which is doing flood fill already. In the interests of not just whining, I persevered, and found the following experimental results:

When fourth argument is:
ReplaceMethod: the entire image goes black (??)
PointMethod: the entire image goes black (??)
FloodfillMethod: flood fills... (ah! what we wanted)
But this does not appear to set the opacity at all. The manual is silent about just what the opacity argument is, but it's an int, so I tried 0, 1, then maxRGB, and eventually discovered the following:

If the value is 0-692: flood fills with black
If the value is 693-100%: flood fills with white

At this point I decided it will be quicker to make my own wheel, but I wonder if there is some monumental misunderstanding I am labouring under...?

(I mean, 692? It's not as if it were 42^2 or anything...)

Re: Cannot get floodFillOpacity to work

Posted: 2011-08-08T00:02:11-07:00
by anthony
to replace opacity channel you will also need to specify a 'A' channel in the channel flags.

this is shownin in the command line examples
http://www.imagemagick.org/Usage/color_basics/#opaque

Which is using the same core library functions.

NOTE: IM v7 will have 'alpha' channel set by default, and as such will flood fill just as you would expect.

Re: Cannot get floodFillOpacity to work

Posted: 2011-08-11T09:13:15-07:00
by Imaginatorium
Thanks for your response...
to replace opacity channel you will also need to specify a 'A' channel in the channel flags.
I can't find "channel flags" in the Magick++ API documentation. Are you saying that to use this I first have to become so familiar with the command line interface that I can guess how the API works?

I have only found this page as documentation on the Image class. Have I missed something?
http://www.imagemagick.org/Magick++/Image.html

The closest thing I can see on that page is "transparent". This takes a single argument (const Color &color_) and the explanation is: "Add matte image to image, setting pixels matching color to transparent." I managed to set transparency/alpha values after using it, but I want to just add an alpha channel, not set arbitrary pixels to "transparent", so it isn't very helpful. Generally the API calls as presented seem to be very fragmentary like this.

I don't want to complain -- I'm getting on with doing things myself on the low-level pixel cache access -- but I wonder if I am missing something.

Re: Cannot get floodFillOpacity to work

Posted: 2011-08-11T22:19:12-07:00
by anthony
channels should be a setting much like things like density. In C it is a bitmask of flags in the image structure.

Re: Cannot get floodFillOpacity to work

Posted: 2011-08-13T20:26:04-07:00
by Imaginatorium
Found it!

Well, here's the list of methods available for the Image class:

http://www.imagemagick.org/Magick++/Ima ... l%20Access

But the one we need is missing: it's "type". Now, when I think of transparency, I think of 'alpha channel', or I think of 'channels', but no, I don't think of 'type'. Anyway, here's the relevant bit from the image.h:

Code: Select all

    // Image representation type (also see type attribute)
    //   Available types:
    //    Bilevel        Grayscale       GrayscaleMatte
    //    Palette        PaletteMatte    TrueColor
    //    TrueColorMatte ColorSeparation ColorSeparationMatte
    void            type ( const ImageType type_ );
Now the quizwork gets easier. I want an alpha channel, so I might think of 'rgba', or 'opaque', or 'transparency', but no, I don't think of 'matte' (which at least in video terminology means a flat colour background). But I suppose we can guess that 'type: TrueColorMatte' is supposed to mean "channels: RGBA". Well, hmm.

Re: Cannot get floodFillOpacity to work

Posted: 2011-08-14T05:21:25-07:00
by anthony
type is something different. It sets the type of image that should be saved (or read in).
Type can add/remove channels but only as a side effect not as its main function.
Channels however restricts some operations to specific channels of an image.

Re: Cannot get floodFillOpacity to work

Posted: 2011-08-27T04:26:20-07:00
by Imaginatorium
anthony wrote:type is something different. It sets the type of image that should be saved (or read in).
Type can add/remove channels but only as a side effect not as its main function.
Channels however restricts some operations to specific channels of an image.
OK. Well, "type" works, in that it enables me in Magick++ to create an image with the channels I want it to include. Can you tell me how I would access "Channels" in Magick++? (I only wanted to add an alpha channel, not to restrict some operation to particular channels.)

Brian Chandler

Re: Cannot get floodFillOpacity to work

Posted: 2012-03-19T02:34:17-07:00
by richardmilne
Hate to revive a dead thread but I wouldn't suppose anyone has had any luck working with FloodFillOpacity in C# using the ImageMagickNET wrapper? It should be as simple as the code below, no?

Code: Select all

ImageMagickNET.MagickNet.InitializeMagick();
ImageMagickNET.Image img = new ImageMagickNET.Image(e.FullPath);

img.FloodFillOpacity(100,5,0, PaintMethod.ReplaceMethod);
img.Write(@"C:\\Path\\To\\Output.png");
It just doesn't seem to be playing ball! The outputted png is being written with an alpha channel, the input image is a jpg. It doesn't seem to make a difference if I feed it a png to start with. I've tried all the different PaintMethod arguments, and tried MatteFloodFill instead, but filling with 255,255,255,0.

All that appears to be happening is the conversion of jpg to png!

Hope someone can help, it's pretty dead over at the project page for ImageMagickNET, and there's very little documentation about for it!