Skip to content

[BUG] Incorrect ChannelCount when reading a 8bit grayscale (mono) png image. #1918

@TonyDragonlake

Description

@TonyDragonlake

Magick.NET version

Version : 14.8.2; NuGet Package : Magick.NET.Core; Magick.NET-Q8-AnyCPU.

Environment (Operating system, version and so on)

OS: Windows10 22H2; CPU : Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz 3.19 GHz; runtime: dotnet 8.

Description

When I checking the function for mono PNG IO, I found incorrect ChannelCount and incorrect pixel array.
I create a mono PNG. But magick found two channels and GetPixels method return incorrect pixel array size (as same as GetPixelsUnsafe).

Steps to Reproduce

Here is the test code, it runs on dotnet8, console app.

// 1) prepare image, single channel
uint width = 100;
uint height = 100;
byte[] pixels = new byte[width * height];
Random.Shared.NextBytes(pixels);
// 2) create image
using var image = new MagickImage();
image.ReadPixels(pixels, new PixelReadSettings(width, height, StorageType.Char, "R"));
// 3) write image
image.Write("test.png");
// 4) reload image
using var image2 = new MagickImage();
image2.Read("test.png");
// 5) check channel count
var channelCount = image2.ChannelCount;
var channels = image2.Channels.ToArray();
Console.WriteLine($"Channel Count : {channelCount}");
Console.WriteLine($"Channels : {string.Join(", ", channels)}");
// 6) check pixels, for the mono image, one byte one pixel
var getPixelsByToArray = image2.GetPixels().ToArray();
var getPixelsByGetArea = image2.GetPixels().GetArea(0, 0, width, height);
Console.WriteLine($"GetPixelsByToArray : {getPixelsByToArray.Length}");
Console.WriteLine($"GetPixelsByGetArea : {getPixelsByGetArea.Length}");
Console.WriteLine($"Pixel Size should be : {width * height}");

/*
Result :
==============================
Channel Count : 2
Channels : Cyan, Index
GetPixelsByToArray : 20000
GetPixelsByGetArea : 20000
Pixel Size should be : 10000
==============================
*/

Images

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions