1 /*
2  * Copyright 2011 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 SkImageEncoder_DEFINED
9 #define SkImageEncoder_DEFINED
10 
11 // TODO: update clients so we can remove this include, they should IWYU
12 #include "include/core/SkBitmap.h"
13 
14 #include "include/core/SkData.h"
15 #include "include/core/SkEncodedImageFormat.h"
16 #include "include/core/SkPixmap.h"
17 #include "include/core/SkStream.h"
18 
19 class SkBitmap;
20 
21 /**
22  * Encode SkPixmap in the given binary image format.
23  *
24  * @param  dst     results are written to this stream.
25  * @param  src     source pixels.
26  * @param  format  image format, not all formats are supported.
27  * @param  quality range from 0-100, this is supported by jpeg and webp.
28  *                 higher values correspond to improved visual quality, but less compression.
29  *
30  * @return false iff input is bad or format is unsupported.
31  *
32  * Will always return false if Skia is compiled without image
33  * encoders.
34  *
35  * For SkEncodedImageFormat::kWEBP, if quality is 100, it will use lossless compression. Otherwise
36  * it will use lossy.
37  *
38  * For examples of encoding an image to a file or to a block of memory,
39  * see tools/ToolUtils.h.
40  */
41 SK_API bool SkEncodeImage(SkWStream* dst, const SkPixmap& src,
42                           SkEncodedImageFormat format, int quality);
43 
44 /**
45  * The following helper function wraps SkEncodeImage().
46  */
47 SK_API bool SkEncodeImage(SkWStream* dst, const SkBitmap& src, SkEncodedImageFormat f, int q);
48 
49 /**
50  * Encode SkPixmap in the given binary image format.
51  *
52  * @param  src     source pixels.
53  * @param  format  image format, not all formats are supported.
54  * @param  quality range from 0-100, this is supported by jpeg and webp.
55  *                 higher values correspond to improved visual quality, but less compression.
56  *
57  * @return encoded data or nullptr if input is bad or format is unsupported.
58  *
59  * Will always return nullptr if Skia is compiled without image
60  * encoders.
61  *
62  * For SkEncodedImageFormat::kWEBP, if quality is 100, it will use lossless compression. Otherwise
63  * it will use lossy.
64  */
65 SK_API sk_sp<SkData> SkEncodePixmap(const SkPixmap& src, SkEncodedImageFormat format, int quality);
66 
67 /**
68  *  Helper that extracts the pixmap from the bitmap, and then calls SkEncodePixmap()
69  */
70 SK_API sk_sp<SkData> SkEncodeBitmap(const SkBitmap& src, SkEncodedImageFormat format, int quality);
71 
72 #endif  // SkImageEncoder_DEFINED
73