一、灰度图像转换
1.1 12位转8位
private void getBitmap12to8Bit(IntPtr Intptr12Bits, ref Bitmap result)
{
int height = 512;
int width = 512;
int stride = 1024;
byte[] buffer12Bit = new byte[height * stride];
byte[] buffer8Bit = new byte[height * width];
Rectangle imgRect = new Rectangle(0, 0, width, height);
BitmapData imgData = result.LockBits(imgRect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
if (buffer8Bit == null)
buffer8Bit = new byte[imgData.Stride];
else
Array.Clear(buffer8Bit, 0, buffer8Bit.Length);
System.Runtime.InteropServices.Marshal.Copy(Intptr12Bits, buffer12Bit, 0, buffer12Bit.Length);
convertBuffer12to8(buffer12Bit, buffer8Bit);
Marshal.Copy(buffer8Bit, 0, imgData.Scan0, buffer8Bit.Length);
result.UnlockBits(imgData);
}
}
private void convertBuffer12to8(byte[] buffer12Bit, byte[] buffer8Bit)
{
for (int src = 0, dst = 0; src < buffer12Bit.Length; dst++)
{
int value12 = buffer12Bit[src++];
value12 = value12 + (buffer12Bit[src++] << 8);
buffer8Bit[dst] = (byte)(value12 / 16);
}
}
1.2 10位转8位
private void getBitmap10to8Bit(IntPtr Intptr10Bits, ref Bitmap result)
{
int height = 512;
int width = 512;
int stride = 1024;
byte[] buffer10Bit = new byte[height * stride];
byte[] buffer8Bit = new byte[height * width];
Rectangle imgRect = new Rectangle(0, 0, width, height);
BitmapData imgData = result.LockBits(imgRect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
if (buffer8Bit == null)
buffer8Bit = new byte[imgData.Stride];
else
Array.Clear(buffer8Bit, 0, buffer8Bit.Length);
System.Runtime.InteropServices.Marshal.Copy(Intptr10Bits, buffer10Bit, 0, buffer10Bit.Length);
convertBuffer10to8(buffer10Bit, buffer8Bit);
Marshal.Copy(buffer8Bit, 0, imgData.Scan0, buffer8Bit.Length);
result.UnlockBits(imgData);
}
private void convertBuffer10to8(byte[] buffer10Bit, byte[] buffer8Bit)
{
for (int src = 0, dst = 0; src < buffer10Bit.Length; dst++)
{
int value12 = buffer10Bit[src++];
value12 = value12 + (buffer10Bit[src++] << 8);
buffer8Bit[dst] = (byte)(value12 / 4);
}
}