【问题标题】:Color channel not saved when saving PNG保存PNG时未保存颜色通道
【发布时间】:2020-01-05 22:10:46
【问题描述】:

我正在尝试制作一个从 PNG 中提取颜色并将它们放入查找纹理的应用程序,然后保存原始 PNG 的副本,该副本使用颜色通道来存储 LUT 的位置。这适用于 1D LUT,但不会为 2D LUT 保存额外的通道。

图像输出后,我在 Photoshop 中加载它以检查通道,红色通道正确保存但绿色通道未正确保存。

哪个通道似乎无关紧要,由于某种原因,它不会为foundRow 保存除 0 之外的任何内容,即使在调试器中单步执行时我可以验证 foundRow 至少达到 3对于这个特定的图像。

private static bool ProcessImage(string filePath, string newPath, Color[,] colors, ref int colorsColumnIndex, ref int colorsRowIndex)
        {
            Console.WriteLine($"Processing file: {filePath}");

            var bmp = new Bitmap(filePath);

            for (int y = 0; y < bmp.Height; y++)
            {
                for (int x = 0; x < bmp.Width; x++)
                {
                    var pixel = bmp.GetPixel(x, y);
                    var lutPixel = Color.FromArgb(pixel.R, pixel.G, pixel.B);

                    int foundColumn;
                    int foundRow;
                    GetColorIndexes(colors, lutPixel, out foundColumn, out foundRow);

                    if (foundColumn < 0 || foundRow < 0)
                    {
                        foundColumn = colorsColumnIndex;
                        foundRow = colorsRowIndex;

                        colors[foundColumn, foundRow] = lutPixel;

                        colorsColumnIndex++;
                        if (colorsColumnIndex >= 256)
                        {
                            colorsColumnIndex = 0;
                            colorsRowIndex++;
                        }
                    }

                    if (colorsColumnIndex >= 256 || colorsRowIndex >= 256)
                    {
                        Console.WriteLine($"ERROR: More than {256 * 256} colors found!");
                        return false;
                    }

                    //if (foundRow != 0)
                        //Console.Write($"Setting pixel at {x}, {y} to R: {foundColumn}, G: {foundRow}");

                    var newPixel = Color.FromArgb(255, foundColumn, foundRow, 0);
                    bmp.SetPixel(x, y, newPixel);
                }
            }

            bmp.Save(newPath, ImageFormat.Png);
            bmp.Dispose();
            Console.WriteLine($"Saved {newPath}");

            return true;
        }

在这些行:

var newPixel = Color.FromArgb(255, foundColumn, foundRow, 0);
bmp.SetPixel(x, y, newPixel);

如果 newPixel 是 ARGB(255, 1, 1, 0),则实际保存的文件会因某种原因导致 ARGB(255, 1, 0, 0)

【问题讨论】:

  • 你能否生成一个更小的minimal reproducible example,它不依赖于我们没有的外部文件或方法?
  • 您的代码是否有可能覆盖了您的像素,我的快速测试表明一切正常
  • 糟糕,我在一段未分享的代码中发现了问题!如果有人感兴趣,我的解决方法是在第 62 行 here

标签: c# .net-core system.drawing


【解决方案1】:

找到问题了!原来我最初发布的代码确实可以正常工作,但我不小心将黑色保存到我不应该拥有的 LUT 的某些部分中。如果有人感兴趣,修复在第 62 行 here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-17
    • 1970-01-01
    • 2019-03-18
    • 1970-01-01
    • 2017-06-16
    • 2016-05-17
    • 2018-10-25
    • 2012-11-19
    相关资源
    最近更新 更多