blob: 49b49786c95a6436bb366ca26a52c991f30daf3f [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
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 Sarettc367d032017-05-05 11:13:26 -040051 ~SkJpegEncoder() override;
Matt Sarett26b44df2017-05-02 16:04:56 -040052
Matt Sarettc367d032017-05-05 11:13:26 -040053protected:
54 bool onEncodeRows(int numRows) override;
55
56private:
57 SkJpegEncoder(std::unique_ptr<SkJpegEncoderMgr>, const SkPixmap& src);
58
59 std::unique_ptr<SkJpegEncoderMgr> fEncoderMgr;
60 typedef SkEncoder INHERITED;
Matt Sarett26b44df2017-05-02 16:04:56 -040061};
62
63#endif