blob: 5e657a5b6aae1c0bc063ec89b8889c7cb52e709e [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;
Matt Sarett26b44df2017-05-02 16:04:56 -040064 };
65
66 /**
67 * Encode the |src| pixels to the |dst| stream.
68 * |options| may be used to control the encoding behavior.
69 *
70 * Returns true on success. Returns false on an invalid or unsupported |src|.
71 */
72 static bool Encode(SkWStream* dst, const SkPixmap& src, const Options& options);
73
74 /**
75 * Create a jpeg encoder that will encode the |src| pixels to the |dst| stream.
76 * |options| may be used to control the encoding behavior.
77 *
78 * |dst| is unowned but must remain valid for the lifetime of the object.
79 *
80 * This returns nullptr on an invalid or unsupported |src|.
81 */
Matt Sarett6a4dc662017-05-11 09:32:59 -040082 static std::unique_ptr<SkEncoder> Make(SkWStream* dst, const SkPixmap& src,
83 const Options& options);
Matt Sarett26b44df2017-05-02 16:04:56 -040084
Matt Sarettc367d032017-05-05 11:13:26 -040085 ~SkJpegEncoder() override;
Matt Sarett26b44df2017-05-02 16:04:56 -040086
Matt Sarettc367d032017-05-05 11:13:26 -040087protected:
88 bool onEncodeRows(int numRows) override;
89
90private:
91 SkJpegEncoder(std::unique_ptr<SkJpegEncoderMgr>, const SkPixmap& src);
92
93 std::unique_ptr<SkJpegEncoderMgr> fEncoderMgr;
94 typedef SkEncoder INHERITED;
Matt Sarett26b44df2017-05-02 16:04:56 -040095};
96
97#endif