Tools: [skip ci] in thumbnail provider replace CxImage library with Win32 API
This commit is contained in:
@@ -32,8 +32,132 @@
|
||||
#include <iostream>
|
||||
#include <zipios++/zipinputstream.h>
|
||||
#include <zipios++/zipfile.h>
|
||||
#include <CxImage/xmemfile.h>
|
||||
#include <CxImage/ximapng.h>
|
||||
#include <wincodec.h>
|
||||
#include <wincodecsdk.h>
|
||||
#pragma comment(lib, "WindowsCodecs.lib")
|
||||
|
||||
// The functions
|
||||
// * CreateStreamOnResource
|
||||
// * LoadBitmapFromStream
|
||||
// * CreateHBITMAP
|
||||
// are taken from https://faithlife.codes/blog/2008/09/displaying_a_splash_screen_with_c_part_i/
|
||||
// The code is released under an MIT-style license
|
||||
|
||||
// Creates a stream object initialized with the data from an executable resource.
|
||||
IStream* CreateStreamOnResource(void* buffer, size_t length)
|
||||
{
|
||||
// initialize return value
|
||||
IStream* ipStream = NULL;
|
||||
|
||||
// allocate memory to hold the resource data
|
||||
HGLOBAL hgblResourceData = GlobalAlloc(GMEM_MOVEABLE, length);
|
||||
if (hgblResourceData == NULL)
|
||||
goto Return;
|
||||
|
||||
// get a pointer to the allocated memory
|
||||
LPVOID pvResourceData = GlobalLock(hgblResourceData);
|
||||
if (pvResourceData == NULL)
|
||||
goto FreeData;
|
||||
|
||||
// copy the data from the resource to the new memory block
|
||||
CopyMemory(pvResourceData, buffer, length);
|
||||
GlobalUnlock(hgblResourceData);
|
||||
|
||||
// create a stream on the HGLOBAL containing the data
|
||||
|
||||
if (SUCCEEDED(CreateStreamOnHGlobal(hgblResourceData, TRUE, &ipStream)))
|
||||
goto Return;
|
||||
|
||||
FreeData:
|
||||
// couldn't create stream; free the memory
|
||||
|
||||
GlobalFree(hgblResourceData);
|
||||
|
||||
Return:
|
||||
// no need to unlock or free the resource
|
||||
return ipStream;
|
||||
}
|
||||
|
||||
IWICBitmapSource* LoadBitmapFromStream(IStream* ipImageStream)
|
||||
{
|
||||
// initialize return value
|
||||
IWICBitmapSource* ipBitmap = NULL;
|
||||
|
||||
// load WIC's PNG decoder
|
||||
IWICBitmapDecoder* ipDecoder = NULL;
|
||||
if (FAILED(CoCreateInstance(CLSID_WICPngDecoder, NULL, CLSCTX_INPROC_SERVER, __uuidof(ipDecoder), reinterpret_cast<void**>(&ipDecoder))))
|
||||
goto Return;
|
||||
|
||||
// load the PNG
|
||||
if (FAILED(ipDecoder->Initialize(ipImageStream, WICDecodeMetadataCacheOnLoad)))
|
||||
goto ReleaseDecoder;
|
||||
|
||||
// check for the presence of the first frame in the bitmap
|
||||
UINT nFrameCount = 0;
|
||||
if (FAILED(ipDecoder->GetFrameCount(&nFrameCount)) || nFrameCount != 1)
|
||||
goto ReleaseDecoder;
|
||||
|
||||
// load the first frame (i.e., the image)
|
||||
IWICBitmapFrameDecode* ipFrame = NULL;
|
||||
if (FAILED(ipDecoder->GetFrame(0, &ipFrame)))
|
||||
goto ReleaseDecoder;
|
||||
|
||||
// convert the image to 32bpp BGRA format with pre-multiplied alpha
|
||||
// (it may not be stored in that format natively in the PNG resource,
|
||||
// but we need this format to create the DIB to use on-screen)
|
||||
WICConvertBitmapSource(GUID_WICPixelFormat32bppPBGRA, ipFrame, &ipBitmap);
|
||||
ipFrame->Release();
|
||||
|
||||
ReleaseDecoder:
|
||||
ipDecoder->Release();
|
||||
Return:
|
||||
return ipBitmap;
|
||||
}
|
||||
|
||||
HBITMAP CreateHBITMAP(IWICBitmapSource* ipBitmap)
|
||||
{
|
||||
// initialize return value
|
||||
HBITMAP hbmp = NULL;
|
||||
|
||||
// get image attributes and check for valid image
|
||||
UINT width = 0;
|
||||
UINT height = 0;
|
||||
if (FAILED(ipBitmap->GetSize(&width, &height)) || width == 0 || height == 0)
|
||||
goto Return;
|
||||
|
||||
// prepare structure giving bitmap information (negative height indicates a top-down DIB)
|
||||
BITMAPINFO bminfo;
|
||||
ZeroMemory(&bminfo, sizeof(bminfo));
|
||||
bminfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
|
||||
bminfo.bmiHeader.biWidth = width;
|
||||
bminfo.bmiHeader.biHeight = -((LONG)height);
|
||||
bminfo.bmiHeader.biPlanes = 1;
|
||||
bminfo.bmiHeader.biBitCount = 32;
|
||||
bminfo.bmiHeader.biCompression = BI_RGB;
|
||||
|
||||
// create a DIB section that can hold the image
|
||||
void* pvImageBits = NULL;
|
||||
HDC hdcScreen = GetDC(NULL);
|
||||
hbmp = CreateDIBSection(hdcScreen, &bminfo, DIB_RGB_COLORS, &pvImageBits, NULL, 0);
|
||||
ReleaseDC(NULL, hdcScreen);
|
||||
if (hbmp == NULL)
|
||||
goto Return;
|
||||
|
||||
// extract the image into the HBITMAP
|
||||
|
||||
const UINT cbStride = width * 4;
|
||||
const UINT cbImage = cbStride * height;
|
||||
if (FAILED(ipBitmap->CopyPixels(NULL, cbStride, cbImage, static_cast<BYTE*>(pvImageBits))))
|
||||
{
|
||||
// couldn't extract image; delete HBITMAP
|
||||
|
||||
DeleteObject(hbmp);
|
||||
hbmp = NULL;
|
||||
}
|
||||
|
||||
Return:
|
||||
return hbmp;
|
||||
}
|
||||
|
||||
CComModule _Module;
|
||||
|
||||
@@ -146,13 +270,17 @@ HRESULT CFCStdExtractor::Extract(HBITMAP* phBmpThumbnail)
|
||||
content.push_back(c);
|
||||
}
|
||||
|
||||
// pass the memory buffer to CxImage library to create
|
||||
// the bitmap handle
|
||||
CxMemFile mem(&(content[0]),content.size());
|
||||
CxImagePNG png;
|
||||
png.Decode(&mem);
|
||||
m_hPreview = png.MakeBitmap();
|
||||
*phBmpThumbnail = m_hPreview;
|
||||
// pass the memory buffer to CxImage library to create the bitmap handle
|
||||
IStream* stream = CreateStreamOnResource(&(content[0]), content.size());
|
||||
if (stream) {
|
||||
IWICBitmapSource* bmpSrc = LoadBitmapFromStream(stream);
|
||||
stream->Release();
|
||||
if (bmpSrc) {
|
||||
m_hPreview = CreateHBITMAP(bmpSrc);
|
||||
*phBmpThumbnail = m_hPreview;
|
||||
bmpSrc->Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(...) {
|
||||
|
||||
Reference in New Issue
Block a user