OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkTypes.h" | 8 #include "SkTypes.h" |
9 | 9 |
10 #if defined(SK_BUILD_FOR_WIN32) | 10 #if defined(SK_BUILD_FOR_WIN32) |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 case kJPEG_Type: | 76 case kJPEG_Type: |
77 type = GUID_ContainerFormatJpeg; | 77 type = GUID_ContainerFormatJpeg; |
78 break; | 78 break; |
79 case kPNG_Type: | 79 case kPNG_Type: |
80 type = GUID_ContainerFormatPng; | 80 type = GUID_ContainerFormatPng; |
81 break; | 81 break; |
82 default: | 82 default: |
83 return false; | 83 return false; |
84 } | 84 } |
85 | 85 |
86 // First convert to BGRA if necessary. | 86 //Convert to 8888 if needed. |
87 const SkBitmap* bitmap; | 87 const SkBitmap* bitmap; |
88 SkBitmap bitmapCopy; | 88 SkBitmap bitmapCopy; |
89 if (kBGRA_8888_SkColorType == bitmapOrig.colorType()) { | 89 if (kN32_SkColorType == bitmapOrig.colorType() && bitmapOrig.isOpaque()) { |
90 bitmap = &bitmapOrig; | 90 bitmap = &bitmapOrig; |
91 } else { | 91 } else { |
92 if (!bitmapOrig.copyTo(&bitmapCopy, kBGRA_8888_SkColorType)) { | 92 if (!bitmapOrig.copyTo(&bitmapCopy, kN32_SkColorType)) { |
93 return false; | 93 return false; |
94 } | 94 } |
95 bitmap = &bitmapCopy; | 95 bitmap = &bitmapCopy; |
96 } | 96 } |
97 | 97 |
98 // WIC expects unpremultiplied pixels. Unpremultiply if necessary. | 98 // We cannot use PBGRA so we need to unpremultiply ourselves |
99 if (kPremul_SkAlphaType == bitmap->alphaType()) { | 99 if (!bitmap->isOpaque()) { |
100 SkAutoLockPixels alp(*bitmap); | 100 SkAutoLockPixels alp(*bitmap); |
| 101 |
101 uint8_t* pixels = reinterpret_cast<uint8_t*>(bitmap->getPixels()); | 102 uint8_t* pixels = reinterpret_cast<uint8_t*>(bitmap->getPixels()); |
102 for (int y = 0; y < bitmap->height(); ++y) { | 103 for (int y = 0; y < bitmap->height(); ++y) { |
103 for (int x = 0; x < bitmap->width(); ++x) { | 104 for (int x = 0; x < bitmap->width(); ++x) { |
104 uint8_t* bytes = pixels + y * bitmap->rowBytes() + x * bitmap->b
ytesPerPixel(); | 105 uint8_t* bytes = pixels + y * bitmap->rowBytes() + x * bitmap->b
ytesPerPixel(); |
| 106 |
105 SkPMColor* src = reinterpret_cast<SkPMColor*>(bytes); | 107 SkPMColor* src = reinterpret_cast<SkPMColor*>(bytes); |
106 SkColor* dst = reinterpret_cast<SkColor*>(bytes); | 108 SkColor* dst = reinterpret_cast<SkColor*>(bytes); |
| 109 |
107 *dst = SkUnPreMultiply::PMColorToColor(*src); | 110 *dst = SkUnPreMultiply::PMColorToColor(*src); |
108 } | 111 } |
109 } | 112 } |
110 } | 113 } |
111 | 114 |
112 // Finally, if we are performing a jpeg encode, we must convert to BGR. | |
113 void* pixels = bitmap->getPixels(); | |
114 size_t rowBytes = bitmap->rowBytes(); | |
115 SkAutoMalloc pixelStorage; | |
116 WICPixelFormatGUID formatDesired = GUID_WICPixelFormat32bppBGRA; | |
117 if (kJPEG_Type == fType) { | |
118 formatDesired = GUID_WICPixelFormat24bppBGR; | |
119 rowBytes = SkAlign4(bitmap->width() * 3); | |
120 pixelStorage.reset(rowBytes * bitmap->height()); | |
121 for (int y = 0; y < bitmap->height(); y++) { | |
122 uint8_t* dstRow = SkTAddOffset<uint8_t>(pixelStorage.get(), y * rowB
ytes); | |
123 for (int x = 0; x < bitmap->width(); x++) { | |
124 uint32_t bgra = *bitmap->getAddr32(x, y); | |
125 dstRow[0] = (uint8_t) (bgra >> 0); | |
126 dstRow[1] = (uint8_t) (bgra >> 8); | |
127 dstRow[2] = (uint8_t) (bgra >> 16); | |
128 dstRow += 3; | |
129 } | |
130 } | |
131 | |
132 pixels = pixelStorage.get(); | |
133 } | |
134 | |
135 | |
136 //Initialize COM. | 115 //Initialize COM. |
137 SkAutoCoInitialize scopedCo; | 116 SkAutoCoInitialize scopedCo; |
138 if (!scopedCo.succeeded()) { | 117 if (!scopedCo.succeeded()) { |
139 return false; | 118 return false; |
140 } | 119 } |
141 | 120 |
142 HRESULT hr = S_OK; | 121 HRESULT hr = S_OK; |
143 | 122 |
144 //Create Windows Imaging Component ImagingFactory. | 123 //Create Windows Imaging Component ImagingFactory. |
145 SkTScopedComPtr<IWICImagingFactory> piImagingFactory; | 124 SkTScopedComPtr<IWICImagingFactory> piImagingFactory; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 | 177 |
199 //Set the size of the frame. | 178 //Set the size of the frame. |
200 const UINT width = bitmap->width(); | 179 const UINT width = bitmap->width(); |
201 const UINT height = bitmap->height(); | 180 const UINT height = bitmap->height(); |
202 if (SUCCEEDED(hr)) { | 181 if (SUCCEEDED(hr)) { |
203 hr = piBitmapFrameEncode->SetSize(width, height); | 182 hr = piBitmapFrameEncode->SetSize(width, height); |
204 } | 183 } |
205 | 184 |
206 //Set the pixel format of the frame. If native encoded format cannot match
BGRA, | 185 //Set the pixel format of the frame. If native encoded format cannot match
BGRA, |
207 //it will choose the closest pixel format that it supports. | 186 //it will choose the closest pixel format that it supports. |
| 187 const WICPixelFormatGUID formatDesired = GUID_WICPixelFormat32bppBGRA; |
208 WICPixelFormatGUID formatGUID = formatDesired; | 188 WICPixelFormatGUID formatGUID = formatDesired; |
209 if (SUCCEEDED(hr)) { | 189 if (SUCCEEDED(hr)) { |
210 hr = piBitmapFrameEncode->SetPixelFormat(&formatGUID); | 190 hr = piBitmapFrameEncode->SetPixelFormat(&formatGUID); |
211 } | 191 } |
212 if (SUCCEEDED(hr)) { | 192 if (SUCCEEDED(hr)) { |
213 //Be sure the image format is the one requested. | 193 //Be sure the image format is the one requested. |
214 hr = IsEqualGUID(formatGUID, formatDesired) ? S_OK : E_FAIL; | 194 hr = IsEqualGUID(formatGUID, formatDesired) ? S_OK : E_FAIL; |
215 } | 195 } |
216 | 196 |
217 //Write the pixels into the frame. | 197 //Write the pixels into the frame. |
218 if (SUCCEEDED(hr)) { | 198 if (SUCCEEDED(hr)) { |
219 SkAutoLockPixels alp(*bitmap); | 199 SkAutoLockPixels alp(*bitmap); |
220 hr = piBitmapFrameEncode->WritePixels(height, | 200 const UINT stride = (UINT) bitmap->rowBytes(); |
221 (UINT) rowBytes, | 201 hr = piBitmapFrameEncode->WritePixels( |
222 (UINT) rowBytes * height, | 202 height |
223 reinterpret_cast<BYTE*>(pixels)); | 203 , stride |
| 204 , stride * height |
| 205 , reinterpret_cast<BYTE*>(bitmap->getPixels())); |
224 } | 206 } |
225 | 207 |
226 if (SUCCEEDED(hr)) { | 208 if (SUCCEEDED(hr)) { |
227 hr = piBitmapFrameEncode->Commit(); | 209 hr = piBitmapFrameEncode->Commit(); |
228 } | 210 } |
229 | 211 |
230 if (SUCCEEDED(hr)) { | 212 if (SUCCEEDED(hr)) { |
231 hr = piEncoder->Commit(); | 213 hr = piEncoder->Commit(); |
232 } | 214 } |
233 | 215 |
234 return SUCCEEDED(hr); | 216 return SUCCEEDED(hr); |
235 } | 217 } |
236 | 218 |
237 /////////////////////////////////////////////////////////////////////////////// | 219 /////////////////////////////////////////////////////////////////////////////// |
238 | 220 |
239 static SkImageEncoder* sk_imageencoder_wic_factory(SkImageEncoder::Type t) { | 221 static SkImageEncoder* sk_imageencoder_wic_factory(SkImageEncoder::Type t) { |
240 switch (t) { | 222 switch (t) { |
241 case SkImageEncoder::kBMP_Type: | 223 case SkImageEncoder::kBMP_Type: |
242 case SkImageEncoder::kICO_Type: | 224 case SkImageEncoder::kICO_Type: |
243 case SkImageEncoder::kPNG_Type: | 225 case SkImageEncoder::kPNG_Type: |
244 case SkImageEncoder::kJPEG_Type: | |
245 break; | 226 break; |
246 default: | 227 default: |
247 return nullptr; | 228 return nullptr; |
248 } | 229 } |
249 return new SkImageEncoder_WIC(t); | 230 return new SkImageEncoder_WIC(t); |
250 } | 231 } |
251 | 232 |
252 static SkImageEncoder_EncodeReg gEReg(sk_imageencoder_wic_factory); | 233 static SkImageEncoder_EncodeReg gEReg(sk_imageencoder_wic_factory); |
253 | 234 |
254 DEFINE_ENCODER_CREATOR(ImageEncoder_WIC); | 235 DEFINE_ENCODER_CREATOR(ImageEncoder_WIC); |
255 | 236 |
256 #endif // defined(SK_BUILD_FOR_WIN32) | 237 #endif // defined(SK_BUILD_FOR_WIN32) |
OLD | NEW |