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