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 | |
| 19 | // TODO (skbug.com/1501): |
| 20 | // Since jpegs are always opaque, this encoder ignores the alpha channel and treats the |
| 21 | // pixels as opaque. |
| 22 | // Another possible behavior is to blend the pixels onto opaque black. We'll need to add |
| 23 | // an option for this - and an SkTransferFunctionBehavior. |
| 24 | |
| 25 | struct Options { |
| 26 | /** |
| 27 | * |fQuality| must be in [0, 100] where 0 corresponds to the lowest quality. |
| 28 | */ |
| 29 | int fQuality = 100; |
| 30 | }; |
| 31 | |
| 32 | /** |
| 33 | * Encode the |src| pixels to the |dst| stream. |
| 34 | * |options| may be used to control the encoding behavior. |
| 35 | * |
| 36 | * Returns true on success. Returns false on an invalid or unsupported |src|. |
| 37 | */ |
| 38 | static bool Encode(SkWStream* dst, const SkPixmap& src, const Options& options); |
| 39 | |
| 40 | /** |
| 41 | * Create a jpeg encoder that will encode the |src| pixels to the |dst| stream. |
| 42 | * |options| may be used to control the encoding behavior. |
| 43 | * |
| 44 | * |dst| is unowned but must remain valid for the lifetime of the object. |
| 45 | * |
| 46 | * This returns nullptr on an invalid or unsupported |src|. |
| 47 | */ |
| 48 | static std::unique_ptr<SkJpegEncoder> Make(SkWStream* dst, const SkPixmap& src, |
| 49 | const Options& options); |
| 50 | |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame^] | 51 | ~SkJpegEncoder() override; |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 52 | |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame^] | 53 | protected: |
| 54 | bool onEncodeRows(int numRows) override; |
| 55 | |
| 56 | private: |
| 57 | SkJpegEncoder(std::unique_ptr<SkJpegEncoderMgr>, const SkPixmap& src); |
| 58 | |
| 59 | std::unique_ptr<SkJpegEncoderMgr> fEncoderMgr; |
| 60 | typedef SkEncoder INHERITED; |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | #endif |