mirrored from https://skia.googlesource.com/skia
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathSkImageGeneratorWIC.cpp
240 lines (212 loc) · 8.79 KB
/
SkImageGeneratorWIC.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
/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "include/codec/SkEncodedOrigin.h"
#include "include/core/SkStream.h"
#include "include/ports/SkImageGeneratorWIC.h"
#include "include/private/base/SkTemplates.h"
#include "src/codec/SkPixmapUtilsPriv.h"
#include "src/utils/win/SkIStream.h"
#include "src/utils/win/SkTScopedComPtr.h"
#include <wincodec.h>
// All Windows SDKs back to XPSP2 export the CLSID_WICImagingFactory symbol.
// In the Windows8 SDK the CLSID_WICImagingFactory symbol is still exported
// but CLSID_WICImagingFactory is then #defined to CLSID_WICImagingFactory2.
// Undo this #define if it has been done so that we link against the symbols
// we intended to link against on all SDKs.
#if defined(CLSID_WICImagingFactory)
#undef CLSID_WICImagingFactory
#endif
namespace {
class ImageGeneratorWIC : public SkImageGenerator {
public:
/*
* Takes ownership of the imagingFactory
* Takes ownership of the imageSource
*/
ImageGeneratorWIC(const SkImageInfo& info, IWICImagingFactory* imagingFactory,
IWICBitmapSource* imageSource, sk_sp<SkData>, SkEncodedOrigin);
protected:
sk_sp<SkData> onRefEncodedData() override;
bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, const Options&)
override;
private:
SkTScopedComPtr<IWICImagingFactory> fImagingFactory;
SkTScopedComPtr<IWICBitmapSource> fImageSource;
sk_sp<SkData> fData;
SkEncodedOrigin fOrigin;
using INHERITED = SkImageGenerator;
};
} // namespace
std::unique_ptr<SkImageGenerator> SkImageGeneratorWIC::MakeFromEncodedWIC(sk_sp<SkData> data) {
// Create Windows Imaging Component ImagingFactory.
SkTScopedComPtr<IWICImagingFactory> imagingFactory;
HRESULT hr = CoCreateInstance(CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&imagingFactory));
if (FAILED(hr)) {
return nullptr;
}
// Create an IStream.
SkTScopedComPtr<IStream> iStream;
// Note that iStream will take ownership of the new memory stream because
// we set |deleteOnRelease| to true.
hr = SkIStream::CreateFromSkStream(std::make_unique<SkMemoryStream>(data), &iStream);
if (FAILED(hr)) {
return nullptr;
}
// Create the decoder from the stream.
SkTScopedComPtr<IWICBitmapDecoder> decoder;
hr = imagingFactory->CreateDecoderFromStream(iStream.get(), nullptr,
WICDecodeMetadataCacheOnDemand, &decoder);
if (FAILED(hr)) {
return nullptr;
}
// Select the first frame from the decoder.
SkTScopedComPtr<IWICBitmapFrameDecode> imageFrame;
hr = decoder->GetFrame(0, &imageFrame);
if (FAILED(hr)) {
return nullptr;
}
// Get the metadata query reader from the frame.
SkEncodedOrigin origin = kDefault_SkEncodedOrigin;
SkTScopedComPtr<IWICMetadataQueryReader> queryReader;
hr = imageFrame->GetMetadataQueryReader(&queryReader);
// WIC decoder doesn't support BMP and ICO metadata, so we only continue
// reading metadata if GetMetadataQueryReader didn't return an error code.
if (SUCCEEDED(hr)) {
// Query for the orientation metadata (assuming JPEG policy).
PROPVARIANT propValue;
PropVariantInit(&propValue);
hr = queryReader->GetMetadataByName(L"/app1/ifd/{ushort=274}", &propValue);
if (SUCCEEDED(hr) && propValue.vt == VT_UI2) {
SkEncodedOrigin originValue = static_cast<SkEncodedOrigin>(propValue.uiVal);
if (originValue >= kTopLeft_SkEncodedOrigin && originValue <= kLast_SkEncodedOrigin) {
origin = originValue;
}
}
}
// Treat the frame as an image source.
SkTScopedComPtr<IWICBitmapSource> imageSource;
hr = imageFrame->QueryInterface(IID_PPV_ARGS(&imageSource));
if (FAILED(hr)) {
return nullptr;
}
// Get the size of the image.
UINT width;
UINT height;
hr = imageSource->GetSize(&width, &height);
if (FAILED(hr)) {
return nullptr;
}
// Get the encoded pixel format.
WICPixelFormatGUID format;
hr = imageSource->GetPixelFormat(&format);
if (FAILED(hr)) {
return nullptr;
}
// Recommend kOpaque if the image is opaque and kPremul otherwise.
// FIXME: We are stuck recommending kPremul for all indexed formats
// (Ex: GUID_WICPixelFormat8bppIndexed) because we don't have
// a way to check if the image has alpha.
SkAlphaType alphaType = kPremul_SkAlphaType;
if (GUID_WICPixelFormat16bppBGR555 == format ||
GUID_WICPixelFormat16bppBGR565 == format ||
GUID_WICPixelFormat32bppBGR101010 == format ||
GUID_WICPixelFormatBlackWhite == format ||
GUID_WICPixelFormat2bppGray == format ||
GUID_WICPixelFormat4bppGray == format ||
GUID_WICPixelFormat8bppGray == format ||
GUID_WICPixelFormat16bppGray == format ||
GUID_WICPixelFormat16bppGrayFixedPoint == format ||
GUID_WICPixelFormat16bppGrayHalf == format ||
GUID_WICPixelFormat32bppGrayFloat == format ||
GUID_WICPixelFormat32bppGrayFixedPoint == format ||
GUID_WICPixelFormat32bppRGBE == format ||
GUID_WICPixelFormat24bppRGB == format ||
GUID_WICPixelFormat24bppBGR == format ||
GUID_WICPixelFormat32bppBGR == format ||
GUID_WICPixelFormat48bppRGB == format ||
GUID_WICPixelFormat48bppBGR == format ||
GUID_WICPixelFormat48bppRGBFixedPoint == format ||
GUID_WICPixelFormat48bppBGRFixedPoint == format ||
GUID_WICPixelFormat48bppRGBHalf == format ||
GUID_WICPixelFormat64bppRGBFixedPoint == format ||
GUID_WICPixelFormat64bppRGBHalf == format ||
GUID_WICPixelFormat96bppRGBFixedPoint == format ||
GUID_WICPixelFormat128bppRGBFloat == format ||
GUID_WICPixelFormat128bppRGBFixedPoint == format ||
GUID_WICPixelFormat32bppRGB == format ||
GUID_WICPixelFormat64bppRGB == format ||
GUID_WICPixelFormat96bppRGBFloat == format ||
GUID_WICPixelFormat32bppCMYK == format ||
GUID_WICPixelFormat64bppCMYK == format ||
GUID_WICPixelFormat8bppY == format ||
GUID_WICPixelFormat8bppCb == format ||
GUID_WICPixelFormat8bppCr == format ||
GUID_WICPixelFormat16bppCbCr == format)
{
alphaType = kOpaque_SkAlphaType;
}
// FIXME: If we change the implementation to handle swizzling ourselves,
// we can support more output formats.
SkImageInfo info = SkImageInfo::MakeS32(width, height, alphaType);
if (SkEncodedOriginSwapsWidthHeight(origin)) {
info = SkPixmapUtils::SwapWidthHeight(info);
}
return std::unique_ptr<SkImageGenerator>(
new ImageGeneratorWIC(info, imagingFactory.release(), imageSource.release(),
std::move(data), origin));
}
ImageGeneratorWIC::ImageGeneratorWIC(const SkImageInfo& info,
IWICImagingFactory* imagingFactory, IWICBitmapSource* imageSource, sk_sp<SkData> data, SkEncodedOrigin origin)
: INHERITED(info)
, fImagingFactory(imagingFactory)
, fImageSource(imageSource)
, fData(std::move(data))
, fOrigin(origin)
{}
sk_sp<SkData> ImageGeneratorWIC::onRefEncodedData() {
return fData;
}
bool ImageGeneratorWIC::onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
const Options&) {
if (kN32_SkColorType != info.colorType()) {
return false;
}
// Create a format converter.
SkTScopedComPtr<IWICFormatConverter> formatConverter;
HRESULT hr = fImagingFactory->CreateFormatConverter(&formatConverter);
if (FAILED(hr)) {
return false;
}
GUID format = GUID_WICPixelFormat32bppPBGRA;
if (kUnpremul_SkAlphaType == info.alphaType()) {
format = GUID_WICPixelFormat32bppBGRA;
}
hr = formatConverter->Initialize(fImageSource.get(), format, WICBitmapDitherTypeNone, nullptr,
0.0, WICBitmapPaletteTypeCustom);
if (FAILED(hr)) {
return false;
}
// Treat the format converter as an image source.
SkTScopedComPtr<IWICBitmapSource> formatConverterSrc;
hr = formatConverter->QueryInterface(IID_PPV_ARGS(&formatConverterSrc));
if (FAILED(hr)) {
return false;
}
SkPixmap dst(info, pixels, rowBytes);
auto decode = [&formatConverterSrc](const SkPixmap& pm) {
// Get the destination pixels.
void* pixelsAddr = pm.writable_addr();
size_t rowBytes = pm.rowBytes();
const SkImageInfo& info = pm.info();
// Set the destination pixels.
HRESULT hr = formatConverterSrc->CopyPixels(nullptr, (UINT) rowBytes, (UINT) rowBytes * info.height(),
(BYTE*) pixelsAddr);
return SUCCEEDED(hr);
};
return SkPixmapUtils::Orient(dst, fOrigin, decode);
}