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) { |
| 23 | GetResourceAsBitmap("mandrill_256.png", bitmap); |
| 24 | } |
| 25 | |
| 26 | static void make_premul_256(SkBitmap* bitmap) { |
| 27 | SkBitmap tmp; |
| 28 | GetResourceAsBitmap("yellow_rose.png", &tmp); |
| 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: { |
| 75 | SkPngEncoder::Options options; |
| 76 | options.fUnpremulBehavior = SkTransferFunctionBehavior::kIgnore; |
| 77 | bool success = SkPngEncoder::Encode(&buf, src, options); |
| 78 | return success ? buf.detachAsData() : nullptr; |
| 79 | } |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 80 | case SkEncodedImageFormat::kJPEG: { |
| 81 | bool success = SkJpegEncoder::Encode(&buf, src, SkJpegEncoder::Options()); |
| 82 | return success ? buf.detachAsData() : nullptr; |
| 83 | } |
Matt Sarett | 04c3731 | 2017-05-05 14:02:13 -0400 | [diff] [blame] | 84 | case SkEncodedImageFormat::kWEBP: { |
| 85 | SkWebpEncoder::Options options; |
| 86 | options.fUnpremulBehavior = SkTransferFunctionBehavior::kIgnore; |
| 87 | bool success = SkWebpEncoder::Encode(&buf, src, options); |
| 88 | return success ? buf.detachAsData() : nullptr; |
| 89 | } |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 90 | default: |
| 91 | SkASSERT(false); |
| 92 | return nullptr; |
| 93 | } |
| 94 | #endif |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 95 | } |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 96 | |
| 97 | class EncodePlatformGM : public GM { |
| 98 | public: |
| 99 | EncodePlatformGM() {} |
| 100 | |
| 101 | protected: |
| 102 | SkString onShortName() override { |
| 103 | return SkString("encode-platform"); |
| 104 | } |
| 105 | |
| 106 | SkISize onISize() override { |
| 107 | return SkISize::Make(256 * SK_ARRAY_COUNT(kTypes), 256 * 3); |
| 108 | } |
| 109 | |
| 110 | void onDraw(SkCanvas* canvas) override { |
| 111 | SkBitmap opaqueBm, premulBm, unpremulBm; |
| 112 | make_opaque_256(&opaqueBm); |
| 113 | make_premul_256(&premulBm); |
| 114 | make_unpremul_256(&unpremulBm); |
| 115 | |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 116 | for (SkEncodedImageFormat type : kTypes) { |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 117 | auto opaqueImage = SkImage::MakeFromEncoded(encode_data(type, opaqueBm)); |
| 118 | auto premulImage = SkImage::MakeFromEncoded(encode_data(type, premulBm)); |
| 119 | auto unpremulImage = SkImage::MakeFromEncoded(encode_data(type, unpremulBm)); |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 120 | |
| 121 | canvas->drawImage(opaqueImage.get(), 0.0f, 0.0f); |
| 122 | canvas->drawImage(premulImage.get(), 0.0f, 256.0f); |
| 123 | canvas->drawImage(unpremulImage.get(), 0.0f, 512.0f); |
| 124 | |
| 125 | canvas->translate(256.0f, 0.0f); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | private: |
| 130 | typedef GM INHERITED; |
| 131 | }; |
| 132 | |
| 133 | DEF_GM( return new EncodePlatformGM; ) |
| 134 | } |