gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:03:34 26 4
gpt4 key购买 nike

以下函数的目的是从从文件加载的位图中获取每个像素的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com