| scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | */ |
| reed | bfefc7c | 2014-06-12 17:40:00 -0700 | [diff] [blame] | 7 | |
| Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkColorPriv.h" |
| 10 | #include "include/core/SkStream.h" |
| 11 | #include "include/core/SkString.h" |
| 12 | #include "include/core/SkTypes.h" |
| 13 | #include "include/effects/SkBlurDrawLooper.h" |
| 14 | #include "samplecode/DecodeFile.h" |
| 15 | #include "samplecode/Sample.h" |
| 16 | #include "src/core/SkBlurMask.h" |
| 17 | #include "src/core/SkOSFile.h" |
| 18 | #include "src/utils/SkOSPath.h" |
| 19 | #include "src/utils/SkUTF.h" |
| 20 | #include "tools/Resources.h" |
| 21 | #include "tools/ToolUtils.h" |
| scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 22 | |
| Mike Reed | b7dad44 | 2019-07-22 14:51:10 -0400 | [diff] [blame] | 23 | #ifdef SK_SUPPORT_LEGACY_DRAWLOOPER |
| 24 | |
| scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 25 | /** |
| 26 | * Interprets c as an unpremultiplied color, and returns the |
| 27 | * premultiplied equivalent. |
| 28 | */ |
| 29 | static SkPMColor premultiply_unpmcolor(SkPMColor c) { |
| 30 | U8CPU a = SkGetPackedA32(c); |
| 31 | U8CPU r = SkGetPackedR32(c); |
| 32 | U8CPU g = SkGetPackedG32(c); |
| 33 | U8CPU b = SkGetPackedB32(c); |
| 34 | return SkPreMultiplyARGB(a, r, g, b); |
| 35 | } |
| 36 | |
| Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 37 | class UnpremulView : public Sample { |
| scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 38 | public: |
| 39 | UnpremulView(SkString res) |
| 40 | : fResPath(res) |
| 41 | , fPremul(true) |
| 42 | , fDecodeSucceeded(false) { |
| 43 | this->nextImage(); |
| 44 | } |
| 45 | |
| 46 | protected: |
| Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 47 | SkString name() override { return SkString("unpremul"); } |
| 48 | |
| Hal Canary | 6cc65e1 | 2019-07-03 15:53:04 -0400 | [diff] [blame] | 49 | bool onChar(SkUnichar uni) override { |
| Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 50 | char utf8[SkUTF::kMaxBytesInUTF8Sequence]; |
| 51 | size_t size = SkUTF::ToUTF8(uni, utf8); |
| scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 52 | // Only consider events for single char keys |
| 53 | if (1 == size) { |
| 54 | switch (utf8[0]) { |
| 55 | case fNextImageChar: |
| 56 | this->nextImage(); |
| 57 | return true; |
| 58 | case fTogglePremulChar: |
| 59 | this->togglePremul(); |
| 60 | return true; |
| 61 | default: |
| 62 | break; |
| 63 | } |
| 64 | } |
| Hal Canary | 6cc65e1 | 2019-07-03 15:53:04 -0400 | [diff] [blame] | 65 | return false; |
| scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 68 | void onDrawBackground(SkCanvas* canvas) override { |
| Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 69 | ToolUtils::draw_checkerboard(canvas, 0xFFCCCCCC, 0xFFFFFFFF, 12); |
| scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 72 | void onDrawContent(SkCanvas* canvas) override { |
| scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 73 | SkPaint paint; |
| 74 | paint.setAntiAlias(true); |
| Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 75 | |
| 76 | SkFont font; |
| 77 | font.setSize(24); |
| reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 78 | auto looper( |
| 79 | SkBlurDrawLooper::Make(SK_ColorBLUE, SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(2)), |
| 80 | 0, 0)); |
| scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 81 | paint.setLooper(looper); |
| Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 82 | SkScalar height = font.getMetrics(nullptr); |
| scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 83 | if (!fDecodeSucceeded) { |
| 84 | SkString failure; |
| 85 | if (fResPath.size() == 0) { |
| 86 | failure.printf("resource path is required!"); |
| 87 | } else { |
| 88 | failure.printf("Failed to decode %s", fCurrFile.c_str()); |
| 89 | } |
| Hal Canary | 89a644b | 2019-01-07 09:36:09 -0500 | [diff] [blame] | 90 | canvas->drawString(failure, 0, height, font, paint); |
| scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 91 | return; |
| 92 | } |
| 93 | |
| 94 | // Name, size of the file, and whether or not it is premultiplied. |
| tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 95 | SkString header(SkOSPath::Basename(fCurrFile.c_str())); |
| scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 96 | header.appendf(" [%dx%d] %s", fBitmap.width(), fBitmap.height(), |
| 97 | (fPremul ? "premultiplied" : "unpremultiplied")); |
| Hal Canary | 89a644b | 2019-01-07 09:36:09 -0500 | [diff] [blame] | 98 | canvas->drawString(header, 0, height, font, paint); |
| scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 99 | canvas->translate(0, height); |
| 100 | |
| 101 | // Help messages |
| 102 | header.printf("Press '%c' to move to the next image.'", fNextImageChar); |
| Hal Canary | 89a644b | 2019-01-07 09:36:09 -0500 | [diff] [blame] | 103 | canvas->drawString(header, 0, height, font, paint); |
| scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 104 | canvas->translate(0, height); |
| 105 | |
| 106 | header.printf("Press '%c' to toggle premultiplied decode.", fTogglePremulChar); |
| Hal Canary | 89a644b | 2019-01-07 09:36:09 -0500 | [diff] [blame] | 107 | canvas->drawString(header, 0, height, font, paint); |
| scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 108 | |
| 109 | // Now draw the image itself. |
| 110 | canvas->translate(height * 2, height * 2); |
| 111 | if (!fPremul) { |
| 112 | // A premultiplied bitmap cannot currently be drawn. |
| scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 113 | // Copy it to a bitmap which can be drawn, converting |
| 114 | // to premultiplied: |
| 115 | SkBitmap bm; |
| reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 116 | bm.allocN32Pixels(fBitmap.width(), fBitmap.height()); |
| scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 117 | for (int i = 0; i < fBitmap.width(); ++i) { |
| 118 | for (int j = 0; j < fBitmap.height(); ++j) { |
| 119 | *bm.getAddr32(i, j) = premultiply_unpmcolor(*fBitmap.getAddr32(i, j)); |
| 120 | } |
| 121 | } |
| 122 | canvas->drawBitmap(bm, 0, 0); |
| 123 | } else { |
| 124 | canvas->drawBitmap(fBitmap, 0, 0); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | private: |
| 129 | const SkString fResPath; |
| 130 | SkString fCurrFile; |
| 131 | bool fPremul; |
| 132 | bool fDecodeSucceeded; |
| 133 | SkBitmap fBitmap; |
| 134 | SkOSFile::Iter fFileIter; |
| 135 | |
| 136 | static const char fNextImageChar = 'j'; |
| 137 | static const char fTogglePremulChar = 'h'; |
| 138 | |
| 139 | void nextImage() { |
| 140 | if (fResPath.size() == 0) { |
| 141 | return; |
| 142 | } |
| 143 | SkString basename; |
| 144 | if (!fFileIter.next(&basename)) { |
| 145 | fFileIter.reset(fResPath.c_str()); |
| 146 | if (!fFileIter.next(&basename)) { |
| 147 | // Perhaps this should draw some error message? |
| 148 | return; |
| 149 | } |
| 150 | } |
| tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 151 | fCurrFile = SkOSPath::Join(fResPath.c_str(), basename.c_str()); |
| scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 152 | this->decodeCurrFile(); |
| 153 | } |
| 154 | |
| 155 | void decodeCurrFile() { |
| 156 | if (fCurrFile.size() == 0) { |
| 157 | fDecodeSucceeded = false; |
| 158 | return; |
| 159 | } |
| msarett | d15750c | 2016-03-18 15:48:49 -0700 | [diff] [blame] | 160 | fDecodeSucceeded = decode_file(fCurrFile.c_str(), &fBitmap, kN32_SkColorType, !fPremul); |
| scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void togglePremul() { |
| 164 | fPremul = !fPremul; |
| 165 | this->decodeCurrFile(); |
| 166 | } |
| 167 | |
| Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 168 | typedef Sample INHERITED; |
| scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | ////////////////////////////////////////////////////////////////////////////// |
| 172 | |
| Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 173 | DEF_SAMPLE( return new UnpremulView(GetResourcePath("images")); ) |
| Mike Reed | b7dad44 | 2019-07-22 14:51:10 -0400 | [diff] [blame] | 174 | |
| 175 | #endif |