Page 1 of 1

ConstituteImage broken for grayscale?

Posted: 2014-06-10T03:53:45-07:00
by timholy
Using the version of ImageMagick distributed with Ubuntu 14.04 (`8:6.7.7.10-6ubuntu3`), the following code

Code: Select all

#include <stdio.h>
#include <stdint.h>
#include <wand/MagickWand.h>

/* uint8_t data[4] = {156, 230, 5, 238}; */
uint8_t data[4] = {0x9c, 0xe6, 0x05, 0xee};

int main()
{
    printf("%d %d %d %d\n", data[0], data[1], data[2], data[3]);
    MagickWandGenesis();
    MagickWand *magick_wand;
    magick_wand = NewMagickWand();
    MagickBooleanType status = MagickConstituteImage(magick_wand,2,2,"I",CharPixel,data);
    uint8_t *buf = (uint8_t*) calloc(4,1);  /* initialized to 0 */
    MagickExportImagePixels(magick_wand,0,0,2,2,"I",CharPixel,buf);
    printf("%d %d %d %d\n", buf[0], buf[1], buf[2], buf[3]);
    DestroyMagickWand(magick_wand);
    MagickWandTerminus();
}
gives me the following output:
$ ./magick
156 230 5 238
85 202 0 218
Something similar to this worked on the version distributed with Ubuntu 12.04.

If I've not made a mistake and this is a useful test, you're welcome to put this code (or a variant) in your unit tests under whichever license you choose.

Re: ConstituteImage broken for grayscale?

Posted: 2014-06-10T04:03:35-07:00
by magick
We tried your code with ImageMagick 6.8.9-3, the current release. It returned the correct results:
  • -> ./magick
    156 230 5 238
    156 230 5 238
We'll take a look at 6.7.7.10 and see if we can spot the source of the bug.

Re: ConstituteImage broken for grayscale?

Posted: 2014-06-10T04:22:10-07:00
by timholy
That's great to hear it's working in the current version. I suspect upgrading mid-release from 6.7->6.8 will be too big of a leap for Ubuntu, so I'm grateful that you're also looking into what's wrong with their version.

Re: ConstituteImage broken for grayscale?

Posted: 2014-06-10T04:41:33-07:00
by snibgo
The numbers are consistent with a conversion from sRGB to RGB. Back in those days (v6.7.7), IM thought that grayscale images were (or should be) RGB. As a workaround in the code, the equivalent of "-set colorspace RGB", or something, should do the trick.

Re: ConstituteImage broken for grayscale?

Posted: 2014-06-10T04:51:04-07:00
by timholy
It's a grayscale image, won't that be problematic? (EDIT: meaning, if I then write the wand as an image file?)

Re: ConstituteImage broken for grayscale?

Posted: 2014-06-10T05:00:38-07:00
by timholy
It looks like it works properly if I add

Code: Select all

MagickSetImageColorspace(magick_wand, GRAYColorspace);
after calling `MagickConstituteImage`. This seems to be different behavior than either the current version or the one shipped with Ubuntu 12.04, so it seems that it should still be viewed as a bug.

Re: ConstituteImage broken for grayscale?

Posted: 2014-06-10T06:58:48-07:00
by snibgo
Personally, I regarded any different treatment of grey images, compared to nearly-but-not-quite grey, as a bug. If IM thought a grey image was non-linear sRGB, it would automatically convert it to linear RGB. I think this is happening here. Perhaps that treatment complied with some standards, and perhaps those standards had a bug. Fortunately, current IM doesn't treat grey images differently.

Put it another way: it could be argued that 6.7.7 performed correctly, conforming to the somewhat unwritten specification, as it was then.