forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththumbnail_handler.cpp
235 lines (191 loc) · 5.12 KB
/
thumbnail_handler.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
// Desktop Integration
// Copyright (C) 2017-2018 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#include "base/base.h"
#include "dio/decode_delegate.h"
#include "dio/decode_file.h"
#include "dio/file_interface.h"
#include "doc/color.h"
#include "doc/document.h"
#include "doc/image_ref.h"
#include "doc/image_traits.h"
#include "doc/pixel_format.h"
#include "doc/sprite.h"
#include "render/render.h"
#include "desktop/win/thumbnail_handler.h"
#include <cassert>
#include <new>
#include <objbase.h>
#include <shlwapi.h>
namespace desktop {
namespace {
class DecodeDelegate : public dio::DecodeDelegate {
public:
DecodeDelegate() : m_sprite(nullptr) { }
~DecodeDelegate() { delete m_sprite; }
bool decodeOneFrame() override { return true; }
void onSprite(doc::Sprite* sprite) override {
m_sprite = sprite;
}
doc::Sprite* sprite() { return m_sprite; }
private:
doc::Sprite* m_sprite;
};
class StreamAdaptor : public dio::FileInterface {
public:
StreamAdaptor(IStream* stream)
: m_stream(stream)
, m_ok(m_stream != nullptr) {
}
bool ok() const {
return m_ok;
}
size_t tell() {
LARGE_INTEGER delta;
delta.QuadPart = 0;
ULARGE_INTEGER newPos;
HRESULT hr = m_stream->Seek(delta, STREAM_SEEK_CUR, &newPos);
if (FAILED(hr)) {
m_ok = false;
return 0;
}
return newPos.QuadPart;
}
void seek(size_t absPos) {
LARGE_INTEGER pos;
pos.QuadPart = absPos;
ULARGE_INTEGER newPos;
HRESULT hr = m_stream->Seek(pos, STREAM_SEEK_SET, &newPos);
if (FAILED(hr))
m_ok = false;
}
uint8_t read8() {
if (!m_ok)
return 0;
unsigned char byte = 0;
ULONG count;
HRESULT hr = m_stream->Read((void*)&byte, 1, &count);
if (FAILED(hr) || count != 1) {
m_ok = false;
return 0;
}
return byte;
}
size_t readBytes(uint8_t* buf, size_t n) {
if (!m_ok)
return 0;
ULONG count;
HRESULT hr = m_stream->Read((void*)buf, (ULONG)n, &count);
if (FAILED(hr) || count != n)
m_ok = false;
return count;
}
void write8(uint8_t value) {
// Do nothing, we don't write in the file
}
IStream* m_stream;
bool m_ok;
};
} // anonymous namespace
// static
HRESULT ThumbnailHandler::CreateInstance(REFIID riid, void** ppv)
{
*ppv = nullptr;
ThumbnailHandler* obj = new (std::nothrow)ThumbnailHandler;
if (!obj)
return E_OUTOFMEMORY;
HRESULT hr = obj->QueryInterface(riid, ppv);
obj->Release();
return hr;
}
ThumbnailHandler::ThumbnailHandler()
: m_ref(1)
{
}
ThumbnailHandler::~ThumbnailHandler()
{
}
// IUnknown
HRESULT ThumbnailHandler::QueryInterface(REFIID riid, void** ppv)
{
*ppv = nullptr;
static const QITAB qit[] = {
QITABENT(ThumbnailHandler, IInitializeWithStream),
QITABENT(ThumbnailHandler, IThumbnailProvider),
{ 0 },
};
return QISearch(this, qit, riid, ppv);
}
ULONG ThumbnailHandler::AddRef()
{
return InterlockedIncrement(&m_ref);
}
ULONG ThumbnailHandler::Release()
{
ULONG ref = InterlockedDecrement(&m_ref);
if (!ref)
delete this;
return ref;
}
// IInitializeWithStream
HRESULT ThumbnailHandler::Initialize(IStream* pStream, DWORD grfMode)
{
if (!pStream)
return E_INVALIDARG;
m_stream.reset();
return pStream->QueryInterface(IID_IStream, (void**)&m_stream);
}
// IThumbnailProvider
HRESULT ThumbnailHandler::GetThumbnail(UINT cx, HBITMAP* phbmp, WTS_ALPHATYPE* pdwAlpha)
{
if (!m_stream.get())
return E_FAIL;
doc::ImageRef image;
int w, h;
try {
DecodeDelegate delegate;
StreamAdaptor adaptor(m_stream.get());
if (!dio::decode_file(&delegate, &adaptor))
return E_FAIL;
doc::Sprite* spr = delegate.sprite();
w = spr->width();
h = spr->height();
image.reset(doc::Image::create(doc::IMAGE_RGB, w, h));
#undef TRANSPARENT // Windows defines TRANSPARENT macro
render::Render render;
render.setBgType(render::BgType::TRANSPARENT);
render.renderSprite(image.get(), spr, 0);
}
catch (const std::exception&) {
// TODO convert exception into a HRESULT
return E_FAIL;
}
BITMAPINFO bi;
ZeroMemory(&bi, sizeof(bi));
bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
bi.bmiHeader.biWidth = w;
bi.bmiHeader.biHeight = -h;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 32;
bi.bmiHeader.biCompression = BI_RGB;
unsigned char* data = nullptr;
*phbmp = CreateDIBSection(nullptr, &bi, DIB_RGB_COLORS, (void**)&data, nullptr, 0);
if (!*phbmp)
return E_FAIL;
for (int y=0; y<h; ++y) {
doc::RgbTraits::address_t row =
(doc::RgbTraits::address_t)image->getPixelAddress(0, y);
for (int x=0; x<w; ++x, ++row) {
doc::color_t c = *row;
*(data++) = doc::rgba_getb(c);
*(data++) = doc::rgba_getg(c);
*(data++) = doc::rgba_getr(c);
*(data++) = doc::rgba_geta(c);
}
}
*pdwAlpha = WTSAT_ARGB;
return S_OK;
}
} // namespace desktop