如何使用 pngcs 中的像素填充位图对象?
How to populate Bitmap object with pixels from pngcs?
像素按顺序存储在 PNG 中(有 2 种变体 - 原始和 Adam 交错)。 RFC 2083 描述了这些布局变体。我想了解如何将流中的像素位置转换为位图中的位置。
有一些障碍 - 序号取决于图像的尺寸(在隔行扫描模式下,可以跳过一些像素,因为它们超出范围)。
这个怎么实现清楚,或者有没有现成的实现?
如果您像您所说的那样使用 pngcs,您将收到一个表示像素数据的整数数组,而无需关心交错。
那就简单了,看https://github.com/leonbloy/pngcs/blob/master/SamplesTests/SampleTileImage.cs的例子,每X个整数就是一个像素,X是由通道数定义的,所以每个整数都是一个像素的一个分量,也就是说每一行都有[组件 * 图像宽度] 整数。
要重建数据,那么一个双循环就足够了,这里是一个包含四个分量 (RGBA) 的 PNG 示例:
int componentSize = 4;
int lineSize = componentSize * imageWidth;
int linesOnImage = imageData.Length / lineSize;
for(int y = 0; y < linesOnImage; y++)
{
for(int x = 0; x < imageWidth; x++)
{
int startPixel = y * lineSize + (x * componentSize);
Color c = Color.FromArgb(imageData[startPixel + 3], imageData[startPixel], imageData[startPixel + 1], imageData[startPixel + 2]);
//Do whatever you need to do with the pixel, you have X and Y coordinates and the color
}
}
请注意,颜色的第一个参数是像素的最后一个分量,因为颜色需要 ARGB 输入,而 PNG 使用 RGBA 顺序。
像素按顺序存储在 PNG 中(有 2 种变体 - 原始和 Adam 交错)。 RFC 2083 描述了这些布局变体。我想了解如何将流中的像素位置转换为位图中的位置。
有一些障碍 - 序号取决于图像的尺寸(在隔行扫描模式下,可以跳过一些像素,因为它们超出范围)。
这个怎么实现清楚,或者有没有现成的实现?
如果您像您所说的那样使用 pngcs,您将收到一个表示像素数据的整数数组,而无需关心交错。
那就简单了,看https://github.com/leonbloy/pngcs/blob/master/SamplesTests/SampleTileImage.cs的例子,每X个整数就是一个像素,X是由通道数定义的,所以每个整数都是一个像素的一个分量,也就是说每一行都有[组件 * 图像宽度] 整数。
要重建数据,那么一个双循环就足够了,这里是一个包含四个分量 (RGBA) 的 PNG 示例:
int componentSize = 4;
int lineSize = componentSize * imageWidth;
int linesOnImage = imageData.Length / lineSize;
for(int y = 0; y < linesOnImage; y++)
{
for(int x = 0; x < imageWidth; x++)
{
int startPixel = y * lineSize + (x * componentSize);
Color c = Color.FromArgb(imageData[startPixel + 3], imageData[startPixel], imageData[startPixel + 1], imageData[startPixel + 2]);
//Do whatever you need to do with the pixel, you have X and Y coordinates and the color
}
}
请注意,颜色的第一个参数是像素的最后一个分量,因为颜色需要 ARGB 输入,而 PNG 使用 RGBA 顺序。