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 | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 16 | class 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 | |
| 24 | struct Options { |
| 25 | /** |
Matt Sarett | 2e61b18 | 2017-05-09 12:46:50 -0400 | [diff] [blame^] | 26 | * |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] | 27 | */ |
| 28 | int fQuality = 100; |
Matt Sarett | 2e61b18 | 2017-05-09 12:46:50 -0400 | [diff] [blame^] | 29 | |
| 30 | /** |
| 31 | * Jpegs must be opaque. This instructs the encoder on how to handle input |
| 32 | * images with alpha. |
| 33 | * |
| 34 | * The default is to ignore the alpha channel and treat the image as opaque. |
| 35 | * Another option is to blend the pixels onto a black background before encoding. |
| 36 | * In the second case, the encoder supports linear or legacy blending. |
| 37 | */ |
| 38 | AlphaOption fAlphaOption = AlphaOption::kIgnore; |
| 39 | SkTransferFunctionBehavior fBlendBehavior = SkTransferFunctionBehavior::kRespect; |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | /** |
| 43 | * Encode the |src| pixels to the |dst| stream. |
| 44 | * |options| may be used to control the encoding behavior. |
| 45 | * |
| 46 | * Returns true on success. Returns false on an invalid or unsupported |src|. |
| 47 | */ |
| 48 | static bool Encode(SkWStream* dst, const SkPixmap& src, const Options& options); |
| 49 | |
| 50 | /** |
| 51 | * Create a jpeg encoder that will encode the |src| pixels to the |dst| stream. |
| 52 | * |options| may be used to control the encoding behavior. |
| 53 | * |
| 54 | * |dst| is unowned but must remain valid for the lifetime of the object. |
| 55 | * |
| 56 | * This returns nullptr on an invalid or unsupported |src|. |
| 57 | */ |
| 58 | static std::unique_ptr<SkJpegEncoder> Make(SkWStream* dst, const SkPixmap& src, |
| 59 | const Options& options); |
| 60 | |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 61 | ~SkJpegEncoder() override; |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 62 | |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 63 | protected: |
| 64 | bool onEncodeRows(int numRows) override; |
| 65 | |
| 66 | private: |
| 67 | SkJpegEncoder(std::unique_ptr<SkJpegEncoderMgr>, const SkPixmap& src); |
| 68 | |
| 69 | std::unique_ptr<SkJpegEncoderMgr> fEncoderMgr; |
| 70 | typedef SkEncoder INHERITED; |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | #endif |