Page 1 of 1

Is it possible to create 8-bit grayscale JPEG-2000?

Posted: 2016-12-21T11:00:34-07:00
by djc88
I am attempting to use Magick.NET to create JPEG-2000 images in C#. (My library came from the ZIP file Magick.NET-7.0.3.500-Q8-x86.) I'm starting from a data array of bytes, creating a C# Bitmap object (called "bmp"), creating a MagickImage from that Bitmap, and writing to a file:

Code: Select all

using (MagickImage m = new MagickImage(bmp))
{
    m.Format = MagickFormat.Jp2;
    m.Settings.SetDefine(MagickFormat.Jp2, "quality", "80");
    m.Write(jpeg2000FullPath);
}
This code successfully creates valid JPEG-2000 files that I can open in FastStone Image Viewer. However, FastStone reports that these are 24-bit RGB files. I want to create 8-bit Grayscale JPEG-2000 files. Is there a command I can call that will change this setting?

I've searched the forum; here are some relevant topics I've found:

This user seems to have had success creating grayscale JPEG-2000 files. Attempting to call equivalent commands in Magick .NET doesn't work for me.
This user seems to have created grayscale JPEG-2000 files. (No details provided.)
This user wanted to create 16-bit grayscale files. (Unresolved.)

Re: Is it possible to create 8-bit grayscale JPEG-2000?

Posted: 2016-12-21T11:12:40-07:00
by snibgo
I don't know how to do it in C#, but it works in v6.9.5-3 with no problem:

Code: Select all

convert rose: -colorspace gray r.jp2
Exiftool repors that r.jp2 is JPEG 2000 with one channel.

Re: Is it possible to create 8-bit grayscale JPEG-2000?

Posted: 2016-12-21T12:11:40-07:00
by djc88
I think you're right; I've verified it for myself with ImageMagick's command-line "convert" command.

I've tried setting the colorspace to Gray in two ways in my C# code (by uncommenting one of the commented lines at a time):

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);
}
I've confirmed with exiftool that the Number Of Components remains 3 in both cases, whereas that value correctly changed to 1 when executed in the command-line.

It appears Magick.NET is not respecting the ColorSpace change. I'll post this question over to the Magick.NET section of the board.

--------

EDIT: FYI, I've posted on the Magick.NET forum, the Magick.NET website, and created an Issue.