setImageBias has no effect

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
Danack
Posts: 73
Joined: 2013-10-14T10:00:25-07:00
Authentication code: 6789

setImageBias has no effect

Post 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.
Danack
Posts: 73
Joined: 2013-10-14T10:00:25-07:00
Authentication code: 6789

Re: setImageBias has no effect

Post 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...
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: setImageBias has no effect

Post by magick »

Thanks for the analysis and patch. We'll apply it to ImageMagick 6.9.0-1 Beta by sometime tomorrow.
Danack
Posts: 73
Joined: 2013-10-14T10:00:25-07:00
Authentication code: 6789

Re: setImageBias has no effect

Post 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
Danack
Posts: 73
Joined: 2013-10-14T10:00:25-07:00
Authentication code: 6789

Re: setImageBias has no effect

Post 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.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: setImageBias has no effect

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