2010-03-04 79 views
1

我想在我的應用程序中實現Photoshop風格的顏色過濾功能。我有一個位圖,和4個複選框(R,G,B,A)。我想知道是什麼做在C#中實現顏色過濾

目前我如下

 Byte[] rgbValues = new Byte[data.Stride * data.Height]; 
     for (int row = 0; row < data.Height; row++) 
     { 
      // Loop through each pixel on this scan line 
      int bufPos = (m_height - row - 1) * m_width; 
      int index = row * data.Stride; 
      for (int col = 0; col < data.Width; col++, bufPos++, index += 4) 
      { 
       bool drawCheckerBoard = true; // for alpha 
       UInt32 rgba = m_image[bufPos]; 
       UInt32 r = EnableRedChannel ? ((rgba >> 0) & 0xFF) : 0x00; 
       UInt32 g = EnableGreenChannel ? ((rgba >> 8) & 0xFF) : 0x00; 
       UInt32 b = EnableBlueChannel ? ((rgba >> 16) & 0xFF) : 0x00; 
       UInt32 a = (rgba >> 24) & 0xFF; 
       ... 
       ... 
      } 
     } 

,然後通常Marshal.Copy和解鎖位等做它的最快的方法...

正如你可以看到它不是一個真正優化的方式,我想要一些更快的方法的建議。

感謝

+0

它真的很慢嗎? – 2010-03-04 04:54:53

回答

0

目前還不清楚,要與各個R,G做什麼,B &一個值,但有一點我看馬上是,你可以移動啓用標誌圈外。

UInt32 rMask = EnableRedChannel ? 0x000000FF : 00; 
    UInt32 gMask = EnableGreenChannel ? 0x0000FF00 : 00; 
    UInt32 bMask = EnableBlueChannel ? 0x00FF0000 : 00; 
    UInt32 aMask = 0xFF000000; 

    for (int row = 0; row < data.Height; row++) 
    { 
     // Loop through each pixel on this scan line 
     int bufPos = (m_height - row - 1) * m_width; 
     int index = row * data.Stride; 
     for (int col = 0; col < data.Width; col++, bufPos++, index += 4) 
     { 
      bool drawCheckerBoard = true; // for alpha 
      UInt32 rgba = m_image[bufPos]; 
      UInt32 r = (rgba & aMask) >> 0; 
      UInt32 g = (rgba & gMask) >> 8; 
      UInt32 b = (rgba & bMask) >> 16; 
      UInt32 a = (rgba & aMask) >> 24; 
      ... 
      ... 
     } 
    } 

前進了一大步,除此之外,你可以建立一個複合面膜,如果你實際上並不需要拔出R,G,B &一個值。

UInt32 mask = 0xFF000000; 
    if (EnableRedChannel) 
     mask |= 0x000000FF; 
    if (EnableGreenChannel) 
     mask |= 0x0000FF00; 
    if (EnableBlueChannel) 
     mask |= 0x00FF0000; 

    for (int row = 0; row < data.Height; row++) 
    { 
     // Loop through each pixel on this scan line 
     int bufPos = (m_height - row - 1) * m_width; 
     int index = row * data.Stride; 
     for (int col = 0; col < data.Width; col++, bufPos++, index += 4) 
     { 
      bool drawCheckerBoard = true; // for alpha 
      UInt32 rgba = m_image[bufPos] & mask; 
      ... 
      ... 
     } 
    } 

您也可能會發現有你m_image你的[]是byte秒的陣列,這將使它更容易挑選出個別顏色通道只需通過數據調整的偏移和步幅有所幫助。

+0

謝謝..會嘗試... – ababeel 2010-03-04 05:01:19