Page 1 of 1

Color values not generated exactly Q32 + HDRI

Posted: 2014-08-08T05:48:16-07:00
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);
}

Re: Color values not generated exactly Q32 + HDRI

Posted: 2014-08-08T07:04:59-07:00
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.