blob: fd7c2041610836b5f858dd1b7c38e83e4eb8e516 [file] [log] [blame]
Matt Sarett26b44df2017-05-02 16:04:56 -04001/*
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 Sarettc367d032017-05-05 11:13:26 -040011#include "SkEncoder.h"
Matt Sarett26b44df2017-05-02 16:04:56 -040012
Matt Sarettc367d032017-05-05 11:13:26 -040013class SkJpegEncoderMgr;
Matt Sarett26b44df2017-05-02 16:04:56 -040014class SkWStream;
15
Matt Sarett6a4dc662017-05-11 09:32:59 -040016class SK_API SkJpegEncoder : public SkEncoder {
Matt Sarett26b44df2017-05-02 16:04:56 -040017public:
18
Matt Sarett2e61b182017-05-09 12:46:50 -040019 enum class AlphaOption {
20 kIgnore,
21 kBlendOnBlack,
22 };
Matt Sarett26b44df2017-05-02 16:04:56 -040023
Matt Sarettfe319082017-05-09 14:02:10 -040024 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 Sarett26b44df2017-05-02 16:04:56 -040041 struct Options {
42 /**
Matt Sarett2e61b182017-05-09 12:46:50 -040043 * |fQuality| must be in [0, 100] where 0 corresponds to the lowest quality.
Matt Sarett26b44df2017-05-02 16:04:56 -040044 */
45 int fQuality = 100;
Matt Sarett2e61b182017-05-09 12:46:50 -040046
47 /**
Matt Sarettfe319082017-05-09 14:02:10 -040048 * 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 Sarett2e61b182017-05-09 12:46:50 -040056 * 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 Sarett26b44df2017-05-02 16:04:56 -040065 };
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 Sarett6a4dc662017-05-11 09:32:59 -040083 static std::unique_ptr<SkEncoder> Make(SkWStream* dst, const SkPixmap& src,
84 const Options& options);
Matt Sarett26b44df2017-05-02 16:04:56 -040085
Matt Sarettc367d032017-05-05 11:13:26 -040086 ~SkJpegEncoder() override;
Matt Sarett26b44df2017-05-02 16:04:56 -040087
Matt Sarettc367d032017-05-05 11:13:26 -040088protected:
89 bool onEncodeRows(int numRows) override;
90
91private:
92 SkJpegEncoder(std::unique_ptr<SkJpegEncoderMgr>, const SkPixmap& src);
93
94 std::unique_ptr<SkJpegEncoderMgr> fEncoderMgr;
95 typedef SkEncoder INHERITED;
Matt Sarett26b44df2017-05-02 16:04:56 -040096};
97
98#endif