Blender V5.3
blendthumb_win32.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <cmath>
12#include <new>
13#include <shlwapi.h>
14#include <string>
15#include <thumbcache.h> /* for #IThumbnailProvider */
16
17#include "Wincodec.h"
18
19#include "blendthumb.hh"
20
21#include "BLI_filereader.hh"
22
23#pragma comment(lib, "shlwapi.lib")
24
25using namespace blender;
26
31class CBlendThumb : public IInitializeWithStream, public IThumbnailProvider {
32 public:
33 CBlendThumb() : _cRef(1), _pStream(nullptr) {}
34
35 virtual ~CBlendThumb()
36 {
37 if (_pStream) {
38 _pStream->Release();
39 }
40 }
41
42 IFACEMETHODIMP QueryInterface(REFIID riid, void **ppv)
43 {
44 static const QITAB qit[] = {
45 QITABENT(CBlendThumb, IInitializeWithStream),
46 QITABENT(CBlendThumb, IThumbnailProvider),
47 {0},
48 };
49 return QISearch(this, qit, riid, ppv);
50 }
51
52 IFACEMETHODIMP_(ULONG) AddRef()
53 {
54 return InterlockedIncrement(&_cRef);
55 }
56
57 IFACEMETHODIMP_(ULONG) Release()
58 {
59 ULONG cRef = InterlockedDecrement(&_cRef);
60 if (!cRef) {
61 delete this;
62 }
63 return cRef;
64 }
65
67 IFACEMETHODIMP Initialize(IStream *pStream, DWORD grfMode);
68
70 IFACEMETHODIMP GetThumbnail(UINT cx, HBITMAP *phbmp, WTS_ALPHATYPE *pdwAlpha);
71
72 private:
73 long _cRef;
74 IStream *_pStream; /* provided in Initialize(). */
75};
76
77HRESULT CBlendThumb_CreateInstance(REFIID riid, void **ppv)
78{
79 CBlendThumb *pNew = new (std::nothrow) CBlendThumb();
80 HRESULT hr = pNew ? S_OK : E_OUTOFMEMORY;
81 if (SUCCEEDED(hr)) {
82 hr = pNew->QueryInterface(riid, ppv);
83 pNew->Release();
84 }
85 return hr;
86}
87
88IFACEMETHODIMP CBlendThumb::Initialize(IStream *pStream, DWORD)
89{
90 if (_pStream != nullptr) {
91 /* Can only be initialized once. */
92 return E_UNEXPECTED;
93 }
94 /* Take a reference to the stream. */
95 return pStream->QueryInterface(&_pStream);
96}
97
103
104 IStream *_pStream;
105};
106
107static int64_t stream_read(FileReader *reader, void *buffer, size_t size)
108{
109 StreamReader *stream = reinterpret_cast<StreamReader *>(reader);
110
111 ULONG readsize;
112 stream->_pStream->Read(buffer, size, &readsize);
113 stream->reader.offset += readsize;
114
115 return int64_t(readsize);
116}
117
118static off64_t stream_seek(FileReader *reader, off64_t offset, int whence)
119{
120 StreamReader *stream = reinterpret_cast<StreamReader *>(reader);
121
122 DWORD origin = STREAM_SEEK_SET;
123 switch (whence) {
124 case SEEK_CUR:
125 origin = STREAM_SEEK_CUR;
126 break;
127 case SEEK_END:
128 origin = STREAM_SEEK_END;
129 break;
130 }
131 LARGE_INTEGER offsetI;
132 offsetI.QuadPart = offset;
133 ULARGE_INTEGER newPos;
134 stream->_pStream->Seek(offsetI, origin, &newPos);
135 stream->reader.offset = newPos.QuadPart;
136
137 return stream->reader.offset;
138}
139
140static void stream_close(FileReader *reader)
141{
142 StreamReader *stream = reinterpret_cast<StreamReader *>(reader);
143 delete stream;
144}
145
146IFACEMETHODIMP CBlendThumb::GetThumbnail(UINT cx, HBITMAP *phbmp, WTS_ALPHATYPE *pdwAlpha)
147{
148 HRESULT hr = S_FALSE;
149
151 file->reader.read = stream_read;
152 file->reader.seek = stream_seek;
153 file->reader.close = stream_close;
154 file->reader.offset = 0;
155 file->_pStream = _pStream;
156
157 file->reader.seek(&file->reader, 0, SEEK_SET);
158
159 /* Extract thumbnail from stream. */
160 Thumbnail thumb;
161 if (blendthumb_create_thumb_from_file(&file->reader, &thumb) != BT_OK) {
162 return S_FALSE;
163 }
164
165 /* Convert to BGRA for Windows. */
166 for (int i = 0; i < thumb.width * thumb.height; i++) {
167 std::swap(thumb.data[4 * i], thumb.data[4 * i + 2]);
168 }
169
170 *phbmp = CreateBitmap(thumb.width, thumb.height, 1, 32, thumb.data.data());
171 if (!*phbmp) {
172 return E_FAIL;
173 }
174 *pdwAlpha = WTSAT_ARGB;
175
176 /* Scale up the thumbnail if required. */
177 if (uint(thumb.width) < cx && uint(thumb.height) < cx) {
178 float scale = 1.0f / (std::max(thumb.width, thumb.height) / float(cx));
179 LONG NewWidth = LONG(thumb.width * scale);
180 LONG NewHeight = LONG(thumb.height * scale);
181
182 IWICImagingFactory *pImgFac;
183 hr = CoCreateInstance(
184 CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pImgFac));
185
186 IWICBitmap *WICBmp;
187 hr = pImgFac->CreateBitmapFromHBITMAP(*phbmp, 0, WICBitmapUseAlpha, &WICBmp);
188
189 BITMAPINFO bmi = {};
190 bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
191 bmi.bmiHeader.biWidth = NewWidth;
192 bmi.bmiHeader.biHeight = -NewHeight;
193 bmi.bmiHeader.biPlanes = 1;
194 bmi.bmiHeader.biBitCount = 32;
195 bmi.bmiHeader.biCompression = BI_RGB;
196
197 BYTE *pBits;
198 HBITMAP ResizedHBmp = CreateDIBSection(
199 nullptr, &bmi, DIB_RGB_COLORS, (void **)&pBits, nullptr, 0);
200 hr = ResizedHBmp ? S_OK : E_OUTOFMEMORY;
201 if (SUCCEEDED(hr)) {
202 IWICBitmapScaler *pIScaler;
203 hr = pImgFac->CreateBitmapScaler(&pIScaler);
204 hr = pIScaler->Initialize(WICBmp, NewWidth, NewHeight, WICBitmapInterpolationModeFant);
205
206 WICRect rect = {0, 0, NewWidth, NewHeight};
207 hr = pIScaler->CopyPixels(&rect, NewWidth * 4, NewWidth * NewHeight * 4, pBits);
208
209 if (SUCCEEDED(hr)) {
210 DeleteObject(*phbmp);
211 *phbmp = ResizedHBmp;
212 }
213 else {
214 DeleteObject(ResizedHBmp);
215 }
216
217 pIScaler->Release();
218 }
219 WICBmp->Release();
220 pImgFac->Release();
221 }
222 else {
223 hr = S_OK;
224 }
225 return hr;
226}
Wrapper for reading from various sources (e.g. raw files, compressed files, memory....
static void stream_close(FileReader *reader)
static int64_t stream_read(FileReader *reader, void *buffer, size_t size)
HRESULT CBlendThumb_CreateInstance(REFIID riid, void **ppv)
static off64_t stream_seek(FileReader *reader, off64_t offset, int whence)
IFACEMETHODIMP Initialize(IStream *pStream, DWORD grfMode)
IFACEMETHODIMP_(ULONG) AddRef()
IFACEMETHODIMP QueryInterface(REFIID riid, void **ppv)
IFACEMETHODIMP GetThumbnail(UINT cx, HBITMAP *phbmp, WTS_ALPHATYPE *pdwAlpha)
IFACEMETHODIMP_(ULONG) Release()
virtual ~CBlendThumb()
const T * data() const
Definition BLI_array.hh:323
float3x3 scale(float3x3 mat, float2 scale)
BMesh Iterators.
__int64 int64_t
Definition BKE_text.h:90
eThumbStatus blendthumb_create_thumb_from_file(FileReader *rawfile, Thumbnail *thumb)
unsigned int uint
Definition BKE_text.h:55
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
Array< uint8_t > data
Definition blendthumb.hh:25