-
Notifications
You must be signed in to change notification settings - Fork 1
/
imageloader.cpp
119 lines (98 loc) · 2.53 KB
/
imageloader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "stdafx.h"
#include "collapsible.h"
#include "imageloader.h"
ImageLoader::ImageLoader()
: m_pImagingFactory(NULL)
{
HRESULT hr;
hr = CoCreateInstance(CLSID_WICImagingFactory, NULL,
CLSCTX_INPROC_SERVER, IID_IWICImagingFactory,
(LPVOID*)&m_pImagingFactory);
if (FAILED(hr)) {
return;
}
}
ImageLoader::~ImageLoader()
{
SafeRelease(&m_pImagingFactory);
}
HRESULT ImageLoader::loadImageResource(ID2D1RenderTarget *pRenderTarget, LPCWSTR szName, ID2D1Bitmap **lpOut)
{
HRESULT hr;
HRSRC imageResHandle = NULL;
HGLOBAL imageResDataHandle = NULL;
void *pImageFile = NULL;
DWORD imageFileSize = 0;
IWICBitmapDecoder *pDecoder = NULL;
IWICBitmapFrameDecode *pSource = NULL;
IWICStream *pStream = NULL;
IWICFormatConverter *pConverter;
ID2D1Bitmap *pImage;
imageResHandle = FindResource(HINST_THISCOMPONENT, szName, L"Image");
hr = imageResHandle ? S_OK : E_FAIL;
if (FAILED(hr)) {
goto cleanup;
}
imageResDataHandle = LoadResource(HINST_THISCOMPONENT, imageResHandle);
hr = imageResHandle ? S_OK : E_FAIL;
if (FAILED(hr)) {
goto cleanup;
}
// Lock it to get a system memory pointer.
pImageFile = LockResource(imageResDataHandle);
hr = pImageFile ? S_OK : E_FAIL;
if (FAILED(hr)) {
goto cleanup;
}
imageFileSize = SizeofResource(HINST_THISCOMPONENT, imageResHandle);
hr = imageFileSize ? S_OK : E_FAIL;
if (FAILED(hr)) {
goto cleanup;
}
hr = m_pImagingFactory->CreateStream(&pStream);
if (FAILED(hr)) {
goto cleanup;
}
hr = pStream->InitializeFromMemory(
reinterpret_cast<BYTE*>(pImageFile),
imageFileSize
);
if (FAILED(hr)) {
goto cleanup;
}
hr = m_pImagingFactory->CreateDecoderFromStream(pStream, NULL,
WICDecodeMetadataCacheOnDemand, &pDecoder);
if (FAILED(hr)) {
goto cleanup;
}
hr = pDecoder->GetFrame(0, &pSource);
if (FAILED(hr)) {
goto cleanup;
}
hr = m_pImagingFactory->CreateFormatConverter(&pConverter);
if (FAILED(hr)) {
goto cleanup;
}
hr = pConverter->Initialize(
pSource,
GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone,
NULL,
0.f,
WICBitmapPaletteTypeMedianCut
);
if (FAILED(hr)) {
goto cleanup;
}
hr = pRenderTarget->CreateBitmapFromWicBitmap(pConverter, NULL, &pImage);
if (FAILED(hr)) {
goto cleanup;
}
*lpOut = pImage;
cleanup:
SafeRelease(&pConverter);
SafeRelease(&pStream);
SafeRelease(&pDecoder);
SafeRelease(&pSource);
return hr;
}