c++ - GDIPlus::位图亮化图像 C++

标签 c++ image-processing gdi+

以下函数的目的是从从文件加载的位图中获取每个像素的 R、G、B 值并将它们增加 10。

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);    
    }

};

最佳答案

  • 您从不检查返回码。
  • 您以读取模式访问位图数据 (Gdiplus::ImageLockModeRead)
  • 您正在按颜色值 pixels[curColor & 0xff] 索引像素 channel 值
  • 永远不要删除分配的 bitmapData 对象

关于c++ - GDIPlus::位图亮化图像 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43295016/

相关文章:

c++ - 具有多重继承的模糊基数

image-processing - 用于图像拼接的相位相关 2D(ImageJ 中的 Stitch 2D)

c++ - 如果没有窗口,opencv cv::addText 异常

用于图像处理的 PHP 或 Python?

c# - 缩放到c#中的一个点

c++ - 在ov5640上使用V4L2设置ctrl

c++ - 尝试重载输出运算符时出错

c++ - "int"类型的参数与 "int"类型的参数不兼容

c++ - GDI+ 图形对象 - 使用 GDI 直接绘制到 DC 时抗锯齿丢失

c# - 通过 C# 图形绘制 "transparent"文本,但以某种方式将绘制的文本变为 "missing",因此它在生成的图像中是透明的