blob: 383306840837eaba62d09d0fbd4d5d29a9eac86c [file] [log] [blame]
scroggo@google.com2bbc2c92013-06-14 15:33:20 +00001/*
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 */
reedbfefc7c2014-06-12 17:40:00 -07007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#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.com2bbc2c92013-06-14 15:33:20 +000022
Mike Reedb7dad442019-07-22 14:51:10 -040023#ifdef SK_SUPPORT_LEGACY_DRAWLOOPER
24
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000025/**
26 * Interprets c as an unpremultiplied color, and returns the
27 * premultiplied equivalent.
28 */
29static SkPMColor premultiply_unpmcolor(SkPMColor c) {
30 U8CPU a = SkGetPackedA32(c);
31 U8CPU r = SkGetPackedR32(c);
32 U8CPU g = SkGetPackedG32(c);
33 U8CPU b = SkGetPackedB32(c);
34 return SkPreMultiplyARGB(a, r, g, b);
35}
36
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040037class UnpremulView : public Sample {
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000038public:
39 UnpremulView(SkString res)
40 : fResPath(res)
41 , fPremul(true)
42 , fDecodeSucceeded(false) {
43 this->nextImage();
44 }
45
46protected:
Hal Canary8a027312019-07-03 10:55:44 -040047 SkString name() override { return SkString("unpremul"); }
48
Hal Canary6cc65e12019-07-03 15:53:04 -040049 bool onChar(SkUnichar uni) override {
Hal Canaryf107a2f2018-07-25 16:52:48 -040050 char utf8[SkUTF::kMaxBytesInUTF8Sequence];
51 size_t size = SkUTF::ToUTF8(uni, utf8);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000052 // Only consider events for single char keys
53 if (1 == size) {
54 switch (utf8[0]) {
55 case fNextImageChar:
56 this->nextImage();
57 return true;
58 case fTogglePremulChar:
59 this->togglePremul();
60 return true;
61 default:
62 break;
63 }
64 }
Hal Canary6cc65e12019-07-03 15:53:04 -040065 return false;
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000066 }
67
mtklein36352bf2015-03-25 18:17:31 -070068 void onDrawBackground(SkCanvas* canvas) override {
Mike Kleinea3f0142019-03-20 11:12:10 -050069 ToolUtils::draw_checkerboard(canvas, 0xFFCCCCCC, 0xFFFFFFFF, 12);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000070 }
71
mtklein36352bf2015-03-25 18:17:31 -070072 void onDrawContent(SkCanvas* canvas) override {
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000073 SkPaint paint;
74 paint.setAntiAlias(true);
Mike Reed91919132019-01-02 12:21:01 -050075
76 SkFont font;
77 font.setSize(24);
reed7b380d02016-03-21 13:25:16 -070078 auto looper(
79 SkBlurDrawLooper::Make(SK_ColorBLUE, SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(2)),
80 0, 0));
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000081 paint.setLooper(looper);
Mike Reed91919132019-01-02 12:21:01 -050082 SkScalar height = font.getMetrics(nullptr);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000083 if (!fDecodeSucceeded) {
84 SkString failure;
85 if (fResPath.size() == 0) {
86 failure.printf("resource path is required!");
87 } else {
88 failure.printf("Failed to decode %s", fCurrFile.c_str());
89 }
Hal Canary89a644b2019-01-07 09:36:09 -050090 canvas->drawString(failure, 0, height, font, paint);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000091 return;
92 }
93
94 // Name, size of the file, and whether or not it is premultiplied.
tfarinaa8e2e152014-07-28 19:26:58 -070095 SkString header(SkOSPath::Basename(fCurrFile.c_str()));
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000096 header.appendf(" [%dx%d] %s", fBitmap.width(), fBitmap.height(),
97 (fPremul ? "premultiplied" : "unpremultiplied"));
Hal Canary89a644b2019-01-07 09:36:09 -050098 canvas->drawString(header, 0, height, font, paint);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000099 canvas->translate(0, height);
100
101 // Help messages
102 header.printf("Press '%c' to move to the next image.'", fNextImageChar);
Hal Canary89a644b2019-01-07 09:36:09 -0500103 canvas->drawString(header, 0, height, font, paint);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000104 canvas->translate(0, height);
105
106 header.printf("Press '%c' to toggle premultiplied decode.", fTogglePremulChar);
Hal Canary89a644b2019-01-07 09:36:09 -0500107 canvas->drawString(header, 0, height, font, paint);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000108
109 // Now draw the image itself.
110 canvas->translate(height * 2, height * 2);
111 if (!fPremul) {
112 // A premultiplied bitmap cannot currently be drawn.
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000113 // Copy it to a bitmap which can be drawn, converting
114 // to premultiplied:
115 SkBitmap bm;
reed84825042014-09-02 12:50:45 -0700116 bm.allocN32Pixels(fBitmap.width(), fBitmap.height());
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000117 for (int i = 0; i < fBitmap.width(); ++i) {
118 for (int j = 0; j < fBitmap.height(); ++j) {
119 *bm.getAddr32(i, j) = premultiply_unpmcolor(*fBitmap.getAddr32(i, j));
120 }
121 }
122 canvas->drawBitmap(bm, 0, 0);
123 } else {
124 canvas->drawBitmap(fBitmap, 0, 0);
125 }
126 }
127
128private:
129 const SkString fResPath;
130 SkString fCurrFile;
131 bool fPremul;
132 bool fDecodeSucceeded;
133 SkBitmap fBitmap;
134 SkOSFile::Iter fFileIter;
135
136 static const char fNextImageChar = 'j';
137 static const char fTogglePremulChar = 'h';
138
139 void nextImage() {
140 if (fResPath.size() == 0) {
141 return;
142 }
143 SkString basename;
144 if (!fFileIter.next(&basename)) {
145 fFileIter.reset(fResPath.c_str());
146 if (!fFileIter.next(&basename)) {
147 // Perhaps this should draw some error message?
148 return;
149 }
150 }
tfarinaa8e2e152014-07-28 19:26:58 -0700151 fCurrFile = SkOSPath::Join(fResPath.c_str(), basename.c_str());
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000152 this->decodeCurrFile();
153 }
154
155 void decodeCurrFile() {
156 if (fCurrFile.size() == 0) {
157 fDecodeSucceeded = false;
158 return;
159 }
msarettd15750c2016-03-18 15:48:49 -0700160 fDecodeSucceeded = decode_file(fCurrFile.c_str(), &fBitmap, kN32_SkColorType, !fPremul);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000161 }
162
163 void togglePremul() {
164 fPremul = !fPremul;
165 this->decodeCurrFile();
166 }
167
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400168 typedef Sample INHERITED;
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000169};
170
171//////////////////////////////////////////////////////////////////////////////
172
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400173DEF_SAMPLE( return new UnpremulView(GetResourcePath("images")); )
Mike Reedb7dad442019-07-22 14:51:10 -0400174
175#endif