bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
bungeman@google.com | 8530296 | 2013-10-11 20:04:08 +0000 | [diff] [blame] | 8 | #include "SkTypes.h" |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
mtklein | 1ee7651 | 2015-11-02 10:20:27 -0800 | [diff] [blame] | 10 | #if defined(SK_BUILD_FOR_WIN32) |
| 11 | |
bungeman@google.com | 8530296 | 2013-10-11 20:04:08 +0000 | [diff] [blame] | 12 | // Workaround for: |
| 13 | // http://connect.microsoft.com/VisualStudio/feedback/details/621653/ |
| 14 | // http://crbug.com/225822 |
| 15 | // In VS2010 both intsafe.h and stdint.h define the following without guards. |
| 16 | // SkTypes brought in windows.h and stdint.h and the following defines are |
| 17 | // not used by this file. However, they may be re-introduced by wincodec.h. |
| 18 | #undef INT8_MIN |
| 19 | #undef INT16_MIN |
| 20 | #undef INT32_MIN |
| 21 | #undef INT64_MIN |
| 22 | #undef INT8_MAX |
| 23 | #undef UINT8_MAX |
| 24 | #undef INT16_MAX |
| 25 | #undef UINT16_MAX |
| 26 | #undef INT32_MAX |
| 27 | #undef UINT32_MAX |
| 28 | #undef INT64_MAX |
| 29 | #undef UINT64_MAX |
| 30 | |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 31 | #include "SkAutoCoInitialize.h" |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 32 | #include "SkAutoMalloc.h" |
msarett | d442d32 | 2016-03-25 11:08:14 -0700 | [diff] [blame] | 33 | #include "SkBitmap.h" |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 34 | #include "SkImageEncoderPriv.h" |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 35 | #include "SkIStream.h" |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 36 | #include "SkImageEncoder.h" |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 37 | #include "SkStream.h" |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 38 | #include "SkTScopedComPtr.h" |
Ben Wagner | 4d1955c | 2017-03-10 13:08:15 -0500 | [diff] [blame] | 39 | #include "SkTemplates.h" |
robertphillips@google.com | 59bfb12 | 2012-11-08 15:06:52 +0000 | [diff] [blame] | 40 | #include "SkUnPreMultiply.h" |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 41 | #include <wincodec.h> |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 42 | |
bungeman@google.com | c18143e | 2013-01-11 20:02:32 +0000 | [diff] [blame] | 43 | //All Windows SDKs back to XPSP2 export the CLSID_WICImagingFactory symbol. |
| 44 | //In the Windows8 SDK the CLSID_WICImagingFactory symbol is still exported |
| 45 | //but CLSID_WICImagingFactory is then #defined to CLSID_WICImagingFactory2. |
| 46 | //Undo this #define if it has been done so that we link against the symbols |
| 47 | //we intended to link against on all SDKs. |
| 48 | #if defined(CLSID_WICImagingFactory) |
| 49 | #undef CLSID_WICImagingFactory |
| 50 | #endif |
| 51 | |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 52 | bool SkEncodeImageWithWIC(SkWStream* stream, const SkPixmap& pixmap, |
| 53 | SkEncodedImageFormat format, int quality) { |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 54 | GUID type; |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 55 | switch (format) { |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 56 | case SkEncodedImageFormat::kJPEG: |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 57 | type = GUID_ContainerFormatJpeg; |
| 58 | break; |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 59 | case SkEncodedImageFormat::kPNG: |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 60 | type = GUID_ContainerFormatPng; |
| 61 | break; |
| 62 | default: |
| 63 | return false; |
| 64 | } |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 65 | SkBitmap bitmapOrig; |
| 66 | if (!bitmapOrig.installPixels(pixmap)) { |
| 67 | return false; |
| 68 | } |
| 69 | bitmapOrig.setImmutable(); |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 70 | |
msarett | 36c38cb | 2016-08-15 18:52:17 -0700 | [diff] [blame] | 71 | // First convert to BGRA if necessary. |
| 72 | SkBitmap bitmap; |
| 73 | if (!bitmapOrig.copyTo(&bitmap, kBGRA_8888_SkColorType)) { |
| 74 | return false; |
bungeman@google.com | 22b4950 | 2011-08-01 19:37:43 +0000 | [diff] [blame] | 75 | } |
| 76 | |
msarett | 36c38cb | 2016-08-15 18:52:17 -0700 | [diff] [blame] | 77 | // WIC expects unpremultiplied pixels. Unpremultiply if necessary. |
| 78 | if (kPremul_SkAlphaType == bitmap.alphaType()) { |
| 79 | uint8_t* pixels = reinterpret_cast<uint8_t*>(bitmap.getPixels()); |
| 80 | for (int y = 0; y < bitmap.height(); ++y) { |
| 81 | for (int x = 0; x < bitmap.width(); ++x) { |
| 82 | uint8_t* bytes = pixels + y * bitmap.rowBytes() + x * bitmap.bytesPerPixel(); |
robertphillips@google.com | 59bfb12 | 2012-11-08 15:06:52 +0000 | [diff] [blame] | 83 | SkPMColor* src = reinterpret_cast<SkPMColor*>(bytes); |
| 84 | SkColor* dst = reinterpret_cast<SkColor*>(bytes); |
robertphillips@google.com | 59bfb12 | 2012-11-08 15:06:52 +0000 | [diff] [blame] | 85 | *dst = SkUnPreMultiply::PMColorToColor(*src); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
msarett | 36c38cb | 2016-08-15 18:52:17 -0700 | [diff] [blame] | 90 | // Finally, if we are performing a jpeg encode, we must convert to BGR. |
| 91 | void* pixels = bitmap.getPixels(); |
| 92 | size_t rowBytes = bitmap.rowBytes(); |
| 93 | SkAutoMalloc pixelStorage; |
| 94 | WICPixelFormatGUID formatDesired = GUID_WICPixelFormat32bppBGRA; |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 95 | if (SkEncodedImageFormat::kJPEG == format) { |
msarett | 36c38cb | 2016-08-15 18:52:17 -0700 | [diff] [blame] | 96 | formatDesired = GUID_WICPixelFormat24bppBGR; |
| 97 | rowBytes = SkAlign4(bitmap.width() * 3); |
| 98 | pixelStorage.reset(rowBytes * bitmap.height()); |
| 99 | for (int y = 0; y < bitmap.height(); y++) { |
| 100 | uint8_t* dstRow = SkTAddOffset<uint8_t>(pixelStorage.get(), y * rowBytes); |
| 101 | for (int x = 0; x < bitmap.width(); x++) { |
| 102 | uint32_t bgra = *bitmap.getAddr32(x, y); |
| 103 | dstRow[0] = (uint8_t) (bgra >> 0); |
| 104 | dstRow[1] = (uint8_t) (bgra >> 8); |
| 105 | dstRow[2] = (uint8_t) (bgra >> 16); |
| 106 | dstRow += 3; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | pixels = pixelStorage.get(); |
| 111 | } |
| 112 | |
| 113 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 114 | //Initialize COM. |
bungeman@google.com | 2e2f3f5 | 2011-09-16 15:37:20 +0000 | [diff] [blame] | 115 | SkAutoCoInitialize scopedCo; |
| 116 | if (!scopedCo.succeeded()) { |
| 117 | return false; |
| 118 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 119 | |
bungeman@google.com | 2e2f3f5 | 2011-09-16 15:37:20 +0000 | [diff] [blame] | 120 | HRESULT hr = S_OK; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 121 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 122 | //Create Windows Imaging Component ImagingFactory. |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 123 | SkTScopedComPtr<IWICImagingFactory> piImagingFactory; |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 124 | if (SUCCEEDED(hr)) { |
| 125 | hr = CoCreateInstance( |
| 126 | CLSID_WICImagingFactory |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 127 | , nullptr |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 128 | , CLSCTX_INPROC_SERVER |
| 129 | , IID_PPV_ARGS(&piImagingFactory) |
| 130 | ); |
| 131 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 132 | |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 133 | //Convert the SkWStream to an IStream. |
| 134 | SkTScopedComPtr<IStream> piStream; |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 135 | if (SUCCEEDED(hr)) { |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 136 | hr = SkWIStream::CreateFromSkWStream(stream, &piStream); |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 137 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 138 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 139 | //Create an encode of the appropriate type. |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 140 | SkTScopedComPtr<IWICBitmapEncoder> piEncoder; |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 141 | if (SUCCEEDED(hr)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 142 | hr = piImagingFactory->CreateEncoder(type, nullptr, &piEncoder); |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 143 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 144 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 145 | if (SUCCEEDED(hr)) { |
| 146 | hr = piEncoder->Initialize(piStream.get(), WICBitmapEncoderNoCache); |
| 147 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 148 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 149 | //Create a the frame. |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 150 | SkTScopedComPtr<IWICBitmapFrameEncode> piBitmapFrameEncode; |
| 151 | SkTScopedComPtr<IPropertyBag2> piPropertybag; |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 152 | if (SUCCEEDED(hr)) { |
| 153 | hr = piEncoder->CreateNewFrame(&piBitmapFrameEncode, &piPropertybag); |
| 154 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 155 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 156 | if (SUCCEEDED(hr)) { |
| 157 | PROPBAG2 name = { 0 }; |
| 158 | name.dwType = PROPBAG2_TYPE_DATA; |
| 159 | name.vt = VT_R4; |
| 160 | name.pstrName = L"ImageQuality"; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 161 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 162 | VARIANT value; |
| 163 | VariantInit(&value); |
| 164 | value.vt = VT_R4; |
| 165 | value.fltVal = (FLOAT)(quality / 100.0); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 166 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 167 | //Ignore result code. |
| 168 | // This returns E_FAIL if the named property is not in the bag. |
| 169 | //TODO(bungeman) enumerate the properties, |
| 170 | // write and set hr iff property exists. |
| 171 | piPropertybag->Write(1, &name, &value); |
| 172 | } |
| 173 | if (SUCCEEDED(hr)) { |
| 174 | hr = piBitmapFrameEncode->Initialize(piPropertybag.get()); |
| 175 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 176 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 177 | //Set the size of the frame. |
msarett | 36c38cb | 2016-08-15 18:52:17 -0700 | [diff] [blame] | 178 | const UINT width = bitmap.width(); |
| 179 | const UINT height = bitmap.height(); |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 180 | if (SUCCEEDED(hr)) { |
| 181 | hr = piBitmapFrameEncode->SetSize(width, height); |
| 182 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 183 | |
msarett | e820dfe | 2016-03-18 12:13:47 -0700 | [diff] [blame] | 184 | //Set the pixel format of the frame. If native encoded format cannot match BGRA, |
| 185 | //it will choose the closest pixel format that it supports. |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 186 | WICPixelFormatGUID formatGUID = formatDesired; |
| 187 | if (SUCCEEDED(hr)) { |
| 188 | hr = piBitmapFrameEncode->SetPixelFormat(&formatGUID); |
| 189 | } |
msarett | 60316a9 | 2016-03-21 13:35:43 -0700 | [diff] [blame] | 190 | if (SUCCEEDED(hr)) { |
| 191 | //Be sure the image format is the one requested. |
| 192 | hr = IsEqualGUID(formatGUID, formatDesired) ? S_OK : E_FAIL; |
| 193 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 194 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 195 | //Write the pixels into the frame. |
| 196 | if (SUCCEEDED(hr)) { |
msarett | 36c38cb | 2016-08-15 18:52:17 -0700 | [diff] [blame] | 197 | hr = piBitmapFrameEncode->WritePixels(height, |
| 198 | (UINT) rowBytes, |
| 199 | (UINT) rowBytes * height, |
| 200 | reinterpret_cast<BYTE*>(pixels)); |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 201 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 202 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 203 | if (SUCCEEDED(hr)) { |
| 204 | hr = piBitmapFrameEncode->Commit(); |
| 205 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 206 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 207 | if (SUCCEEDED(hr)) { |
| 208 | hr = piEncoder->Commit(); |
| 209 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 210 | |
bungeman@google.com | 242bb89 | 2011-06-22 20:42:34 +0000 | [diff] [blame] | 211 | return SUCCEEDED(hr); |
| 212 | } |
| 213 | |
mtklein | 1ee7651 | 2015-11-02 10:20:27 -0800 | [diff] [blame] | 214 | #endif // defined(SK_BUILD_FOR_WIN32) |