epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * 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.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
bungeman@google.com | 8530296 | 2013-10-11 20:04:08 +0000 | [diff] [blame] | 9 | #include "SkTypes.h" |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
mtklein | 1ee7651 | 2015-11-02 10:20:27 -0800 | [diff] [blame] | 11 | #if defined(SK_BUILD_FOR_WIN32) |
| 12 | |
bungeman@google.com | 8530296 | 2013-10-11 20:04:08 +0000 | [diff] [blame] | 13 | // 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.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 32 | #include <wincodec.h> |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 33 | #include "SkAutoCoInitialize.h" |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 34 | #include "SkImageDecoder.h" |
| 35 | #include "SkImageEncoder.h" |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 36 | #include "SkIStream.h" |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 37 | #include "SkMovie.h" |
| 38 | #include "SkStream.h" |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 39 | #include "SkTScopedComPtr.h" |
robertphillips@google.com | 59bfb12 | 2012-11-08 15:06:52 +0000 | [diff] [blame] | 40 | #include "SkUnPreMultiply.h" |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 41 | |
bungeman@google.com | c18143e | 2013-01-11 20:02:32 +0000 | [diff] [blame] | 42 | //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.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 51 | class SkImageDecoder_WIC : public SkImageDecoder { |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 52 | public: |
| 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 |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 65 | * kDecodePixels_WICMode, in which case it must not be nullptr. |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 66 | * @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.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 71 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 72 | Result onDecode(SkStream* stream, SkBitmap* bm, Mode mode) override; |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 75 | struct FormatConversion { |
| 76 | GUID fGuidFormat; |
| 77 | SkImageDecoder::Format fFormat; |
| 78 | }; |
| 79 | |
| 80 | static 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 | |
| 88 | static 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 | |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 97 | SkImageDecoder::Result SkImageDecoder_WIC::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) { |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 98 | 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 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 107 | return this->decodeStream(stream, bm, wicMode, nullptr) ? kSuccess : kFailure; |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | bool SkImageDecoder_WIC::decodeStream(SkStream* stream, SkBitmap* bm, WICModes wicMode, |
| 111 | Format* format) const { |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 112 | //Initialize COM. |
bungeman@google.com | 2e2f3f5 | 2011-09-16 15:37:20 +0000 | [diff] [blame] | 113 | SkAutoCoInitialize scopedCo; |
| 114 | if (!scopedCo.succeeded()) { |
| 115 | return false; |
| 116 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 117 | |
bungeman@google.com | 2e2f3f5 | 2011-09-16 15:37:20 +0000 | [diff] [blame] | 118 | HRESULT hr = S_OK; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 119 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 120 | //Create Windows Imaging Component ImagingFactory. |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 121 | SkTScopedComPtr<IWICImagingFactory> piImagingFactory; |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 122 | if (SUCCEEDED(hr)) { |
| 123 | hr = CoCreateInstance( |
| 124 | CLSID_WICImagingFactory |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 125 | , nullptr |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 126 | , CLSCTX_INPROC_SERVER |
| 127 | , IID_PPV_ARGS(&piImagingFactory) |
| 128 | ); |
| 129 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 130 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 131 | //Convert SkStream to IStream. |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 132 | SkTScopedComPtr<IStream> piStream; |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 133 | if (SUCCEEDED(hr)) { |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 134 | hr = SkIStream::CreateFromSkStream(stream, false, &piStream); |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 135 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 136 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 137 | //Make sure we're at the beginning of the stream. |
| 138 | if (SUCCEEDED(hr)) { |
| 139 | LARGE_INTEGER liBeginning = { 0 }; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 140 | hr = piStream->Seek(liBeginning, STREAM_SEEK_SET, nullptr); |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 141 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 142 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 143 | //Create the decoder from the stream content. |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 144 | SkTScopedComPtr<IWICBitmapDecoder> piBitmapDecoder; |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 145 | if (SUCCEEDED(hr)) { |
| 146 | hr = piImagingFactory->CreateDecoderFromStream( |
| 147 | piStream.get() //Image to be decoded |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 148 | , nullptr //No particular vendor |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 149 | , WICDecodeMetadataCacheOnDemand //Cache metadata when needed |
| 150 | , &piBitmapDecoder //Pointer to the decoder |
| 151 | ); |
| 152 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 153 | |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 154 | if (kDecodeFormat_WICMode == wicMode) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 155 | SkASSERT(format != nullptr); |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 156 | //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.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 168 | //Get the first frame from the decoder. |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 169 | SkTScopedComPtr<IWICBitmapFrameDecode> piBitmapFrameDecode; |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 170 | if (SUCCEEDED(hr)) { |
| 171 | hr = piBitmapDecoder->GetFrame(0, &piBitmapFrameDecode); |
| 172 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 173 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 174 | //Get the BitmapSource interface of the frame. |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 175 | SkTScopedComPtr<IWICBitmapSource> piBitmapSourceOriginal; |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 176 | if (SUCCEEDED(hr)) { |
| 177 | hr = piBitmapFrameDecode->QueryInterface( |
| 178 | IID_PPV_ARGS(&piBitmapSourceOriginal) |
| 179 | ); |
| 180 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 181 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 182 | //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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 188 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 189 | //Exit early if we're only looking for the bitmap bounds. |
| 190 | if (SUCCEEDED(hr)) { |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 191 | bm->setInfo(SkImageInfo::MakeN32Premul(width, height)); |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 192 | if (kDecodeBounds_WICMode == wicMode) { |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 193 | return true; |
| 194 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 195 | if (!this->allocPixelRef(bm, nullptr)) { |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 196 | return false; |
| 197 | } |
| 198 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 199 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 200 | //Create a format converter. |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 201 | SkTScopedComPtr<IWICFormatConverter> piFormatConverter; |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 202 | if (SUCCEEDED(hr)) { |
| 203 | hr = piImagingFactory->CreateFormatConverter(&piFormatConverter); |
| 204 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 205 | |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 206 | GUID destinationPixelFormat; |
| 207 | if (this->getRequireUnpremultipliedColors()) { |
| 208 | destinationPixelFormat = GUID_WICPixelFormat32bppBGRA; |
| 209 | } else { |
| 210 | destinationPixelFormat = GUID_WICPixelFormat32bppPBGRA; |
| 211 | } |
| 212 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 213 | if (SUCCEEDED(hr)) { |
| 214 | hr = piFormatConverter->Initialize( |
| 215 | piBitmapSourceOriginal.get() //Input bitmap to convert |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 216 | , destinationPixelFormat //Destination pixel format |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 217 | , WICBitmapDitherTypeNone //Specified dither patterm |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 218 | , nullptr //Specify a particular palette |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 219 | , 0.f //Alpha threshold |
| 220 | , WICBitmapPaletteTypeCustom //Palette translation type |
| 221 | ); |
| 222 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 223 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 224 | //Get the BitmapSource interface of the format converter. |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 225 | SkTScopedComPtr<IWICBitmapSource> piBitmapSourceConverted; |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 226 | if (SUCCEEDED(hr)) { |
| 227 | hr = piFormatConverter->QueryInterface( |
| 228 | IID_PPV_ARGS(&piBitmapSourceConverted) |
| 229 | ); |
| 230 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 231 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 232 | //Copy the pixels into the bitmap. |
| 233 | if (SUCCEEDED(hr)) { |
bungeman@google.com | 955bb07 | 2011-08-01 20:18:45 +0000 | [diff] [blame] | 234 | SkAutoLockPixels alp(*bm); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 235 | bm->eraseColor(SK_ColorTRANSPARENT); |
robertphillips@google.com | 8b16931 | 2013-10-15 17:47:36 +0000 | [diff] [blame] | 236 | const UINT stride = (UINT) bm->rowBytes(); |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 237 | hr = piBitmapSourceConverted->CopyPixels( |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 238 | nullptr, //Get all the pixels |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 239 | stride, |
| 240 | stride * height, |
| 241 | reinterpret_cast<BYTE *>(bm->getPixels()) |
| 242 | ); |
robertphillips@google.com | 59bfb12 | 2012-11-08 15:06:52 +0000 | [diff] [blame] | 243 | |
| 244 | // Note: we don't need to premultiply here since we specified PBGRA |
reed@google.com | e7e29b7 | 2013-10-21 14:31:20 +0000 | [diff] [blame] | 245 | if (SkBitmap::ComputeIsOpaque(*bm)) { |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 246 | bm->setAlphaType(kOpaque_SkAlphaType); |
| 247 | } |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 248 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 249 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 250 | return SUCCEEDED(hr); |
| 251 | } |
| 252 | |
| 253 | ///////////////////////////////////////////////////////////////////////// |
| 254 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 255 | extern SkImageDecoder* image_decoder_from_stream(SkStreamRewindable*); |
scroggo@google.com | 4c6adf9 | 2013-04-17 21:07:55 +0000 | [diff] [blame] | 256 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 257 | SkImageDecoder* SkImageDecoder::Factory(SkStreamRewindable* stream) { |
scroggo@google.com | 4c6adf9 | 2013-04-17 21:07:55 +0000 | [diff] [blame] | 258 | SkImageDecoder* decoder = image_decoder_from_stream(stream); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 259 | if (nullptr == decoder) { |
scroggo@google.com | 4c6adf9 | 2013-04-17 21:07:55 +0000 | [diff] [blame] | 260 | // If no image decoder specific to the stream exists, use SkImageDecoder_WIC. |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 261 | return new SkImageDecoder_WIC; |
scroggo@google.com | 4c6adf9 | 2013-04-17 21:07:55 +0000 | [diff] [blame] | 262 | } else { |
| 263 | return decoder; |
| 264 | } |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | ///////////////////////////////////////////////////////////////////////// |
| 268 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 269 | SkMovie* SkMovie::DecodeStream(SkStreamRewindable* stream) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 270 | return nullptr; |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | ///////////////////////////////////////////////////////////////////////// |
| 274 | |
| 275 | class SkImageEncoder_WIC : public SkImageEncoder { |
| 276 | public: |
| 277 | SkImageEncoder_WIC(Type t) : fType(t) {} |
| 278 | |
| 279 | protected: |
| 280 | virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality); |
| 281 | |
| 282 | private: |
| 283 | Type fType; |
| 284 | }; |
| 285 | |
| 286 | bool SkImageEncoder_WIC::onEncode(SkWStream* stream |
bungeman@google.com | 22b4950 | 2011-08-01 19:37:43 +0000 | [diff] [blame] | 287 | , const SkBitmap& bitmapOrig |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 288 | , int quality) |
| 289 | { |
| 290 | GUID type; |
| 291 | switch (fType) { |
scroggo@google.com | 4c6adf9 | 2013-04-17 21:07:55 +0000 | [diff] [blame] | 292 | case kBMP_Type: |
| 293 | type = GUID_ContainerFormatBmp; |
| 294 | break; |
scroggo@google.com | 4c6adf9 | 2013-04-17 21:07:55 +0000 | [diff] [blame] | 295 | case kICO_Type: |
| 296 | type = GUID_ContainerFormatIco; |
| 297 | break; |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 298 | 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.com | 22b4950 | 2011-08-01 19:37:43 +0000 | [diff] [blame] | 308 | //Convert to 8888 if needed. |
| 309 | const SkBitmap* bitmap; |
| 310 | SkBitmap bitmapCopy; |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 311 | if (kN32_SkColorType == bitmapOrig.colorType() && bitmapOrig.isOpaque()) { |
bungeman@google.com | 22b4950 | 2011-08-01 19:37:43 +0000 | [diff] [blame] | 312 | bitmap = &bitmapOrig; |
| 313 | } else { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 314 | if (!bitmapOrig.copyTo(&bitmapCopy, kN32_SkColorType)) { |
bungeman@google.com | 22b4950 | 2011-08-01 19:37:43 +0000 | [diff] [blame] | 315 | return false; |
| 316 | } |
| 317 | bitmap = &bitmapCopy; |
| 318 | } |
| 319 | |
robertphillips@google.com | 59bfb12 | 2012-11-08 15:06:52 +0000 | [diff] [blame] | 320 | // 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.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 337 | //Initialize COM. |
bungeman@google.com | 2e2f3f5 | 2011-09-16 15:37:20 +0000 | [diff] [blame] | 338 | SkAutoCoInitialize scopedCo; |
| 339 | if (!scopedCo.succeeded()) { |
| 340 | return false; |
| 341 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 342 | |
bungeman@google.com | 2e2f3f5 | 2011-09-16 15:37:20 +0000 | [diff] [blame] | 343 | HRESULT hr = S_OK; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 344 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 345 | //Create Windows Imaging Component ImagingFactory. |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 346 | SkTScopedComPtr<IWICImagingFactory> piImagingFactory; |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 347 | if (SUCCEEDED(hr)) { |
| 348 | hr = CoCreateInstance( |
| 349 | CLSID_WICImagingFactory |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 350 | , nullptr |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 351 | , CLSCTX_INPROC_SERVER |
| 352 | , IID_PPV_ARGS(&piImagingFactory) |
| 353 | ); |
| 354 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 355 | |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 356 | //Convert the SkWStream to an IStream. |
| 357 | SkTScopedComPtr<IStream> piStream; |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 358 | if (SUCCEEDED(hr)) { |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 359 | hr = SkWIStream::CreateFromSkWStream(stream, &piStream); |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 360 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 361 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 362 | //Create an encode of the appropriate type. |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 363 | SkTScopedComPtr<IWICBitmapEncoder> piEncoder; |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 364 | if (SUCCEEDED(hr)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 365 | hr = piImagingFactory->CreateEncoder(type, nullptr, &piEncoder); |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 366 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 367 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 368 | if (SUCCEEDED(hr)) { |
| 369 | hr = piEncoder->Initialize(piStream.get(), WICBitmapEncoderNoCache); |
| 370 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 371 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 372 | //Create a the frame. |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 373 | SkTScopedComPtr<IWICBitmapFrameEncode> piBitmapFrameEncode; |
| 374 | SkTScopedComPtr<IPropertyBag2> piPropertybag; |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 375 | if (SUCCEEDED(hr)) { |
| 376 | hr = piEncoder->CreateNewFrame(&piBitmapFrameEncode, &piPropertybag); |
| 377 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 378 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 379 | 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 384 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 385 | VARIANT value; |
| 386 | VariantInit(&value); |
| 387 | value.vt = VT_R4; |
| 388 | value.fltVal = (FLOAT)(quality / 100.0); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 389 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 390 | //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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 399 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 400 | //Set the size of the frame. |
bungeman@google.com | 22b4950 | 2011-08-01 19:37:43 +0000 | [diff] [blame] | 401 | const UINT width = bitmap->width(); |
| 402 | const UINT height = bitmap->height(); |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 403 | if (SUCCEEDED(hr)) { |
| 404 | hr = piBitmapFrameEncode->SetSize(width, height); |
| 405 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 406 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 407 | //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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 417 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 418 | //Write the pixels into the frame. |
| 419 | if (SUCCEEDED(hr)) { |
bungeman@google.com | 955bb07 | 2011-08-01 20:18:45 +0000 | [diff] [blame] | 420 | SkAutoLockPixels alp(*bitmap); |
robertphillips@google.com | 8b16931 | 2013-10-15 17:47:36 +0000 | [diff] [blame] | 421 | const UINT stride = (UINT) bitmap->rowBytes(); |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 422 | hr = piBitmapFrameEncode->WritePixels( |
| 423 | height |
bungeman@google.com | 4b18f57 | 2013-07-22 15:21:23 +0000 | [diff] [blame] | 424 | , stride |
| 425 | , stride * height |
bungeman@google.com | 22b4950 | 2011-08-01 19:37:43 +0000 | [diff] [blame] | 426 | , reinterpret_cast<BYTE*>(bitmap->getPixels())); |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 427 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 428 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 429 | if (SUCCEEDED(hr)) { |
| 430 | hr = piBitmapFrameEncode->Commit(); |
| 431 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 432 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 433 | if (SUCCEEDED(hr)) { |
| 434 | hr = piEncoder->Commit(); |
| 435 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 436 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 437 | return SUCCEEDED(hr); |
| 438 | } |
| 439 | |
scroggo@google.com | 4c6adf9 | 2013-04-17 21:07:55 +0000 | [diff] [blame] | 440 | /////////////////////////////////////////////////////////////////////////////// |
| 441 | |
scroggo@google.com | 4c6adf9 | 2013-04-17 21:07:55 +0000 | [diff] [blame] | 442 | static SkImageEncoder* sk_imageencoder_wic_factory(SkImageEncoder::Type t) { |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 443 | switch (t) { |
scroggo@google.com | 4c6adf9 | 2013-04-17 21:07:55 +0000 | [diff] [blame] | 444 | case SkImageEncoder::kBMP_Type: |
scroggo@google.com | 4c6adf9 | 2013-04-17 21:07:55 +0000 | [diff] [blame] | 445 | case SkImageEncoder::kICO_Type: |
| 446 | case SkImageEncoder::kJPEG_Type: |
| 447 | case SkImageEncoder::kPNG_Type: |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 448 | break; |
| 449 | default: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 450 | return nullptr; |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 451 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 452 | return new SkImageEncoder_WIC(t); |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 453 | } |
scroggo@google.com | 4c6adf9 | 2013-04-17 21:07:55 +0000 | [diff] [blame] | 454 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 455 | static SkImageEncoder_EncodeReg gEReg(sk_imageencoder_wic_factory); |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 456 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 457 | static SkImageDecoder::Format get_format_wic(SkStreamRewindable* stream) { |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 458 | SkImageDecoder::Format format; |
| 459 | SkImageDecoder_WIC codec; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 460 | if (!codec.decodeStream(stream, nullptr, SkImageDecoder_WIC::kDecodeFormat_WICMode, &format)) { |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 461 | format = SkImageDecoder::kUnknown_Format; |
| 462 | } |
| 463 | return format; |
| 464 | } |
| 465 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 466 | static SkImageDecoder_FormatReg gFormatReg(get_format_wic); |
mtklein | 1ee7651 | 2015-11-02 10:20:27 -0800 | [diff] [blame] | 467 | |
| 468 | #endif // defined(SK_BUILD_FOR_WIN32) |