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