无法将JPEG编码的BITMAP转换为Gdiplus :: Bitmap对象

时间:2012-04-26 23:39:15

标签: c++ bitmap gdi+ jpeg gdi

我正在使用c ++做一些图像工作,我希望能够使用jpeg压缩文件。 GDI +库似乎有我需要的东西,特别是Gdiplus :: Bitmap :: GetPixel()例程是我最终想要使用的。

有几个Gdiplus :: Bitmap构造函数,我想要使用的构造函数采用BITMAPINFO结构和指向图像位的指针。 Bitmap.Bitmap(const BITMAPINFO* lpbmi, VOID* lpbits) 我已经从钩子 StretchDIBits() 获得了这些信息所以我甚至没有创建原始的BITMAP,而是给了我。但是压缩是在位图 (BITMAPINFO.BITMAPINFOHEADER.biCompression = BI_JPEG) 上设置为 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类没有对我提供的数据做任何事情?

0 个答案:

没有答案