2

I'm using Image Sharp in an ASP.Net Core project and now need to prepare PNG images for a device that can only handle 2 colour indexed PNG images.

If I use the following code, then the images are saved as palleted 2 colour images, but the result is corrupt, looks to me that there is no pixel packing happening.

private void ConvertToMonoPng(Stream stream, string fileName)
{
    Image<Rgba32> image = Image.Load(fileName);

    var enc = new PngEncoder();
    enc.CompressionLevel = PngCompressionLevel.Level9;
    enc.ColorType = PngColorType.Palette;
    enc.BitDepth = PngBitDepth.Bit1;

    image.Save(stream, enc);
    image.Dispose();
}

The results are:

Source Image

Source image

Output Image

Output image

I know that the ImageSharp library doesn't currently support sub-byte packed pixels in memory, but since there does seem to be some support in the PNGEncoder, I'm hoping that saving monochrome PNG's is supported and I'm just doing something wrong.

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
  • I'd guess not, given the 'todo' here: https://github.com/SixLabors/ImageSharp/blob/v1.0.0-beta5/src/ImageSharp/Formats/Png/PngEncoderCore.cs#L271 It might be simple enough to add some extra cases there if you know what you're doing? But if that's not it you might have to just step through ImageSharp writing the PNG until you spot something it's doing wrong and fix that. – Rup Sep 25 '18 at 11:41
  • 1
    Thanks for pointing me to the right place. Yes, it looks like there is no support for 1bpp greyscale, but it does seem to handle 2 colour palleted PNG's which is what I was trying (and works but creates corrupt images) - either would suit my requirements. I'm fairly sure now that this sits somewhere between a bug and unsupported function, so will have a go at adding support inside ImageSharp. – Lawrence Owen Sep 25 '18 at 13:41
  • After seeing this question I've started a work-in-progress PR for ImageSharp that will add the encoding functionality you require. Once it is complete and merged I'll add an answer below with the build version number that contains the missing features. https://github.com/SixLabors/ImageSharp/pull/712 – James South Sep 26 '18 at 20:23
  • 1
    @James thank you that's an amazing response! Looking forward to testing the update. – Lawrence Owen Sep 27 '18 at 11:07
  • You gave me the prod I needed. :) – James South Sep 28 '18 at 11:25

1 Answers1

3

As of build 1.0.0-dev001921 this is now possible. I've added support for 1,2, and 4 bit encoding to accompany the existing 8 and 16 bit encoding.

You can install the package via the nightly MyGet feed using the following command.

PM> Install-Package SixLabors.ImageSharp -Version 1.0.0-dev001921 -Source 
https://www.myget.org/F/sixlabors/api/v3/index.json

Here's the output.

1 bit palette

1 bit palette

2 bit palette

2 bit palette

4 bit palette

4 bit palette

1 bit grayscale

1 bit grayscale

2 bit grayscale

2 bit grayscale

4 bit grayscale

4 bit grayscale

James South
  • 10,147
  • 4
  • 59
  • 115