1 /* 2 * Copyright 2017 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef SkWebpEncoder_DEFINED 9 #define SkWebpEncoder_DEFINED 10 11 #include "include/core/SkRefCnt.h" 12 #include "include/core/SkSpan.h" // IWYU pragma: keep 13 #include "include/encode/SkEncoder.h" 14 #include "include/private/base/SkAPI.h" 15 16 class SkPixmap; 17 class SkWStream; 18 class SkData; 19 class GrDirectContext; 20 class SkImage; 21 struct skcms_ICCProfile; 22 23 namespace SkWebpEncoder { 24 25 enum class Compression { 26 kLossy, 27 kLossless, 28 }; 29 30 struct SK_API Options { 31 /** 32 * |fCompression| determines whether we will use webp lossy or lossless compression. 33 * 34 * |fQuality| must be in [0.0f, 100.0f]. 35 * If |fCompression| is kLossy, |fQuality| corresponds to the visual quality of the 36 * encoding. Decreasing the quality will result in a smaller encoded image. 37 * If |fCompression| is kLossless, |fQuality| corresponds to the amount of effort 38 * put into the encoding. Lower values will compress faster into larger files, 39 * while larger values will compress slower into smaller files. 40 * 41 * This scheme is designed to match the libwebp API. 42 */ 43 Compression fCompression = Compression::kLossy; 44 float fQuality = 100.0f; 45 46 /** 47 * An optional ICC profile to override the default behavior. 48 * 49 * The default behavior is to generate an ICC profile using a primary matrix and 50 * analytic transfer function. If the color space of |src| cannot be represented 51 * in this way (e.g, it is HLG or PQ), then no profile will be embedded. 52 */ 53 const skcms_ICCProfile* fICCProfile = nullptr; 54 const char* fICCProfileDescription = nullptr; 55 }; 56 57 /** 58 * Encode the |src| pixels to the |dst| stream. 59 * |options| may be used to control the encoding behavior. 60 * 61 * Returns true on success. Returns false on an invalid or unsupported |src|. 62 */ 63 SK_API bool Encode(SkWStream* dst, const SkPixmap& src, const Options& options); 64 65 /** 66 * Encode the provided image and return the resulting bytes. If the image was created as 67 * a texture-backed image on a GPU context, that |ctx| must be provided so the pixels 68 * can be read before being encoded. For raster-backed images, |ctx| can be nullptr. 69 * |options| may be used to control the encoding behavior. 70 * 71 * Returns nullptr if the pixels could not be read or encoding otherwise fails. 72 */ 73 SK_API sk_sp<SkData> Encode(GrDirectContext* ctx, const SkImage* img, const Options& options); 74 75 /** 76 * Encode the |src| frames to the |dst| stream. 77 * |options| may be used to control the encoding behavior. 78 * 79 * The size of the first frame will be used as the canvas size. If any other frame does 80 * not match the canvas size, this is an error. 81 * 82 * Returns true on success. Returns false on an invalid or unsupported |src|. 83 * 84 * Note: libwebp API also supports set background color, loop limit and customize 85 * lossy/lossless for each frame. These could be added later as needed. 86 */ 87 SK_API bool EncodeAnimated(SkWStream* dst, 88 SkSpan<const SkEncoder::Frame> src, 89 const Options& options); 90 } // namespace SkWebpEncoder 91 92 #endif 93