Page 1 of 1

Alpha/Transparency No Longer Preserved Using Filters

Posted: 2013-02-07T21:26:23-07:00
by jflux
Alpha is no longer being processed with the StatisticImage / filter functions in Magickwand for PHP. Was hoping an extra undocumented command had been included to do this, but from the source, doesn't appear so.

MagickMedianFilterImage and MagickReduceNoiseImage both have the problem, possibly others I haven't run across.

Had a thread over at viewtopic.php?f=3&t=22720
thinking it was a related transparency issue, but their tests suggest it's Magickwand for PHP.

The current default behavior of median filter is to drop transparency unless -alpha on or -channel rgba is applied. (works on a command line, but Magickwand for PHP fails to pass or include the alpha option).

Versions < 6.7 automatically included the alpha (or the default behavior may have been alpha on), as it works on our production system running 6.6.3. Starting at 6.7 the median filters break when alpha is used. (which one reason to use the filter is to smooth across alpha differences)

Basically, since alpha is not processed in the filter, we're left with a black overlay with sporadic solids representing the data.


Here's a quick incomplete sample involving a solid background and a foreground with alpha data.
A median filter is applied to the foreground, and the two composited. Current version mangles the alpha to opaque before processing, then covers the background in a solid black or white. Leave off the filter, or run in 6.6 and alpha behaves as expected.

$bg_in = file_get_contents("bg_opaque.jpg"); // Any solid background
$bg = NewMagickWand();
MagickReadImageBlob($bg,$bg_in);

$image_in = file_get_contents("alpha_data_image.gif"); // Any image with transparency
$data_layer = NewMagickWand();
MagickReadImageBlob($data_layer,$image_in);

MagickMedianFilterImage($data_layer,.2); // No Longer Preserves Alpha, and no option to pass alpha

MagickCompositeImage($bg,$data_layer,MW_OverCompositeOp, 0, 0);

// v6.6, nice transparent overlay, all versions after v6.6 solid blob, hard corners, and no background..

I assume it's a bug, but perhaps a method has been created to handle this that I'm missing. Our current code is broken by the 1 median filter unless it's run on v6.6.

Since it appears to be a PHP/Magickwand issue, moved the thread over here.

Thanks for any help or insight.

Re: Alpha/Transparency No Longer Preserved Using Filters

Posted: 2013-02-07T23:02:28-07:00
by fmw42
try it with a png transparent image. gif does not support 8-bit transparency, only binary. so -median will try to change values near the boundary of black and white in the transparency. perhaps that is causing an issue?

Sorry, I do not use any IM API, only the command line and it works fine there with png and -alpha on.

Can you enable the equivalent of -matte or -channel rgba or both in your API? I am not sure if the IM developer created MagicWand for PHP or not? Did you try with Imagick, though it has not kept up with IM changes.

Does it work with PHP exec()?

Re: Alpha/Transparency No Longer Preserved Using Filters

Posted: 2013-02-07T23:10:45-07:00
by fmw42
Looking at http://www.magickwand.org/, I see MW_ActivateAlphaChannel. Does that help?

Re: Alpha/Transparency No Longer Preserved Using Filters

Posted: 2013-02-07T23:18:09-07:00
by jflux
I tried MW_ActivateAlphaChannel and it still blacked out the entire background, blocked up the overlay, playing with it now to see if I missed something, but appears to do the same. Haven't tried imagick yet.

Attempted with both .gif , .png, and images strictly generated by Magickwand. All have perfect transparency and processing until they go thru the median filter on a current build. All work perfectly with the median filter in 6.6.3-7 and 6.5.1-0.

I don't see any sign of an alpha tag being passed in the source code of Magickwand for PHP for the median function, an option would need to be compiled in. Believe that is the root issue.

Wondering if earlier versions of the Magick function defualted to use alpha, but was possibly changed to opacity with alpha available as a passed option? Then the PHP maintainer overlooked updating the specific median function, or may have not realized a need for alpha with median. It's definitely highly useful with alpha, using it for radar analysis.

Re: Alpha/Transparency No Longer Preserved Using Filters

Posted: 2013-02-08T00:55:37-07:00
by jflux
Possible progress..

Just discovered
convert input.png -alpha on -statistic median 3x3 out.png
does NOT preserve alpha on my end.. thought it did, but doesn't.

convert input.png -channel rgba -statistic median 3x3 out.png
DOES preserve alpha..

(using 6.8.2-4 on Linux Mint)

Doesn't solve the Magickwand for PHP behavior, but maybe a step.

Don't see an rgba option in "Magickwand for PHP" but tried without success
MagickSetImageDepth($wand_layer,32,MW_AllChannels)
MagickSetOption($wand_layer,"channel","RGBA");
(along with a boatload of other settings).

Since alpha works in everything except the filter function, still think the "Magickwand for PHP" compilation to the filter function is the issue.

Just noticed in diff that 0x002F was changed from AllChannels to CompositeChannels, and AllChannels = ~0UL in 6.6.9.7 , and then later statistics.c , lots of channel changes between these versions, and switch ups between TrueAlpha vs Opacity. Specifically involving the statistics and filter funcs.

Re: Alpha/Transparency No Longer Preserved Using Filters

Posted: 2013-02-09T00:43:35-07:00
by jflux
OK, I've created a workaround / new feature that someone on the dev team can make better, hopefully implement.. Also found bugs in regular magickwand..

Since the filter functions are listed as deprecated, I've written an add-on to implement MagickStatisticImageChannel in Magickwand for PHP,

BUG ALERT.. There is a bug in imagemagick in /wand/magick-image.h and /wand/magick-image.c that must be corrected for this to work . The MagickStatisticImage vs MagickStatisticImageChannel functions are flipped (according to their inputs and spec sheet).

MagickStatisticImage should be MagickStatisticImageChannel

Code: Select all

 MagickStatisticImageChannel(MagickWand *,const ChannelType,const StatisticType,
    const size_t,const size_t),
    
  MagickStatisticImage(MagickWand *,const StatisticType,const size_t,
    const size_t),
In /wand/magick-image.c , MagickStatisticImage is technically missing (but not needed).
The function by that name should be renamed MagickStatisticImageChannel (again based on it's spec and inputs that allow the much needed channel selection).
(As such, the MagickStatisticImage in /wand/magick-image.h could be removed or commented.)

Access To Non-Deprecated Magickwand Statistic Function:
In Magickwand for PHP / magickwand.c Add:
(It Works as is :) ..but could use corrections on the Zend char inputs by anyone who knows more, feel free to correct away! )

Code: Select all

PHP_FE( magickstatisticimagechannel, NULL )	


PHP_FUNCTION( magickstatisticimagechannel )
{
	MW_PRINT_DEBUG_INFO
	
	MagickWand *magick_wand;
	zval *magick_wand_rsrc_zvl_p;
    char *channel, *type;
    int channel_len, type_len;
    size_t width, height;

	MW_GET_7_ARG(	"rssll",
					&magick_wand_rsrc_zvl_p,
					&channel, &channel_len, 
					&type, &type_len, 
					&width, &height 
					);

    MW_CHECK_PARAM_STR_LEN( channel_len );
    MW_CHECK_PARAM_STR_LEN( type_len );

	MW_GET_POINTER_FROM_RSRC( MagickWand, magick_wand, &magick_wand_rsrc_zvl_p );

    MW_BOOL_FUNC_RETVAL_BOOL( MagickStatisticImageChannel( magick_wand, (ChannelType) *channel, (StatisticType) *type, (size_t) width, (size_t) height) );
}
In Magickwand for PHP / magickwand.h Add:

Code: Select all

	PHP_FUNCTION( magickstatisticimagechannel );
Hopefully someone can correct my issues with the Zend inputs.. It works as it is, but I'm sure I've got something weird going on. Left it as is since it's WORKING :) but looks like passing the filter name is bugged. (but luckily the filter I want is working by default)

To accomplish the original desired median filter, keeping transparency, using new, non-deprecated function:

Code: Select all

MagickStatisticImageChannel($wand_layer,OpacityChannel,MedianStatistic,.2,.2);
Channels Available:
  • UndefinedChannel
    RedChannel
    GrayChannel
    CyanChannel
    GreenChannel
    MagentaChannel
    BlueChannel
    YellowChannel
    AlphaChannel
    OpacityChannel
    MatteChannel
    BlackChannel
    IndexChannel
    CompositeChannels
    AllChannels
    TrueAlphaChannel
    RGBChannels
    GrayChannels
    SyncChannels
    DefaultChannels
Statistics Available: according to statistic.h
(ATM looks like only the default is working, likely my code / Zend char input)
StatisticTypes:
  • UndefinedStatistic
    GradientStatistic
    MaximumStatistic
    MeanStatistic
    MedianStatistic
    MinimumStatistic
    ModeStatistic
    NonpeakStatistic
    StandardDeviationStatistic
Hope this is helpful and can ultimately be tweaked/corrected and implemented.
Simple fix, was a nightmare to sort thru all the pieces of code, but makes a powerful addition to Magickwand for PHP.

Re: Alpha/Transparency No Longer Preserved Using Filters

Posted: 2013-02-09T06:52:53-07:00
by magick
We can reproduce the problem you posted and have a patch in ImageMagick 6.8.2-8 Beta available by sometime tomorrow. Thanks.

Re: Alpha/Transparency No Longer Preserved Using Filters

Posted: 2013-02-09T20:52:14-07:00
by jflux
Awesome! Will Magickwand for PHP also be updated to include the MagickStatisticImageChannel function? (its the real function desired, already in wand but not magickwand for php)

Now that I'm getting more familiar with the source (rather than just the thrown together quickfix above), I wouldn't mind volunteering more code and time to help keep Magickwand for PHP up to date.

Thanks! Great job as always with imagemagick!

** edit** just checked the php source. Wow, Already updated and enhanced, nice!