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