【发布时间】:2011-08-02 15:12:25
【问题描述】:
我一直在研究位图解码器,但我处理像素数据的算法似乎不太正确:
public IntPtr ReadPixels(Stream fs, int offset, int width, int height, int bpp)
{
IntPtr bBits;
int pixelCount = bpp * width * height;
int Row = 0;
decimal value = ((bpp*width)/32)/4;
int RowSize = (int)Math.Ceiling(value);
int ArraySize = RowSize * Math.Abs(height);
int Col = 0;
Byte[] BMPData = new Byte[ArraySize];
BinaryReader r = new BinaryReader(fs);
r.BaseStream.Seek(offset, SeekOrigin.Begin);
while (Row < height)
{
Byte ReadByte;
if (!(Col >= RowSize))
{
ReadByte = r.ReadByte();
BMPData[(Row * RowSize) + Col] = ReadByte;
Col += 1;
}
if (Col >= RowSize)
{
Col = 0;
Row += 1;
}
}
bBits = System.Runtime.InteropServices.Marshal.AllocHGlobal(BMPData.Length);
System.Runtime.InteropServices.Marshal.Copy(BMPData, 0, bBits, BMPData.Length);
return bBits;
}
我只能处理单色位图,并且在某些情况下,位图的某些部分处理得很好。没有一个被压缩,它们被颠倒渲染并翻转。我真的可以在这方面提供一些帮助。
【问题讨论】:
-
@user646265:你为什么使用这个十进制值 = ((bpp*width)/32)/4 ?这个是来做什么的?我的意思是 /32 和 /4...
-
32 和 4 是具有浮动子像素和 4 个通道的图像的专用案例。不适用于任何其他图像数据格式。
标签: c# .net image image-processing