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