| 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 |
/** |
| 48 |
* Encode the |src| pixels to the |dst| stream. |
| 49 |
* |options| may be used to control the encoding behavior. |
| 50 |
* |
| 51 |
* Returns true on success. Returns false on an invalid or unsupported |src|. |
| 52 |
*/ |
| 53 |
SK_API bool Encode(SkWStream* dst, const SkPixmap& src, const Options& options); |
| 54 |
|
| 55 |
/** |
| 56 |
* Returns the encoded data for the pixmap, or nullptr on failure. |
| 57 |
*/ |
| 58 |
SK_API sk_sp<SkData> Encode(const SkPixmap& src, const Options& options); |
| 59 |
|
| 60 |
/** |
| 61 |
* Encode the provided image and return the resulting bytes. If the image was created as |
| 62 |
* a texture-backed image on a GPU context, that |ctx| must be provided so the pixels |
| 63 |
* can be read before being encoded. For raster-backed images, |ctx| can be nullptr. |
| 64 |
* |options| may be used to control the encoding behavior. |
| 65 |
* |
| 66 |
* Returns nullptr if the pixels could not be read or encoding otherwise fails. |
| 67 |
*/ |
| 68 |
SK_API sk_sp<SkData> Encode(GrDirectContext* ctx, const SkImage* img, const Options& options); |
| 69 |
|
| 70 |
/** |
| 71 |
* Encode the |src| frames to the |dst| stream. |
| 72 |
* |options| may be used to control the encoding behavior. |
| 73 |
* |
| 74 |
* The size of the first frame will be used as the canvas size. If any other frame does |
| 75 |
* not match the canvas size, this is an error. |
| 76 |
* |
| 77 |
* Returns true on success. Returns false on an invalid or unsupported |src|. |
| 78 |
* |
| 79 |
* Note: libwebp API also supports set background color, loop limit and customize |
| 80 |
* lossy/lossless for each frame. These could be added later as needed. |
| 81 |
*/ |
| 82 |
SK_API bool EncodeAnimated(SkWStream* dst, |
| 83 |
SkSpan<const SkEncoder::Frame> src, |
| 84 |
const Options& options); |
| 85 |
} // namespace SkWebpEncoder |
| 86 |
|
| 87 |
#endif |