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

blendthumb_win32.cc « src « blendthumb « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 206309b6caf836ea9f8f7a06779a5da4fba9a54f (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
/* SPDX-License-Identifier: GPL-2.0-or-later */

/** \file
 * \ingroup blendthumb
 *
 * Thumbnail from Blend file extraction for MS-Windows.
 */

#include <math.h>
#include <new>
#include <shlwapi.h>
#include <string>
#include <thumbcache.h> /* for #IThumbnailProvider */

#include "Wincodec.h"

#include "blendthumb.hh"

#include "BLI_filereader.h"

#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();
    }
  }

  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 in Initialize(). */
};

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;
}

IFACEMETHODIMP CBlendThumb::Initialize(IStream *pStream, DWORD)
{
  if (_pStream != NULL) {
    /* Can only be initialized once. */
    return E_UNEXPECTED;
  }
  /* Take a reference to the stream. */
  return pStream->QueryInterface(&_pStream);
}

/**
 * #FileReader compatible wrapper around the Windows stream that gives access to the .blend file.
 */
typedef struct {
  FileReader reader;

  IStream *_pStream;
} StreamReader;

static ssize_t stream_read(FileReader *reader, void *buffer, size_t size)
{
  StreamReader *stream = (StreamReader *)reader;

  ULONG readsize;
  stream->_pStream->Read(buffer, size, &readsize);
  stream->reader.offset += readsize;

  return ssize_t(readsize);
}

static off64_t stream_seek(FileReader *reader, off64_t offset, int whence)
{
  StreamReader *stream = (StreamReader *)reader;

  DWORD origin = STREAM_SEEK_SET;
  switch (whence) {
    case SEEK_CUR:
      origin = STREAM_SEEK_CUR;
      break;
    case SEEK_END:
      origin = STREAM_SEEK_END;
      break;
  }
  LARGE_INTEGER offsetI;
  offsetI.QuadPart = offset;
  ULARGE_INTEGER newPos;
  stream->_pStream->Seek(offsetI, origin, &newPos);
  stream->reader.offset = newPos.QuadPart;

  return stream->reader.offset;
}

static void stream_close(FileReader *reader)
{
  StreamReader *stream = (StreamReader *)reader;
  delete stream;
}

IFACEMETHODIMP CBlendThumb::GetThumbnail(UINT cx, HBITMAP *phbmp, WTS_ALPHATYPE *pdwAlpha)
{
  HRESULT hr = S_FALSE;

  StreamReader *file = new StreamReader;
  file->reader.read = stream_read;
  file->reader.seek = stream_seek;
  file->reader.close = stream_close;
  file->reader.offset = 0;
  file->_pStream = _pStream;

  file->reader.seek(&file->reader, 0, SEEK_SET);

  /* Extract thumbnail from stream. */
  Thumbnail thumb;
  if (blendthumb_create_thumb_from_file(&file->reader, &thumb) != BT_OK) {
    return S_FALSE;
  }

  /* Convert to BGRA for Windows. */
  for (int i = 0; i < thumb.width * thumb.height; i++) {
    std::swap(thumb.data[4 * i], thumb.data[4 * i + 2]);
  }

  *phbmp = CreateBitmap(thumb.width, thumb.height, 1, 32, thumb.data.data());
  if (!*phbmp) {
    return E_FAIL;
  }
  *pdwAlpha = WTSAT_ARGB;

  /* Scale up the thumbnail if required. */
  if (uint(thumb.width) < cx && uint(thumb.height) < cx) {
    float scale = 1.0f / (std::max(thumb.width, thumb.height) / float(cx));
    LONG NewWidth = LONG(thumb.width * scale);
    LONG NewHeight = LONG(thumb.height * scale);

    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;
  }
  return hr;
}