Magick.NET icon indicating copy to clipboard operation
Magick.NET copied to clipboard

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

Open TonyDragonlake opened this issue 3 months ago • 1 comments

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

TonyDragonlake avatar Oct 09 '25 17:10 TonyDragonlake

Can you clarify why you think it is incorrect? Your image is stored in an indexed format and when it's read both the index and the actual color are a channel of the image and that is why you have two channels.

dlemstra avatar Oct 11 '25 20:10 dlemstra