reed@android.com | b08eb2b | 2009-01-06 20:16:26 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2009 The Android Open Source Project |
reed@android.com | b08eb2b | 2009-01-06 20:16:26 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | b08eb2b | 2009-01-06 20:16:26 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 8 | #include "SkImageEncoderPriv.h" |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 9 | #include "SkJpegEncoder.h" |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 10 | #include "SkPngEncoder.h" |
Matt Sarett | 04c3731 | 2017-05-05 14:02:13 -0400 | [diff] [blame] | 11 | #include "SkWebpEncoder.h" |
reed@android.com | b08eb2b | 2009-01-06 20:16:26 +0000 | [diff] [blame] | 12 | |
Matt Sarett | 22469d9 | 2017-05-05 15:40:05 -0400 | [diff] [blame] | 13 | #ifndef SK_HAS_JPEG_LIBRARY |
| 14 | bool SkJpegEncoder::Encode(SkWStream*, const SkPixmap&, const Options&) { return false; } |
Matt Sarett | e6c210a | 2017-05-11 17:39:04 -0400 | [diff] [blame] | 15 | std::unique_ptr<SkEncoder> SkJpegEncoder::Make(SkWStream*, const SkPixmap&, const Options&) { |
Matt Sarett | 22469d9 | 2017-05-05 15:40:05 -0400 | [diff] [blame] | 16 | return nullptr; |
| 17 | } |
| 18 | #endif |
| 19 | |
| 20 | #ifndef SK_HAS_PNG_LIBRARY |
| 21 | bool SkPngEncoder::Encode(SkWStream*, const SkPixmap&, const Options&) { return false; } |
Matt Sarett | e6c210a | 2017-05-11 17:39:04 -0400 | [diff] [blame] | 22 | std::unique_ptr<SkEncoder> SkPngEncoder::Make(SkWStream*, const SkPixmap&, const Options&) { |
Matt Sarett | 22469d9 | 2017-05-05 15:40:05 -0400 | [diff] [blame] | 23 | return nullptr; |
| 24 | } |
| 25 | #endif |
| 26 | |
| 27 | #ifndef SK_HAS_WEBP_LIBRARY |
| 28 | bool SkWebpEncoder::Encode(SkWStream*, const SkPixmap&, const Options&) { return false; } |
| 29 | #endif |
| 30 | |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 31 | bool SkEncodeImage(SkWStream* dst, const SkPixmap& src, |
| 32 | SkEncodedImageFormat format, int quality) { |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 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) { |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 40 | case SkEncodedImageFormat::kJPEG: { |
| 41 | SkJpegEncoder::Options opts; |
| 42 | opts.fQuality = quality; |
| 43 | return SkJpegEncoder::Encode(dst, src, opts); |
| 44 | } |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 45 | case SkEncodedImageFormat::kPNG: { |
| 46 | SkPngEncoder::Options opts; |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 47 | return SkPngEncoder::Encode(dst, src, opts); |
| 48 | } |
Matt Sarett | 04c3731 | 2017-05-05 14:02:13 -0400 | [diff] [blame] | 49 | case SkEncodedImageFormat::kWEBP: { |
| 50 | SkWebpEncoder::Options opts; |
Leon Scroggins III | e7f165b | 2018-12-10 10:29:18 -0500 | [diff] [blame] | 51 | if (quality == 100) { |
| 52 | opts.fCompression = SkWebpEncoder::Compression::kLossless; |
| 53 | // Note: SkEncodeImage treats 0 quality as the lowest quality |
| 54 | // (greatest compression) and 100 as the highest quality (least |
| 55 | // compression). For kLossy, this matches libwebp's |
| 56 | // interpretation, so it is passed directly to libwebp. But |
| 57 | // with kLossless, libwebp always creates the highest quality |
| 58 | // image. In this case, fQuality is reinterpreted as how much |
| 59 | // effort (time) to put into making a smaller file. This API |
| 60 | // does not provide a way to specify this value (though it can |
| 61 | // be specified by using SkWebpEncoder::Encode) so we have to |
| 62 | // pick one arbitrarily. This value matches that chosen by |
| 63 | // blink::ImageEncoder::ComputeWebpOptions as well |
| 64 | // WebPConfigInit. |
| 65 | opts.fQuality = 75; |
| 66 | } else { |
| 67 | opts.fCompression = SkWebpEncoder::Compression::kLossy; |
| 68 | opts.fQuality = quality; |
| 69 | } |
Matt Sarett | 04c3731 | 2017-05-05 14:02:13 -0400 | [diff] [blame] | 70 | return SkWebpEncoder::Encode(dst, src, opts); |
| 71 | } |
Matt Sarett | 84014f0 | 2017-01-10 11:28:54 -0500 | [diff] [blame] | 72 | default: |
| 73 | return false; |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 74 | } |
| 75 | #endif |
reed | 53b9e2e | 2014-12-19 12:26:07 -0800 | [diff] [blame] | 76 | } |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 77 | |
| 78 | bool SkEncoder::encodeRows(int numRows) { |
| 79 | SkASSERT(numRows > 0 && fCurrRow < fSrc.height()); |
| 80 | if (numRows <= 0 || fCurrRow >= fSrc.height()) { |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | if (fCurrRow + numRows > fSrc.height()) { |
| 85 | numRows = fSrc.height() - fCurrRow; |
| 86 | } |
| 87 | |
| 88 | if (!this->onEncodeRows(numRows)) { |
| 89 | // If we fail, short circuit any future calls. |
| 90 | fCurrRow = fSrc.height(); |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | return true; |
| 95 | } |
Mike Reed | 25eef6b | 2017-12-08 16:20:58 -0500 | [diff] [blame] | 96 | |
| 97 | sk_sp<SkData> SkEncodePixmap(const SkPixmap& src, SkEncodedImageFormat format, int quality) { |
| 98 | SkDynamicMemoryWStream stream; |
| 99 | return SkEncodeImage(&stream, src, format, quality) ? stream.detachAsData() : nullptr; |
| 100 | } |
| 101 | |
| 102 | sk_sp<SkData> SkEncodeBitmap(const SkBitmap& src, SkEncodedImageFormat format, int quality) { |
| 103 | SkPixmap pixmap; |
| 104 | return src.peekPixels(&pixmap) ? SkEncodePixmap(pixmap, format, quality) : nullptr; |
| 105 | } |