我正在使用 c++ 做一些图像工作,我希望能够使用 jpeg 压缩文件。GDI+ 库似乎有我需要的东西,特别是 Gdiplus::Bitmap::GetPixel() 例程是我最终想要使用的。
有几个 Gdiplus::Bitmap 构造函数,我想使用的一个接受一个 BITMAPINFO 结构和一个指向图像位的指针。Bitmap.Bitmap(const BITMAPINFO* lpbmi, VOID* lpbits) 我已经从挂钩的StretchDIBits()中获得了这些信息,所以我什至没有创建原始的 BITMAP,它是给我的。但是位图上是为JPEG设置的压缩(BITMAPINFO.BITMAPINFOHEADER.biCompression = BI_JPEG),所以解析不好,也不知道怎么做,或者想写个jpeg解压算法。
以下是我用来尝试利用 GDI+ 库来查看 jpeg 压缩图像中的单个像素的代码:
// lpbmi and lpBits come from a hooked StretchDIBits call. I did not generate this data.
// The following is some sample data that i've seen go in and not work:
//
// lpbmi->bmiHeader.biSize = 40
// lpbmi->bmiHeader.biWidth = 1299
// lpbmi->bmiHeader.biHeight = -1
// lpbmi->bmiHeader.biPlanes = 1
// lpbmi->bmiHeader.biBitCount = 0
// lpbmi->bmiHeader.biCompression = 4 // resolves to BI_JPEG
// lpbmi->bmiHeader.biSizeImage = 955
// lpbmi->bmiHeader.biXpelsPerMeter = 0
// lpbmi->bmiHeader.biYpelsPerMeter = 0
// lpbmi->bmiHeader.biClrUsed = 0
// lpbmi->bmiHeader.biClrImportant = 0
//
// This data looks valid to me, so i don't know why nothing happens when trying to convert
// it into a Gdiplus::Bitmap
void SomeMethod(const BITMAPINFO * lpbmi, const void * lpBits)
{
// Apparently, this is needed before using GDI+ libraries. I experimented
// without this and nothing different happend. Not even a crash.
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
// Because the BitmapClass doesn't take in a (const void *), I have to copy the data
// into another array. Not much i can do about it because i'm given lpBits as a
// (const void *)
void * data = malloc(lpbmi->bmiHeader.biSizeImage);
memcpy(data, lpBits, lpbmi->bmiHeader.biSizeImage);
// I "expect" that this bascially reads in the data provided and creates our
// Bitmap class.
Bitmap bitmap(lpbmi, data);
// The problem is, Width and Height are always 0. Can't figure out why.
UINT Width = bitmap->GetWidth(); // Why always 0????
UINT Height = bitmap->GetHeight(); // Why always 0????
//////////////////////
// Some more code ...
//////////////////////
}
任何人都知道为什么 Gdiplus::Bitmap 类没有对我提供的数据做任何事情?