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