| 1 |
/* |
| 2 |
* Copyright 2016 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "include/codec/SkEncodedOrigin.h" |
| 9 |
#include "include/core/SkStream.h" |
| 10 |
#include "include/ports/SkImageGeneratorWIC.h" |
| 11 |
#include "include/private/base/SkTemplates.h" |
| 12 |
#include "src/codec/SkPixmapUtilsPriv.h" |
| 13 |
#include "src/utils/win/SkIStream.h" |
| 14 |
#include "src/utils/win/SkTScopedComPtr.h" |
| 15 |
|
| 16 |
#include <wincodec.h> |
| 17 |
|
| 18 |
// All Windows SDKs back to XPSP2 export the CLSID_WICImagingFactory symbol. |
| 19 |
// In the Windows8 SDK the CLSID_WICImagingFactory symbol is still exported |
| 20 |
// but CLSID_WICImagingFactory is then #defined to CLSID_WICImagingFactory2. |
| 21 |
// Undo this #define if it has been done so that we link against the symbols |
| 22 |
// we intended to link against on all SDKs. |
| 23 |
#if defined(CLSID_WICImagingFactory) |
| 24 |
#undef CLSID_WICImagingFactory |
| 25 |
#endif |
| 26 |
|
| 27 |
namespace { |
| 28 |
class ImageGeneratorWIC : public SkImageGenerator { |
| 29 |
public: |
| 30 |
/* |
| 31 |
* Takes ownership of the imagingFactory |
| 32 |
* Takes ownership of the imageSource |
| 33 |
*/ |
| 34 |
ImageGeneratorWIC(const SkImageInfo& info, |
| 35 |
IWICImagingFactory* imagingFactory, |
| 36 |
IWICBitmapSource* imageSource, |
| 37 |
sk_sp<const SkData>, |
| 38 |
SkEncodedOrigin); |
| 39 |
|
| 40 |
protected: |
| 41 |
sk_sp<const SkData> onRefEncodedData() override; |
| 42 |
|
| 43 |
bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, const Options&) |
| 44 |
override; |
| 45 |
|
| 46 |
private: |
| 47 |
SkTScopedComPtr<IWICImagingFactory> fImagingFactory; |
| 48 |
SkTScopedComPtr<IWICBitmapSource> fImageSource; |
| 49 |
sk_sp<const SkData> fData; |
| 50 |
SkEncodedOrigin fOrigin; |
| 51 |
|
| 52 |
using INHERITED = SkImageGenerator; |
| 53 |
}; |
| 54 |
} // namespace |
| 55 |
|
| 56 |
std::unique_ptr<SkImageGenerator> SkImageGeneratorWIC::MakeFromEncodedWIC( |
| 57 |
sk_sp<const SkData> data) { |
| 58 |
// Create Windows Imaging Component ImagingFactory. |
| 59 |
SkTScopedComPtr<IWICImagingFactory> imagingFactory; |
| 60 |
HRESULT hr = CoCreateInstance(CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER, |
| 61 |
IID_PPV_ARGS(&imagingFactory)); |
| 62 |
if (FAILED(hr)) { |
| 63 |
return nullptr; |
| 64 |
} |
| 65 |
|
| 66 |
// Create an IStream. |
| 67 |
SkTScopedComPtr<IStream> iStream; |
| 68 |
// Note that iStream will take ownership of the new memory stream because |
| 69 |
// we set |deleteOnRelease| to true. |
| 70 |
hr = SkIStream::CreateFromSkStream(std::make_unique<SkMemoryStream>(data), &iStream); |
| 71 |
if (FAILED(hr)) { |
| 72 |
return nullptr; |
| 73 |
} |
| 74 |
|
| 75 |
// Create the decoder from the stream. |
| 76 |
SkTScopedComPtr<IWICBitmapDecoder> decoder; |
| 77 |
hr = imagingFactory->CreateDecoderFromStream(iStream.get(), nullptr, |
| 78 |
WICDecodeMetadataCacheOnDemand, &decoder); |
| 79 |
if (FAILED(hr)) { |
| 80 |
return nullptr; |
| 81 |
} |
| 82 |
|
| 83 |
// Select the first frame from the decoder. |
| 84 |
SkTScopedComPtr<IWICBitmapFrameDecode> imageFrame; |
| 85 |
hr = decoder->GetFrame(0, &imageFrame); |
| 86 |
if (FAILED(hr)) { |
| 87 |
return nullptr; |
| 88 |
} |
| 89 |
|
| 90 |
// Get the metadata query reader from the frame. |
| 91 |
SkEncodedOrigin origin = kDefault_SkEncodedOrigin; |
| 92 |
SkTScopedComPtr<IWICMetadataQueryReader> queryReader; |
| 93 |
hr = imageFrame->GetMetadataQueryReader(&queryReader); |
| 94 |
|
| 95 |
// WIC decoder doesn't support BMP and ICO metadata, so we only continue |
| 96 |
// reading metadata if GetMetadataQueryReader didn't return an error code. |
| 97 |
if (SUCCEEDED(hr)) { |
| 98 |
// Query for the orientation metadata (assuming JPEG policy). |
| 99 |
PROPVARIANT propValue; |
| 100 |
PropVariantInit(&propValue); |
| 101 |
hr = queryReader->GetMetadataByName(L"/app1/ifd/{ushort=274}", &propValue); |
| 102 |
if (SUCCEEDED(hr) && propValue.vt == VT_UI2) { |
| 103 |
SkEncodedOrigin originValue = static_cast<SkEncodedOrigin>(propValue.uiVal); |
| 104 |
if (originValue >= kTopLeft_SkEncodedOrigin && originValue <= kLast_SkEncodedOrigin) { |
| 105 |
origin = originValue; |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
// Treat the frame as an image source. |
| 111 |
SkTScopedComPtr<IWICBitmapSource> imageSource; |
| 112 |
hr = imageFrame->QueryInterface(IID_PPV_ARGS(&imageSource)); |
| 113 |
if (FAILED(hr)) { |
| 114 |
return nullptr; |
| 115 |
} |
| 116 |
|
| 117 |
// Get the size of the image. |
| 118 |
UINT width; |
| 119 |
UINT height; |
| 120 |
hr = imageSource->GetSize(&width, &height); |
| 121 |
if (FAILED(hr)) { |
| 122 |
return nullptr; |
| 123 |
} |
| 124 |
|
| 125 |
// Get the encoded pixel format. |
| 126 |
WICPixelFormatGUID format; |
| 127 |
hr = imageSource->GetPixelFormat(&format); |
| 128 |
if (FAILED(hr)) { |
| 129 |
return nullptr; |
| 130 |
} |
| 131 |
|
| 132 |
// Recommend kOpaque if the image is opaque and kPremul otherwise. |
| 133 |
// FIXME: We are stuck recommending kPremul for all indexed formats |
| 134 |
// (Ex: GUID_WICPixelFormat8bppIndexed) because we don't have |
| 135 |
// a way to check if the image has alpha. |
| 136 |
SkAlphaType alphaType = kPremul_SkAlphaType; |
| 137 |
|
| 138 |
if (GUID_WICPixelFormat16bppBGR555 == format || |
| 139 |
GUID_WICPixelFormat16bppBGR565 == format || |
| 140 |
GUID_WICPixelFormat32bppBGR101010 == format || |
| 141 |
GUID_WICPixelFormatBlackWhite == format || |
| 142 |
GUID_WICPixelFormat2bppGray == format || |
| 143 |
GUID_WICPixelFormat4bppGray == format || |
| 144 |
GUID_WICPixelFormat8bppGray == format || |
| 145 |
GUID_WICPixelFormat16bppGray == format || |
| 146 |
GUID_WICPixelFormat16bppGrayFixedPoint == format || |
| 147 |
GUID_WICPixelFormat16bppGrayHalf == format || |
| 148 |
GUID_WICPixelFormat32bppGrayFloat == format || |
| 149 |
GUID_WICPixelFormat32bppGrayFixedPoint == format || |
| 150 |
GUID_WICPixelFormat32bppRGBE == format || |
| 151 |
GUID_WICPixelFormat24bppRGB == format || |
| 152 |
GUID_WICPixelFormat24bppBGR == format || |
| 153 |
GUID_WICPixelFormat32bppBGR == format || |
| 154 |
GUID_WICPixelFormat48bppRGB == format || |
| 155 |
GUID_WICPixelFormat48bppBGR == format || |
| 156 |
GUID_WICPixelFormat48bppRGBFixedPoint == format || |
| 157 |
GUID_WICPixelFormat48bppBGRFixedPoint == format || |
| 158 |
GUID_WICPixelFormat48bppRGBHalf == format || |
| 159 |
GUID_WICPixelFormat64bppRGBFixedPoint == format || |
| 160 |
GUID_WICPixelFormat64bppRGBHalf == format || |
| 161 |
GUID_WICPixelFormat96bppRGBFixedPoint == format || |
| 162 |
GUID_WICPixelFormat128bppRGBFloat == format || |
| 163 |
GUID_WICPixelFormat128bppRGBFixedPoint == format || |
| 164 |
GUID_WICPixelFormat32bppRGB == format || |
| 165 |
GUID_WICPixelFormat64bppRGB == format || |
| 166 |
GUID_WICPixelFormat96bppRGBFloat == format || |
| 167 |
GUID_WICPixelFormat32bppCMYK == format || |
| 168 |
GUID_WICPixelFormat64bppCMYK == format || |
| 169 |
GUID_WICPixelFormat8bppY == format || |
| 170 |
GUID_WICPixelFormat8bppCb == format || |
| 171 |
GUID_WICPixelFormat8bppCr == format || |
| 172 |
GUID_WICPixelFormat16bppCbCr == format) |
| 173 |
{ |
| 174 |
alphaType = kOpaque_SkAlphaType; |
| 175 |
} |
| 176 |
|
| 177 |
// FIXME: If we change the implementation to handle swizzling ourselves, |
| 178 |
// we can support more output formats. |
| 179 |
SkImageInfo info = SkImageInfo::MakeS32(width, height, alphaType); |
| 180 |
if (SkEncodedOriginSwapsWidthHeight(origin)) { |
| 181 |
info = SkPixmapUtils::SwapWidthHeight(info); |
| 182 |
} |
| 183 |
return std::unique_ptr<SkImageGenerator>( |
| 184 |
new ImageGeneratorWIC(info, imagingFactory.release(), imageSource.release(), |
| 185 |
std::move(data), origin)); |
| 186 |
} |
| 187 |
|
| 188 |
ImageGeneratorWIC::ImageGeneratorWIC(const SkImageInfo& info, |
| 189 |
IWICImagingFactory* imagingFactory, |
| 190 |
IWICBitmapSource* imageSource, |
| 191 |
sk_sp<const SkData> data, |
| 192 |
SkEncodedOrigin origin) |
| 193 |
: INHERITED(info) |
| 194 |
, fImagingFactory(imagingFactory) |
| 195 |
, fImageSource(imageSource) |
| 196 |
, fData(std::move(data)) |
| 197 |
, fOrigin(origin) {} |
| 198 |
|
| 199 |
sk_sp<const SkData> ImageGeneratorWIC::onRefEncodedData() { return fData; } |
| 200 |
|
| 201 |
bool ImageGeneratorWIC::onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
| 202 |
const Options&) { |
| 203 |
if (kN32_SkColorType != info.colorType()) { |
| 204 |
return false; |
| 205 |
} |
| 206 |
|
| 207 |
// Create a format converter. |
| 208 |
SkTScopedComPtr<IWICFormatConverter> formatConverter; |
| 209 |
HRESULT hr = fImagingFactory->CreateFormatConverter(&formatConverter); |
| 210 |
if (FAILED(hr)) { |
| 211 |
return false; |
| 212 |
} |
| 213 |
|
| 214 |
GUID format = GUID_WICPixelFormat32bppPBGRA; |
| 215 |
if (kUnpremul_SkAlphaType == info.alphaType()) { |
| 216 |
format = GUID_WICPixelFormat32bppBGRA; |
| 217 |
} |
| 218 |
|
| 219 |
hr = formatConverter->Initialize(fImageSource.get(), format, WICBitmapDitherTypeNone, nullptr, |
| 220 |
0.0, WICBitmapPaletteTypeCustom); |
| 221 |
if (FAILED(hr)) { |
| 222 |
return false; |
| 223 |
} |
| 224 |
|
| 225 |
// Treat the format converter as an image source. |
| 226 |
SkTScopedComPtr<IWICBitmapSource> formatConverterSrc; |
| 227 |
hr = formatConverter->QueryInterface(IID_PPV_ARGS(&formatConverterSrc)); |
| 228 |
if (FAILED(hr)) { |
| 229 |
return false; |
| 230 |
} |
| 231 |
|
| 232 |
SkPixmap dst(info, pixels, rowBytes); |
| 233 |
auto decode = [&formatConverterSrc](const SkPixmap& pm) { |
| 234 |
// Get the destination pixels. |
| 235 |
void* pixelsAddr = pm.writable_addr(); |
| 236 |
size_t rowBytes = pm.rowBytes(); |
| 237 |
const SkImageInfo& info = pm.info(); |
| 238 |
|
| 239 |
// Set the destination pixels. |
| 240 |
HRESULT hr = formatConverterSrc->CopyPixels(nullptr, (UINT) rowBytes, (UINT) rowBytes * info.height(), |
| 241 |
(BYTE*) pixelsAddr); |
| 242 |
return SUCCEEDED(hr); |
| 243 |
}; |
| 244 |
return SkPixmapUtils::Orient(dst, fOrigin, decode); |
| 245 |
} |