Color values not generated exactly Q32 + HDRI

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

Color values not generated exactly Q32 + HDRI

Post by Danack »

Hi, it looks like in some cases ImageMagick is generating a value for colors that is slightly off from what it should be, leading to values that are outside of the QuantumRange in some cases.

The code below is compile against Image Magick with Q32 and HDRI enabled.
color 'red' 1.000000 bits are: 3ff0000000100000
color 'rgb(255, 0, 0)' 1.000000 bits are: 3fefffffe0200000
color '#ff0000' 1.000000 bits are: 3ff0000000100000
unity 1.000000 bits are: 3ff0000000000000
Version is ImageMagick 6.8.9-7 Q32 x86_64 2014-08-08 http://www.imagemagick.org
The values all round to 1 as float, but when they are scaled to maximum QuantumRange, the error leads to an off by one error e.g. the quantum scaled value for the red channel, created by doing 'PixelSetColor(pixel_wand, "red");' is 4294967296.0 rather than 4294967295.0

Code: Select all

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


static void printBits(void *c, size_t n)
{
  unsigned char *t = c;
  if (c == NULL)
    return;
  while (n > 0) {
    --n;
    printf("%02x", t[n]);
  }
  printf("\n");
}

  
int main(int argc,char **argv) {
    MagickWandGenesis();
      
    size_t version_number;
    char * versionString;
    double unity = 1.0;
    double red;
    char * color;
    int i;

    PixelWand *pixel_wand = NULL;
    
    char *colors[] = {"red", "rgb(255, 0, 0)", "#ff0000"};

    pixel_wand = NewPixelWand();
        
    for (i=0; i<3 ; i++) {
    
        color = colors[i];
            
        PixelSetColor(pixel_wand, color);

        red = PixelGetRed(pixel_wand);
    
        printf("color '%s' %f bits are: ", color, red);
        printBits(&red, sizeof red);
    }
    
    printf("unity %f bits are: ", unity);
    printBits(&unity, sizeof unity);
    
    versionString = (char *)MagickGetVersion(&version_number);
    printf("Version is %s \n", versionString);
      
    MagickWandTerminus();
    
    return(0);
}
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Color values not generated exactly Q32 + HDRI

Post by magick »

We can reproduce the problem you posted and have a patch in ImageMagick 6.8.9-3 Beta available by sometime tomorrow. Thanks.
Post Reply