Page 1 of 1

setImageBias has no effect

Posted: 2014-06-13T05:30:21-07:00
by Danack
According to the manual: "MagickSetImageBias() sets the image bias for any method that convolves an image (e.g. MagickConvolveImage())."

However it appears to have no actual effect. The code below is a C test case based on a StackOverflow question

Code: Select all


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wand/MagickWand.h>


void ThrowWandException(MagickWand *wand) {
    char *description; 
    ExceptionType severity; 
    description = MagickGetException(wand, &severity); 
    (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); 
    description=(char *) MagickRelinquishMemory(description); 
    exit(-1); 
}


int main(int argc,char **argv) {

    MagickWand *magick_wand;
    MagickBooleanType status;
    unsigned long range;
    double kernel[] = { -0.70,0,0.70,-0.70,0,0.70,-0.70,0,0.70 };

    MagickWandGenesis();
    magick_wand = NewMagickWand();
    status = MagickReadImage(magick_wand, "./stack.jpg");
    if (status == MagickFalse) {
        ThrowWandException(magick_wand);
    }

    status = MagickTransformImageColorspace(magick_wand, GRAYColorspace);


    MagickGetQuantumRange(&range);
    
    
    status = MagickSetImageBias(magick_wand, 0.5);
    
    //Not sure if this is meant to be a quantum value
    //It makes no difference either way
    //status = MagickSetImageBias(magick_wand, range / 2);
    
    if (status == MagickFalse) {
        ThrowWandException(magick_wand);
    }

    status = MagickConvolveImageChannel(magick_wand, DefaultChannels, 3, kernel);
    if (status == MagickFalse) {
        ThrowWandException(magick_wand);
    }
    
    status = MagickWriteImages(magick_wand, "setImageBias_%d.jpg", MagickTrue);
    if (status == MagickFalse) {
        ThrowWandException(magick_wand);
    }

    magick_wand = DestroyMagickWand(magick_wand);
    MagickWandTerminus();
    
    return(0);
}


Expected result - image has 50% grey for 'flat' areas:
Image

Actual result - image has black for 'flat' areas:
Image

I don't have a windows box available for testing on that.

Re: setImageBias has no effect

Posted: 2014-12-14T14:58:05-07:00
by Danack
Hi,

I believe I have found the cause of this issue, and the fix appears relatively simple. In file magick/effect.c the following changes (or a close approximation of them) need to be done near line 889:

+double
+ bias;
+
+bias=image->bias;

Line 914-ish:

-convolve_image=MorphologyApply(image,channel,ConvolveMorphology,1,
- kernel_info,UndefinedCompositeOp,0,exception);
+convolve_image=MorphologyApply(image,channel,ConvolveMorphology,1,
+ kernel_info,UndefinedCompositeOp,bias,exception);

I'm guessing that this function isn't called by the ImageMagick utilities - it is only called by the various API versions of ImageMagick, which explains why this bug doesn't affect the command line convert utility? Though that probably ought to be checked...

Re: setImageBias has no effect

Posted: 2014-12-14T15:06:52-07:00
by magick
Thanks for the analysis and patch. We'll apply it to ImageMagick 6.9.0-1 Beta by sometime tomorrow.

Re: setImageBias has no effect

Posted: 2014-12-14T15:41:01-07:00
by Danack
btw It looks like the following functions are also hard-coded to be zero for the bias value; it's possible some of them should be observing the image bias value:

BlurImageChannel, EdgeImage, EmbossImage, GaussianBlurImageChannel, SharpenImageChannel, and CannyEdgeImage

Re: setImageBias has no effect

Posted: 2015-06-03T07:29:32-07:00
by Danack
Hi,

I never got around to checking this was fixed. I just tried it against ImageMagick 6.9.1-4 and it does not appear to be fixed. Looking at the code in 6.9.1-4 the bias is still not used in ConvolveImageChannel.

Code: Select all

convolve_image=MorphologyApply(image,channel,ConvolveMorphology,1,
      kernel_info,UndefinedCompositeOp,0.0,exception);
And similarly it is missing from the other functions that call MorphologyApply.

Re: setImageBias has no effect

Posted: 2016-02-05T18:14:14-07:00
by magick
Thanks for the problem report. We can reproduce it and will have a patch to fix it in GIT master branch @ https://github.com/ImageMagick/ImageMagick later today. The patch will be available in the beta releases of ImageMagick @ http://www.imagemagick.org/download/beta/ by sometime tomorrow.