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