| 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 SkJpegEncoder_DEFINED |
| 9 |
#define SkJpegEncoder_DEFINED |
| 10 |
|
| 11 |
#include "include/codec/SkEncodedOrigin.h" |
| 12 |
#include "include/core/SkRefCnt.h" |
| 13 |
#include "include/private/base/SkAPI.h" |
| 14 |
|
| 15 |
#include <memory> |
| 16 |
#include <optional> |
| 17 |
|
| 18 |
class SkColorSpace; |
| 19 |
class SkData; |
| 20 |
class SkEncoder; |
| 21 |
class SkPixmap; |
| 22 |
class SkWStream; |
| 23 |
class SkImage; |
| 24 |
class GrDirectContext; |
| 25 |
class SkYUVAPixmaps; |
| 26 |
struct skcms_ICCProfile; |
| 27 |
|
| 28 |
namespace SkJpegEncoder { |
| 29 |
|
| 30 |
enum class AlphaOption { |
| 31 |
kIgnore, |
| 32 |
kBlendOnBlack, |
| 33 |
}; |
| 34 |
|
| 35 |
enum class Downsample { |
| 36 |
/** |
| 37 |
* Reduction by a factor of two in both the horizontal and vertical directions. |
| 38 |
*/ |
| 39 |
k420, |
| 40 |
|
| 41 |
/** |
| 42 |
* Reduction by a factor of two in the horizontal direction. |
| 43 |
*/ |
| 44 |
k422, |
| 45 |
|
| 46 |
/** |
| 47 |
* No downsampling. |
| 48 |
*/ |
| 49 |
k444, |
| 50 |
}; |
| 51 |
|
| 52 |
struct Options { |
| 53 |
/** |
| 54 |
* |fQuality| must be in [0, 100] where 0 corresponds to the lowest quality. |
| 55 |
*/ |
| 56 |
int fQuality = 100; |
| 57 |
|
| 58 |
/** |
| 59 |
* Choose the downsampling factor for the U and V components. This is only |
| 60 |
* meaningful if the |src| is not kGray, since kGray will not be encoded as YUV. |
| 61 |
* This is ignored in favor of |src|'s subsampling when |src| is an SkYUVAPixmaps. |
| 62 |
* |
| 63 |
* Our default value matches the libjpeg-turbo default. |
| 64 |
*/ |
| 65 |
Downsample fDownsample = Downsample::k420; |
| 66 |
|
| 67 |
/** |
| 68 |
* Jpegs must be opaque. This instructs the encoder on how to handle input |
| 69 |
* images with alpha. |
| 70 |
* |
| 71 |
* The default is to ignore the alpha channel and treat the image as opaque. |
| 72 |
* Another option is to blend the pixels onto a black background before encoding. |
| 73 |
* In the second case, the encoder supports linear or legacy blending. |
| 74 |
*/ |
| 75 |
AlphaOption fAlphaOption = AlphaOption::kIgnore; |
| 76 |
|
| 77 |
/** |
| 78 |
* Optional XMP metadata. |
| 79 |
*/ |
| 80 |
const SkData* xmpMetadata = nullptr; |
| 81 |
|
| 82 |
std::optional<SkEncodedOrigin> fOrigin; |
| 83 |
}; |
| 84 |
|
| 85 |
/** |
| 86 |
* Encode the |src| pixels to the |dst| stream. |
| 87 |
* |options| may be used to control the encoding behavior. |
| 88 |
* |
| 89 |
* Returns true on success. Returns false on an invalid or unsupported |src|. |
| 90 |
*/ |
| 91 |
SK_API bool Encode(SkWStream* dst, const SkPixmap& src, const Options& options); |
| 92 |
SK_API bool Encode(SkWStream* dst, |
| 93 |
const SkYUVAPixmaps& src, |
| 94 |
const SkColorSpace* srcColorSpace, |
| 95 |
const Options& options); |
| 96 |
|
| 97 |
/** |
| 98 |
* Returns the encoded data for the pixmap, or nullptr on failure. |
| 99 |
*/ |
| 100 |
SK_API sk_sp<SkData> Encode(const SkPixmap& src, const Options& options); |
| 101 |
|
| 102 |
/** |
| 103 |
* Encode the provided image and return the resulting bytes. If the image was created as |
| 104 |
* a texture-backed image on a GPU context, that |ctx| must be provided so the pixels |
| 105 |
* can be read before being encoded. For raster-backed images, |ctx| can be nullptr. |
| 106 |
* |options| may be used to control the encoding behavior. |
| 107 |
* |
| 108 |
* Returns nullptr if the pixels could not be read or encoding otherwise fails. |
| 109 |
*/ |
| 110 |
SK_API sk_sp<SkData> Encode(GrDirectContext* ctx, const SkImage* img, const Options& options); |
| 111 |
|
| 112 |
/** |
| 113 |
* Create a jpeg encoder that will encode the |src| pixels to the |dst| stream. |
| 114 |
* |options| may be used to control the encoding behavior. |
| 115 |
* |
| 116 |
* |dst| is unowned but must remain valid for the lifetime of the object. |
| 117 |
* |
| 118 |
* This returns nullptr on an invalid or unsupported |src|. |
| 119 |
*/ |
| 120 |
SK_API std::unique_ptr<SkEncoder> Make(SkWStream* dst, const SkPixmap& src, const Options& options); |
| 121 |
SK_API std::unique_ptr<SkEncoder> Make(SkWStream* dst, |
| 122 |
const SkYUVAPixmaps& src, |
| 123 |
const SkColorSpace* srcColorSpace, |
| 124 |
const Options& options); |
| 125 |
} // namespace SkJpegEncoder |
| 126 |
|
| 127 |
#endif |