ConstituteImage broken for grayscale?

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
timholy
Posts: 13
Joined: 2013-03-04T07:34:13-07:00
Authentication code: 6789

ConstituteImage broken for grayscale?

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

Re: ConstituteImage broken for grayscale?

Post 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.
timholy
Posts: 13
Joined: 2013-03-04T07:34:13-07:00
Authentication code: 6789

Re: ConstituteImage broken for grayscale?

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: ConstituteImage broken for grayscale?

Post 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.
snibgo's IM pages: im.snibgo.com
timholy
Posts: 13
Joined: 2013-03-04T07:34:13-07:00
Authentication code: 6789

Re: ConstituteImage broken for grayscale?

Post by timholy »

It's a grayscale image, won't that be problematic? (EDIT: meaning, if I then write the wand as an image file?)
timholy
Posts: 13
Joined: 2013-03-04T07:34:13-07:00
Authentication code: 6789

Re: ConstituteImage broken for grayscale?

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: ConstituteImage broken for grayscale?

Post 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.
snibgo's IM pages: im.snibgo.com
Post Reply