blob: fe11044e7380465002ffe1c362d54153e425a74a [file] [log] [blame]
Matt Sarett04c37312017-05-05 14:02:13 -04001/*
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 Lubickbab392f2023-04-11 13:51:02 -040011#include "include/core/SkRefCnt.h"
Kevin Lubickc69d9992023-02-15 08:04:24 -050012#include "include/core/SkSpan.h" // IWYU pragma: keep
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/encode/SkEncoder.h"
Kevin Lubickc69d9992023-02-15 08:04:24 -050014#include "include/private/base/SkAPI.h"
Matt Sarett04c37312017-05-05 14:02:13 -040015
Kevin Lubickc69d9992023-02-15 08:04:24 -050016class SkPixmap;
Matt Sarett04c37312017-05-05 14:02:13 -040017class SkWStream;
Kevin Lubickbab392f2023-04-11 13:51:02 -040018class SkData;
19class GrDirectContext;
20class SkImage;
Christopher Camerone078f2a2022-10-12 13:44:03 +020021struct skcms_ICCProfile;
Matt Sarett04c37312017-05-05 14:02:13 -040022
Matt Saretteb7a6932017-05-11 13:10:06 -040023namespace SkWebpEncoder {
Matt Sarett04c37312017-05-05 14:02:13 -040024
Kevin Lubick3353d1d2023-04-11 18:37:09 -040025enum class Compression {
26 kLossy,
27 kLossless,
28};
Matt Sarettd5a16912017-05-16 17:06:52 -040029
Kevin Lubick3353d1d2023-04-11 18:37:09 -040030struct 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 Sarett04c37312017-05-05 14:02:13 -040045
46 /**
Kevin Lubick3353d1d2023-04-11 18:37:09 -040047 * An optional ICC profile to override the default behavior.
Matt Sarett04c37312017-05-05 14:02:13 -040048 *
Kevin Lubick3353d1d2023-04-11 18:37:09 -040049 * 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 Sarett04c37312017-05-05 14:02:13 -040052 */
Kevin Lubick3353d1d2023-04-11 18:37:09 -040053 const skcms_ICCProfile* fICCProfile = nullptr;
54 const char* fICCProfileDescription = nullptr;
55};
Yue Li7ba94e92022-08-31 14:47:22 -070056
Kevin Lubick3353d1d2023-04-11 18:37:09 -040057/**
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 */
63SK_API bool Encode(SkWStream* dst, const SkPixmap& src, const Options& options);
Kevin Lubickbab392f2023-04-11 13:51:02 -040064
Kevin Lubick3353d1d2023-04-11 18:37:09 -040065/**
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*/
73SK_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 */
87SK_API bool EncodeAnimated(SkWStream* dst,
88 SkSpan<const SkEncoder::Frame> src,
89 const Options& options);
John Stilesa6841be2020-08-06 14:11:56 -040090} // namespace SkWebpEncoder
Matt Sarett04c37312017-05-05 14:02:13 -040091
92#endif