msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | #include "gm.h" |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 9 | |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 10 | #include "Resources.h" |
| 11 | #include "SkCanvas.h" |
| 12 | #include "SkData.h" |
Florin Malita | ab244f0 | 2017-05-03 19:16:58 +0000 | [diff] [blame] | 13 | #include "SkImage.h" |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 14 | #include "SkImageEncoderPriv.h" |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 15 | #include "SkJpegEncoder.h" |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 16 | #include "SkPngEncoder.h" |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 17 | #include "SkUnPreMultiply.h" |
Matt Sarett | 04c3731 | 2017-05-05 14:02:13 -0400 | [diff] [blame] | 18 | #include "SkWebpEncoder.h" |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 19 | |
| 20 | namespace skiagm { |
| 21 | |
| 22 | static void make_opaque_256(SkBitmap* bitmap) { |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 23 | GetResourceAsBitmap("images/mandrill_256.png", bitmap); |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | static void make_premul_256(SkBitmap* bitmap) { |
| 27 | SkBitmap tmp; |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 28 | GetResourceAsBitmap("images/yellow_rose.png", &tmp); |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 29 | tmp.extractSubset(bitmap, SkIRect::MakeWH(256, 256)); |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | static void make_unpremul_256(SkBitmap* bitmap) { |
| 33 | make_premul_256(bitmap); |
| 34 | for (int y = 0; y < bitmap->height(); y++) { |
| 35 | for (int x = 0; x < bitmap->width(); x++) { |
| 36 | SkPMColor* pixel = bitmap->getAddr32(x, y); |
| 37 | *pixel = SkUnPreMultiply::UnPreMultiplyPreservingByteOrder(*pixel); |
| 38 | } |
| 39 | } |
| 40 | bitmap->setAlphaType(kUnpremul_SkAlphaType); |
| 41 | } |
| 42 | |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 43 | #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 44 | static SkEncodedImageFormat kTypes[] { |
| 45 | SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kGIF, |
| 46 | SkEncodedImageFormat::kBMP, SkEncodedImageFormat::kICO, |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 47 | }; |
| 48 | #elif defined(SK_BUILD_FOR_WIN) |
| 49 | // Use PNG multiple times because our WIC encoder does not support GIF, BMP, or ICO. |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 50 | static SkEncodedImageFormat kTypes[] { |
| 51 | SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG, |
| 52 | SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kPNG, |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 53 | }; |
| 54 | #else |
| 55 | // Use WEBP in place of GIF. Use PNG two extra times. We don't support GIF, BMP, or ICO. |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 56 | static SkEncodedImageFormat kTypes[] { |
| 57 | SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kWEBP, |
| 58 | SkEncodedImageFormat::kPNG, SkEncodedImageFormat::kPNG, |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 59 | }; |
| 60 | #endif |
| 61 | |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 62 | static sk_sp<SkData> encode_data(SkEncodedImageFormat type, const SkBitmap& bitmap) { |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 63 | SkPixmap src; |
| 64 | if (!bitmap.peekPixels(&src)) { |
| 65 | return nullptr; |
| 66 | } |
| 67 | SkDynamicMemoryWStream buf; |
| 68 | #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
| 69 | return SkEncodeImageWithCG(&buf, src, type) ? buf.detachAsData() : nullptr; |
| 70 | #elif defined(SK_BUILD_FOR_WIN) |
| 71 | return SkEncodeImageWithWIC(&buf, src, type, 100) ? buf.detachAsData() : nullptr; |
| 72 | #else |
| 73 | switch (type) { |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 74 | case SkEncodedImageFormat::kPNG: { |
Brian Osman | b62f50c | 2018-07-12 14:44:27 -0400 | [diff] [blame^] | 75 | bool success = SkPngEncoder::Encode(&buf, src, SkPngEncoder::Options()); |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 76 | return success ? buf.detachAsData() : nullptr; |
| 77 | } |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 78 | case SkEncodedImageFormat::kJPEG: { |
| 79 | bool success = SkJpegEncoder::Encode(&buf, src, SkJpegEncoder::Options()); |
| 80 | return success ? buf.detachAsData() : nullptr; |
| 81 | } |
Matt Sarett | 04c3731 | 2017-05-05 14:02:13 -0400 | [diff] [blame] | 82 | case SkEncodedImageFormat::kWEBP: { |
Brian Osman | b62f50c | 2018-07-12 14:44:27 -0400 | [diff] [blame^] | 83 | bool success = SkWebpEncoder::Encode(&buf, src, SkWebpEncoder::Options()); |
Matt Sarett | 04c3731 | 2017-05-05 14:02:13 -0400 | [diff] [blame] | 84 | return success ? buf.detachAsData() : nullptr; |
| 85 | } |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 86 | default: |
| 87 | SkASSERT(false); |
| 88 | return nullptr; |
| 89 | } |
| 90 | #endif |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 91 | } |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 92 | |
| 93 | class EncodePlatformGM : public GM { |
| 94 | public: |
| 95 | EncodePlatformGM() {} |
| 96 | |
| 97 | protected: |
| 98 | SkString onShortName() override { |
| 99 | return SkString("encode-platform"); |
| 100 | } |
| 101 | |
| 102 | SkISize onISize() override { |
| 103 | return SkISize::Make(256 * SK_ARRAY_COUNT(kTypes), 256 * 3); |
| 104 | } |
| 105 | |
| 106 | void onDraw(SkCanvas* canvas) override { |
| 107 | SkBitmap opaqueBm, premulBm, unpremulBm; |
| 108 | make_opaque_256(&opaqueBm); |
| 109 | make_premul_256(&premulBm); |
| 110 | make_unpremul_256(&unpremulBm); |
| 111 | |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 112 | for (SkEncodedImageFormat type : kTypes) { |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 113 | auto opaqueImage = SkImage::MakeFromEncoded(encode_data(type, opaqueBm)); |
| 114 | auto premulImage = SkImage::MakeFromEncoded(encode_data(type, premulBm)); |
| 115 | auto unpremulImage = SkImage::MakeFromEncoded(encode_data(type, unpremulBm)); |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 116 | |
| 117 | canvas->drawImage(opaqueImage.get(), 0.0f, 0.0f); |
| 118 | canvas->drawImage(premulImage.get(), 0.0f, 256.0f); |
| 119 | canvas->drawImage(unpremulImage.get(), 0.0f, 512.0f); |
| 120 | |
| 121 | canvas->translate(256.0f, 0.0f); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | private: |
| 126 | typedef GM INHERITED; |
| 127 | }; |
| 128 | |
| 129 | DEF_GM( return new EncodePlatformGM; ) |
| 130 | } |