blob: fb9735c017cd771a63762725ab299ad91bf21908 [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
scroggo@google.com2bbc2c92013-06-14 15:33:20 +00008#include "gm.h"
tfarinabcbc1782014-06-18 14:32:48 -07009
halcanaryb0cce2c2015-01-26 12:49:00 -080010#include "sk_tool_utils.h"
msarettd15750c2016-03-18 15:48:49 -070011#include "DecodeFile.h"
tfarinabcbc1782014-06-18 14:32:48 -070012#include "Resources.h"
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000013#include "SampleCode.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000014#include "SkBlurMask.h"
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000015#include "SkBlurDrawLooper.h"
16#include "SkCanvas.h"
17#include "SkColorPriv.h"
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000018#include "SkOSFile.h"
19#include "SkStream.h"
20#include "SkString.h"
21#include "SkSystemEventTypes.h"
22#include "SkTypes.h"
23#include "SkUtils.h"
24#include "SkView.h"
25
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000026/**
27 * Interprets c as an unpremultiplied color, and returns the
28 * premultiplied equivalent.
29 */
30static SkPMColor premultiply_unpmcolor(SkPMColor c) {
31 U8CPU a = SkGetPackedA32(c);
32 U8CPU r = SkGetPackedR32(c);
33 U8CPU g = SkGetPackedG32(c);
34 U8CPU b = SkGetPackedB32(c);
35 return SkPreMultiplyARGB(a, r, g, b);
36}
37
38class UnpremulView : public SampleView {
39public:
40 UnpremulView(SkString res)
41 : fResPath(res)
42 , fPremul(true)
43 , fDecodeSucceeded(false) {
44 this->nextImage();
45 }
46
47protected:
48 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -070049 bool onQuery(SkEvent* evt) override {
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000050 if (SampleCode::TitleQ(*evt)) {
51 SampleCode::TitleR(evt, "unpremul");
52 return true;
53 }
54 SkUnichar uni;
55 if (SampleCode::CharQ(*evt, &uni)) {
56 char utf8[kMaxBytesInUTF8Sequence];
57 size_t size = SkUTF8_FromUnichar(uni, utf8);
58 // Only consider events for single char keys
59 if (1 == size) {
60 switch (utf8[0]) {
61 case fNextImageChar:
62 this->nextImage();
63 return true;
64 case fTogglePremulChar:
65 this->togglePremul();
66 return true;
67 default:
68 break;
69 }
70 }
71 }
72 return this->INHERITED::onQuery(evt);
73 }
74
mtklein36352bf2015-03-25 18:17:31 -070075 void onDrawBackground(SkCanvas* canvas) override {
halcanaryb0cce2c2015-01-26 12:49:00 -080076 sk_tool_utils::draw_checkerboard(canvas, 0xFFCCCCCC, 0xFFFFFFFF, 12);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000077 }
78
mtklein36352bf2015-03-25 18:17:31 -070079 void onDrawContent(SkCanvas* canvas) override {
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000080 SkPaint paint;
81 paint.setAntiAlias(true);
82 paint.setTextSize(SkIntToScalar(24));
reed5e1ddb12015-12-21 08:52:45 -080083 SkAutoTUnref<SkDrawLooper> looper(
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000084 SkBlurDrawLooper::Create(SK_ColorBLUE,
85 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(2)),
86 0, 0));
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000087 paint.setLooper(looper);
halcanary96fcdcc2015-08-27 07:41:13 -070088 SkScalar height = paint.getFontMetrics(nullptr);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000089 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.
tfarinaa8e2e152014-07-28 19:26:58 -0700101 SkString header(SkOSPath::Basename(fCurrFile.c_str()));
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000102 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;
reed84825042014-09-02 12:50:45 -0700123 bm.allocN32Pixels(fBitmap.width(), fBitmap.height());
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000124 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
135private:
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 }
tfarinaa8e2e152014-07-28 19:26:58 -0700158 fCurrFile = SkOSPath::Join(fResPath.c_str(), basename.c_str());
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000159 this->decodeCurrFile();
160 }
161
162 void decodeCurrFile() {
163 if (fCurrFile.size() == 0) {
164 fDecodeSucceeded = false;
165 return;
166 }
msarettd15750c2016-03-18 15:48:49 -0700167 fDecodeSucceeded = decode_file(fCurrFile.c_str(), &fBitmap, kN32_SkColorType, !fPremul);
halcanary96fcdcc2015-08-27 07:41:13 -0700168 this->inval(nullptr);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000169 }
170
171 void togglePremul() {
172 fPremul = !fPremul;
173 this->decodeCurrFile();
174 }
175
176 typedef SampleView INHERITED;
177};
178
179//////////////////////////////////////////////////////////////////////////////
180
181static SkView* MyFactory() {
tfarinabcbc1782014-06-18 14:32:48 -0700182 return new UnpremulView(GetResourcePath());
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000183}
184static SkViewRegister reg(MyFactory);