I have an image created using Clut(). The source colors are very few. Now when I save as a PNG with ColorSpace set to sRGB and ColorType set to TrueColor, the image is still saved as an indexed 8-bit image. AFAIK, this only happens when the color range in the image is small. Am I doing something wrong?

I'm not familiar with Clut(), but if I understand your problem right, you'll need
image.Format = MagickFormat.Png24;
or
image.Settings.SetDefine("png:color-type", "2");
before your
image.Write("out.png");
Seems counter-intuitive that an image would be downgraded when it has ColorType explicitly set to True Color.
In certain color operations, IM does reset or modify certain settings by default (like grayscale becoming color if you perform certain draw operations), but even by manually setting the ColorType, PNG or any format should be saved by default at that color level and degrade only when explicitly instructed. That's why I feel this might be a bug.
Also, ColorType should take precedence when a define is not set.
Just stumbled over the same issue:
images containing only few colors were saved to Png8, despite ColortType.TrueColor etc.. in the original image. But i found that there is s specific MagickFormat.Png00type which saves the png using the same bitdepth / colortype as the original. I would have assumed that the naming would be the other way arround: MagickFormat.Png doing no optimization/conversion at all , and a specific name doing them ...
Nevertheless , my issue was solved by using MagickFormat.Png00
@FrogsInSpace - thanks so much! This should fix a big problem we have had.
Can we close this @daxpandhi?
@dlemstra If this intended behavior, I highly suggest Png00 should be used by default for saving PNGs. When using Q16-HDR, it would be the anticipated behavior to not have to specify a file format just to save in the quality that was already specified by GreyscaleLinear, etc.
But if that won't be possible for any reason, then we can close this issue.
Sorry to interfere again, but seems that Png00 still does down converting color depth if it can't determine the sourceformat. It only seems to work if you operate on a MagickImage that directly loaded the truecolor image from disk itself. Currently it does'nt work for me when i generate them from a System.Drawing.Bitmap . Will invesitage further
@FrogsInSpace not at all - this is very helpful to know as a couple of very important Studio users are pissed their output from my app (took hours to build) was low quality. So I'm trying to investigate all avenues.
I set mine to Png48 (or Png64 as required) instead of 00. Does that help your situation?
Btw, Tiff is doing this too. I now have this workaround code based on new tests:
private static void PrepareFileFormat(string ext, IMagickImage m)
{
switch (ext)
{
case ".tif":
m.Format = MagickFormat.Tiff64;
break;
case ".png":
m.Format = MagickFormat.Png48;
break;
}
m.Alpha(AlphaOption.Off);
}
In the meantime, this seems to hint some other possible way arround the issue
https://github.com/dlemstra/Magick.NET/issues/225
I have the same issue, but found a different workaround. I'm loading a RGB 8 bit gray image, the ColorType is set to TrueColor. When I write out the image as a tif I get a grayscale (single channel) image, and the ColorType has changed to Grayscale.
When I set the ColorType to TrueColor, even though that was its value already, it saves out a 3 channel image, as expected.
var image = new MagickImage(@"E:\magick\single_channel\full.tif"); // 3 channel gray image.
image.ColorType.Dump(); // TrueColor
image.Write(@"E:\magick\single_channel\out_full.tif"); // Single Channel
image.ColorType.Dump(); // GrayScale
new MagickImage(@"E:\magick\single_channel\out_full.tif").ColorType.Dump(); // Grayscale
var image = new MagickImage(@"E:\magick\single_channel\full.tif"); // 3 channel gray image.
image.ColorType.Dump(); // TrueColor
// Set it to TrueColor eventhough it's already set.
if (image.ColorType == ColorType.TrueColor)
{
image.ColorType = ColorType.TrueColor;
}
image.Write(@"E:\magick\single_channel\out_true_color_full.tif"); // 3 channel
new MagickImage(@"E:\magick\single_channel\out_true_color_full.tif").ColorType.Dump(); // TrueColor
I'm loading a RGB 8 bit gray image, the ColorType is set to TrueColor. When I write out the image as a tif I get a grayscale (single channel) image, and the ColorType has changed to Grayscale.
You'll need -define colorspace:auto-grayscale=off. In Magick.NET use SetAttribute("colorspace:auto-grayscale", "false");
Most helpful comment
Just stumbled over the same issue:
images containing only few colors were saved to Png8, despite
ColortType.TrueColoretc.. in the original image. But i found that there is s specificMagickFormat.Png00type which saves the png using the same bitdepth / colortype as the original. I would have assumed that the naming would be the other way arround:MagickFormat.Pngdoing no optimization/conversion at all , and a specific name doing them ...Nevertheless , my issue was solved by using
MagickFormat.Png00