2012-03-17 76 views
2

我编写了下面的代码来操作图像的颜色。我想以某种方式撕开图像的每个像素。因此,对于每个像素,我想访问5位红色,6位绿色和5位蓝色(按照16位图像)。我如何改变我的代码来做到这一点?我想我必须以某种方式将我设置的字节值转换为位?字节到BMP中获取RGB的位

任何帮助将是伟大的。

 private Bitmap InvertBitmap(Bitmap bmp) 
     { 

      unsafe 
      { 
       //create an empty bitmap the same size as original 
       Bitmap newBitmap = new Bitmap(bmp.Width, bmp.Height); 

      //lock the original bitmap in memory 
      System.Drawing.Imaging.BitmapData originalData = bmp.LockBits(
       new Rectangle(0, 0, bmp.Width, bmp.Height), 
       System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); 

      //lock the new bitmap in memory 
      System.Drawing.Imaging.BitmapData newData = newBitmap.LockBits(
       new Rectangle(0, 0, bmp.Width, bmp.Height), 
       System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); 

      //set the number of bytes per pixel 
      int pixelSize = 3; 

      for (int y = 0; y < bmp.Height; y++) 
      { 
       //get the data from the original image 
       byte* originalImageRow = (byte*)originalData.Scan0 + (y * originalData.Stride); 

       //get the data from the new image 
       byte* newImageRow = (byte*)newData.Scan0 + (y * newData.Stride); 

       for (int x = 0; x < bmp.Width; x++) 
       { 

        //set the new image's pixel to the inverted version 

        newImageRow[x * pixelSize] = (byte)(255 - originalImageRow[x * pixelSize + 0]); //B 
        newImageRow[x * pixelSize + 1] = (byte)(255 - originalImageRow[x * pixelSize + 1]); //G 
        newImageRow[x * pixelSize + 2] = (byte)(255 - originalImageRow[x * pixelSize + 2]); //R 
       } 
      } 

      //unlock the bitmaps 
      newBitmap.UnlockBits(newData); 
      bmp.UnlockBits(originalData); 

      return newBitmap; 
     } 
} 
+0

565是16-bpp编码。 24-bpp图像有8位用于R,G和B,没有什么特别的需要。 – 2012-03-17 01:34:27

+0

woops,你的权利,我想采取24位和操纵它,以便它是一个16位颜色的24位图像,对不起,将修复 – BigBug 2012-03-17 01:52:39

+0

不知道这是否合理。我想你想和R和B与0xf8和G与0xfc设置低位为零。使用&运算符。很难看出差异,特别是在液晶显示器上。最快的方法就是将Graphics.DrawImage()图像转换为16bpp位图。 – 2012-03-17 01:55:19

回答

2

如果有一个16位的整数x,则可以通过第一掩蔽那些位用二进制AND,那么结果移位提取在其内的位的范围。像这样:

int x = 33808; // 1000010000010000, for testing 

int r = (x & 63488) >> 11; // 63488 = 1111100000000000 
int g = (x & 2016) >> 5;  // 2016 = 0000011111100000 
int b = (x & 31);    // 31 = 0000000000011111 

// r = 10000 
// g = 100000 
// b = 10000 

我希望有帮助。

+0

嗯,所以你的意思是将所有R G和B值存储在16位整数?然后从那里几乎解析int获得位? – BigBug 2012-03-17 00:46:39

+0

我希望能够将R值内的2位更改为图像每个像素中不同的东西。 – BigBug 2012-03-17 00:47:07

1

RGB24是每个颜色通道1个字节,所以你不需要做任何操作就可以从你已有的数据中提取它们。 “获取位”并没有什么意义,因为你可以设置它们的值,例如

newImageRow[x * pixelSize] = (byte)(originalImageRow[x * pixelSize + 0] | 0x80); //B 

将设置新的图像蓝色通道与原始图像蓝色通道,但将高阶位设置为1。

newImageRow[x * pixelSize] = (byte)(originalImageRow[x * pixelSize + 0]^0xFF); //B 

将反转信道。

所以你真的只需要使用按位运算符(| & >> < < ^)对你已有的数据。