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