commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 17 | #include "SkImageEncoderPriv.h" |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 18 | |
| 19 | #ifdef SK_HAS_WEBP_LIBRARY |
| 20 | |
| 21 | #include "SkBitmap.h" |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 22 | #include "SkColorPriv.h" |
Matt Sarett | 5df93de | 2017-03-22 21:52:47 +0000 | [diff] [blame] | 23 | #include "SkImageEncoderFns.h" |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 24 | #include "SkStream.h" |
| 25 | #include "SkTemplates.h" |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 26 | #include "SkUnPreMultiply.h" |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 27 | #include "SkUtils.h" |
Matt Sarett | 04c3731 | 2017-05-05 14:02:13 -0400 | [diff] [blame] | 28 | #include "SkWebpEncoder.h" |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 29 | |
Matt Sarett | 46a45ba | 2017-04-06 20:34:38 +0000 | [diff] [blame] | 30 | // A WebP encoder only, on top of (subset of) libwebp |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 31 | // For more information on WebP image format, and libwebp library, see: |
| 32 | // http://code.google.com/speed/webp/ |
| 33 | // http://www.webmproject.org/code/#libwebp_webp_image_decoder_library |
| 34 | // http://review.webmproject.org/gitweb?p=libwebp.git |
| 35 | |
| 36 | #include <stdio.h> |
| 37 | extern "C" { |
| 38 | // If moving libwebp out of skia source tree, path for webp headers must be |
| 39 | // updated accordingly. Here, we enforce using local copy in webp sub-directory. |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 40 | #include "webp/encode.h" |
Matt Sarett | 46a45ba | 2017-04-06 20:34:38 +0000 | [diff] [blame] | 41 | #include "webp/mux.h" |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Matt Sarett | 7abfb5e | 2017-04-05 17:36:04 -0400 | [diff] [blame] | 44 | static transform_scanline_proc choose_proc(const SkImageInfo& info, |
| 45 | SkTransferFunctionBehavior unpremulBehavior) { |
| 46 | const bool isSRGBTransferFn = |
| 47 | (SkTransferFunctionBehavior::kRespect == unpremulBehavior) && info.gammaCloseToSRGB(); |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 48 | switch (info.colorType()) { |
| 49 | case kRGBA_8888_SkColorType: |
| 50 | switch (info.alphaType()) { |
| 51 | case kOpaque_SkAlphaType: |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 52 | return transform_scanline_RGBX; |
| 53 | case kUnpremul_SkAlphaType: |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 54 | return transform_scanline_memcpy; |
| 55 | case kPremul_SkAlphaType: |
Matt Sarett | 7abfb5e | 2017-04-05 17:36:04 -0400 | [diff] [blame] | 56 | return isSRGBTransferFn ? transform_scanline_srgbA : |
| 57 | transform_scanline_rgbA; |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 58 | default: |
| 59 | return nullptr; |
commit-bot@chromium.org | 5007aab | 2014-02-26 21:35:17 +0000 | [diff] [blame] | 60 | } |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 61 | case kBGRA_8888_SkColorType: |
| 62 | switch (info.alphaType()) { |
| 63 | case kOpaque_SkAlphaType: |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 64 | return transform_scanline_BGRX; |
| 65 | case kUnpremul_SkAlphaType: |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 66 | return transform_scanline_BGRA; |
| 67 | case kPremul_SkAlphaType: |
Matt Sarett | 7abfb5e | 2017-04-05 17:36:04 -0400 | [diff] [blame] | 68 | return isSRGBTransferFn ? transform_scanline_sbgrA : |
| 69 | transform_scanline_bgrA; |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 70 | default: |
| 71 | return nullptr; |
commit-bot@chromium.org | 5007aab | 2014-02-26 21:35:17 +0000 | [diff] [blame] | 72 | } |
reed | 0689d7b | 2014-06-14 05:30:20 -0700 | [diff] [blame] | 73 | case kRGB_565_SkColorType: |
Matt Sarett | 5521356 | 2017-01-23 19:37:37 -0500 | [diff] [blame] | 74 | if (!info.isOpaque()) { |
| 75 | return nullptr; |
| 76 | } |
| 77 | |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 78 | return transform_scanline_565; |
| 79 | case kARGB_4444_SkColorType: |
| 80 | switch (info.alphaType()) { |
| 81 | case kOpaque_SkAlphaType: |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 82 | return transform_scanline_444; |
| 83 | case kPremul_SkAlphaType: |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 84 | return transform_scanline_4444; |
| 85 | default: |
| 86 | return nullptr; |
| 87 | } |
reed | 0689d7b | 2014-06-14 05:30:20 -0700 | [diff] [blame] | 88 | case kIndex_8_SkColorType: |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 89 | switch (info.alphaType()) { |
| 90 | case kOpaque_SkAlphaType: |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 91 | return transform_scanline_index8_opaque; |
| 92 | case kUnpremul_SkAlphaType: |
| 93 | case kPremul_SkAlphaType: |
| 94 | // If the color table is premultiplied, we'll fix it before calling the |
| 95 | // scanline proc. |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 96 | return transform_scanline_index8_unpremul; |
| 97 | default: |
| 98 | return nullptr; |
| 99 | } |
| 100 | case kGray_8_SkColorType: |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 101 | return transform_scanline_gray; |
Matt Sarett | 5521356 | 2017-01-23 19:37:37 -0500 | [diff] [blame] | 102 | case kRGBA_F16_SkColorType: |
| 103 | if (!info.colorSpace() || !info.colorSpace()->gammaIsLinear()) { |
| 104 | return nullptr; |
| 105 | } |
| 106 | |
| 107 | switch (info.alphaType()) { |
| 108 | case kOpaque_SkAlphaType: |
| 109 | case kUnpremul_SkAlphaType: |
| 110 | return transform_scanline_F16_to_8888; |
| 111 | case kPremul_SkAlphaType: |
| 112 | return transform_scanline_F16_premul_to_8888; |
| 113 | default: |
| 114 | return nullptr; |
| 115 | } |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 116 | default: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 117 | return nullptr; |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | static int stream_writer(const uint8_t* data, size_t data_size, |
| 122 | const WebPPicture* const picture) { |
| 123 | SkWStream* const stream = (SkWStream*)picture->custom_ptr; |
| 124 | return stream->write(data, data_size) ? 1 : 0; |
| 125 | } |
| 126 | |
Matt Sarett | d5a1691 | 2017-05-16 17:06:52 -0400 | [diff] [blame] | 127 | bool SkWebpEncoder::Encode(SkWStream* stream, const SkPixmap& pixmap, const Options& opts) { |
Matt Sarett | 04c3731 | 2017-05-05 14:02:13 -0400 | [diff] [blame] | 128 | if (!SkPixmapIsValid(pixmap, opts.fUnpremulBehavior)) { |
| 129 | return false; |
Matt Sarett | 5521356 | 2017-01-23 19:37:37 -0500 | [diff] [blame] | 130 | } |
| 131 | |
Matt Sarett | 7abfb5e | 2017-04-05 17:36:04 -0400 | [diff] [blame] | 132 | const transform_scanline_proc proc = choose_proc(pixmap.info(), opts.fUnpremulBehavior); |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 133 | if (!proc) { |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 134 | return false; |
| 135 | } |
Matt Sarett | 5521356 | 2017-01-23 19:37:37 -0500 | [diff] [blame] | 136 | |
| 137 | int bpp; |
| 138 | if (kRGBA_F16_SkColorType == pixmap.colorType()) { |
| 139 | bpp = 4; |
| 140 | } else { |
| 141 | bpp = pixmap.isOpaque() ? 3 : 4; |
| 142 | } |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 143 | |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 144 | if (nullptr == pixmap.addr()) { |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 145 | return false; |
| 146 | } |
| 147 | |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 148 | const SkPMColor* colors = nullptr; |
| 149 | SkPMColor storage[256]; |
| 150 | if (kIndex_8_SkColorType == pixmap.colorType()) { |
| 151 | if (!pixmap.ctable()) { |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | colors = pixmap.ctable()->readColors(); |
| 156 | if (kPremul_SkAlphaType == pixmap.alphaType()) { |
Matt Sarett | 5521356 | 2017-01-23 19:37:37 -0500 | [diff] [blame] | 157 | // Unpremultiply the colors. |
| 158 | const SkImageInfo rgbaInfo = pixmap.info().makeColorType(kRGBA_8888_SkColorType); |
Matt Sarett | 7abfb5e | 2017-04-05 17:36:04 -0400 | [diff] [blame] | 159 | transform_scanline_proc proc = choose_proc(rgbaInfo, opts.fUnpremulBehavior); |
Matt Sarett | 5521356 | 2017-01-23 19:37:37 -0500 | [diff] [blame] | 160 | proc((char*) storage, (const char*) colors, pixmap.ctable()->count(), 4, nullptr); |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 161 | colors = storage; |
| 162 | } |
| 163 | } |
| 164 | |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 165 | WebPConfig webp_config; |
Matt Sarett | 04c3731 | 2017-05-05 14:02:13 -0400 | [diff] [blame] | 166 | if (!WebPConfigPreset(&webp_config, WEBP_PRESET_DEFAULT, opts.fQuality)) { |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 167 | return false; |
| 168 | } |
| 169 | |
| 170 | WebPPicture pic; |
| 171 | WebPPictureInit(&pic); |
Matt Sarett | 46a45ba | 2017-04-06 20:34:38 +0000 | [diff] [blame] | 172 | SkAutoTCallVProc<WebPPicture, WebPPictureFree> autoPic(&pic); |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 173 | pic.width = pixmap.width(); |
| 174 | pic.height = pixmap.height(); |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 175 | pic.writer = stream_writer; |
Matt Sarett | 46a45ba | 2017-04-06 20:34:38 +0000 | [diff] [blame] | 176 | |
Matt Sarett | 2f68787 | 2017-05-19 19:12:54 -0400 | [diff] [blame] | 177 | // Set compression, method, and pixel format. |
| 178 | // libwebp recommends using BGRA for lossless and YUV for lossy. |
| 179 | // The choices of |webp_config.method| currently just match Chrome's defaults. We |
| 180 | // could potentially expose this decision to the client. |
| 181 | if (Compression::kLossy == opts.fCompression) { |
| 182 | webp_config.lossless = 0; |
| 183 | #ifndef SK_WEBP_ENCODER_USE_DEFAULT_METHOD |
| 184 | webp_config.method = 3; |
| 185 | #endif |
| 186 | pic.use_argb = 0; |
| 187 | } else { |
| 188 | webp_config.lossless = 1; |
| 189 | webp_config.method = 0; |
| 190 | pic.use_argb = 1; |
| 191 | } |
| 192 | |
Matt Sarett | 46a45ba | 2017-04-06 20:34:38 +0000 | [diff] [blame] | 193 | // If there is no need to embed an ICC profile, we write directly to the input stream. |
| 194 | // Otherwise, we will first encode to |tmp| and use a mux to add the ICC chunk. libwebp |
| 195 | // forces us to have an encoded image before we can add a profile. |
| 196 | sk_sp<SkData> icc = pixmap.colorSpace() ? icc_from_color_space(*pixmap.colorSpace()) : nullptr; |
| 197 | SkDynamicMemoryWStream tmp; |
| 198 | pic.custom_ptr = icc ? (void*)&tmp : (void*)stream; |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 199 | |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 200 | const uint8_t* src = (uint8_t*)pixmap.addr(); |
commit-bot@chromium.org | 5007aab | 2014-02-26 21:35:17 +0000 | [diff] [blame] | 201 | const int rgbStride = pic.width * bpp; |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 202 | const size_t rowBytes = pixmap.rowBytes(); |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 203 | |
| 204 | // Import (for each scanline) the bit-map image (in appropriate color-space) |
| 205 | // to RGB color space. |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 206 | std::unique_ptr<uint8_t[]> rgb(new uint8_t[rgbStride * pic.height]); |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 207 | for (int y = 0; y < pic.height; ++y) { |
Matt Sarett | 62bb280 | 2017-01-23 12:28:02 -0500 | [diff] [blame] | 208 | proc((char*) &rgb[y * rgbStride], (const char*) &src[y * rowBytes], pic.width, bpp, colors); |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Matt Sarett | 46a45ba | 2017-04-06 20:34:38 +0000 | [diff] [blame] | 211 | auto importProc = WebPPictureImportRGB; |
| 212 | if (3 != bpp) { |
Matt Sarett | 5521356 | 2017-01-23 19:37:37 -0500 | [diff] [blame] | 213 | if (pixmap.isOpaque()) { |
Matt Sarett | 46a45ba | 2017-04-06 20:34:38 +0000 | [diff] [blame] | 214 | importProc = WebPPictureImportRGBX; |
Matt Sarett | 5521356 | 2017-01-23 19:37:37 -0500 | [diff] [blame] | 215 | } else { |
Matt Sarett | 46a45ba | 2017-04-06 20:34:38 +0000 | [diff] [blame] | 216 | importProc = WebPPictureImportRGBA; |
Matt Sarett | 5521356 | 2017-01-23 19:37:37 -0500 | [diff] [blame] | 217 | } |
commit-bot@chromium.org | 5007aab | 2014-02-26 21:35:17 +0000 | [diff] [blame] | 218 | } |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 219 | |
Matt Sarett | 46a45ba | 2017-04-06 20:34:38 +0000 | [diff] [blame] | 220 | if (!importProc(&pic, &rgb[0], rgbStride)) { |
| 221 | return false; |
| 222 | } |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 223 | |
Matt Sarett | 46a45ba | 2017-04-06 20:34:38 +0000 | [diff] [blame] | 224 | if (!WebPEncode(&webp_config, &pic)) { |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | if (icc) { |
| 229 | sk_sp<SkData> encodedData = tmp.detachAsData(); |
| 230 | WebPData encoded = { encodedData->bytes(), encodedData->size() }; |
| 231 | WebPData iccChunk = { icc->bytes(), icc->size() }; |
| 232 | |
| 233 | SkAutoTCallVProc<WebPMux, WebPMuxDelete> mux(WebPMuxNew()); |
| 234 | if (WEBP_MUX_OK != WebPMuxSetImage(mux, &encoded, 0)) { |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | if (WEBP_MUX_OK != WebPMuxSetChunk(mux, "ICCP", &iccChunk, 0)) { |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | WebPData assembled; |
| 243 | if (WEBP_MUX_OK != WebPMuxAssemble(mux, &assembled)) { |
| 244 | return false; |
| 245 | } |
| 246 | |
| 247 | stream->write(assembled.bytes, assembled.size); |
| 248 | WebPDataClear(&assembled); |
| 249 | } |
| 250 | |
| 251 | return true; |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 252 | } |
Matt Sarett | 5521356 | 2017-01-23 19:37:37 -0500 | [diff] [blame] | 253 | |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 254 | #endif |