-
Notifications
You must be signed in to change notification settings - Fork 0
/
UvttThumbnailProvider.cpp
287 lines (257 loc) · 7.67 KB
/
UvttThumbnailProvider.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved
#include <shlwapi.h>
#include <Wincrypt.h> // For CryptStringToBinary.
#include <thumbcache.h> // For IThumbnailProvider.
#include <wincodec.h> // Windows Imaging Codecs
#include <msxml6.h>
#include <new>
#include <winrt/windows.data.json.h>
#include <winrt/windows.storage.streams.h>
#include <winrt/windows.foundation.h>
#include <iostream>
#include <winrt/base.h>
using namespace winrt;
using namespace Windows::Storage::Streams;
using namespace Windows::Data::Json;
#pragma comment(lib, "shlwapi.lib")
#pragma comment(lib, "windowscodecs.lib")
#pragma comment(lib, "Crypt32.lib")
#pragma comment(lib, "msxml6.lib")
// this thumbnail provider implements IInitializeWithStream to enable being hosted
// in an isolated process for robustness
class CRecipeThumbProvider : public IInitializeWithStream,
public IThumbnailProvider
{
public:
CRecipeThumbProvider() : _cRef(1), _pStream(NULL)
{
}
virtual ~CRecipeThumbProvider()
{
if (_pStream)
{
_pStream->Release();
}
}
// IUnknown
IFACEMETHODIMP QueryInterface(REFIID riid, void** ppv)
{
static const QITAB qit[] =
{
QITABENT(CRecipeThumbProvider, IInitializeWithStream),
QITABENT(CRecipeThumbProvider, IThumbnailProvider),
{ 0 },
};
return QISearch(this, qit, riid, ppv);
}
IFACEMETHODIMP_(ULONG) AddRef()
{
return InterlockedIncrement(&_cRef);
}
IFACEMETHODIMP_(ULONG) Release()
{
ULONG cRef = InterlockedDecrement(&_cRef);
if (!cRef)
{
delete this;
}
return cRef;
}
// IInitializeWithStream
IFACEMETHODIMP Initialize(IStream* pStream, DWORD grfMode);
// IThumbnailProvider
IFACEMETHODIMP GetThumbnail(UINT cx, HBITMAP* phbmp, WTS_ALPHATYPE* pdwAlpha);
private:
HRESULT _GetBase64EncodedImageString(UINT cx, PWSTR* ppszResult);
HRESULT _GetStreamFromString(PCWSTR pszImageName, IStream** ppStream);
long _cRef;
IStream* _pStream; // provided during initialization.
};
HRESULT CRecipeThumbProvider_CreateInstance(REFIID riid, void** ppv)
{
CRecipeThumbProvider* pNew = new (std::nothrow) CRecipeThumbProvider();
HRESULT hr = pNew ? S_OK : E_OUTOFMEMORY;
if (SUCCEEDED(hr))
{
hr = pNew->QueryInterface(riid, ppv);
pNew->Release();
}
return hr;
}
// IInitializeWithStream
IFACEMETHODIMP CRecipeThumbProvider::Initialize(IStream* pStream, DWORD)
{
HRESULT hr = E_UNEXPECTED; // can only be inited once
if (_pStream == NULL)
{
// take a reference to the stream if we have not been inited yet
hr = pStream->QueryInterface(&_pStream);
}
return hr;
}
HRESULT CRecipeThumbProvider::_GetBase64EncodedImageString(UINT /* cx */, PWSTR* ppszResult)
{
*ppszResult = NULL;
const size_t bufferSize = 4096;
std::vector<char> buffer(bufferSize);
std::stringstream ss;
ULONG bytesRead = 0;
while (SUCCEEDED(_pStream->Read(buffer.data(), bufferSize, &bytesRead)) && bytesRead > 0)
{
ss.write(buffer.data(), bytesRead);
}
try {
JsonObject json = JsonObject::Parse(to_hstring(ss.str()));
hstring image = json.GetNamedString(L"image");
SHStrDupW((wchar_t*)image.c_str(), ppszResult);
return S_OK;
}
catch (...) {
return E_UNEXPECTED;
}
}
// Decodes the base64-encoded string to a stream.
HRESULT CRecipeThumbProvider::_GetStreamFromString(PCWSTR pszImageName, IStream** ppImageStream)
{
HRESULT hr = E_FAIL;
DWORD dwDecodedImageSize = 0;
DWORD dwSkipChars = 0;
DWORD dwActualFormat = 0;
// Base64-decode the string
BOOL fSuccess = CryptStringToBinaryW(pszImageName, NULL, CRYPT_STRING_BASE64,
NULL, &dwDecodedImageSize, &dwSkipChars, &dwActualFormat);
if (fSuccess)
{
BYTE* pbDecodedImage = (BYTE*)LocalAlloc(LPTR, dwDecodedImageSize);
if (pbDecodedImage)
{
fSuccess = CryptStringToBinaryW(pszImageName, lstrlenW(pszImageName), CRYPT_STRING_BASE64,
pbDecodedImage, &dwDecodedImageSize, &dwSkipChars, &dwActualFormat);
if (fSuccess)
{
*ppImageStream = SHCreateMemStream(pbDecodedImage, dwDecodedImageSize);
if (*ppImageStream != NULL)
{
hr = S_OK;
}
}
LocalFree(pbDecodedImage);
}
}
return hr;
}
HRESULT ConvertBitmapSourceTo32BPPHBITMAP(IWICBitmapSource* pBitmapSource,
IWICImagingFactory* pImagingFactory,
HBITMAP* phbmp)
{
*phbmp = NULL;
IWICBitmapSource* pBitmapSourceConverted = NULL;
WICPixelFormatGUID guidPixelFormatSource;
HRESULT hr = pBitmapSource->GetPixelFormat(&guidPixelFormatSource);
if (SUCCEEDED(hr) && (guidPixelFormatSource != GUID_WICPixelFormat32bppBGRA))
{
IWICFormatConverter* pFormatConverter;
hr = pImagingFactory->CreateFormatConverter(&pFormatConverter);
if (SUCCEEDED(hr))
{
// Create the appropriate pixel format converter
hr = pFormatConverter->Initialize(pBitmapSource, GUID_WICPixelFormat32bppBGRA, WICBitmapDitherTypeNone, NULL, 0, WICBitmapPaletteTypeCustom);
if (SUCCEEDED(hr))
{
hr = pFormatConverter->QueryInterface(&pBitmapSourceConverted);
}
pFormatConverter->Release();
}
}
else
{
hr = pBitmapSource->QueryInterface(&pBitmapSourceConverted); // No conversion necessary
}
if (SUCCEEDED(hr))
{
UINT nWidth, nHeight;
hr = pBitmapSourceConverted->GetSize(&nWidth, &nHeight);
if (SUCCEEDED(hr))
{
BITMAPINFO bmi = {};
bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
bmi.bmiHeader.biWidth = nWidth;
bmi.bmiHeader.biHeight = -static_cast<LONG>(nHeight);
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
BYTE* pBits;
HBITMAP hbmp = CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, reinterpret_cast<void**>(&pBits), NULL, 0);
hr = hbmp ? S_OK : E_OUTOFMEMORY;
if (SUCCEEDED(hr))
{
WICRect rect = { 0, 0, (int)nWidth, (int)nHeight };
// Convert the pixels and store them in the HBITMAP. Note: the name of the function is a little
// misleading - we're not doing any extraneous copying here. CopyPixels is actually converting the
// image into the given buffer.
hr = pBitmapSourceConverted->CopyPixels(&rect, nWidth * 4, nWidth * nHeight * 4, pBits);
if (SUCCEEDED(hr))
{
*phbmp = hbmp;
}
else
{
DeleteObject(hbmp);
}
}
}
pBitmapSourceConverted->Release();
}
return hr;
}
HRESULT WICCreate32BitsPerPixelHBITMAP(IStream* pstm, UINT /* cx */, HBITMAP* phbmp, WTS_ALPHATYPE* pdwAlpha)
{
*phbmp = NULL;
IWICImagingFactory* pImagingFactory;
HRESULT hr = CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pImagingFactory));
if (SUCCEEDED(hr))
{
IWICBitmapDecoder* pDecoder;
hr = pImagingFactory->CreateDecoderFromStream(pstm, &GUID_VendorMicrosoft, WICDecodeMetadataCacheOnDemand, &pDecoder);
if (SUCCEEDED(hr))
{
IWICBitmapFrameDecode* pBitmapFrameDecode;
hr = pDecoder->GetFrame(0, &pBitmapFrameDecode);
if (SUCCEEDED(hr))
{
hr = ConvertBitmapSourceTo32BPPHBITMAP(pBitmapFrameDecode, pImagingFactory, phbmp);
if (SUCCEEDED(hr))
{
*pdwAlpha = WTSAT_ARGB;
}
pBitmapFrameDecode->Release();
}
pDecoder->Release();
}
pImagingFactory->Release();
}
return hr;
}
// IThumbnailProvider
IFACEMETHODIMP CRecipeThumbProvider::GetThumbnail(UINT cx, HBITMAP* phbmp, WTS_ALPHATYPE* pdwAlpha)
{
PWSTR pszBase64EncodedImageString;
HRESULT hr = _GetBase64EncodedImageString(cx, &pszBase64EncodedImageString);
if (SUCCEEDED(hr))
{
IStream* pImageStream;
hr = _GetStreamFromString(pszBase64EncodedImageString, &pImageStream);
if (SUCCEEDED(hr))
{
hr = WICCreate32BitsPerPixelHBITMAP(pImageStream, cx, phbmp, pdwAlpha);;
pImageStream->Release();
}
CoTaskMemFree(pszBase64EncodedImageString);
}
return hr;
}