gpt4 book ai didi

c# - 这个解码器有什么问题? (在 C# 中)- 帮助填充额外字节

转载 作者:行者123 更新时间:2023-11-30 18:05:20 25 4
gpt4 key购买 nike

我这里有一个 PCX 解码器,用 C# 编写,旨在返回一个 IntPtr,它指向一个未压缩字节数组(PCX 文件使用 RLE 压缩,但我的解码器应该能够处理这个)。我已经从文件中读取了宽度、尺寸和调色板,对于大多数图像,图像将仅将文件渲染为位图,但有些图像无法正确渲染。图像在那里,颜色也在那里,但实际的位图看起来像是被对角线切割了 4 或 5 次并重新排列。我检查了图像中的平面数量,bpp 也很好。

我认为我的代码有问题,所以如果有人看到错误,请告诉我。


编辑 2:

正如 Guffa 指出的那样,我没有处理任何填充。谁能指出我正确的方向?


代码(抱歉,这里有很多,但它是实际的像素处理器):

IntPtr pBits;
Boolean bRepeat;
Int32 RepeatCount;
Byte ReadByte;
Int32 Row = 0;
Int32 Col = 0;

Byte[] PCXData = new Byte[BytesPerScanline * ScanLines]; //BytesPerScanline * ScanLines);

BinaryReader r = new BinaryReader(file);
r.BaseStream.Seek(128, SeekOrigin.Begin);

while (Row < ScanLines)
{
ReadByte = r.ReadByte();
bRepeat = (0xc0 == (ReadByte & 0xC0));
RepeatCount = (ReadByte & 0x3f);

if (!(Col >= BytesPerScanline))
{
if (bRepeat)
{
ReadByte = r.ReadByte();
while (RepeatCount > 0)
{
PCXData[(Row * BytesPerScanline) + Col] = ReadByte;
RepeatCount -= 1;
Col += 1;
}
}
else
{
PCXData[(Row * BytesPerScanline) + Col] = ReadByte;
Col += 1;
}
}

if (Col >= BytesPerScanline)
{
Col = 0;
Row += 1;
}
}

pBits = System.Runtime.InteropServices.Marshal.AllocHGlobal(PCXData.Length);
System.Runtime.InteropServices.Marshal.Copy(PCXData, 0, pBits, PCXData.Length);

return pBits;

最佳答案

一方面,您没有正确处理非托管资源(例如 BinaryReader)。完成后调用 r.Dispose(),或者将其包装在 using block 中,如下所示:

using(BinaryReader r = new BinaryReader(file))
{
...
}

并且始终对任何实现了 IDisposable 的对象执行此操作.

关于c# - 这个解码器有什么问题? (在 C# 中)- 帮助填充额外字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5679845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com