-
Notifications
You must be signed in to change notification settings - Fork 2
/
D2DBitmapFile.cpp
100 lines (86 loc) · 2.27 KB
/
D2DBitmapFile.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
#include "StdAfx.h"
#include "StimServer.h"
#include "D2DBitmapFile.h"
#include "OutputWnd.h"
CD2DBitmapFile::CD2DBitmapFile(void)
{
}
CD2DBitmapFile::~CD2DBitmapFile(void)
{
}
bool CD2DBitmapFile::Init(LPCWSTR wzFilename)
{
HRESULT hr = CoCreateInstance(
CLSID_WICImagingFactory, nullptr,
CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&m_pWICfactory) );
ASSERT(hr == S_OK);
if (FAILED(hr)) return false;
hr = m_pWICfactory->CreateDecoderFromFilename(
wzFilename,
nullptr,
GENERIC_READ,
WICDecodeMetadataCacheOnLoad,
&m_pDecoder);
if (FAILED(hr))
{
CString errString = _T("Error reading image file: ");
errString += wzFilename;
switch (hr)
{
case 0x80070003:
errString += "\r\n Cannot find the path specified.";
break;
case 0x8007052E:
errString += "\r\n Logon Failure: unknown user name or bad password.";
break;
case 0x88982F50:
errString += "\r\n Codec not found.";
break;
default:
COutputList::AddString(errString);
errString.Format(_T(" Error Code (hResult): %X"), hr);
}
COutputList::AddString(errString);
m_pWICfactory->Release();
return false;
}
return true;
}
HRESULT CD2DBitmapFile::GetFrameCount(UINT32* nFrames)
{
return m_pDecoder->GetFrameCount(nFrames);
}
HRESULT CD2DBitmapFile::LoadBitmapFile(UINT32 iFrame, ID2D1Bitmap** pBitmap)
{
IWICFormatConverter *pConverter;
IWICBitmapFrameDecode *pBitmapSource;
HRESULT hr = m_pDecoder->GetFrame(iFrame, &pBitmapSource);
ASSERT(hr == S_OK);
if (!SUCCEEDED(hr)) return hr;
// Convert the image format to 32bppPBGRA
// (DXGI_FORMAT_B8G8R8A8_UNORM + D2D1_ALPHA_MODE_PREMULTIPLIED).
hr = m_pWICfactory->CreateFormatConverter(&pConverter);
ASSERT(hr == S_OK);
if (!SUCCEEDED(hr)) return hr;
hr = pConverter->Initialize(
pBitmapSource,
GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone,
nullptr,
0.f,
WICBitmapPaletteTypeMedianCut);
ASSERT(hr == S_OK);
if (!SUCCEEDED(hr)) return hr;
// Create a Direct2D bitmap from the WIC bitmap.
hr = theApp.m_pContext->CreateBitmapFromWicBitmap(
pConverter,
pBitmap);
pConverter->Release();
pBitmapSource->Release();
return hr;
}
void CD2DBitmapFile::Cleanup(void)
{
m_pDecoder->Release();
m_pWICfactory->Release();
}