2017-04-08 87 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迭代器的行吗? –