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