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