blob: fa408836a4a4c09f71d3389ea1121b9d3f653cc8 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bungeman@google.com242bb892011-06-22 20:42:34 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
bungeman@google.com242bb892011-06-22 20:42:34 +00007 */
8
bungeman@google.com85302962013-10-11 20:04:08 +00009#include "SkTypes.h"
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
mtklein1ee76512015-11-02 10:20:27 -080011#if defined(SK_BUILD_FOR_WIN32)
12
bungeman@google.com85302962013-10-11 20:04:08 +000013// Workaround for:
14// http://connect.microsoft.com/VisualStudio/feedback/details/621653/
15// http://crbug.com/225822
16// In VS2010 both intsafe.h and stdint.h define the following without guards.
17// SkTypes brought in windows.h and stdint.h and the following defines are
18// not used by this file. However, they may be re-introduced by wincodec.h.
19#undef INT8_MIN
20#undef INT16_MIN
21#undef INT32_MIN
22#undef INT64_MIN
23#undef INT8_MAX
24#undef UINT8_MAX
25#undef INT16_MAX
26#undef UINT16_MAX
27#undef INT32_MAX
28#undef UINT32_MAX
29#undef INT64_MAX
30#undef UINT64_MAX
31
bungeman@google.com242bb892011-06-22 20:42:34 +000032#include <wincodec.h>
bungeman@google.com9df621d2011-06-23 21:43:52 +000033#include "SkAutoCoInitialize.h"
bungeman@google.com242bb892011-06-22 20:42:34 +000034#include "SkImageDecoder.h"
35#include "SkImageEncoder.h"
bungeman@google.com9df621d2011-06-23 21:43:52 +000036#include "SkIStream.h"
bungeman@google.com242bb892011-06-22 20:42:34 +000037#include "SkMovie.h"
38#include "SkStream.h"
bungeman@google.com9df621d2011-06-23 21:43:52 +000039#include "SkTScopedComPtr.h"
robertphillips@google.com59bfb122012-11-08 15:06:52 +000040#include "SkUnPreMultiply.h"
bungeman@google.com242bb892011-06-22 20:42:34 +000041
bungeman@google.comc18143e2013-01-11 20:02:32 +000042//All Windows SDKs back to XPSP2 export the CLSID_WICImagingFactory symbol.
43//In the Windows8 SDK the CLSID_WICImagingFactory symbol is still exported
44//but CLSID_WICImagingFactory is then #defined to CLSID_WICImagingFactory2.
45//Undo this #define if it has been done so that we link against the symbols
46//we intended to link against on all SDKs.
47#if defined(CLSID_WICImagingFactory)
48#undef CLSID_WICImagingFactory
49#endif
50
bungeman@google.com242bb892011-06-22 20:42:34 +000051class SkImageDecoder_WIC : public SkImageDecoder {
scroggo@google.com39edf4c2013-04-25 17:33:51 +000052public:
53 // Decoding modes corresponding to SkImageDecoder::Mode, plus an extra mode for decoding
54 // only the format.
55 enum WICModes {
56 kDecodeFormat_WICMode,
57 kDecodeBounds_WICMode,
58 kDecodePixels_WICMode,
59 };
60
61 /**
62 * Helper function to decode an SkStream.
63 * @param stream SkStream to decode. Must be at the beginning.
64 * @param bm SkBitmap to decode into. Only used if wicMode is kDecodeBounds_WICMode or
halcanary96fcdcc2015-08-27 07:41:13 -070065 * kDecodePixels_WICMode, in which case it must not be nullptr.
scroggo@google.com39edf4c2013-04-25 17:33:51 +000066 * @param format Out parameter for the SkImageDecoder::Format of the SkStream. Only used if
67 * wicMode is kDecodeFormat_WICMode.
68 */
69 bool decodeStream(SkStream* stream, SkBitmap* bm, WICModes wicMode, Format* format) const;
70
bungeman@google.com242bb892011-06-22 20:42:34 +000071protected:
mtklein36352bf2015-03-25 18:17:31 -070072 Result onDecode(SkStream* stream, SkBitmap* bm, Mode mode) override;
bungeman@google.com242bb892011-06-22 20:42:34 +000073};
74
scroggo@google.com39edf4c2013-04-25 17:33:51 +000075struct FormatConversion {
76 GUID fGuidFormat;
77 SkImageDecoder::Format fFormat;
78};
79
80static const FormatConversion gFormatConversions[] = {
81 { GUID_ContainerFormatBmp, SkImageDecoder::kBMP_Format },
82 { GUID_ContainerFormatGif, SkImageDecoder::kGIF_Format },
83 { GUID_ContainerFormatIco, SkImageDecoder::kICO_Format },
84 { GUID_ContainerFormatJpeg, SkImageDecoder::kJPEG_Format },
85 { GUID_ContainerFormatPng, SkImageDecoder::kPNG_Format },
86};
87
88static SkImageDecoder::Format GuidContainerFormat_to_Format(REFGUID guid) {
89 for (size_t i = 0; i < SK_ARRAY_COUNT(gFormatConversions); i++) {
90 if (IsEqualGUID(guid, gFormatConversions[i].fGuidFormat)) {
91 return gFormatConversions[i].fFormat;
92 }
93 }
94 return SkImageDecoder::kUnknown_Format;
95}
96
scroggo2a120802014-10-22 12:07:00 -070097SkImageDecoder::Result SkImageDecoder_WIC::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
scroggo@google.com39edf4c2013-04-25 17:33:51 +000098 WICModes wicMode;
99 switch (mode) {
100 case SkImageDecoder::kDecodeBounds_Mode:
101 wicMode = kDecodeBounds_WICMode;
102 break;
103 case SkImageDecoder::kDecodePixels_Mode:
104 wicMode = kDecodePixels_WICMode;
105 break;
106 }
halcanary96fcdcc2015-08-27 07:41:13 -0700107 return this->decodeStream(stream, bm, wicMode, nullptr) ? kSuccess : kFailure;
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000108}
109
110bool SkImageDecoder_WIC::decodeStream(SkStream* stream, SkBitmap* bm, WICModes wicMode,
111 Format* format) const {
bungeman@google.com242bb892011-06-22 20:42:34 +0000112 //Initialize COM.
bungeman@google.com2e2f3f52011-09-16 15:37:20 +0000113 SkAutoCoInitialize scopedCo;
114 if (!scopedCo.succeeded()) {
115 return false;
116 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000117
bungeman@google.com2e2f3f52011-09-16 15:37:20 +0000118 HRESULT hr = S_OK;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000119
bungeman@google.com242bb892011-06-22 20:42:34 +0000120 //Create Windows Imaging Component ImagingFactory.
bungeman@google.com9df621d2011-06-23 21:43:52 +0000121 SkTScopedComPtr<IWICImagingFactory> piImagingFactory;
bungeman@google.com242bb892011-06-22 20:42:34 +0000122 if (SUCCEEDED(hr)) {
123 hr = CoCreateInstance(
124 CLSID_WICImagingFactory
halcanary96fcdcc2015-08-27 07:41:13 -0700125 , nullptr
bungeman@google.com242bb892011-06-22 20:42:34 +0000126 , CLSCTX_INPROC_SERVER
127 , IID_PPV_ARGS(&piImagingFactory)
128 );
129 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000130
bungeman@google.com242bb892011-06-22 20:42:34 +0000131 //Convert SkStream to IStream.
bungeman@google.com9df621d2011-06-23 21:43:52 +0000132 SkTScopedComPtr<IStream> piStream;
bungeman@google.com242bb892011-06-22 20:42:34 +0000133 if (SUCCEEDED(hr)) {
bungeman@google.com9df621d2011-06-23 21:43:52 +0000134 hr = SkIStream::CreateFromSkStream(stream, false, &piStream);
bungeman@google.com242bb892011-06-22 20:42:34 +0000135 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000136
bungeman@google.com242bb892011-06-22 20:42:34 +0000137 //Make sure we're at the beginning of the stream.
138 if (SUCCEEDED(hr)) {
139 LARGE_INTEGER liBeginning = { 0 };
halcanary96fcdcc2015-08-27 07:41:13 -0700140 hr = piStream->Seek(liBeginning, STREAM_SEEK_SET, nullptr);
bungeman@google.com242bb892011-06-22 20:42:34 +0000141 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000142
bungeman@google.com242bb892011-06-22 20:42:34 +0000143 //Create the decoder from the stream content.
bungeman@google.com9df621d2011-06-23 21:43:52 +0000144 SkTScopedComPtr<IWICBitmapDecoder> piBitmapDecoder;
bungeman@google.com242bb892011-06-22 20:42:34 +0000145 if (SUCCEEDED(hr)) {
146 hr = piImagingFactory->CreateDecoderFromStream(
147 piStream.get() //Image to be decoded
halcanary96fcdcc2015-08-27 07:41:13 -0700148 , nullptr //No particular vendor
bungeman@google.com242bb892011-06-22 20:42:34 +0000149 , WICDecodeMetadataCacheOnDemand //Cache metadata when needed
150 , &piBitmapDecoder //Pointer to the decoder
151 );
152 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000153
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000154 if (kDecodeFormat_WICMode == wicMode) {
halcanary96fcdcc2015-08-27 07:41:13 -0700155 SkASSERT(format != nullptr);
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000156 //Get the format
157 if (SUCCEEDED(hr)) {
158 GUID guidFormat;
159 hr = piBitmapDecoder->GetContainerFormat(&guidFormat);
160 if (SUCCEEDED(hr)) {
161 *format = GuidContainerFormat_to_Format(guidFormat);
162 return true;
163 }
164 }
165 return false;
166 }
167
bungeman@google.com242bb892011-06-22 20:42:34 +0000168 //Get the first frame from the decoder.
bungeman@google.com9df621d2011-06-23 21:43:52 +0000169 SkTScopedComPtr<IWICBitmapFrameDecode> piBitmapFrameDecode;
bungeman@google.com242bb892011-06-22 20:42:34 +0000170 if (SUCCEEDED(hr)) {
171 hr = piBitmapDecoder->GetFrame(0, &piBitmapFrameDecode);
172 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000173
bungeman@google.com242bb892011-06-22 20:42:34 +0000174 //Get the BitmapSource interface of the frame.
bungeman@google.com9df621d2011-06-23 21:43:52 +0000175 SkTScopedComPtr<IWICBitmapSource> piBitmapSourceOriginal;
bungeman@google.com242bb892011-06-22 20:42:34 +0000176 if (SUCCEEDED(hr)) {
177 hr = piBitmapFrameDecode->QueryInterface(
178 IID_PPV_ARGS(&piBitmapSourceOriginal)
179 );
180 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000181
bungeman@google.com242bb892011-06-22 20:42:34 +0000182 //Get the size of the bitmap.
183 UINT width;
184 UINT height;
185 if (SUCCEEDED(hr)) {
186 hr = piBitmapSourceOriginal->GetSize(&width, &height);
187 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000188
bungeman@google.com242bb892011-06-22 20:42:34 +0000189 //Exit early if we're only looking for the bitmap bounds.
190 if (SUCCEEDED(hr)) {
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000191 bm->setInfo(SkImageInfo::MakeN32Premul(width, height));
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000192 if (kDecodeBounds_WICMode == wicMode) {
bungeman@google.com242bb892011-06-22 20:42:34 +0000193 return true;
194 }
halcanary96fcdcc2015-08-27 07:41:13 -0700195 if (!this->allocPixelRef(bm, nullptr)) {
bungeman@google.com242bb892011-06-22 20:42:34 +0000196 return false;
197 }
198 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000199
bungeman@google.com242bb892011-06-22 20:42:34 +0000200 //Create a format converter.
bungeman@google.com9df621d2011-06-23 21:43:52 +0000201 SkTScopedComPtr<IWICFormatConverter> piFormatConverter;
bungeman@google.com242bb892011-06-22 20:42:34 +0000202 if (SUCCEEDED(hr)) {
203 hr = piImagingFactory->CreateFormatConverter(&piFormatConverter);
204 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000205
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000206 GUID destinationPixelFormat;
207 if (this->getRequireUnpremultipliedColors()) {
208 destinationPixelFormat = GUID_WICPixelFormat32bppBGRA;
209 } else {
210 destinationPixelFormat = GUID_WICPixelFormat32bppPBGRA;
211 }
212
bungeman@google.com242bb892011-06-22 20:42:34 +0000213 if (SUCCEEDED(hr)) {
214 hr = piFormatConverter->Initialize(
215 piBitmapSourceOriginal.get() //Input bitmap to convert
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000216 , destinationPixelFormat //Destination pixel format
bungeman@google.com242bb892011-06-22 20:42:34 +0000217 , WICBitmapDitherTypeNone //Specified dither patterm
halcanary96fcdcc2015-08-27 07:41:13 -0700218 , nullptr //Specify a particular palette
bungeman@google.com242bb892011-06-22 20:42:34 +0000219 , 0.f //Alpha threshold
220 , WICBitmapPaletteTypeCustom //Palette translation type
221 );
222 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000223
bungeman@google.com242bb892011-06-22 20:42:34 +0000224 //Get the BitmapSource interface of the format converter.
bungeman@google.com9df621d2011-06-23 21:43:52 +0000225 SkTScopedComPtr<IWICBitmapSource> piBitmapSourceConverted;
bungeman@google.com242bb892011-06-22 20:42:34 +0000226 if (SUCCEEDED(hr)) {
227 hr = piFormatConverter->QueryInterface(
228 IID_PPV_ARGS(&piBitmapSourceConverted)
229 );
230 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000231
bungeman@google.com242bb892011-06-22 20:42:34 +0000232 //Copy the pixels into the bitmap.
233 if (SUCCEEDED(hr)) {
bungeman@google.com955bb072011-08-01 20:18:45 +0000234 SkAutoLockPixels alp(*bm);
junov@google.comdbfac8a2012-12-06 21:47:40 +0000235 bm->eraseColor(SK_ColorTRANSPARENT);
robertphillips@google.com8b169312013-10-15 17:47:36 +0000236 const UINT stride = (UINT) bm->rowBytes();
bungeman@google.com242bb892011-06-22 20:42:34 +0000237 hr = piBitmapSourceConverted->CopyPixels(
halcanary96fcdcc2015-08-27 07:41:13 -0700238 nullptr, //Get all the pixels
bungeman@google.com242bb892011-06-22 20:42:34 +0000239 stride,
240 stride * height,
241 reinterpret_cast<BYTE *>(bm->getPixels())
242 );
robertphillips@google.com59bfb122012-11-08 15:06:52 +0000243
244 // Note: we don't need to premultiply here since we specified PBGRA
reed@google.come7e29b72013-10-21 14:31:20 +0000245 if (SkBitmap::ComputeIsOpaque(*bm)) {
reed@google.com383a6972013-10-21 14:00:07 +0000246 bm->setAlphaType(kOpaque_SkAlphaType);
247 }
bungeman@google.com242bb892011-06-22 20:42:34 +0000248 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000249
bungeman@google.com242bb892011-06-22 20:42:34 +0000250 return SUCCEEDED(hr);
251}
252
253/////////////////////////////////////////////////////////////////////////
254
scroggo@google.comb5571b32013-09-25 21:34:24 +0000255extern SkImageDecoder* image_decoder_from_stream(SkStreamRewindable*);
scroggo@google.com4c6adf92013-04-17 21:07:55 +0000256
scroggo@google.comb5571b32013-09-25 21:34:24 +0000257SkImageDecoder* SkImageDecoder::Factory(SkStreamRewindable* stream) {
scroggo@google.com4c6adf92013-04-17 21:07:55 +0000258 SkImageDecoder* decoder = image_decoder_from_stream(stream);
halcanary96fcdcc2015-08-27 07:41:13 -0700259 if (nullptr == decoder) {
scroggo@google.com4c6adf92013-04-17 21:07:55 +0000260 // If no image decoder specific to the stream exists, use SkImageDecoder_WIC.
halcanary385fe4d2015-08-26 13:07:48 -0700261 return new SkImageDecoder_WIC;
scroggo@google.com4c6adf92013-04-17 21:07:55 +0000262 } else {
263 return decoder;
264 }
bungeman@google.com242bb892011-06-22 20:42:34 +0000265}
266
267/////////////////////////////////////////////////////////////////////////
268
scroggo@google.comb5571b32013-09-25 21:34:24 +0000269SkMovie* SkMovie::DecodeStream(SkStreamRewindable* stream) {
halcanary96fcdcc2015-08-27 07:41:13 -0700270 return nullptr;
bungeman@google.com242bb892011-06-22 20:42:34 +0000271}
272
273/////////////////////////////////////////////////////////////////////////
274
275class SkImageEncoder_WIC : public SkImageEncoder {
276public:
277 SkImageEncoder_WIC(Type t) : fType(t) {}
278
279protected:
280 virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality);
281
282private:
283 Type fType;
284};
285
286bool SkImageEncoder_WIC::onEncode(SkWStream* stream
bungeman@google.com22b49502011-08-01 19:37:43 +0000287 , const SkBitmap& bitmapOrig
bungeman@google.com242bb892011-06-22 20:42:34 +0000288 , int quality)
289{
290 GUID type;
291 switch (fType) {
scroggo@google.com4c6adf92013-04-17 21:07:55 +0000292 case kBMP_Type:
293 type = GUID_ContainerFormatBmp;
294 break;
scroggo@google.com4c6adf92013-04-17 21:07:55 +0000295 case kICO_Type:
296 type = GUID_ContainerFormatIco;
297 break;
bungeman@google.com242bb892011-06-22 20:42:34 +0000298 case kJPEG_Type:
299 type = GUID_ContainerFormatJpeg;
300 break;
301 case kPNG_Type:
302 type = GUID_ContainerFormatPng;
303 break;
304 default:
305 return false;
306 }
307
bungeman@google.com22b49502011-08-01 19:37:43 +0000308 //Convert to 8888 if needed.
309 const SkBitmap* bitmap;
310 SkBitmap bitmapCopy;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000311 if (kN32_SkColorType == bitmapOrig.colorType() && bitmapOrig.isOpaque()) {
bungeman@google.com22b49502011-08-01 19:37:43 +0000312 bitmap = &bitmapOrig;
313 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000314 if (!bitmapOrig.copyTo(&bitmapCopy, kN32_SkColorType)) {
bungeman@google.com22b49502011-08-01 19:37:43 +0000315 return false;
316 }
317 bitmap = &bitmapCopy;
318 }
319
robertphillips@google.com59bfb122012-11-08 15:06:52 +0000320 // We cannot use PBGRA so we need to unpremultiply ourselves
321 if (!bitmap->isOpaque()) {
322 SkAutoLockPixels alp(*bitmap);
323
324 uint8_t* pixels = reinterpret_cast<uint8_t*>(bitmap->getPixels());
325 for (int y = 0; y < bitmap->height(); ++y) {
326 for (int x = 0; x < bitmap->width(); ++x) {
327 uint8_t* bytes = pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel();
328
329 SkPMColor* src = reinterpret_cast<SkPMColor*>(bytes);
330 SkColor* dst = reinterpret_cast<SkColor*>(bytes);
331
332 *dst = SkUnPreMultiply::PMColorToColor(*src);
333 }
334 }
335 }
336
bungeman@google.com242bb892011-06-22 20:42:34 +0000337 //Initialize COM.
bungeman@google.com2e2f3f52011-09-16 15:37:20 +0000338 SkAutoCoInitialize scopedCo;
339 if (!scopedCo.succeeded()) {
340 return false;
341 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000342
bungeman@google.com2e2f3f52011-09-16 15:37:20 +0000343 HRESULT hr = S_OK;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000344
bungeman@google.com242bb892011-06-22 20:42:34 +0000345 //Create Windows Imaging Component ImagingFactory.
bungeman@google.com9df621d2011-06-23 21:43:52 +0000346 SkTScopedComPtr<IWICImagingFactory> piImagingFactory;
bungeman@google.com242bb892011-06-22 20:42:34 +0000347 if (SUCCEEDED(hr)) {
348 hr = CoCreateInstance(
349 CLSID_WICImagingFactory
halcanary96fcdcc2015-08-27 07:41:13 -0700350 , nullptr
bungeman@google.com242bb892011-06-22 20:42:34 +0000351 , CLSCTX_INPROC_SERVER
352 , IID_PPV_ARGS(&piImagingFactory)
353 );
354 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000355
bungeman@google.com9df621d2011-06-23 21:43:52 +0000356 //Convert the SkWStream to an IStream.
357 SkTScopedComPtr<IStream> piStream;
bungeman@google.com242bb892011-06-22 20:42:34 +0000358 if (SUCCEEDED(hr)) {
bungeman@google.com9df621d2011-06-23 21:43:52 +0000359 hr = SkWIStream::CreateFromSkWStream(stream, &piStream);
bungeman@google.com242bb892011-06-22 20:42:34 +0000360 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000361
bungeman@google.com242bb892011-06-22 20:42:34 +0000362 //Create an encode of the appropriate type.
bungeman@google.com9df621d2011-06-23 21:43:52 +0000363 SkTScopedComPtr<IWICBitmapEncoder> piEncoder;
bungeman@google.com242bb892011-06-22 20:42:34 +0000364 if (SUCCEEDED(hr)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700365 hr = piImagingFactory->CreateEncoder(type, nullptr, &piEncoder);
bungeman@google.com242bb892011-06-22 20:42:34 +0000366 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000367
bungeman@google.com242bb892011-06-22 20:42:34 +0000368 if (SUCCEEDED(hr)) {
369 hr = piEncoder->Initialize(piStream.get(), WICBitmapEncoderNoCache);
370 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000371
bungeman@google.com242bb892011-06-22 20:42:34 +0000372 //Create a the frame.
bungeman@google.com9df621d2011-06-23 21:43:52 +0000373 SkTScopedComPtr<IWICBitmapFrameEncode> piBitmapFrameEncode;
374 SkTScopedComPtr<IPropertyBag2> piPropertybag;
bungeman@google.com242bb892011-06-22 20:42:34 +0000375 if (SUCCEEDED(hr)) {
376 hr = piEncoder->CreateNewFrame(&piBitmapFrameEncode, &piPropertybag);
377 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000378
bungeman@google.com242bb892011-06-22 20:42:34 +0000379 if (SUCCEEDED(hr)) {
380 PROPBAG2 name = { 0 };
381 name.dwType = PROPBAG2_TYPE_DATA;
382 name.vt = VT_R4;
383 name.pstrName = L"ImageQuality";
rmistry@google.comd6176b02012-08-23 18:14:13 +0000384
bungeman@google.com242bb892011-06-22 20:42:34 +0000385 VARIANT value;
386 VariantInit(&value);
387 value.vt = VT_R4;
388 value.fltVal = (FLOAT)(quality / 100.0);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000389
bungeman@google.com242bb892011-06-22 20:42:34 +0000390 //Ignore result code.
391 // This returns E_FAIL if the named property is not in the bag.
392 //TODO(bungeman) enumerate the properties,
393 // write and set hr iff property exists.
394 piPropertybag->Write(1, &name, &value);
395 }
396 if (SUCCEEDED(hr)) {
397 hr = piBitmapFrameEncode->Initialize(piPropertybag.get());
398 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000399
bungeman@google.com242bb892011-06-22 20:42:34 +0000400 //Set the size of the frame.
bungeman@google.com22b49502011-08-01 19:37:43 +0000401 const UINT width = bitmap->width();
402 const UINT height = bitmap->height();
bungeman@google.com242bb892011-06-22 20:42:34 +0000403 if (SUCCEEDED(hr)) {
404 hr = piBitmapFrameEncode->SetSize(width, height);
405 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000406
bungeman@google.com242bb892011-06-22 20:42:34 +0000407 //Set the pixel format of the frame.
408 const WICPixelFormatGUID formatDesired = GUID_WICPixelFormat32bppBGRA;
409 WICPixelFormatGUID formatGUID = formatDesired;
410 if (SUCCEEDED(hr)) {
411 hr = piBitmapFrameEncode->SetPixelFormat(&formatGUID);
412 }
413 if (SUCCEEDED(hr)) {
414 //Be sure the image format is the one requested.
415 hr = IsEqualGUID(formatGUID, formatDesired) ? S_OK : E_FAIL;
416 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000417
bungeman@google.com242bb892011-06-22 20:42:34 +0000418 //Write the pixels into the frame.
419 if (SUCCEEDED(hr)) {
bungeman@google.com955bb072011-08-01 20:18:45 +0000420 SkAutoLockPixels alp(*bitmap);
robertphillips@google.com8b169312013-10-15 17:47:36 +0000421 const UINT stride = (UINT) bitmap->rowBytes();
bungeman@google.com242bb892011-06-22 20:42:34 +0000422 hr = piBitmapFrameEncode->WritePixels(
423 height
bungeman@google.com4b18f572013-07-22 15:21:23 +0000424 , stride
425 , stride * height
bungeman@google.com22b49502011-08-01 19:37:43 +0000426 , reinterpret_cast<BYTE*>(bitmap->getPixels()));
bungeman@google.com242bb892011-06-22 20:42:34 +0000427 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000428
bungeman@google.com242bb892011-06-22 20:42:34 +0000429 if (SUCCEEDED(hr)) {
430 hr = piBitmapFrameEncode->Commit();
431 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000432
bungeman@google.com242bb892011-06-22 20:42:34 +0000433 if (SUCCEEDED(hr)) {
434 hr = piEncoder->Commit();
435 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000436
bungeman@google.com242bb892011-06-22 20:42:34 +0000437 return SUCCEEDED(hr);
438}
439
scroggo@google.com4c6adf92013-04-17 21:07:55 +0000440///////////////////////////////////////////////////////////////////////////////
441
scroggo@google.com4c6adf92013-04-17 21:07:55 +0000442static SkImageEncoder* sk_imageencoder_wic_factory(SkImageEncoder::Type t) {
bungeman@google.com242bb892011-06-22 20:42:34 +0000443 switch (t) {
scroggo@google.com4c6adf92013-04-17 21:07:55 +0000444 case SkImageEncoder::kBMP_Type:
scroggo@google.com4c6adf92013-04-17 21:07:55 +0000445 case SkImageEncoder::kICO_Type:
446 case SkImageEncoder::kJPEG_Type:
447 case SkImageEncoder::kPNG_Type:
bungeman@google.com242bb892011-06-22 20:42:34 +0000448 break;
449 default:
halcanary96fcdcc2015-08-27 07:41:13 -0700450 return nullptr;
bungeman@google.com242bb892011-06-22 20:42:34 +0000451 }
halcanary385fe4d2015-08-26 13:07:48 -0700452 return new SkImageEncoder_WIC(t);
bungeman@google.com242bb892011-06-22 20:42:34 +0000453}
scroggo@google.com4c6adf92013-04-17 21:07:55 +0000454
scroggo@google.comb5571b32013-09-25 21:34:24 +0000455static SkImageEncoder_EncodeReg gEReg(sk_imageencoder_wic_factory);
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000456
scroggo@google.comb5571b32013-09-25 21:34:24 +0000457static SkImageDecoder::Format get_format_wic(SkStreamRewindable* stream) {
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000458 SkImageDecoder::Format format;
459 SkImageDecoder_WIC codec;
halcanary96fcdcc2015-08-27 07:41:13 -0700460 if (!codec.decodeStream(stream, nullptr, SkImageDecoder_WIC::kDecodeFormat_WICMode, &format)) {
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000461 format = SkImageDecoder::kUnknown_Format;
462 }
463 return format;
464}
465
scroggo@google.comb5571b32013-09-25 21:34:24 +0000466static SkImageDecoder_FormatReg gFormatReg(get_format_wic);
mtklein1ee76512015-11-02 10:20:27 -0800467
468#endif // defined(SK_BUILD_FOR_WIN32)