24位到1位映射转换

本文关键字:转换 映射 1位 24位 | 更新日期: 2023-09-27 18:19:56

对于移动应用程序,我必须从24位位图中生成1位位图。问题是,结果不正确,所以我做了这个小项目,在我的台式电脑上试用。正如你所看到的,创作是有效的,但结果并不好。

你几乎看不到任何东西,因为很多比特不再位于正确的位置,而是向左或向右移动了一些像素。

这是我用来创建的代码:

int z = 0;
int bitNumber = 0;
//the new 1Bit-byte-Array 
byte[] oneBitImage = new byte[(bmp.Height * bmp.Width) / 8];
BitmapData bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
unsafe
{
  byte* p = (byte*)(void*)bmData.Scan0.ToPointer();
  int stopAddress = (int)p + bmp.Height * bmData.Stride;
  while ((int)p != stopAddress)
  {
    if (*p < 128) // is black
    oneBitImage[z] = (byte)(oneBitImage[z] | Exp(bitNumber));   //Set a Bit on the specified position
    p += 3;
    bitNumber++;
    if (bitNumber == 8)
    {
      bitNumber = 0;
      z++;
    }
  }
}
bmp.UnlockBits(bmData);
//Convert into 1-bit-bmp to check result
Bitmap newbmp = new Bitmap(bmp.Width, bmp.Height, PixelFormat.Format1bppIndexed);
BitmapData bmpData = newbmp.LockBits(
                             new Rectangle(0, 0, newbmp.Width, newbmp.Height),
                             ImageLockMode.WriteOnly, newbmp.PixelFormat);
Marshal.Copy(oneBitImage, 0, bmpData.Scan0, oneBitImage.Length);
newbmp.UnlockBits(bmpData);
newbmp.Save(fileName, ImageFormat.Bmp);

简短解释:我每隔三个字节运行一次,如果这个字节——3字节组中的第一个字节(24位中的像素)——低于128,我会在指定位置放置一位。EXP给了我指数。。。

24位到1位映射转换

切换每个输出字节中的位。位7应为位0等。

我会将三个字节转换为"真实"颜色(长值或类似值),看看结果是否大于16.7m的一半。