blob: 5fac3dcaee9104d21feec77e076556589f596cf6 [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
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000023/**
24 * Interprets c as an unpremultiplied color, and returns the
25 * premultiplied equivalent.
26 */
27static 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 Wagnerb2c4ea62018-08-08 11:36:17 -040035class UnpremulView : public Sample {
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000036public:
37 UnpremulView(SkString res)
38 : fResPath(res)
39 , fPremul(true)
40 , fDecodeSucceeded(false) {
41 this->nextImage();
42 }
43
44protected:
Hal Canary8a027312019-07-03 10:55:44 -040045 SkString name() override { return SkString("unpremul"); }
46
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040047 bool onQuery(Sample::Event* evt) override {
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000048 SkUnichar uni;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040049 if (Sample::CharQ(*evt, &uni)) {
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 }
65 }
66 return this->INHERITED::onQuery(evt);
67 }
68
mtklein36352bf2015-03-25 18:17:31 -070069 void onDrawBackground(SkCanvas* canvas) override {
Mike Kleinea3f0142019-03-20 11:12:10 -050070 ToolUtils::draw_checkerboard(canvas, 0xFFCCCCCC, 0xFFFFFFFF, 12);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000071 }
72
mtklein36352bf2015-03-25 18:17:31 -070073 void onDrawContent(SkCanvas* canvas) override {
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000074 SkPaint paint;
75 paint.setAntiAlias(true);
Mike Reed91919132019-01-02 12:21:01 -050076
77 SkFont font;
78 font.setSize(24);
reed7b380d02016-03-21 13:25:16 -070079 auto looper(
80 SkBlurDrawLooper::Make(SK_ColorBLUE, SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(2)),
81 0, 0));
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000082 paint.setLooper(looper);
Mike Reed91919132019-01-02 12:21:01 -050083 SkScalar height = font.getMetrics(nullptr);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000084 if (!fDecodeSucceeded) {
85 SkString failure;
86 if (fResPath.size() == 0) {
87 failure.printf("resource path is required!");
88 } else {
89 failure.printf("Failed to decode %s", fCurrFile.c_str());
90 }
Hal Canary89a644b2019-01-07 09:36:09 -050091 canvas->drawString(failure, 0, height, font, paint);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000092 return;
93 }
94
95 // Name, size of the file, and whether or not it is premultiplied.
tfarinaa8e2e152014-07-28 19:26:58 -070096 SkString header(SkOSPath::Basename(fCurrFile.c_str()));
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000097 header.appendf(" [%dx%d] %s", fBitmap.width(), fBitmap.height(),
98 (fPremul ? "premultiplied" : "unpremultiplied"));
Hal Canary89a644b2019-01-07 09:36:09 -050099 canvas->drawString(header, 0, height, font, paint);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000100 canvas->translate(0, height);
101
102 // Help messages
103 header.printf("Press '%c' to move to the next image.'", fNextImageChar);
Hal Canary89a644b2019-01-07 09:36:09 -0500104 canvas->drawString(header, 0, height, font, paint);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000105 canvas->translate(0, height);
106
107 header.printf("Press '%c' to toggle premultiplied decode.", fTogglePremulChar);
Hal Canary89a644b2019-01-07 09:36:09 -0500108 canvas->drawString(header, 0, height, font, paint);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000109
110 // Now draw the image itself.
111 canvas->translate(height * 2, height * 2);
112 if (!fPremul) {
113 // A premultiplied bitmap cannot currently be drawn.
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000114 // Copy it to a bitmap which can be drawn, converting
115 // to premultiplied:
116 SkBitmap bm;
reed84825042014-09-02 12:50:45 -0700117 bm.allocN32Pixels(fBitmap.width(), fBitmap.height());
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000118 for (int i = 0; i < fBitmap.width(); ++i) {
119 for (int j = 0; j < fBitmap.height(); ++j) {
120 *bm.getAddr32(i, j) = premultiply_unpmcolor(*fBitmap.getAddr32(i, j));
121 }
122 }
123 canvas->drawBitmap(bm, 0, 0);
124 } else {
125 canvas->drawBitmap(fBitmap, 0, 0);
126 }
127 }
128
129private:
130 const SkString fResPath;
131 SkString fCurrFile;
132 bool fPremul;
133 bool fDecodeSucceeded;
134 SkBitmap fBitmap;
135 SkOSFile::Iter fFileIter;
136
137 static const char fNextImageChar = 'j';
138 static const char fTogglePremulChar = 'h';
139
140 void nextImage() {
141 if (fResPath.size() == 0) {
142 return;
143 }
144 SkString basename;
145 if (!fFileIter.next(&basename)) {
146 fFileIter.reset(fResPath.c_str());
147 if (!fFileIter.next(&basename)) {
148 // Perhaps this should draw some error message?
149 return;
150 }
151 }
tfarinaa8e2e152014-07-28 19:26:58 -0700152 fCurrFile = SkOSPath::Join(fResPath.c_str(), basename.c_str());
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000153 this->decodeCurrFile();
154 }
155
156 void decodeCurrFile() {
157 if (fCurrFile.size() == 0) {
158 fDecodeSucceeded = false;
159 return;
160 }
msarettd15750c2016-03-18 15:48:49 -0700161 fDecodeSucceeded = decode_file(fCurrFile.c_str(), &fBitmap, kN32_SkColorType, !fPremul);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000162 }
163
164 void togglePremul() {
165 fPremul = !fPremul;
166 this->decodeCurrFile();
167 }
168
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400169 typedef Sample INHERITED;
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000170};
171
172//////////////////////////////////////////////////////////////////////////////
173
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400174DEF_SAMPLE( return new UnpremulView(GetResourcePath("images")); )