blob: 108d70914f6b1ff339fd057db291829d4d403921 [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"
Ben Wagnerbf111d72016-11-07 18:05:29 -050019#include "SkOSPath.h"
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000020#include "SkStream.h"
21#include "SkString.h"
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000022#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));
reed7b380d02016-03-21 13:25:16 -070083 auto looper(
84 SkBlurDrawLooper::Make(SK_ColorBLUE, SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(2)),
85 0, 0));
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000086 paint.setLooper(looper);
halcanary96fcdcc2015-08-27 07:41:13 -070087 SkScalar height = paint.getFontMetrics(nullptr);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000088 if (!fDecodeSucceeded) {
89 SkString failure;
90 if (fResPath.size() == 0) {
91 failure.printf("resource path is required!");
92 } else {
93 failure.printf("Failed to decode %s", fCurrFile.c_str());
94 }
Cary Clark2a475ea2017-04-28 15:35:12 -040095 canvas->drawString(failure, 0, height, paint);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000096 return;
97 }
98
99 // Name, size of the file, and whether or not it is premultiplied.
tfarinaa8e2e152014-07-28 19:26:58 -0700100 SkString header(SkOSPath::Basename(fCurrFile.c_str()));
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000101 header.appendf(" [%dx%d] %s", fBitmap.width(), fBitmap.height(),
102 (fPremul ? "premultiplied" : "unpremultiplied"));
Cary Clark2a475ea2017-04-28 15:35:12 -0400103 canvas->drawString(header, 0, height, paint);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000104 canvas->translate(0, height);
105
106 // Help messages
107 header.printf("Press '%c' to move to the next image.'", fNextImageChar);
Cary Clark2a475ea2017-04-28 15:35:12 -0400108 canvas->drawString(header, 0, height, paint);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000109 canvas->translate(0, height);
110
111 header.printf("Press '%c' to toggle premultiplied decode.", fTogglePremulChar);
Cary Clark2a475ea2017-04-28 15:35:12 -0400112 canvas->drawString(header, 0, height, paint);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000113
114 // Now draw the image itself.
115 canvas->translate(height * 2, height * 2);
116 if (!fPremul) {
117 // A premultiplied bitmap cannot currently be drawn.
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000118 // Copy it to a bitmap which can be drawn, converting
119 // to premultiplied:
120 SkBitmap bm;
reed84825042014-09-02 12:50:45 -0700121 bm.allocN32Pixels(fBitmap.width(), fBitmap.height());
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000122 for (int i = 0; i < fBitmap.width(); ++i) {
123 for (int j = 0; j < fBitmap.height(); ++j) {
124 *bm.getAddr32(i, j) = premultiply_unpmcolor(*fBitmap.getAddr32(i, j));
125 }
126 }
127 canvas->drawBitmap(bm, 0, 0);
128 } else {
129 canvas->drawBitmap(fBitmap, 0, 0);
130 }
131 }
132
133private:
134 const SkString fResPath;
135 SkString fCurrFile;
136 bool fPremul;
137 bool fDecodeSucceeded;
138 SkBitmap fBitmap;
139 SkOSFile::Iter fFileIter;
140
141 static const char fNextImageChar = 'j';
142 static const char fTogglePremulChar = 'h';
143
144 void nextImage() {
145 if (fResPath.size() == 0) {
146 return;
147 }
148 SkString basename;
149 if (!fFileIter.next(&basename)) {
150 fFileIter.reset(fResPath.c_str());
151 if (!fFileIter.next(&basename)) {
152 // Perhaps this should draw some error message?
153 return;
154 }
155 }
tfarinaa8e2e152014-07-28 19:26:58 -0700156 fCurrFile = SkOSPath::Join(fResPath.c_str(), basename.c_str());
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000157 this->decodeCurrFile();
158 }
159
160 void decodeCurrFile() {
161 if (fCurrFile.size() == 0) {
162 fDecodeSucceeded = false;
163 return;
164 }
msarettd15750c2016-03-18 15:48:49 -0700165 fDecodeSucceeded = decode_file(fCurrFile.c_str(), &fBitmap, kN32_SkColorType, !fPremul);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000166 }
167
168 void togglePremul() {
169 fPremul = !fPremul;
170 this->decodeCurrFile();
171 }
172
173 typedef SampleView INHERITED;
174};
175
176//////////////////////////////////////////////////////////////////////////////
177
178static SkView* MyFactory() {
Hal Canaryc465d132017-12-08 10:21:31 -0500179 return new UnpremulView(GetResourcePath("images"));
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000180}
181static SkViewRegister reg(MyFactory);