Matt Sarett | 04c3731 | 2017-05-05 14:02:13 -0400 | [diff] [blame] | 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 | |
Kevin Lubick | bab392f | 2023-04-11 13:51:02 -0400 | [diff] [blame] | 11 | #include "include/core/SkRefCnt.h" |
Kevin Lubick | c69d999 | 2023-02-15 08:04:24 -0500 | [diff] [blame] | 12 | #include "include/core/SkSpan.h" // IWYU pragma: keep |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/encode/SkEncoder.h" |
Kevin Lubick | c69d999 | 2023-02-15 08:04:24 -0500 | [diff] [blame] | 14 | #include "include/private/base/SkAPI.h" |
Matt Sarett | 04c3731 | 2017-05-05 14:02:13 -0400 | [diff] [blame] | 15 | |
Kevin Lubick | c69d999 | 2023-02-15 08:04:24 -0500 | [diff] [blame] | 16 | class SkPixmap; |
Matt Sarett | 04c3731 | 2017-05-05 14:02:13 -0400 | [diff] [blame] | 17 | class SkWStream; |
Kevin Lubick | bab392f | 2023-04-11 13:51:02 -0400 | [diff] [blame] | 18 | class SkData; |
| 19 | class GrDirectContext; |
| 20 | class SkImage; |
Christopher Cameron | e078f2a | 2022-10-12 13:44:03 +0200 | [diff] [blame] | 21 | struct skcms_ICCProfile; |
Matt Sarett | 04c3731 | 2017-05-05 14:02:13 -0400 | [diff] [blame] | 22 | |
Matt Sarett | eb7a693 | 2017-05-11 13:10:06 -0400 | [diff] [blame] | 23 | namespace SkWebpEncoder { |
Matt Sarett | 04c3731 | 2017-05-05 14:02:13 -0400 | [diff] [blame] | 24 | |
Kevin Lubick | 3353d1d | 2023-04-11 18:37:09 -0400 | [diff] [blame] | 25 | enum class Compression { |
| 26 | kLossy, |
| 27 | kLossless, |
| 28 | }; |
Matt Sarett | d5a1691 | 2017-05-16 17:06:52 -0400 | [diff] [blame] | 29 | |
Kevin Lubick | 3353d1d | 2023-04-11 18:37:09 -0400 | [diff] [blame] | 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; |
Matt Sarett | 04c3731 | 2017-05-05 14:02:13 -0400 | [diff] [blame] | 45 | |
| 46 | /** |
Kevin Lubick | 3353d1d | 2023-04-11 18:37:09 -0400 | [diff] [blame] | 47 | * An optional ICC profile to override the default behavior. |
Matt Sarett | 04c3731 | 2017-05-05 14:02:13 -0400 | [diff] [blame] | 48 | * |
Kevin Lubick | 3353d1d | 2023-04-11 18:37:09 -0400 | [diff] [blame] | 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. |
Matt Sarett | 04c3731 | 2017-05-05 14:02:13 -0400 | [diff] [blame] | 52 | */ |
Kevin Lubick | 3353d1d | 2023-04-11 18:37:09 -0400 | [diff] [blame] | 53 | const skcms_ICCProfile* fICCProfile = nullptr; |
| 54 | const char* fICCProfileDescription = nullptr; |
| 55 | }; |
Yue Li | 7ba94e9 | 2022-08-31 14:47:22 -0700 | [diff] [blame] | 56 | |
Kevin Lubick | 3353d1d | 2023-04-11 18:37:09 -0400 | [diff] [blame] | 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); |
Kevin Lubick | bab392f | 2023-04-11 13:51:02 -0400 | [diff] [blame] | 64 | |
Kevin Lubick | 3353d1d | 2023-04-11 18:37:09 -0400 | [diff] [blame] | 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); |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 90 | } // namespace SkWebpEncoder |
Matt Sarett | 04c3731 | 2017-05-05 14:02:13 -0400 | [diff] [blame] | 91 | |
| 92 | #endif |