commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 1 | /* |
Leon Scroggins III | 20baaee | 2020-03-09 11:48:03 -0400 | [diff] [blame] | 2 | * Copyright 2010 The Android Open Source Project |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 3 | * |
Leon Scroggins III | 20baaee | 2020-03-09 11:48:03 -0400 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/images/SkImageEncoderPriv.h" |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 9 | |
Leon Scroggins III | a77f30c | 2020-03-09 14:23:30 -0400 | [diff] [blame] | 10 | #ifdef SK_ENCODE_WEBP |
Mike Klein | ab737e4 | 2019-05-15 21:58:15 +0000 | [diff] [blame] | 11 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkBitmap.h" |
| 13 | #include "include/core/SkStream.h" |
| 14 | #include "include/core/SkUnPreMultiply.h" |
| 15 | #include "include/encode/SkWebpEncoder.h" |
| 16 | #include "include/private/SkColorData.h" |
Leon Scroggins III | 1adcac5 | 2020-05-28 17:12:59 -0400 | [diff] [blame] | 17 | #include "include/private/SkImageInfoPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "include/private/SkTemplates.h" |
| 19 | #include "src/images/SkImageEncoderFns.h" |
| 20 | #include "src/utils/SkUTF.h" |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 21 | |
Matt Sarett | 46a45ba | 2017-04-06 20:34:38 +0000 | [diff] [blame] | 22 | // A WebP encoder only, on top of (subset of) libwebp |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 23 | // For more information on WebP image format, and libwebp library, see: |
| 24 | // http://code.google.com/speed/webp/ |
| 25 | // http://www.webmproject.org/code/#libwebp_webp_image_decoder_library |
| 26 | // http://review.webmproject.org/gitweb?p=libwebp.git |
| 27 | |
| 28 | #include <stdio.h> |
| 29 | extern "C" { |
| 30 | // If moving libwebp out of skia source tree, path for webp headers must be |
| 31 | // 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] | 32 | #include "webp/encode.h" |
Matt Sarett | 46a45ba | 2017-04-06 20:34:38 +0000 | [diff] [blame] | 33 | #include "webp/mux.h" |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 34 | } |
| 35 | |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 36 | static int stream_writer(const uint8_t* data, size_t data_size, |
| 37 | const WebPPicture* const picture) { |
| 38 | SkWStream* const stream = (SkWStream*)picture->custom_ptr; |
| 39 | return stream->write(data, data_size) ? 1 : 0; |
| 40 | } |
| 41 | |
Leon Scroggins III | 1adcac5 | 2020-05-28 17:12:59 -0400 | [diff] [blame] | 42 | using WebPPictureImportProc = int (*) (WebPPicture* picture, const uint8_t* pixels, int stride); |
| 43 | |
Matt Sarett | d5a1691 | 2017-05-16 17:06:52 -0400 | [diff] [blame] | 44 | bool SkWebpEncoder::Encode(SkWStream* stream, const SkPixmap& pixmap, const Options& opts) { |
Brian Osman | e1adc3a | 2018-06-04 09:21:17 -0400 | [diff] [blame] | 45 | if (!SkPixmapIsValid(pixmap)) { |
Matt Sarett | 04c3731 | 2017-05-05 14:02:13 -0400 | [diff] [blame] | 46 | return false; |
Matt Sarett | 5521356 | 2017-01-23 19:37:37 -0500 | [diff] [blame] | 47 | } |
| 48 | |
Leon Scroggins III | 1adcac5 | 2020-05-28 17:12:59 -0400 | [diff] [blame] | 49 | if (SkColorTypeIsAlphaOnly(pixmap.colorType())) { |
| 50 | // Maintain the existing behavior of not supporting encoding alpha-only images. |
| 51 | // TODO: Support encoding alpha only to an image with alpha but no color? |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 52 | return false; |
| 53 | } |
Matt Sarett | 5521356 | 2017-01-23 19:37:37 -0500 | [diff] [blame] | 54 | |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 55 | if (nullptr == pixmap.addr()) { |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 56 | return false; |
| 57 | } |
| 58 | |
| 59 | WebPConfig webp_config; |
Matt Sarett | 04c3731 | 2017-05-05 14:02:13 -0400 | [diff] [blame] | 60 | if (!WebPConfigPreset(&webp_config, WEBP_PRESET_DEFAULT, opts.fQuality)) { |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 61 | return false; |
| 62 | } |
| 63 | |
| 64 | WebPPicture pic; |
| 65 | WebPPictureInit(&pic); |
Matt Sarett | 46a45ba | 2017-04-06 20:34:38 +0000 | [diff] [blame] | 66 | SkAutoTCallVProc<WebPPicture, WebPPictureFree> autoPic(&pic); |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 67 | pic.width = pixmap.width(); |
| 68 | pic.height = pixmap.height(); |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 69 | pic.writer = stream_writer; |
Matt Sarett | 46a45ba | 2017-04-06 20:34:38 +0000 | [diff] [blame] | 70 | |
Matt Sarett | 2f68787 | 2017-05-19 19:12:54 -0400 | [diff] [blame] | 71 | // Set compression, method, and pixel format. |
| 72 | // libwebp recommends using BGRA for lossless and YUV for lossy. |
| 73 | // The choices of |webp_config.method| currently just match Chrome's defaults. We |
| 74 | // could potentially expose this decision to the client. |
| 75 | if (Compression::kLossy == opts.fCompression) { |
| 76 | webp_config.lossless = 0; |
| 77 | #ifndef SK_WEBP_ENCODER_USE_DEFAULT_METHOD |
| 78 | webp_config.method = 3; |
| 79 | #endif |
| 80 | pic.use_argb = 0; |
| 81 | } else { |
| 82 | webp_config.lossless = 1; |
| 83 | webp_config.method = 0; |
| 84 | pic.use_argb = 1; |
| 85 | } |
| 86 | |
Matt Sarett | 46a45ba | 2017-04-06 20:34:38 +0000 | [diff] [blame] | 87 | // If there is no need to embed an ICC profile, we write directly to the input stream. |
| 88 | // Otherwise, we will first encode to |tmp| and use a mux to add the ICC chunk. libwebp |
| 89 | // 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] | 90 | sk_sp<SkData> icc = icc_from_color_space(pixmap.info()); |
Matt Sarett | 46a45ba | 2017-04-06 20:34:38 +0000 | [diff] [blame] | 91 | SkDynamicMemoryWStream tmp; |
| 92 | pic.custom_ptr = icc ? (void*)&tmp : (void*)stream; |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 93 | |
Leon Scroggins III | 1adcac5 | 2020-05-28 17:12:59 -0400 | [diff] [blame] | 94 | { |
| 95 | const SkColorType ct = pixmap.colorType(); |
| 96 | const bool premul = pixmap.alphaType() == kPremul_SkAlphaType; |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 97 | |
Leon Scroggins III | 1adcac5 | 2020-05-28 17:12:59 -0400 | [diff] [blame] | 98 | SkBitmap tmpBm; |
| 99 | WebPPictureImportProc importProc = nullptr; |
| 100 | const SkPixmap* src = &pixmap; |
| 101 | if ( ct == kRGB_888x_SkColorType) { importProc = WebPPictureImportRGBX; } |
| 102 | else if (!premul && ct == kRGBA_8888_SkColorType) { importProc = WebPPictureImportRGBA; } |
| 103 | #ifdef WebPPictureImportBGRA |
| 104 | else if (!premul && ct == kBGRA_8888_SkColorType) { importProc = WebPPictureImportBGRA; } |
| 105 | #endif |
| 106 | else { |
Matt Sarett | 46a45ba | 2017-04-06 20:34:38 +0000 | [diff] [blame] | 107 | importProc = WebPPictureImportRGBA; |
Leon Scroggins III | 1adcac5 | 2020-05-28 17:12:59 -0400 | [diff] [blame] | 108 | auto info = pixmap.info().makeColorType(kRGBA_8888_SkColorType) |
| 109 | .makeAlphaType(kUnpremul_SkAlphaType); |
| 110 | if (!tmpBm.tryAllocPixels(info) |
| 111 | || !pixmap.readPixels(tmpBm.info(), tmpBm.getPixels(), tmpBm.rowBytes())) { |
| 112 | return false; |
| 113 | } |
| 114 | src = &tmpBm.pixmap(); |
Matt Sarett | 5521356 | 2017-01-23 19:37:37 -0500 | [diff] [blame] | 115 | } |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 116 | |
Leon Scroggins III | 1adcac5 | 2020-05-28 17:12:59 -0400 | [diff] [blame] | 117 | if (!importProc(&pic, reinterpret_cast<const uint8_t*>(src->addr()), src->rowBytes())) { |
| 118 | return false; |
| 119 | } |
Matt Sarett | 46a45ba | 2017-04-06 20:34:38 +0000 | [diff] [blame] | 120 | } |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 121 | |
Matt Sarett | 46a45ba | 2017-04-06 20:34:38 +0000 | [diff] [blame] | 122 | if (!WebPEncode(&webp_config, &pic)) { |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | if (icc) { |
| 127 | sk_sp<SkData> encodedData = tmp.detachAsData(); |
| 128 | WebPData encoded = { encodedData->bytes(), encodedData->size() }; |
| 129 | WebPData iccChunk = { icc->bytes(), icc->size() }; |
| 130 | |
| 131 | SkAutoTCallVProc<WebPMux, WebPMuxDelete> mux(WebPMuxNew()); |
| 132 | if (WEBP_MUX_OK != WebPMuxSetImage(mux, &encoded, 0)) { |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | if (WEBP_MUX_OK != WebPMuxSetChunk(mux, "ICCP", &iccChunk, 0)) { |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | WebPData assembled; |
| 141 | if (WEBP_MUX_OK != WebPMuxAssemble(mux, &assembled)) { |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | stream->write(assembled.bytes, assembled.size); |
| 146 | WebPDataClear(&assembled); |
| 147 | } |
| 148 | |
| 149 | return true; |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 150 | } |
Mike Klein | ab737e4 | 2019-05-15 21:58:15 +0000 | [diff] [blame] | 151 | |
| 152 | #endif |