OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 #include "SkTypes.h" | 9 #include "SkTypes.h" |
10 | 10 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 enum WICModes { | 53 enum WICModes { |
54 kDecodeFormat_WICMode, | 54 kDecodeFormat_WICMode, |
55 kDecodeBounds_WICMode, | 55 kDecodeBounds_WICMode, |
56 kDecodePixels_WICMode, | 56 kDecodePixels_WICMode, |
57 }; | 57 }; |
58 | 58 |
59 /** | 59 /** |
60 * Helper function to decode an SkStream. | 60 * Helper function to decode an SkStream. |
61 * @param stream SkStream to decode. Must be at the beginning. | 61 * @param stream SkStream to decode. Must be at the beginning. |
62 * @param bm SkBitmap to decode into. Only used if wicMode is kDecodeBoun
ds_WICMode or | 62 * @param bm SkBitmap to decode into. Only used if wicMode is kDecodeBoun
ds_WICMode or |
63 * kDecodePixels_WICMode, in which case it must not be NULL. | 63 * kDecodePixels_WICMode, in which case it must not be nullptr. |
64 * @param format Out parameter for the SkImageDecoder::Format of the SkStre
am. Only used if | 64 * @param format Out parameter for the SkImageDecoder::Format of the SkStre
am. Only used if |
65 * wicMode is kDecodeFormat_WICMode. | 65 * wicMode is kDecodeFormat_WICMode. |
66 */ | 66 */ |
67 bool decodeStream(SkStream* stream, SkBitmap* bm, WICModes wicMode, Format*
format) const; | 67 bool decodeStream(SkStream* stream, SkBitmap* bm, WICModes wicMode, Format*
format) const; |
68 | 68 |
69 protected: | 69 protected: |
70 Result onDecode(SkStream* stream, SkBitmap* bm, Mode mode) override; | 70 Result onDecode(SkStream* stream, SkBitmap* bm, Mode mode) override; |
71 }; | 71 }; |
72 | 72 |
73 struct FormatConversion { | 73 struct FormatConversion { |
(...skipping 21 matching lines...) Expand all Loading... |
95 SkImageDecoder::Result SkImageDecoder_WIC::onDecode(SkStream* stream, SkBitmap*
bm, Mode mode) { | 95 SkImageDecoder::Result SkImageDecoder_WIC::onDecode(SkStream* stream, SkBitmap*
bm, Mode mode) { |
96 WICModes wicMode; | 96 WICModes wicMode; |
97 switch (mode) { | 97 switch (mode) { |
98 case SkImageDecoder::kDecodeBounds_Mode: | 98 case SkImageDecoder::kDecodeBounds_Mode: |
99 wicMode = kDecodeBounds_WICMode; | 99 wicMode = kDecodeBounds_WICMode; |
100 break; | 100 break; |
101 case SkImageDecoder::kDecodePixels_Mode: | 101 case SkImageDecoder::kDecodePixels_Mode: |
102 wicMode = kDecodePixels_WICMode; | 102 wicMode = kDecodePixels_WICMode; |
103 break; | 103 break; |
104 } | 104 } |
105 return this->decodeStream(stream, bm, wicMode, NULL) ? kSuccess : kFailure; | 105 return this->decodeStream(stream, bm, wicMode, nullptr) ? kSuccess : kFailur
e; |
106 } | 106 } |
107 | 107 |
108 bool SkImageDecoder_WIC::decodeStream(SkStream* stream, SkBitmap* bm, WICModes w
icMode, | 108 bool SkImageDecoder_WIC::decodeStream(SkStream* stream, SkBitmap* bm, WICModes w
icMode, |
109 Format* format) const { | 109 Format* format) const { |
110 //Initialize COM. | 110 //Initialize COM. |
111 SkAutoCoInitialize scopedCo; | 111 SkAutoCoInitialize scopedCo; |
112 if (!scopedCo.succeeded()) { | 112 if (!scopedCo.succeeded()) { |
113 return false; | 113 return false; |
114 } | 114 } |
115 | 115 |
116 HRESULT hr = S_OK; | 116 HRESULT hr = S_OK; |
117 | 117 |
118 //Create Windows Imaging Component ImagingFactory. | 118 //Create Windows Imaging Component ImagingFactory. |
119 SkTScopedComPtr<IWICImagingFactory> piImagingFactory; | 119 SkTScopedComPtr<IWICImagingFactory> piImagingFactory; |
120 if (SUCCEEDED(hr)) { | 120 if (SUCCEEDED(hr)) { |
121 hr = CoCreateInstance( | 121 hr = CoCreateInstance( |
122 CLSID_WICImagingFactory | 122 CLSID_WICImagingFactory |
123 , NULL | 123 , nullptr |
124 , CLSCTX_INPROC_SERVER | 124 , CLSCTX_INPROC_SERVER |
125 , IID_PPV_ARGS(&piImagingFactory) | 125 , IID_PPV_ARGS(&piImagingFactory) |
126 ); | 126 ); |
127 } | 127 } |
128 | 128 |
129 //Convert SkStream to IStream. | 129 //Convert SkStream to IStream. |
130 SkTScopedComPtr<IStream> piStream; | 130 SkTScopedComPtr<IStream> piStream; |
131 if (SUCCEEDED(hr)) { | 131 if (SUCCEEDED(hr)) { |
132 hr = SkIStream::CreateFromSkStream(stream, false, &piStream); | 132 hr = SkIStream::CreateFromSkStream(stream, false, &piStream); |
133 } | 133 } |
134 | 134 |
135 //Make sure we're at the beginning of the stream. | 135 //Make sure we're at the beginning of the stream. |
136 if (SUCCEEDED(hr)) { | 136 if (SUCCEEDED(hr)) { |
137 LARGE_INTEGER liBeginning = { 0 }; | 137 LARGE_INTEGER liBeginning = { 0 }; |
138 hr = piStream->Seek(liBeginning, STREAM_SEEK_SET, NULL); | 138 hr = piStream->Seek(liBeginning, STREAM_SEEK_SET, nullptr); |
139 } | 139 } |
140 | 140 |
141 //Create the decoder from the stream content. | 141 //Create the decoder from the stream content. |
142 SkTScopedComPtr<IWICBitmapDecoder> piBitmapDecoder; | 142 SkTScopedComPtr<IWICBitmapDecoder> piBitmapDecoder; |
143 if (SUCCEEDED(hr)) { | 143 if (SUCCEEDED(hr)) { |
144 hr = piImagingFactory->CreateDecoderFromStream( | 144 hr = piImagingFactory->CreateDecoderFromStream( |
145 piStream.get() //Image to be decoded | 145 piStream.get() //Image to be decoded |
146 , NULL //No particular vendor | 146 , nullptr //No particular vendor |
147 , WICDecodeMetadataCacheOnDemand //Cache metadata when needed | 147 , WICDecodeMetadataCacheOnDemand //Cache metadata when needed |
148 , &piBitmapDecoder //Pointer to the decoder | 148 , &piBitmapDecoder //Pointer to the decoder |
149 ); | 149 ); |
150 } | 150 } |
151 | 151 |
152 if (kDecodeFormat_WICMode == wicMode) { | 152 if (kDecodeFormat_WICMode == wicMode) { |
153 SkASSERT(format != NULL); | 153 SkASSERT(format != nullptr); |
154 //Get the format | 154 //Get the format |
155 if (SUCCEEDED(hr)) { | 155 if (SUCCEEDED(hr)) { |
156 GUID guidFormat; | 156 GUID guidFormat; |
157 hr = piBitmapDecoder->GetContainerFormat(&guidFormat); | 157 hr = piBitmapDecoder->GetContainerFormat(&guidFormat); |
158 if (SUCCEEDED(hr)) { | 158 if (SUCCEEDED(hr)) { |
159 *format = GuidContainerFormat_to_Format(guidFormat); | 159 *format = GuidContainerFormat_to_Format(guidFormat); |
160 return true; | 160 return true; |
161 } | 161 } |
162 } | 162 } |
163 return false; | 163 return false; |
(...skipping 19 matching lines...) Expand all Loading... |
183 if (SUCCEEDED(hr)) { | 183 if (SUCCEEDED(hr)) { |
184 hr = piBitmapSourceOriginal->GetSize(&width, &height); | 184 hr = piBitmapSourceOriginal->GetSize(&width, &height); |
185 } | 185 } |
186 | 186 |
187 //Exit early if we're only looking for the bitmap bounds. | 187 //Exit early if we're only looking for the bitmap bounds. |
188 if (SUCCEEDED(hr)) { | 188 if (SUCCEEDED(hr)) { |
189 bm->setInfo(SkImageInfo::MakeN32Premul(width, height)); | 189 bm->setInfo(SkImageInfo::MakeN32Premul(width, height)); |
190 if (kDecodeBounds_WICMode == wicMode) { | 190 if (kDecodeBounds_WICMode == wicMode) { |
191 return true; | 191 return true; |
192 } | 192 } |
193 if (!this->allocPixelRef(bm, NULL)) { | 193 if (!this->allocPixelRef(bm, nullptr)) { |
194 return false; | 194 return false; |
195 } | 195 } |
196 } | 196 } |
197 | 197 |
198 //Create a format converter. | 198 //Create a format converter. |
199 SkTScopedComPtr<IWICFormatConverter> piFormatConverter; | 199 SkTScopedComPtr<IWICFormatConverter> piFormatConverter; |
200 if (SUCCEEDED(hr)) { | 200 if (SUCCEEDED(hr)) { |
201 hr = piImagingFactory->CreateFormatConverter(&piFormatConverter); | 201 hr = piImagingFactory->CreateFormatConverter(&piFormatConverter); |
202 } | 202 } |
203 | 203 |
204 GUID destinationPixelFormat; | 204 GUID destinationPixelFormat; |
205 if (this->getRequireUnpremultipliedColors()) { | 205 if (this->getRequireUnpremultipliedColors()) { |
206 destinationPixelFormat = GUID_WICPixelFormat32bppBGRA; | 206 destinationPixelFormat = GUID_WICPixelFormat32bppBGRA; |
207 } else { | 207 } else { |
208 destinationPixelFormat = GUID_WICPixelFormat32bppPBGRA; | 208 destinationPixelFormat = GUID_WICPixelFormat32bppPBGRA; |
209 } | 209 } |
210 | 210 |
211 if (SUCCEEDED(hr)) { | 211 if (SUCCEEDED(hr)) { |
212 hr = piFormatConverter->Initialize( | 212 hr = piFormatConverter->Initialize( |
213 piBitmapSourceOriginal.get() //Input bitmap to convert | 213 piBitmapSourceOriginal.get() //Input bitmap to convert |
214 , destinationPixelFormat //Destination pixel format | 214 , destinationPixelFormat //Destination pixel format |
215 , WICBitmapDitherTypeNone //Specified dither patterm | 215 , WICBitmapDitherTypeNone //Specified dither patterm |
216 , NULL //Specify a particular palette | 216 , nullptr //Specify a particular palette |
217 , 0.f //Alpha threshold | 217 , 0.f //Alpha threshold |
218 , WICBitmapPaletteTypeCustom //Palette translation type | 218 , WICBitmapPaletteTypeCustom //Palette translation type |
219 ); | 219 ); |
220 } | 220 } |
221 | 221 |
222 //Get the BitmapSource interface of the format converter. | 222 //Get the BitmapSource interface of the format converter. |
223 SkTScopedComPtr<IWICBitmapSource> piBitmapSourceConverted; | 223 SkTScopedComPtr<IWICBitmapSource> piBitmapSourceConverted; |
224 if (SUCCEEDED(hr)) { | 224 if (SUCCEEDED(hr)) { |
225 hr = piFormatConverter->QueryInterface( | 225 hr = piFormatConverter->QueryInterface( |
226 IID_PPV_ARGS(&piBitmapSourceConverted) | 226 IID_PPV_ARGS(&piBitmapSourceConverted) |
227 ); | 227 ); |
228 } | 228 } |
229 | 229 |
230 //Copy the pixels into the bitmap. | 230 //Copy the pixels into the bitmap. |
231 if (SUCCEEDED(hr)) { | 231 if (SUCCEEDED(hr)) { |
232 SkAutoLockPixels alp(*bm); | 232 SkAutoLockPixels alp(*bm); |
233 bm->eraseColor(SK_ColorTRANSPARENT); | 233 bm->eraseColor(SK_ColorTRANSPARENT); |
234 const UINT stride = (UINT) bm->rowBytes(); | 234 const UINT stride = (UINT) bm->rowBytes(); |
235 hr = piBitmapSourceConverted->CopyPixels( | 235 hr = piBitmapSourceConverted->CopyPixels( |
236 NULL, //Get all the pixels | 236 nullptr, //Get all the pixels |
237 stride, | 237 stride, |
238 stride * height, | 238 stride * height, |
239 reinterpret_cast<BYTE *>(bm->getPixels()) | 239 reinterpret_cast<BYTE *>(bm->getPixels()) |
240 ); | 240 ); |
241 | 241 |
242 // Note: we don't need to premultiply here since we specified PBGRA | 242 // Note: we don't need to premultiply here since we specified PBGRA |
243 if (SkBitmap::ComputeIsOpaque(*bm)) { | 243 if (SkBitmap::ComputeIsOpaque(*bm)) { |
244 bm->setAlphaType(kOpaque_SkAlphaType); | 244 bm->setAlphaType(kOpaque_SkAlphaType); |
245 } | 245 } |
246 } | 246 } |
247 | 247 |
248 return SUCCEEDED(hr); | 248 return SUCCEEDED(hr); |
249 } | 249 } |
250 | 250 |
251 ///////////////////////////////////////////////////////////////////////// | 251 ///////////////////////////////////////////////////////////////////////// |
252 | 252 |
253 extern SkImageDecoder* image_decoder_from_stream(SkStreamRewindable*); | 253 extern SkImageDecoder* image_decoder_from_stream(SkStreamRewindable*); |
254 | 254 |
255 SkImageDecoder* SkImageDecoder::Factory(SkStreamRewindable* stream) { | 255 SkImageDecoder* SkImageDecoder::Factory(SkStreamRewindable* stream) { |
256 SkImageDecoder* decoder = image_decoder_from_stream(stream); | 256 SkImageDecoder* decoder = image_decoder_from_stream(stream); |
257 if (NULL == decoder) { | 257 if (nullptr == decoder) { |
258 // If no image decoder specific to the stream exists, use SkImageDecoder
_WIC. | 258 // If no image decoder specific to the stream exists, use SkImageDecoder
_WIC. |
259 return new SkImageDecoder_WIC; | 259 return new SkImageDecoder_WIC; |
260 } else { | 260 } else { |
261 return decoder; | 261 return decoder; |
262 } | 262 } |
263 } | 263 } |
264 | 264 |
265 ///////////////////////////////////////////////////////////////////////// | 265 ///////////////////////////////////////////////////////////////////////// |
266 | 266 |
267 SkMovie* SkMovie::DecodeStream(SkStreamRewindable* stream) { | 267 SkMovie* SkMovie::DecodeStream(SkStreamRewindable* stream) { |
268 return NULL; | 268 return nullptr; |
269 } | 269 } |
270 | 270 |
271 ///////////////////////////////////////////////////////////////////////// | 271 ///////////////////////////////////////////////////////////////////////// |
272 | 272 |
273 class SkImageEncoder_WIC : public SkImageEncoder { | 273 class SkImageEncoder_WIC : public SkImageEncoder { |
274 public: | 274 public: |
275 SkImageEncoder_WIC(Type t) : fType(t) {} | 275 SkImageEncoder_WIC(Type t) : fType(t) {} |
276 | 276 |
277 protected: | 277 protected: |
278 virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality); | 278 virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 return false; | 338 return false; |
339 } | 339 } |
340 | 340 |
341 HRESULT hr = S_OK; | 341 HRESULT hr = S_OK; |
342 | 342 |
343 //Create Windows Imaging Component ImagingFactory. | 343 //Create Windows Imaging Component ImagingFactory. |
344 SkTScopedComPtr<IWICImagingFactory> piImagingFactory; | 344 SkTScopedComPtr<IWICImagingFactory> piImagingFactory; |
345 if (SUCCEEDED(hr)) { | 345 if (SUCCEEDED(hr)) { |
346 hr = CoCreateInstance( | 346 hr = CoCreateInstance( |
347 CLSID_WICImagingFactory | 347 CLSID_WICImagingFactory |
348 , NULL | 348 , nullptr |
349 , CLSCTX_INPROC_SERVER | 349 , CLSCTX_INPROC_SERVER |
350 , IID_PPV_ARGS(&piImagingFactory) | 350 , IID_PPV_ARGS(&piImagingFactory) |
351 ); | 351 ); |
352 } | 352 } |
353 | 353 |
354 //Convert the SkWStream to an IStream. | 354 //Convert the SkWStream to an IStream. |
355 SkTScopedComPtr<IStream> piStream; | 355 SkTScopedComPtr<IStream> piStream; |
356 if (SUCCEEDED(hr)) { | 356 if (SUCCEEDED(hr)) { |
357 hr = SkWIStream::CreateFromSkWStream(stream, &piStream); | 357 hr = SkWIStream::CreateFromSkWStream(stream, &piStream); |
358 } | 358 } |
359 | 359 |
360 //Create an encode of the appropriate type. | 360 //Create an encode of the appropriate type. |
361 SkTScopedComPtr<IWICBitmapEncoder> piEncoder; | 361 SkTScopedComPtr<IWICBitmapEncoder> piEncoder; |
362 if (SUCCEEDED(hr)) { | 362 if (SUCCEEDED(hr)) { |
363 hr = piImagingFactory->CreateEncoder(type, NULL, &piEncoder); | 363 hr = piImagingFactory->CreateEncoder(type, nullptr, &piEncoder); |
364 } | 364 } |
365 | 365 |
366 if (SUCCEEDED(hr)) { | 366 if (SUCCEEDED(hr)) { |
367 hr = piEncoder->Initialize(piStream.get(), WICBitmapEncoderNoCache); | 367 hr = piEncoder->Initialize(piStream.get(), WICBitmapEncoderNoCache); |
368 } | 368 } |
369 | 369 |
370 //Create a the frame. | 370 //Create a the frame. |
371 SkTScopedComPtr<IWICBitmapFrameEncode> piBitmapFrameEncode; | 371 SkTScopedComPtr<IWICBitmapFrameEncode> piBitmapFrameEncode; |
372 SkTScopedComPtr<IPropertyBag2> piPropertybag; | 372 SkTScopedComPtr<IPropertyBag2> piPropertybag; |
373 if (SUCCEEDED(hr)) { | 373 if (SUCCEEDED(hr)) { |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 /////////////////////////////////////////////////////////////////////////////// | 438 /////////////////////////////////////////////////////////////////////////////// |
439 | 439 |
440 static SkImageEncoder* sk_imageencoder_wic_factory(SkImageEncoder::Type t) { | 440 static SkImageEncoder* sk_imageencoder_wic_factory(SkImageEncoder::Type t) { |
441 switch (t) { | 441 switch (t) { |
442 case SkImageEncoder::kBMP_Type: | 442 case SkImageEncoder::kBMP_Type: |
443 case SkImageEncoder::kICO_Type: | 443 case SkImageEncoder::kICO_Type: |
444 case SkImageEncoder::kJPEG_Type: | 444 case SkImageEncoder::kJPEG_Type: |
445 case SkImageEncoder::kPNG_Type: | 445 case SkImageEncoder::kPNG_Type: |
446 break; | 446 break; |
447 default: | 447 default: |
448 return NULL; | 448 return nullptr; |
449 } | 449 } |
450 return new SkImageEncoder_WIC(t); | 450 return new SkImageEncoder_WIC(t); |
451 } | 451 } |
452 | 452 |
453 static SkImageEncoder_EncodeReg gEReg(sk_imageencoder_wic_factory); | 453 static SkImageEncoder_EncodeReg gEReg(sk_imageencoder_wic_factory); |
454 | 454 |
455 static SkImageDecoder::Format get_format_wic(SkStreamRewindable* stream) { | 455 static SkImageDecoder::Format get_format_wic(SkStreamRewindable* stream) { |
456 SkImageDecoder::Format format; | 456 SkImageDecoder::Format format; |
457 SkImageDecoder_WIC codec; | 457 SkImageDecoder_WIC codec; |
458 if (!codec.decodeStream(stream, NULL, SkImageDecoder_WIC::kDecodeFormat_WICM
ode, &format)) { | 458 if (!codec.decodeStream(stream, nullptr, SkImageDecoder_WIC::kDecodeFormat_W
ICMode, &format)) { |
459 format = SkImageDecoder::kUnknown_Format; | 459 format = SkImageDecoder::kUnknown_Format; |
460 } | 460 } |
461 return format; | 461 return format; |
462 } | 462 } |
463 | 463 |
464 static SkImageDecoder_FormatReg gFormatReg(get_format_wic); | 464 static SkImageDecoder_FormatReg gFormatReg(get_format_wic); |
OLD | NEW |