C# 像素位深8位、10位、12位之间的转换


一、灰度图像转换

1.1 12位转8位

// 将IntPtr的12位图像转换为8位图像result
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];
	
	//for (int i = 0; i < height; i++)
	//{
	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);
	}
}
//将12位(16bits)转化成8位(8bits)
private void convertBuffer12to8(byte[] buffer12Bit, byte[] buffer8Bit)
{
	 for (int src = 0, dst = 0; src < buffer12Bit.Length; dst++)
	 {
	     //int value16 = buffer12Bit[src++];
	     //value16 = value16 + (buffer12Bit[src++] << 8);
	     //buffer8Bit[dst] = (byte)(value16 / 257.0 + 0.5);
	     int value12 = buffer12Bit[src++];
	     value12 = value12 + (buffer12Bit[src++] << 8);
	     buffer8Bit[dst] = (byte)(value12 / 16);
	 }
}

1.2 10位转8位

// 将IntPtr的10位图像转换为8位图像result
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];
	
	 //for (int i = 0; i < height; i++)
	 //{
	 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);
}
//将10位(16bits)转化成8位(8bits)
private void convertBuffer10to8(byte[] buffer10Bit, byte[] buffer8Bit)
{
	for (int src = 0, dst = 0; src < buffer10Bit.Length; dst++)
	{
	    //int value16 = buffer12Bit[src++];
	    //value16 = value16 + (buffer12Bit[src++] << 8);
	    //buffer8Bit[dst] = (byte)(value16 / 257.0 + 0.5);
	    int value12 = buffer10Bit[src++];
	    value12 = value12 + (buffer10Bit[src++] << 8);
	    buffer8Bit[dst] = (byte)(value12 / 4);
	}
}
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
将24转换8位图的过程中,需要用到调色板。调色板是一个包含256种颜色的表格,每个颜色都由三个8位组成的RGB值来表示。具体的转换步骤如下: 1. 创建一个空白的8位图,并设置它的宽度和高度与原始24图相同。 2. 创建一个调色板,其中包含256种颜色。可以使用ColorPalette类来创建调色板。 3. 遍历原始24图的每个像素,将它的RGB值转换成一个0到255之间的整数,然后将该整数作为调色板中对应颜色的索引,将索引值写入新的8位图中。 4. 将调色板与新的8位图相关联,使用Bitmap类的SetPixel和GetPixel方法可以完成这一步操作。 5. 最后保存新的8位图即可。 下面是一个C#代码示例,可以将24转换8位图: ```csharp public static Bitmap ConvertTo8bpp(Bitmap bmp) { // 创建一个新的8位Bitmap newBmp = new Bitmap(bmp.Width, bmp.Height, PixelFormat.Format8bppIndexed); // 创建调色板 ColorPalette pal = newBmp.Palette; for (int i = 0; i < 256; i++) { pal.Entries[i] = Color.FromArgb(i, i, i); } newBmp.Palette = pal; // 遍历原始图的每个像素,并将RGB值转换成索引 for (int y = 0; y < bmp.Height; y++) { for (int x = 0; x < bmp.Width; x++) { Color color = bmp.GetPixel(x, y); int index = (int)(0.299 * color.R + 0.587 * color.G + 0.114 * color.B); newBmp.SetPixel(x, y, Color.FromArgb(index, index, index)); } } return newBmp; } ``` 在这个示例中,使用了YUV颜色空间的转换公式将RGB值转换成了索引。可以根据具体需求使用不同的转换公式。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值