| 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 SkPngEncoder_DEFINED |
| 9 |
#define SkPngEncoder_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkDataTable.h" |
| 12 |
#include "include/core/SkRefCnt.h" |
| 13 |
#include "include/private/SkHdrMetadata.h" |
| 14 |
#include "include/private/base/SkAPI.h" |
| 15 |
|
| 16 |
// TODO(kjlubick) update clients to directly include this |
| 17 |
#include "include/encode/SkEncoder.h" // IWYU pragma: keep |
| 18 |
|
| 19 |
#include <memory> |
| 20 |
|
| 21 |
class GrDirectContext; |
| 22 |
class SkData; |
| 23 |
class SkImage; |
| 24 |
class SkPixmap; |
| 25 |
class SkWStream; |
| 26 |
struct skcms_ICCProfile; |
| 27 |
struct SkGainmapInfo; |
| 28 |
|
| 29 |
namespace SkPngEncoder { |
| 30 |
|
| 31 |
enum class FilterFlag : int { |
| 32 |
kZero = 0x00, |
| 33 |
kNone = 0x08, |
| 34 |
kSub = 0x10, |
| 35 |
kUp = 0x20, |
| 36 |
kAvg = 0x40, |
| 37 |
kPaeth = 0x80, |
| 38 |
kAll = kNone | kSub | kUp | kAvg | kPaeth, |
| 39 |
}; |
| 40 |
|
| 41 |
inline FilterFlag operator|(FilterFlag x, FilterFlag y) { return (FilterFlag)((int)x | (int)y); } |
| 42 |
|
| 43 |
struct Options { |
| 44 |
/** |
| 45 |
* Selects which filtering strategies to use. |
| 46 |
* |
| 47 |
* If a single filter is chosen, libpng will use that filter for every row. |
| 48 |
* |
| 49 |
* If multiple filters are chosen, libpng will use a heuristic to guess which filter |
| 50 |
* will encode smallest, then apply that filter. This happens on a per row basis, |
| 51 |
* different rows can use different filters. |
| 52 |
* |
| 53 |
* Using a single filter (or less filters) is typically faster. Trying all of the |
| 54 |
* filters may help minimize the output file size. |
| 55 |
* |
| 56 |
* Our default value matches libpng's default. |
| 57 |
*/ |
| 58 |
FilterFlag fFilterFlags = FilterFlag::kAll; |
| 59 |
|
| 60 |
/** |
| 61 |
* Must be in [0, 9] where 9 corresponds to maximal compression. This value is passed |
| 62 |
* directly to zlib. 0 is a special case to skip zlib entirely, creating dramatically |
| 63 |
* larger pngs. |
| 64 |
* |
| 65 |
* Our default value matches libpng's default. |
| 66 |
*/ |
| 67 |
int fZLibLevel = 6; |
| 68 |
|
| 69 |
/** |
| 70 |
* Represents comments in the tEXt ancillary chunk of the png. |
| 71 |
* The 2i-th entry is the keyword for the i-th comment, |
| 72 |
* and the (2i + 1)-th entry is the text for the i-th comment. |
| 73 |
*/ |
| 74 |
sk_sp<SkDataTable> fComments; |
| 75 |
|
| 76 |
/** |
| 77 |
* Container for any HDR metadata to include in the encoded image. |
| 78 |
*/ |
| 79 |
skhdr::Metadata fHdrMetadata; |
| 80 |
|
| 81 |
/** |
| 82 |
* If non-null, then a gainmap and its metadata will be encoded as png chunks. |
| 83 |
* The gainmap will be encoded in a gmAP chunk as a full PNG container. The |
| 84 |
* gainmap info will be encoded in a gdAT chunk inside of the gmAP chunk. |
| 85 |
* This effectively is Option B proposed in this discussion for adding gainmaps |
| 86 |
* into PNG: https://github.com/w3c/png/issues/380#issuecomment-2325163149. |
| 87 |
* |
| 88 |
* Note that if fGainmapInfo is null, then fGainmap will fail to encode, as the |
| 89 |
* gainmap metadata is required to correctly interpret the encoded gainmap. |
| 90 |
*/ |
| 91 |
const SkPixmap* fGainmap = nullptr; |
| 92 |
const SkGainmapInfo* fGainmapInfo = nullptr; |
| 93 |
}; |
| 94 |
|
| 95 |
/** |
| 96 |
* Encode the |src| pixels to the |dst| stream. |
| 97 |
* |options| may be used to control the encoding behavior. |
| 98 |
* |
| 99 |
* Returns true on success. Returns false on an invalid or unsupported |src|. |
| 100 |
*/ |
| 101 |
SK_API bool Encode(SkWStream* dst, const SkPixmap& src, const Options& options); |
| 102 |
|
| 103 |
/** |
| 104 |
* Returns the encoded data for the pixmap, or nullptr on failure. |
| 105 |
*/ |
| 106 |
SK_API sk_sp<SkData> Encode(const SkPixmap& src, const Options& options); |
| 107 |
|
| 108 |
/** |
| 109 |
* Encode the provided image and return the resulting bytes. If the image was created as |
| 110 |
* a texture-backed image on a GPU context, that |ctx| must be provided so the pixels |
| 111 |
* can be read before being encoded. For raster-backed images, |ctx| can be nullptr. |
| 112 |
* |options| may be used to control the encoding behavior. |
| 113 |
* |
| 114 |
* Returns nullptr if the pixels could not be read or encoding otherwise fails. |
| 115 |
*/ |
| 116 |
SK_API sk_sp<SkData> Encode(GrDirectContext* ctx, const SkImage* img, const Options& options); |
| 117 |
|
| 118 |
/** |
| 119 |
* Create a png encoder that will encode the |src| pixels to the |dst| stream. |
| 120 |
* |options| may be used to control the encoding behavior. |
| 121 |
* |
| 122 |
* The primary use of this is incremental encoding of the pixels. |
| 123 |
* |
| 124 |
* |dst| is unowned but must remain valid for the lifetime of the object. |
| 125 |
* |
| 126 |
* This returns nullptr on an invalid or unsupported |src|. |
| 127 |
*/ |
| 128 |
SK_API std::unique_ptr<SkEncoder> Make(SkWStream* dst, const SkPixmap& src, const Options& options); |
| 129 |
|
| 130 |
} // namespace SkPngEncoder |
| 131 |
|
| 132 |
#endif |