Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BlenderThumb.cpp « src « blendthumb « windows « release - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 508b9f74852fa31f8023c56fa3df43fca6a31f4f (plain)
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

#include <shlwapi.h>
#include <thumbcache.h> // For IThumbnailProvider.
#include <new>

#pragma comment(lib, "shlwapi.lib")

// this thumbnail provider implements IInitializeWithStream to enable being hosted
// in an isolated process for robustness

class CBlendThumb : public IInitializeWithStream, public IThumbnailProvider
{
public:
	CBlendThumb() : _cRef(1), _pStream(NULL) {}

	virtual ~CBlendThumb()
	{
		if (_pStream)
		{
			_pStream->Release();
		}
	}

	// IUnknown
	IFACEMETHODIMP QueryInterface(REFIID riid, void **ppv)
	{
		static const QITAB qit[] =
		{
			QITABENT(CBlendThumb, IInitializeWithStream),
			QITABENT(CBlendThumb, 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:
	long _cRef;
	IStream *_pStream;	 // provided during initialization.
};

HRESULT CBlendThumb_CreateInstance(REFIID riid, void **ppv)
{
	CBlendThumb *pNew = new (std::nothrow) CBlendThumb();
	HRESULT hr = pNew ? S_OK : E_OUTOFMEMORY;
	if (SUCCEEDED(hr))
	{
		hr = pNew->QueryInterface(riid, ppv);
		pNew->Release();
	}
	return hr;
}

// IInitializeWithStream
IFACEMETHODIMP CBlendThumb::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;
}

#include <math.h>
#include <zlib.h>
#include "Wincodec.h"
const unsigned char gzip_magic[3] = { 0x1f, 0x8b, 0x08 };

// IThumbnailProvider
IFACEMETHODIMP CBlendThumb::GetThumbnail(UINT cx, HBITMAP *phbmp, WTS_ALPHATYPE *pdwAlpha)
{
	ULONG BytesRead;
	HRESULT hr = S_FALSE;
	LARGE_INTEGER SeekPos;

	// Compressed?	
	unsigned char in_magic[3];
	_pStream->Read(&in_magic,3,&BytesRead);
	bool gzipped = true;
	for ( int i=0; i < 3; i++ )
		if ( in_magic[i] != gzip_magic[i] )
		{
			gzipped = false;
			break;
		}

	if (gzipped)
	{
		// Zlib inflate
		z_stream stream;
		stream.zalloc = Z_NULL;
		stream.zfree = Z_NULL;
		stream.opaque = Z_NULL;

		// Get compressed file length
		SeekPos.QuadPart = 0;
		_pStream->Seek(SeekPos,STREAM_SEEK_END,NULL);

		// Get compressed and uncompressed size
		uLong source_size;
		uLongf dest_size;
		//SeekPos.QuadPart = -4; // last 4 bytes define size of uncompressed file
		//ULARGE_INTEGER Tell;
		//_pStream->Seek(SeekPos,STREAM_SEEK_END,&Tell);
		//source_size = (uLong)Tell.QuadPart + 4; // src
		//_pStream->Read(&dest_size,4,&BytesRead); // dest
		dest_size = 1024*70; // thumbnail is currently always inside the first 65KB...if it moves or enlargens this line will have to change or go!
		source_size = (uLong)max(SeekPos.QuadPart,dest_size); // for safety, assume no compression
		
		// Input
		Bytef* src = new Bytef[source_size];
		stream.next_in = (Bytef*)src;
		stream.avail_in = (uInt)source_size;
		
		// Output
		Bytef* dest = new Bytef[dest_size];
		stream.next_out = (Bytef*)dest;
		stream.avail_out = dest_size; 

		// IStream to src
		SeekPos.QuadPart = 0;
		_pStream->Seek(SeekPos,STREAM_SEEK_SET,NULL);
		_pStream->Read(src,source_size,&BytesRead);
		
		// Do the inflation
		int err;
		err = inflateInit2(&stream,16); // 16 means "gzip"...nice!
		err = inflate(&stream, Z_FINISH);		
		err = inflateEnd(&stream);
				
		// Replace the IStream, which is read-only
		_pStream->Release();
		_pStream = SHCreateMemStream(dest,dest_size);
		
		delete[] src;
		delete[] dest;
	}

	// Blender version, early out if sub 2.5
	SeekPos.QuadPart = 9;
	_pStream->Seek(SeekPos,STREAM_SEEK_SET,NULL);
	char version[4];
	version[3] = '\0';
	_pStream->Read(&version,3,&BytesRead);
	if ( BytesRead != 3)
		return E_UNEXPECTED;
	int iVersion = atoi(version);
	if ( iVersion < 250 )
		return S_FALSE;
	
	// 32 or 64 bit blend?
	SeekPos.QuadPart = 7;
	_pStream->Seek(SeekPos,STREAM_SEEK_SET,NULL);

	char _PointerSize;
	_pStream->Read(&_PointerSize,1,&BytesRead);

	int PointerSize	= _PointerSize == '_' ? 4 : 8;
	int HeaderSize	= 16 + PointerSize;

	// Find and read thumbnail ("TEST") block
	SeekPos.QuadPart = 12;
	_pStream->Seek(SeekPos,STREAM_SEEK_SET,NULL);
	int BlockOffset = 12;
	while ( _pStream )
	{
		// Scan current block
		char BlockName[5];
		BlockName[4] = '\0';
		int	BlockSize = 0;

		if (_pStream->Read(BlockName,4,&BytesRead) == S_OK && _pStream->Read((void*)&BlockSize,4,&BytesRead) == S_OK)
		{
			if ( strcmp (BlockName,"TEST") != 0 )
			{
				SeekPos.QuadPart = BlockOffset += HeaderSize + BlockSize;
				_pStream->Seek(SeekPos,STREAM_SEEK_SET,NULL);
				continue;
			}
		}
		else break; // eof

		// Found the block
		SeekPos.QuadPart = BlockOffset + HeaderSize;
		_pStream->Seek(SeekPos,STREAM_SEEK_SET,NULL);

		int width, height;
		_pStream->Read((char*)&width,4,&BytesRead);
		_pStream->Read((char*)&height,4,&BytesRead);
		BlockSize -= 8;

		// Isolate RGBA data
		char* pRGBA = new char[BlockSize];
		_pStream->Read(pRGBA,BlockSize,&BytesRead);

		if (BytesRead != (ULONG)BlockSize)
			return E_UNEXPECTED;

		// Convert to BGRA for Windows
		for (int i=0; i < BlockSize; i+=4 )
		{
			#define RED_BYTE pRGBA[i]
			#define BLUE_BYTE pRGBA[i+2]

			char red = RED_BYTE;
			RED_BYTE = BLUE_BYTE;
			BLUE_BYTE = red;
		}
		
		// Flip vertically (Blender stores it upside-down)
		unsigned int LineSize = width*4;
		char* FlippedImage = new char[BlockSize];
		for (int i=0; i<height; i++)
		{
			if ( 0 != memcpy_s(&FlippedImage[ (height - i - 1)*LineSize ],LineSize,&pRGBA[ i*LineSize ],LineSize))
				return E_UNEXPECTED;
		}
		delete[] pRGBA;
		pRGBA = FlippedImage;

		// Create image
		*phbmp = CreateBitmap(width,height,1,32,pRGBA);
		if (!*phbmp)
			return E_FAIL;
		*pdwAlpha = WTSAT_ARGB; // it's actually BGRA, not sure why this works

		// Scale down if required
		if ( (unsigned)width > cx || (unsigned)height > cx )
		{
			float scale = 1.0f / (max(width,height) / (float)cx);
			LONG NewWidth = (LONG)(width *scale);
			LONG NewHeight = (LONG)(height *scale);

#ifdef _DEBUG
#if 1
			MessageBox(0,L"Attach now",L"Debugging",MB_OK);
#endif
#endif
			IWICImagingFactory *pImgFac;
			hr = CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pImgFac));
			
			IWICBitmap* WICBmp;
			hr = pImgFac->CreateBitmapFromHBITMAP(*phbmp,0,WICBitmapUseAlpha,&WICBmp);
			
			BITMAPINFO bmi = {};
			bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
			bmi.bmiHeader.biWidth = NewWidth;
			bmi.bmiHeader.biHeight = -NewHeight;
			bmi.bmiHeader.biPlanes = 1;
			bmi.bmiHeader.biBitCount = 32;
			bmi.bmiHeader.biCompression = BI_RGB;

			BYTE *pBits;
			HBITMAP ResizedHBmp = CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, (void**)&pBits, NULL, 0);
			hr = ResizedHBmp ? S_OK : E_OUTOFMEMORY;
			if (SUCCEEDED(hr))
			{				
				IWICBitmapScaler* pIScaler;
				hr = pImgFac->CreateBitmapScaler(&pIScaler);
				hr = pIScaler->Initialize(WICBmp,NewWidth,NewHeight,WICBitmapInterpolationModeFant);
								
				WICRect rect = {0, 0, NewWidth, NewHeight};
				hr = pIScaler->CopyPixels(&rect, NewWidth * 4, NewWidth * NewHeight * 4, pBits);

				if (SUCCEEDED(hr))
				{
					DeleteObject(*phbmp);
					*phbmp = ResizedHBmp;
				}
				else
					DeleteObject(ResizedHBmp);

				pIScaler->Release();
			}
			WICBmp->Release();
			pImgFac->Release();
		}
		else
			hr = S_OK;
		break;
	}
	return hr;
}