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