blob: 07290aeb9f60afab4af5ec6f753bea1099cb01a7 [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 Sarettc367d032017-05-05 11:13:26 -040016class 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
24 struct Options {
25 /**
Matt Sarett2e61b182017-05-09 12:46:50 -040026 * |fQuality| must be in [0, 100] where 0 corresponds to the lowest quality.
Matt Sarett26b44df2017-05-02 16:04:56 -040027 */
28 int fQuality = 100;
Matt Sarett2e61b182017-05-09 12:46:50 -040029
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 Sarett26b44df2017-05-02 16:04:56 -040040 };
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 Sarettc367d032017-05-05 11:13:26 -040061 ~SkJpegEncoder() override;
Matt Sarett26b44df2017-05-02 16:04:56 -040062
Matt Sarettc367d032017-05-05 11:13:26 -040063protected:
64 bool onEncodeRows(int numRows) override;
65
66private:
67 SkJpegEncoder(std::unique_ptr<SkJpegEncoderMgr>, const SkPixmap& src);
68
69 std::unique_ptr<SkJpegEncoderMgr> fEncoderMgr;
70 typedef SkEncoder INHERITED;
Matt Sarett26b44df2017-05-02 16:04:56 -040071};
72
73#endif