Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 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 | |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 11 | #include "SkEncoder.h" |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 12 | |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 13 | class SkJpegEncoderMgr; |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 14 | class SkWStream; |
| 15 | |
Matt Sarett | 6a4dc66 | 2017-05-11 09:32:59 -0400 | [diff] [blame^] | 16 | class SK_API SkJpegEncoder : public SkEncoder { |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 17 | public: |
| 18 | |
Matt Sarett | 2e61b18 | 2017-05-09 12:46:50 -0400 | [diff] [blame] | 19 | enum class AlphaOption { |
| 20 | kIgnore, |
| 21 | kBlendOnBlack, |
| 22 | }; |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 23 | |
Matt Sarett | fe31908 | 2017-05-09 14:02:10 -0400 | [diff] [blame] | 24 | enum class Downsample { |
| 25 | /** |
| 26 | * Reduction by a factor of two in both the horizontal and vertical directions. |
| 27 | */ |
| 28 | k420, |
| 29 | |
| 30 | /** |
| 31 | * Reduction by a factor of two in the horizontal direction. |
| 32 | */ |
| 33 | k422, |
| 34 | |
| 35 | /** |
| 36 | * No downsampling. |
| 37 | */ |
| 38 | k444, |
| 39 | }; |
| 40 | |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 41 | struct Options { |
| 42 | /** |
Matt Sarett | 2e61b18 | 2017-05-09 12:46:50 -0400 | [diff] [blame] | 43 | * |fQuality| must be in [0, 100] where 0 corresponds to the lowest quality. |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 44 | */ |
| 45 | int fQuality = 100; |
Matt Sarett | 2e61b18 | 2017-05-09 12:46:50 -0400 | [diff] [blame] | 46 | |
| 47 | /** |
Matt Sarett | fe31908 | 2017-05-09 14:02:10 -0400 | [diff] [blame] | 48 | * Choose the downsampling factor for the U and V components. This is only |
| 49 | * meaningful if the |src| is not kGray, since kGray will not be encoded as YUV. |
| 50 | * |
| 51 | * Our default value matches the libjpeg-turbo default. |
| 52 | */ |
| 53 | Downsample fDownsample = Downsample::k420; |
| 54 | |
| 55 | /** |
Matt Sarett | 2e61b18 | 2017-05-09 12:46:50 -0400 | [diff] [blame] | 56 | * Jpegs must be opaque. This instructs the encoder on how to handle input |
| 57 | * images with alpha. |
| 58 | * |
| 59 | * The default is to ignore the alpha channel and treat the image as opaque. |
| 60 | * Another option is to blend the pixels onto a black background before encoding. |
| 61 | * In the second case, the encoder supports linear or legacy blending. |
| 62 | */ |
| 63 | AlphaOption fAlphaOption = AlphaOption::kIgnore; |
| 64 | SkTransferFunctionBehavior fBlendBehavior = SkTransferFunctionBehavior::kRespect; |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | /** |
| 68 | * Encode the |src| pixels to the |dst| stream. |
| 69 | * |options| may be used to control the encoding behavior. |
| 70 | * |
| 71 | * Returns true on success. Returns false on an invalid or unsupported |src|. |
| 72 | */ |
| 73 | static bool Encode(SkWStream* dst, const SkPixmap& src, const Options& options); |
| 74 | |
| 75 | /** |
| 76 | * Create a jpeg encoder that will encode the |src| pixels to the |dst| stream. |
| 77 | * |options| may be used to control the encoding behavior. |
| 78 | * |
| 79 | * |dst| is unowned but must remain valid for the lifetime of the object. |
| 80 | * |
| 81 | * This returns nullptr on an invalid or unsupported |src|. |
| 82 | */ |
Matt Sarett | 6a4dc66 | 2017-05-11 09:32:59 -0400 | [diff] [blame^] | 83 | static std::unique_ptr<SkEncoder> Make(SkWStream* dst, const SkPixmap& src, |
| 84 | const Options& options); |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 85 | |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 86 | ~SkJpegEncoder() override; |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 87 | |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 88 | protected: |
| 89 | bool onEncodeRows(int numRows) override; |
| 90 | |
| 91 | private: |
| 92 | SkJpegEncoder(std::unique_ptr<SkJpegEncoderMgr>, const SkPixmap& src); |
| 93 | |
| 94 | std::unique_ptr<SkJpegEncoderMgr> fEncoderMgr; |
| 95 | typedef SkEncoder INHERITED; |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | #endif |