From 6a4dc66990f73b9443816e69054f101386197593 Mon Sep 17 00:00:00 2001 From: Matt Sarett Date: Thu, 11 May 2017 09:32:59 -0400 Subject: Add SkJpegEncoder and SkWebpEncoder to public API Bug: 713862 Change-Id: I05d8b3a506f817d63b0bc3ab3f9233e24bbe3be0 Reviewed-on: https://skia-review.googlesource.com/16233 Commit-Queue: Matt Sarett Reviewed-by: Leon Scroggins Reviewed-by: Mike Reed --- include/encode/SkJpegEncoder.h | 98 ++++++++++++++++++++++++++++++++++++++++++ include/encode/SkPngEncoder.h | 4 +- include/encode/SkWebpEncoder.h | 40 +++++++++++++++++ 3 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 include/encode/SkJpegEncoder.h create mode 100644 include/encode/SkWebpEncoder.h (limited to 'include/encode') diff --git a/include/encode/SkJpegEncoder.h b/include/encode/SkJpegEncoder.h new file mode 100644 index 0000000000..fd7c204161 --- /dev/null +++ b/include/encode/SkJpegEncoder.h @@ -0,0 +1,98 @@ +/* + * Copyright 2017 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef SkJpegEncoder_DEFINED +#define SkJpegEncoder_DEFINED + +#include "SkEncoder.h" + +class SkJpegEncoderMgr; +class SkWStream; + +class SK_API SkJpegEncoder : public SkEncoder { +public: + + enum class AlphaOption { + kIgnore, + kBlendOnBlack, + }; + + enum class Downsample { + /** + * Reduction by a factor of two in both the horizontal and vertical directions. + */ + k420, + + /** + * Reduction by a factor of two in the horizontal direction. + */ + k422, + + /** + * No downsampling. + */ + k444, + }; + + struct Options { + /** + * |fQuality| must be in [0, 100] where 0 corresponds to the lowest quality. + */ + int fQuality = 100; + + /** + * Choose the downsampling factor for the U and V components. This is only + * meaningful if the |src| is not kGray, since kGray will not be encoded as YUV. + * + * Our default value matches the libjpeg-turbo default. + */ + Downsample fDownsample = Downsample::k420; + + /** + * Jpegs must be opaque. This instructs the encoder on how to handle input + * images with alpha. + * + * The default is to ignore the alpha channel and treat the image as opaque. + * Another option is to blend the pixels onto a black background before encoding. + * In the second case, the encoder supports linear or legacy blending. + */ + AlphaOption fAlphaOption = AlphaOption::kIgnore; + SkTransferFunctionBehavior fBlendBehavior = SkTransferFunctionBehavior::kRespect; + }; + + /** + * Encode the |src| pixels to the |dst| stream. + * |options| may be used to control the encoding behavior. + * + * Returns true on success. Returns false on an invalid or unsupported |src|. + */ + static bool Encode(SkWStream* dst, const SkPixmap& src, const Options& options); + + /** + * Create a jpeg encoder that will encode the |src| pixels to the |dst| stream. + * |options| may be used to control the encoding behavior. + * + * |dst| is unowned but must remain valid for the lifetime of the object. + * + * This returns nullptr on an invalid or unsupported |src|. + */ + static std::unique_ptr Make(SkWStream* dst, const SkPixmap& src, + const Options& options); + + ~SkJpegEncoder() override; + +protected: + bool onEncodeRows(int numRows) override; + +private: + SkJpegEncoder(std::unique_ptr, const SkPixmap& src); + + std::unique_ptr fEncoderMgr; + typedef SkEncoder INHERITED; +}; + +#endif diff --git a/include/encode/SkPngEncoder.h b/include/encode/SkPngEncoder.h index adcc061113..6de800f367 100644 --- a/include/encode/SkPngEncoder.h +++ b/include/encode/SkPngEncoder.h @@ -76,8 +76,8 @@ public: * * This returns nullptr on an invalid or unsupported |src|. */ - static std::unique_ptr Make(SkWStream* dst, const SkPixmap& src, - const Options& options); + static std::unique_ptr Make(SkWStream* dst, const SkPixmap& src, + const Options& options); ~SkPngEncoder() override; diff --git a/include/encode/SkWebpEncoder.h b/include/encode/SkWebpEncoder.h new file mode 100644 index 0000000000..58ee06d1c6 --- /dev/null +++ b/include/encode/SkWebpEncoder.h @@ -0,0 +1,40 @@ +/* + * Copyright 2017 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef SkWebpEncoder_DEFINED +#define SkWebpEncoder_DEFINED + +#include "SkEncoder.h" + +class SkWStream; + +namespace SK_API SkWebpEncoder { + + struct Options { + /** + * |fQuality| must be in [0.0f, 100.0f] where 0.0f corresponds to the lowest quality. + */ + float fQuality = 100.0f; + + /** + * If the input is premultiplied, this controls the unpremultiplication behavior. + * The encoder can convert to linear before unpremultiplying or ignore the transfer + * function and unpremultiply the input as is. + */ + SkTransferFunctionBehavior fUnpremulBehavior = SkTransferFunctionBehavior::kRespect; + }; + + /** + * Encode the |src| pixels to the |dst| stream. + * |options| may be used to control the encoding behavior. + * + * Returns true on success. Returns false on an invalid or unsupported |src|. + */ + bool Encode(SkWStream* dst, const SkPixmap& src, const Options& options); +}; + +#endif -- cgit v1.2.3