1 /*
2 * Copyright 2009 The Android Open Source Project
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 #include "SkImageEncoderPriv.h"
9 #include "SkJpegEncoder.h"
10 #include "SkPngEncoder.h"
11 #include "SkWebpEncoder.h"
12
13 #ifndef SK_HAS_JPEG_LIBRARY
Encode(SkWStream *,const SkPixmap &,const Options &)14 bool SkJpegEncoder::Encode(SkWStream*, const SkPixmap&, const Options&) { return false; }
Make(SkWStream *,const SkPixmap &,const Options &)15 std::unique_ptr<SkEncoder> SkJpegEncoder::Make(SkWStream*, const SkPixmap&, const Options&) {
16 return nullptr;
17 }
18 #endif
19
20 #ifndef SK_HAS_PNG_LIBRARY
Encode(SkWStream *,const SkPixmap &,const Options &)21 bool SkPngEncoder::Encode(SkWStream*, const SkPixmap&, const Options&) { return false; }
Make(SkWStream *,const SkPixmap &,const Options &)22 std::unique_ptr<SkEncoder> SkPngEncoder::Make(SkWStream*, const SkPixmap&, const Options&) {
23 return nullptr;
24 }
25 #endif
26
27 #ifndef SK_HAS_WEBP_LIBRARY
Encode(SkWStream *,const SkPixmap &,const Options &)28 bool SkWebpEncoder::Encode(SkWStream*, const SkPixmap&, const Options&) { return false; }
29 #endif
30
SkEncodeImage(SkWStream * dst,const SkPixmap & src,SkEncodedImageFormat format,int quality)31 bool SkEncodeImage(SkWStream* dst, const SkPixmap& src,
32 SkEncodedImageFormat format, int quality) {
33 #ifdef SK_USE_CG_ENCODER
34 (void)quality;
35 return SkEncodeImageWithCG(dst, src, format);
36 #elif SK_USE_WIC_ENCODER
37 return SkEncodeImageWithWIC(dst, src, format, quality);
38 #else
39 switch(format) {
40 case SkEncodedImageFormat::kJPEG: {
41 SkJpegEncoder::Options opts;
42 opts.fQuality = quality;
43 return SkJpegEncoder::Encode(dst, src, opts);
44 }
45 case SkEncodedImageFormat::kPNG: {
46 SkPngEncoder::Options opts;
47 return SkPngEncoder::Encode(dst, src, opts);
48 }
49 case SkEncodedImageFormat::kWEBP: {
50 SkWebpEncoder::Options opts;
51 opts.fCompression = SkWebpEncoder::Compression::kLossy;
52 opts.fQuality = quality;
53 return SkWebpEncoder::Encode(dst, src, opts);
54 }
55 default:
56 return false;
57 }
58 #endif
59 }
60
encodeRows(int numRows)61 bool SkEncoder::encodeRows(int numRows) {
62 SkASSERT(numRows > 0 && fCurrRow < fSrc.height());
63 if (numRows <= 0 || fCurrRow >= fSrc.height()) {
64 return false;
65 }
66
67 if (fCurrRow + numRows > fSrc.height()) {
68 numRows = fSrc.height() - fCurrRow;
69 }
70
71 if (!this->onEncodeRows(numRows)) {
72 // If we fail, short circuit any future calls.
73 fCurrRow = fSrc.height();
74 return false;
75 }
76
77 return true;
78 }
79
SkEncodePixmap(const SkPixmap & src,SkEncodedImageFormat format,int quality)80 sk_sp<SkData> SkEncodePixmap(const SkPixmap& src, SkEncodedImageFormat format, int quality) {
81 SkDynamicMemoryWStream stream;
82 return SkEncodeImage(&stream, src, format, quality) ? stream.detachAsData() : nullptr;
83 }
84
SkEncodeBitmap(const SkBitmap & src,SkEncodedImageFormat format,int quality)85 sk_sp<SkData> SkEncodeBitmap(const SkBitmap& src, SkEncodedImageFormat format, int quality) {
86 SkPixmap pixmap;
87 return src.peekPixels(&pixmap) ? SkEncodePixmap(pixmap, format, quality) : nullptr;
88 }
89