64 / 32bpp BitmapData字节处理

时间:2018-06-07 18:46:24

标签: c# bitmap system.drawing

我正在使用一些系统来使用64 / 32bpp图像的bitmapdata,我不知道这种按位操作是否正确。

Marshal.Copy(pointer, bytePixelData, 0, bytePixelDataLength);

if (bytesPerPixel == 8)
{
    int ushortPixelDataLength = bytePixelData.Length / 2;
    ushort[] ushortPixelData = new ushort[ushortPixelDataLength];

    for (int i = 0; i < ushortPixelDataLength; i++)
    {
        ushortPixelData[i] = (ushort)(bytePixelData[2 * i] << 8 | bytePixelData[2 * i + 1]);
    }

    for (int y = 0; y < bmpData.Height; y++)
    {
        for (int x = 0; x < bmpData.Width; x++)
        {
            long offset = y * bmpData.Stride + x * bytesPerPixel / 2;
            //Do stuff, in this case, opacity change.
            ushortPixelData[offset + (bytesPerPixel / 2) - 1] = (ushort)Math.Round(opacity * ushort.MaxValue);
            if (isPreMultiplied)
            {
                for (int _ushort = 0; _ushort < (bytesPerPixel / 2) - 2; _ushort++)
                {
                    ushortPixelData[offset + _ushort] = (ushort)(ushortPixelData[offset + _ushort] * opacity);
                }
            }
        }
    }
    for (int i = 0; i < ushortPixelDataLength; i++)
    {
        bytePixelData[i] = (byte)(ushortPixelData[i] >> 8);
        bytePixelData[i + 1] = (byte)(ushortPixelData[i]);
    }
}
else if (bytesPerPixel == 4)
{//32bpp, no problem here.
}
else
{
    throw new NotSupportedException("Go to sleep");
}

Marshal.Copy(bytePixelData, 0, pointer, bytePixelDataLength);

有什么方法可以清洁吗?我的代码是否正确? 如果我想实现128bpp我该怎么办? 是否有内置的方法在WPF(System.Windows ...)中执行此操作?

0 个答案:

没有答案