Gray colorspace not working when encoding JPEG-2000

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
djc88
Posts: 9
Joined: 2016-12-21T07:51:47-07:00
Authentication code: 1151

Gray colorspace not working when encoding JPEG-2000

Post by djc88 »

I am attempting to create an 8-bit grayscale JPEG-2000 file with Magick.NET. (I am using the library found in the ZIP file Magick.NET-7.0.3.500-Q8-x86.) I can create a valid jp2 file, but no matter what I do, the file always has an RGB colorspace and 3 bands.

User "snibgo" explained to me (see here) that this functionality does work with ImageMagick in the command line. You simply set the "colorspace" parameter to "gray," and, as confirmed with exiftool, the colorspace correctly changes to Grayscale, and the Number Of Components changes to 1.

The following C# code fails to change the output to grayscale:

Code: Select all

using (MagickImage m = new MagickImage(bmp))
{
    m.Format = MagickFormat.Jp2;
    //m.Settings.SetDefine(MagickFormat.Jp2, "colorspace", "gray");
    //m.Settings.ColorSpace = ColorSpace.Gray;
    m.Settings.SetDefine(MagickFormat.Jp2, "quality", "80");
    m.Write(jpeg2000FullPath);
}
(To use, uncomment one of the above lines at a time.) In both cases, the result (as seen in exiftool) remains a colorspace of RGB and a Number Of Components of 3.

Can anyone confirm this behavior? Is this a bug? If so, is there somewhere I can file a bug report?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Gray colorspace not working when encoding JPEG-2000

Post by snibgo »

I don't know C#, or even Magick++, but perhaps your code doesn't convert the pixels to gray. Have you tried:

Code: Select all

image.type( GrayscaleType );
... which is Magick++? See http://www.imagemagick.org/Magick++/Image++.html
snibgo's IM pages: im.snibgo.com
djc88
Posts: 9
Joined: 2016-12-21T07:51:47-07:00
Authentication code: 1151

Re: Gray colorspace not working when encoding JPEG-2000

Post by djc88 »

I've added a change to the ColorType as follows:

Code: Select all

using (MagickImage m = new MagickImage(bmp))
{
    m.Format = MagickFormat.Jp2;
    //m.Settings.SetDefine(MagickFormat.Jp2, "colorspace", "gray");
    m.Settings.ColorSpace = ColorSpace.Gray;
    m.ColorType = ColorType.Grayscale;
    m.Settings.SetDefine(MagickFormat.Jp2, "quality", "80");
    m.Write(jpeg2000FullPath);
}
In exiftool, the metadata values DO change to the correct values (Number Of Components = 1, Color Space = Grayscale), BUT the file size remains nearly the same as it was without the changes (dropping from 1,716 KB to 1,713 KB). Whereas, when I change the colorspace via the command line, the file size drops substantially (from 1,716 KB to 1,187 KB).

Therefore, I am suspicious that this change is only influencing the metadata, and not actually changing the imagery.
djc88
Posts: 9
Joined: 2016-12-21T07:51:47-07:00
Authentication code: 1151

Re: Gray colorspace not working when encoding JPEG-2000

Post by djc88 »

One more thing: changing the "ColorSpace" of the MagickImage has the same effect as I described in the previous post: changed metadata, but only marginal change in file size.

Added this line of code before the "SetDefine" command:

Code: Select all

m.ColorSpace = ColorSpace.Gray;
djc88
Posts: 9
Joined: 2016-12-21T07:51:47-07:00
Authentication code: 1151

Re: Gray colorspace not working when encoding JPEG-2000

Post by djc88 »

Sorry to make multiple posts, but I do have one more finding: changing the property Settings.ColorType has no effect at all; Number Of Components remains 3, Color Space is sRGB, and file size is unchanged (1,716 KB).

I also read (see here) that the "DetermineColorType" function sometimes changes the image to grayscale. That was not the case for me: Number Of Components remains 3, Color Space remains sRGB, and file size remains unchanged.

Code:

Code: Select all

using (MagickImage m = new MagickImage(bmp))
{
    m.Format = MagickFormat.Jp2;
    //m.Settings.SetDefine(MagickFormat.Jp2, "colorspace", "gray");
    //m.Settings.ColorSpace = ColorSpace.Gray;
    //m.ColorType = ColorType.Grayscale;
    //m.ColorSpace = ColorSpace.Gray;
    //m.DetermineColorType();
    m.Settings.ColorType = ColorType.Grayscale;
    m.Settings.SetDefine(MagickFormat.Jp2, "quality", "80");
    m.Write(jpeg2000FullPath);
}
(Again, uncomment one at a time for testing.)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Gray colorspace not working when encoding JPEG-2000

Post by snibgo »

I doubt that filesize is a reliable indicator. If your input image contains no colour, the JPG compressor may realize this so it compresses only one channel into the file (at the binary level).

Is your input a colour image? If not, I suggest you try it with a colour image.
snibgo's IM pages: im.snibgo.com
djc88
Posts: 9
Joined: 2016-12-21T07:51:47-07:00
Authentication code: 1151

Re: Gray colorspace not working when encoding JPEG-2000

Post by djc88 »

The reason I am suspicious of these changes is that the file size does change substantially when I convert the files via the command line.

I begin with an 8-bit grayscale bitmap file, test.bmp.

With the command

Code: Select all

convert test.bmp test.jp2
, the file size of test.jp2 is 1,716 KB.

With the command

Code: Select all

convert test.bmp -colorspace gray test.jp2
, the file size of test.jp2 is 1,176 KB.

With the variations of Magick.NET code (colorspace, colortype, etc., as discussed earlier), the file size does not change appreciably.

Therefore, ImageMagick on the command line and Magick.NET are behaving differently.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Gray colorspace not working when encoding JPEG-2000

Post by fmw42 »

djc88 wrote:The reason I am suspicious of these changes is that the file size does change substantially when I convert the files via the command line.

I begin with an 8-bit grayscale bitmap file, test.bmp.

With the command

Code: Select all

convert test.bmp test.jp2
, the file size of test.jp2 is 1,716 KB.

With the command

Code: Select all

convert test.bmp -colorspace gray test.jp2
, the file size of test.jp2 is 1,176 KB.

With the variations of Magick.NET code (colorspace, colortype, etc., as discussed earlier), the file size does not change appreciably.

Therefore, ImageMagick on the command line and Magick.NET are behaving differently.

Both command line examples here, you report the same file size of 1,716 KB contrary to your statement at the top that the file sizes change. But you used a grayscale input, so the file sizes should be the same. What happens with a color input?
djc88
Posts: 9
Joined: 2016-12-21T07:51:47-07:00
Authentication code: 1151

Re: Gray colorspace not working when encoding JPEG-2000

Post by djc88 »

fmw42 wrote:Both command line examples here, you report the same file size of 1,716 KB contrary to your statement at the top that the file sizes change.
No; please read the numbers more closely. The change without changing ColorSpace to gray is 1,716 KB, and with changing ColorSpace, it's 1,176 KB.

I may have made a mistake: now that I ran the command line ImageMagick convert tool again and checked the results with exiftool, file size without ColorSpace change is 1,716 KB, and with, 1,187 KB.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Gray colorspace not working when encoding JPEG-2000

Post by fmw42 »

Sorry, I had a moment of dislexia

Perhaps you should post your bmp file, so others can test with it.

What version of OpenJpeg are you using.
djc88
Posts: 9
Joined: 2016-12-21T07:51:47-07:00
Authentication code: 1151

Re: Gray colorspace not working when encoding JPEG-2000

Post by djc88 »

I don't know the OpenJpeg version. I do know that the Magick.NET version is 7.0.3.500-Q8-x86.

I've created an Issue on the Magick.NET website and uploaded a test 8-bit bitmap image. See here for a link.
Post Reply