gpt4 book ai didi

c++ - GDI+加载一个jpg并另存为24bit png的问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:20:15 24 4
gpt4 key购买 nike

问题

大家好!

我有这段代码,它使我的 jpg 图像循环通过改变像素并最终将其保存为 png 类型。问题是生成的图像的位深度为 32 位。我需要它是 24 位的,任何人都可以阐明正确的设置方法吗?我是否在寻找将像素格式设置为 PixelFormat24bppRGB 的正确轨道?

代码

static inline void Brighten(Gdiplus::Bitmap* img)
{
int width = img->GetWidth()/8,height = img->GetHeight(), max = (width*height),r,g,b;
Gdiplus::Color pixel;
for(int a = 0,x = 0, y = -1; a < max; ++a)
{
x = a%width;
if(x == 0)
++y;
img->GetPixel(x,y,&pixel);
r = pixel.GetR();
g = pixel.GetG();
b = pixel.GetB();
if (r > 245) r = 245;
if (g > 245) g = 245;
if (b > 245) b = 245;
r += 10;
g += 10;
b += 10;
pixel = Gdiplus::Color(r,g,b);
img->SetPixel(x,y,pixel);;
}
}

ULONG_PTR m_dwToken = 0;
Gdiplus::GdiplusStartupInput input;
Gdiplus::GdiplusStartupOutput output;
Gdiplus::GdiplusStartup( &m_dwToken, &input, &output );
USES_CONVERSION_EX;
Gdiplus::ImageCodecInfo* pEncoders = static_cast< Gdiplus::ImageCodecInfo* >( _ATL_SAFE_ALLOCA(1040, _ATL_SAFE_ALLOCA_DEF_THRESHOLD));
Gdiplus::DllExports::GdipGetImageEncoders(5, 1040, pEncoders );
CLSID clsidEncoder = pEncoders[4].Clsid;

Gdiplus::Bitmap img1((CT2W)L"IMG_1.JPG");
Brighten(&img1);
img1.Save((CT2W)L"IMG_1_R3.PNG",&clsidEncoder,NULL);

提前致谢!

最佳答案

好的,我实际上修复了它,我使用 LockBits() 直接访问这些位并修改它们,这给了我急需的性能提升并将其保持为 24 位图像。

static inline void Brighten(Gdiplus::Bitmap* img)
{
int width = img->GetWidth(),height = img->GetHeight(),r,g,b;
Gdiplus::Rect rect(0,0,width,height);
Gdiplus::BitmapData data;
img->LockBits(&rect,Gdiplus::ImageLockModeWrite,PixelFormat24bppRGB,&data);
int stride = data.Stride,offset = stride - width*3;
byte * p1 = (byte *)(void *)data.Scan0;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
r = p1[0];
g = p1[1];
b = p1[2];
if (r > 245) r = 245;
if (g > 245) g = 245;
if (b > 245) b = 245;
r += 10;
g += 10;
b += 10;
p1[0] = r;
p1[1] = g;
p1[2] = b;
p1 += 3;
}
p1 += offset;
}
img->UnlockBits(&data);
}

感谢您的帮助!

关于c++ - GDI+加载一个jpg并另存为24bit png的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2444148/

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