From 46a45ba907b81517dc301781443cc63fe3b5ccb0 Mon Sep 17 00:00:00 2001 From: Matt Sarett Date: Thu, 6 Apr 2017 20:34:38 +0000 Subject: [PATCH] Revert "Manual revert: Add support for writing ICC profiles to webp encoder" This reverts commit 4293a1e5f259cf12560d6e794c1a05ad4dbc0b32. Reason for revert: Relanding, libwebp updated in Android master Original change's description: > Manual revert: Add support for writing ICC profiles to webp encoder > > Bug: skia: > Change-Id: I4e70bee8c2ea8dbd5ae1e84aa097f5a7e2e62721 > Reviewed-on: https://skia-review.googlesource.com/11444 > Reviewed-by: Matt Sarett > Reviewed-by: Leon Scroggins > Commit-Queue: Matt Sarett > TBR=msarett@google.com,scroggo@google.com,reviews@skia.org,jzern@google.com NOPRESUBMIT=true NOTREECHECKS=true NOTRY=true Change-Id: I73aa9e8183241ada4ec6451567bce3d3d18995cc Reviewed-on: https://skia-review.googlesource.com/11523 Reviewed-by: Matt Sarett Commit-Queue: Matt Sarett --- src/images/SkWEBPImageEncoder.cpp | 56 +++++++++++++++++++++++++------ tests/CodecTest.cpp | 2 ++ third_party/libwebp/BUILD.gn | 4 +++ 3 files changed, 51 insertions(+), 11 deletions(-) diff --git a/src/images/SkWEBPImageEncoder.cpp b/src/images/SkWEBPImageEncoder.cpp index 66b425bff5..c0cd4a6cae 100644 --- a/src/images/SkWEBPImageEncoder.cpp +++ b/src/images/SkWEBPImageEncoder.cpp @@ -26,7 +26,7 @@ #include "SkUnPreMultiply.h" #include "SkUtils.h" -// A WebP decoder only, on top of (subset of) libwebp +// A WebP encoder only, on top of (subset of) libwebp // For more information on WebP image format, and libwebp library, see: // http://code.google.com/speed/webp/ // http://www.webmproject.org/code/#libwebp_webp_image_decoder_library @@ -37,6 +37,7 @@ extern "C" { // If moving libwebp out of skia source tree, path for webp headers must be // updated accordingly. Here, we enforce using local copy in webp sub-directory. #include "webp/encode.h" +#include "webp/mux.h" } static transform_scanline_proc choose_proc(const SkImageInfo& info, @@ -171,10 +172,17 @@ static bool do_encode(SkWStream* stream, const SkPixmap& pixmap, const SkEncodeO WebPPicture pic; WebPPictureInit(&pic); + SkAutoTCallVProc autoPic(&pic); pic.width = pixmap.width(); pic.height = pixmap.height(); pic.writer = stream_writer; - pic.custom_ptr = (void*)stream; + + // If there is no need to embed an ICC profile, we write directly to the input stream. + // Otherwise, we will first encode to |tmp| and use a mux to add the ICC chunk. libwebp + // forces us to have an encoded image before we can add a profile. + sk_sp icc = pixmap.colorSpace() ? icc_from_color_space(*pixmap.colorSpace()) : nullptr; + SkDynamicMemoryWStream tmp; + pic.custom_ptr = icc ? (void*)&tmp : (void*)stream; const uint8_t* src = (uint8_t*)pixmap.addr(); const int rgbStride = pic.width * bpp; @@ -187,21 +195,47 @@ static bool do_encode(SkWStream* stream, const SkPixmap& pixmap, const SkEncodeO proc((char*) &rgb[y * rgbStride], (const char*) &src[y * rowBytes], pic.width, bpp, colors); } - bool ok; - if (bpp == 3) { - ok = SkToBool(WebPPictureImportRGB(&pic, &rgb[0], rgbStride)); - } else { + auto importProc = WebPPictureImportRGB; + if (3 != bpp) { if (pixmap.isOpaque()) { - ok = SkToBool(WebPPictureImportRGBX(&pic, &rgb[0], rgbStride)); + importProc = WebPPictureImportRGBX; } else { - ok = SkToBool(WebPPictureImportRGBA(&pic, &rgb[0], rgbStride)); + importProc = WebPPictureImportRGBA; } } - ok = ok && WebPEncode(&webp_config, &pic); - WebPPictureFree(&pic); + if (!importProc(&pic, &rgb[0], rgbStride)) { + return false; + } - return ok; + if (!WebPEncode(&webp_config, &pic)) { + return false; + } + + if (icc) { + sk_sp encodedData = tmp.detachAsData(); + WebPData encoded = { encodedData->bytes(), encodedData->size() }; + WebPData iccChunk = { icc->bytes(), icc->size() }; + + SkAutoTCallVProc mux(WebPMuxNew()); + if (WEBP_MUX_OK != WebPMuxSetImage(mux, &encoded, 0)) { + return false; + } + + if (WEBP_MUX_OK != WebPMuxSetChunk(mux, "ICCP", &iccChunk, 0)) { + return false; + } + + WebPData assembled; + if (WEBP_MUX_OK != WebPMuxAssemble(mux, &assembled)) { + return false; + } + + stream->write(assembled.bytes, assembled.size); + WebPDataClear(&assembled); + } + + return true; } bool SkEncodeImageAsWEBP(SkWStream* stream, const SkPixmap& src, int quality) { diff --git a/tests/CodecTest.cpp b/tests/CodecTest.cpp index 323a49159f..2c601fadb0 100644 --- a/tests/CodecTest.cpp +++ b/tests/CodecTest.cpp @@ -1590,6 +1590,8 @@ static void test_encode_icc(skiatest::Reporter* r, SkEncodedImageFormat format, DEF_TEST(Codec_EncodeICC, r) { test_encode_icc(r, SkEncodedImageFormat::kPNG, SkTransferFunctionBehavior::kRespect); test_encode_icc(r, SkEncodedImageFormat::kJPEG, SkTransferFunctionBehavior::kRespect); + test_encode_icc(r, SkEncodedImageFormat::kWEBP, SkTransferFunctionBehavior::kRespect); test_encode_icc(r, SkEncodedImageFormat::kPNG, SkTransferFunctionBehavior::kIgnore); test_encode_icc(r, SkEncodedImageFormat::kJPEG, SkTransferFunctionBehavior::kIgnore); + test_encode_icc(r, SkEncodedImageFormat::kWEBP, SkTransferFunctionBehavior::kIgnore); } diff --git a/third_party/libwebp/BUILD.gn b/third_party/libwebp/BUILD.gn index 3e9bd06020..7fec982e35 100644 --- a/third_party/libwebp/BUILD.gn +++ b/third_party/libwebp/BUILD.gn @@ -128,6 +128,10 @@ if (skia_use_system_libwebp) { "../externals/libwebp/src/enc/tree_enc.c", "../externals/libwebp/src/enc/vp8l_enc.c", "../externals/libwebp/src/enc/webp_enc.c", + "../externals/libwebp/src/mux/anim_encode.c", + "../externals/libwebp/src/mux/muxedit.c", + "../externals/libwebp/src/mux/muxinternal.c", + "../externals/libwebp/src/mux/muxread.c", "../externals/libwebp/src/utils/bit_reader_utils.c", "../externals/libwebp/src/utils/bit_writer_utils.c", "../externals/libwebp/src/utils/color_cache_utils.c",