blob: 21613f90f0e1632289e1e5cbd49186afc0406d9d [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
11#include "SkPixmap.h"
12#include "SkTypes.h"
13
14class SkWStream;
15
16class SkJpegEncoder : SkNoncopyable {
17public:
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
51 /**
52 * Encode |numRows| rows of input. If the caller requests more rows than are remaining
53 * in the src, this will encode all of the remaining rows. |numRows| must be greater
54 * than zero.
55 */
56 bool encodeRows(int numRows);
57
58 virtual ~SkJpegEncoder() {}
59};
60
61#endif