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