| 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 //Convert to 8888 if needed. | 86 // First convert to BGRA if necessary. |
| 87 const SkBitmap* bitmap; | 87 SkBitmap bitmap; |
| 88 SkBitmap bitmapCopy; | 88 if (!bitmapOrig.copyTo(&bitmap, kBGRA_8888_SkColorType)) { |
| 89 if (kN32_SkColorType == bitmapOrig.colorType() && bitmapOrig.isOpaque()) { | 89 return false; |
| 90 bitmap = &bitmapOrig; | |
| 91 } else { | |
| 92 if (!bitmapOrig.copyTo(&bitmapCopy, kN32_SkColorType)) { | |
| 93 return false; | |
| 94 } | |
| 95 bitmap = &bitmapCopy; | |
| 96 } | 90 } |
| 97 | 91 |
| 98 // We cannot use PBGRA so we need to unpremultiply ourselves | 92 // WIC expects unpremultiplied pixels. Unpremultiply if necessary. |
| 99 if (!bitmap->isOpaque()) { | 93 if (kPremul_SkAlphaType == bitmap.alphaType()) { |
| 100 SkAutoLockPixels alp(*bitmap); | 94 uint8_t* pixels = reinterpret_cast<uint8_t*>(bitmap.getPixels()); |
| 101 | 95 for (int y = 0; y < bitmap.height(); ++y) { |
| 102 uint8_t* pixels = reinterpret_cast<uint8_t*>(bitmap->getPixels()); | 96 for (int x = 0; x < bitmap.width(); ++x) { |
| 103 for (int y = 0; y < bitmap->height(); ++y) { | 97 uint8_t* bytes = pixels + y * bitmap.rowBytes() + x * bitmap.byt
esPerPixel(); |
| 104 for (int x = 0; x < bitmap->width(); ++x) { | |
| 105 uint8_t* bytes = pixels + y * bitmap->rowBytes() + x * bitmap->b
ytesPerPixel(); | |
| 106 | |
| 107 SkPMColor* src = reinterpret_cast<SkPMColor*>(bytes); | 98 SkPMColor* src = reinterpret_cast<SkPMColor*>(bytes); |
| 108 SkColor* dst = reinterpret_cast<SkColor*>(bytes); | 99 SkColor* dst = reinterpret_cast<SkColor*>(bytes); |
| 109 | |
| 110 *dst = SkUnPreMultiply::PMColorToColor(*src); | 100 *dst = SkUnPreMultiply::PMColorToColor(*src); |
| 111 } | 101 } |
| 112 } | 102 } |
| 113 } | 103 } |
| 114 | 104 |
| 105 // Finally, if we are performing a jpeg encode, we must convert to BGR. |
| 106 void* pixels = bitmap.getPixels(); |
| 107 size_t rowBytes = bitmap.rowBytes(); |
| 108 SkAutoMalloc pixelStorage; |
| 109 WICPixelFormatGUID formatDesired = GUID_WICPixelFormat32bppBGRA; |
| 110 if (kJPEG_Type == fType) { |
| 111 formatDesired = GUID_WICPixelFormat24bppBGR; |
| 112 rowBytes = SkAlign4(bitmap.width() * 3); |
| 113 pixelStorage.reset(rowBytes * bitmap.height()); |
| 114 for (int y = 0; y < bitmap.height(); y++) { |
| 115 uint8_t* dstRow = SkTAddOffset<uint8_t>(pixelStorage.get(), y * rowB
ytes); |
| 116 for (int x = 0; x < bitmap.width(); x++) { |
| 117 uint32_t bgra = *bitmap.getAddr32(x, y); |
| 118 dstRow[0] = (uint8_t) (bgra >> 0); |
| 119 dstRow[1] = (uint8_t) (bgra >> 8); |
| 120 dstRow[2] = (uint8_t) (bgra >> 16); |
| 121 dstRow += 3; |
| 122 } |
| 123 } |
| 124 |
| 125 pixels = pixelStorage.get(); |
| 126 } |
| 127 |
| 128 |
| 115 //Initialize COM. | 129 //Initialize COM. |
| 116 SkAutoCoInitialize scopedCo; | 130 SkAutoCoInitialize scopedCo; |
| 117 if (!scopedCo.succeeded()) { | 131 if (!scopedCo.succeeded()) { |
| 118 return false; | 132 return false; |
| 119 } | 133 } |
| 120 | 134 |
| 121 HRESULT hr = S_OK; | 135 HRESULT hr = S_OK; |
| 122 | 136 |
| 123 //Create Windows Imaging Component ImagingFactory. | 137 //Create Windows Imaging Component ImagingFactory. |
| 124 SkTScopedComPtr<IWICImagingFactory> piImagingFactory; | 138 SkTScopedComPtr<IWICImagingFactory> piImagingFactory; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 // This returns E_FAIL if the named property is not in the bag. | 183 // This returns E_FAIL if the named property is not in the bag. |
| 170 //TODO(bungeman) enumerate the properties, | 184 //TODO(bungeman) enumerate the properties, |
| 171 // write and set hr iff property exists. | 185 // write and set hr iff property exists. |
| 172 piPropertybag->Write(1, &name, &value); | 186 piPropertybag->Write(1, &name, &value); |
| 173 } | 187 } |
| 174 if (SUCCEEDED(hr)) { | 188 if (SUCCEEDED(hr)) { |
| 175 hr = piBitmapFrameEncode->Initialize(piPropertybag.get()); | 189 hr = piBitmapFrameEncode->Initialize(piPropertybag.get()); |
| 176 } | 190 } |
| 177 | 191 |
| 178 //Set the size of the frame. | 192 //Set the size of the frame. |
| 179 const UINT width = bitmap->width(); | 193 const UINT width = bitmap.width(); |
| 180 const UINT height = bitmap->height(); | 194 const UINT height = bitmap.height(); |
| 181 if (SUCCEEDED(hr)) { | 195 if (SUCCEEDED(hr)) { |
| 182 hr = piBitmapFrameEncode->SetSize(width, height); | 196 hr = piBitmapFrameEncode->SetSize(width, height); |
| 183 } | 197 } |
| 184 | 198 |
| 185 //Set the pixel format of the frame. If native encoded format cannot match
BGRA, | 199 //Set the pixel format of the frame. If native encoded format cannot match
BGRA, |
| 186 //it will choose the closest pixel format that it supports. | 200 //it will choose the closest pixel format that it supports. |
| 187 const WICPixelFormatGUID formatDesired = GUID_WICPixelFormat32bppBGRA; | |
| 188 WICPixelFormatGUID formatGUID = formatDesired; | 201 WICPixelFormatGUID formatGUID = formatDesired; |
| 189 if (SUCCEEDED(hr)) { | 202 if (SUCCEEDED(hr)) { |
| 190 hr = piBitmapFrameEncode->SetPixelFormat(&formatGUID); | 203 hr = piBitmapFrameEncode->SetPixelFormat(&formatGUID); |
| 191 } | 204 } |
| 192 if (SUCCEEDED(hr)) { | 205 if (SUCCEEDED(hr)) { |
| 193 //Be sure the image format is the one requested. | 206 //Be sure the image format is the one requested. |
| 194 hr = IsEqualGUID(formatGUID, formatDesired) ? S_OK : E_FAIL; | 207 hr = IsEqualGUID(formatGUID, formatDesired) ? S_OK : E_FAIL; |
| 195 } | 208 } |
| 196 | 209 |
| 197 //Write the pixels into the frame. | 210 //Write the pixels into the frame. |
| 198 if (SUCCEEDED(hr)) { | 211 if (SUCCEEDED(hr)) { |
| 199 SkAutoLockPixels alp(*bitmap); | 212 hr = piBitmapFrameEncode->WritePixels(height, |
| 200 const UINT stride = (UINT) bitmap->rowBytes(); | 213 (UINT) rowBytes, |
| 201 hr = piBitmapFrameEncode->WritePixels( | 214 (UINT) rowBytes * height, |
| 202 height | 215 reinterpret_cast<BYTE*>(pixels)); |
| 203 , stride | |
| 204 , stride * height | |
| 205 , reinterpret_cast<BYTE*>(bitmap->getPixels())); | |
| 206 } | 216 } |
| 207 | 217 |
| 208 if (SUCCEEDED(hr)) { | 218 if (SUCCEEDED(hr)) { |
| 209 hr = piBitmapFrameEncode->Commit(); | 219 hr = piBitmapFrameEncode->Commit(); |
| 210 } | 220 } |
| 211 | 221 |
| 212 if (SUCCEEDED(hr)) { | 222 if (SUCCEEDED(hr)) { |
| 213 hr = piEncoder->Commit(); | 223 hr = piEncoder->Commit(); |
| 214 } | 224 } |
| 215 | 225 |
| 216 return SUCCEEDED(hr); | 226 return SUCCEEDED(hr); |
| 217 } | 227 } |
| 218 | 228 |
| 219 /////////////////////////////////////////////////////////////////////////////// | 229 /////////////////////////////////////////////////////////////////////////////// |
| 220 | 230 |
| 221 static SkImageEncoder* sk_imageencoder_wic_factory(SkImageEncoder::Type t) { | 231 static SkImageEncoder* sk_imageencoder_wic_factory(SkImageEncoder::Type t) { |
| 222 switch (t) { | 232 switch (t) { |
| 223 case SkImageEncoder::kBMP_Type: | 233 case SkImageEncoder::kBMP_Type: |
| 224 case SkImageEncoder::kICO_Type: | 234 case SkImageEncoder::kICO_Type: |
| 225 case SkImageEncoder::kPNG_Type: | 235 case SkImageEncoder::kPNG_Type: |
| 236 case SkImageEncoder::kJPEG_Type: |
| 226 break; | 237 break; |
| 227 default: | 238 default: |
| 228 return nullptr; | 239 return nullptr; |
| 229 } | 240 } |
| 230 return new SkImageEncoder_WIC(t); | 241 return new SkImageEncoder_WIC(t); |
| 231 } | 242 } |
| 232 | 243 |
| 233 static SkImageEncoder_EncodeReg gEReg(sk_imageencoder_wic_factory); | 244 static SkImageEncoder_EncodeReg gEReg(sk_imageencoder_wic_factory); |
| 234 | 245 |
| 235 DEFINE_ENCODER_CREATOR(ImageEncoder_WIC); | 246 DEFINE_ENCODER_CREATOR(ImageEncoder_WIC); |
| 236 | 247 |
| 237 #endif // defined(SK_BUILD_FOR_WIN32) | 248 #endif // defined(SK_BUILD_FOR_WIN32) |
| OLD | NEW |