保存 PNG 时未保存颜色通道
Color channel not saved when saving PNG
我正在尝试制作一个从 PNG 中提取颜色并将其放入查找纹理中的应用程序,然后保存使用颜色通道存储 LUT 位置的原始 PNG 的副本。这适用于 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)
找到问题了!事实证明我最初发布的代码确实可以正常工作,但我不小心将黑色保存到我不应该拥有的 LUT 的某些部分。如果有人感兴趣,修复在第 62 行 here。
我正在尝试制作一个从 PNG 中提取颜色并将其放入查找纹理中的应用程序,然后保存使用颜色通道存储 LUT 位置的原始 PNG 的副本。这适用于 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)
找到问题了!事实证明我最初发布的代码确实可以正常工作,但我不小心将黑色保存到我不应该拥有的 LUT 的某些部分。如果有人感兴趣,修复在第 62 行 here。