2017-04-08 86 views
1

以下功能的目的是從文件加載一個位圖得到的各像素的R,G,B值,並通過10分GDIPlus ::位圖亮膚圖像C++

void PerformTransformation(Gdiplus::Bitmap* bitmap, LPCTSTR SaveFileName) { 
    Gdiplus::BitmapData* bitmapData = new Gdiplus::BitmapData; 
    UINT Width = bitmap->GetWidth(); 
    UINT Height = bitmap->GetHeight(); 
    Gdiplus::Rect rect(0, 0,Width,Height); 
    bitmap->LockBits(&rect, Gdiplus::ImageLockModeRead, PixelFormat32bppARGB, bitmapData); 


    byte* pixels = (byte*)bitmapData->Scan0; 
    INT iStride = abs(bitmapData->Stride); 

    for (UINT col = 0; col < Width; ++col) 
     for (UINT row = 0; row < Height; ++row) 
     { 

      unsigned int curColor = pixels[row * iStride/4 + col]; 
      int b = curColor & 0xff; 
      int g = (curColor & 0xff00) >> 8; 
      int r = (curColor & 0xff0000) >> 16;    
      if ((r + 10) > 255) r = 255; else r += 10; 
      if ((g + 10) > 255) g = 255; else g += 10; 
      if ((b + 10) > 255) b = 255; else b += 10; 

      pixels[curColor & 0xff ] = b; 
      pixels[curColor & 0xff00 >> 8] = g; 
      pixels[curColor & 0xff0000 >> 16] = r; 

     } 

    bitmap->UnlockBits(bitmapData);    
    CLSID pngClsid; 
    GetEncoderClsid(L"image/png", &pngClsid); 
    bitmap->Save(SaveFileName, &pngClsid, NULL);  
} 

然而,當增加它們檢查保存文件,亮度沒有增加。我試圖增加值來更新每個R,G,B值爲100,但圖像保持不變,似乎我沒有正確設置新值。

任何人都可以看到我做錯了什麼?

編輯: 下面的一些指導後,我現在有圖像增亮,但只增加了四分之一的圖像。

enter image description here

改變的代碼

void PerformTransformation(Gdiplus::Bitmap* bitmap, LPCTSTR SaveFileName) { 
    Gdiplus::BitmapData* bitmapData = new Gdiplus::BitmapData; 
    UINT Width = bitmap->GetWidth(); 
    UINT Height = bitmap->GetHeight(); 
    Gdiplus::Rect rect(0, 0,Width,Height); 
    // Lock a 5x3 rectangular portion of the bitmap for reading. 
    bitmap->LockBits(&rect, Gdiplus::ImageLockModeWrite, 
PixelFormat32bppARGB, bitmapData); 

    byte* Pixels = (byte*)bitmapData->Scan0; 
    INT stride_bytes_count = abs(bitmapData->Stride); 
    UINT row_index, col_index; 
    byte pixel[4]; 
    for (col_index = 0; col_index < Width; ++col_index) { 
     for (row_index = 0; row_index < Height; ++row_index) 
     { 

      unsigned int curColor = Pixels[row_index * stride_bytes_count/
4 + col_index]; 
      int b = curColor & 0xff; 
      int g = (curColor & 0xff00) >> 8; 
      int r = (curColor & 0xff0000) >> 16; 
      if ((r + 10) > 255) r = 255; else r += 10; 
      if ((g + 10) > 255) g = 255; else g += 10; 
      if ((b + 10) > 255) b = 255; else b += 10; 


      pixel[0] = b; 
      pixel[1] = g; 
      pixel[2] = r; 
      Pixels[row_index * stride_bytes_count/4 + col_index] = *pixel; 
     } 
    } 



    bitmap->UnlockBits(bitmapData);  
    ::DeleteObject(bitmapData); 
    CLSID pngClsid; 
    GetEncoderClsid(L"image/png", &pngClsid); 
    bitmap->Save(SaveFileName, &pngClsid, NULL);  
    } 

}; 
+0

取出索引像素的信道值3行以'像素'開始,並用一行開始替換它們像素[row * iStride/4 + col] = r | (g << 8)| (b << 16)' –

+0

你問了PixelFormat32bppARGB,好主意,但是它使像素爲4個字節,而不是1個字節。所以你必須用'UINT *'來解決它,而不是'byte *'。 –

+0

嗯謝謝你,但是現在我改變了保存任何想法時圖像總是黑色的? –

回答

0
  • 你從來不檢查返回代碼。
  • 您在讀取模式下訪問的位圖數據(Gdiplus :: ImageLockModeRead)
  • 你被顏色值的像素[curColor & 0xff的]
  • 你從不刪除分配BitmapData對象
+0

嗨,謝謝,請立即改變!在這種情況下,我應該如何索引像素通道值? –

+0

將像素定義爲字節[4]的數組,並處理在每個像素寫入(要移動到下一個像素)之後和每個步幅結束之後(移動到下一個步幅的第一個像素)後應遞增的像素迭代器。 – dodo951

+0

在這種情況下,變量是我的Pixel迭代器的行嗎? –