blob: ee3ec826b88335cf3cc1f8a8ba203a8aa72ee446 [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"
tfarinabcbc1782014-06-18 14:32:48 -070011#include "Resources.h"
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000012#include "SampleCode.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000013#include "SkBlurMask.h"
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000014#include "SkBlurDrawLooper.h"
15#include "SkCanvas.h"
16#include "SkColorPriv.h"
17#include "SkForceLinking.h"
18#include "SkImageDecoder.h"
19#include "SkOSFile.h"
20#include "SkStream.h"
21#include "SkString.h"
22#include "SkSystemEventTypes.h"
23#include "SkTypes.h"
24#include "SkUtils.h"
25#include "SkView.h"
26
27__SK_FORCE_IMAGE_DECODER_LINKING;
28
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000029/**
30 * Interprets c as an unpremultiplied color, and returns the
31 * premultiplied equivalent.
32 */
33static SkPMColor premultiply_unpmcolor(SkPMColor c) {
34 U8CPU a = SkGetPackedA32(c);
35 U8CPU r = SkGetPackedR32(c);
36 U8CPU g = SkGetPackedG32(c);
37 U8CPU b = SkGetPackedB32(c);
38 return SkPreMultiplyARGB(a, r, g, b);
39}
40
41class UnpremulView : public SampleView {
42public:
43 UnpremulView(SkString res)
44 : fResPath(res)
45 , fPremul(true)
46 , fDecodeSucceeded(false) {
47 this->nextImage();
48 }
49
50protected:
51 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -070052 bool onQuery(SkEvent* evt) override {
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000053 if (SampleCode::TitleQ(*evt)) {
54 SampleCode::TitleR(evt, "unpremul");
55 return true;
56 }
57 SkUnichar uni;
58 if (SampleCode::CharQ(*evt, &uni)) {
59 char utf8[kMaxBytesInUTF8Sequence];
60 size_t size = SkUTF8_FromUnichar(uni, utf8);
61 // Only consider events for single char keys
62 if (1 == size) {
63 switch (utf8[0]) {
64 case fNextImageChar:
65 this->nextImage();
66 return true;
67 case fTogglePremulChar:
68 this->togglePremul();
69 return true;
70 default:
71 break;
72 }
73 }
74 }
75 return this->INHERITED::onQuery(evt);
76 }
77
mtklein36352bf2015-03-25 18:17:31 -070078 void onDrawBackground(SkCanvas* canvas) override {
halcanaryb0cce2c2015-01-26 12:49:00 -080079 sk_tool_utils::draw_checkerboard(canvas, 0xFFCCCCCC, 0xFFFFFFFF, 12);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000080 }
81
mtklein36352bf2015-03-25 18:17:31 -070082 void onDrawContent(SkCanvas* canvas) override {
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000083 SkPaint paint;
84 paint.setAntiAlias(true);
85 paint.setTextSize(SkIntToScalar(24));
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000086 SkAutoTUnref<SkBlurDrawLooper> looper(
87 SkBlurDrawLooper::Create(SK_ColorBLUE,
88 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(2)),
89 0, 0));
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000090 paint.setLooper(looper);
91 SkScalar height = paint.getFontMetrics(NULL);
92 if (!fDecodeSucceeded) {
93 SkString failure;
94 if (fResPath.size() == 0) {
95 failure.printf("resource path is required!");
96 } else {
97 failure.printf("Failed to decode %s", fCurrFile.c_str());
98 }
99 canvas->drawText(failure.c_str(), failure.size(), 0, height, paint);
100 return;
101 }
102
103 // Name, size of the file, and whether or not it is premultiplied.
tfarinaa8e2e152014-07-28 19:26:58 -0700104 SkString header(SkOSPath::Basename(fCurrFile.c_str()));
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000105 header.appendf(" [%dx%d] %s", fBitmap.width(), fBitmap.height(),
106 (fPremul ? "premultiplied" : "unpremultiplied"));
107 canvas->drawText(header.c_str(), header.size(), 0, height, paint);
108 canvas->translate(0, height);
109
110 // Help messages
111 header.printf("Press '%c' to move to the next image.'", fNextImageChar);
112 canvas->drawText(header.c_str(), header.size(), 0, height, paint);
113 canvas->translate(0, height);
114
115 header.printf("Press '%c' to toggle premultiplied decode.", fTogglePremulChar);
116 canvas->drawText(header.c_str(), header.size(), 0, height, paint);
117
118 // Now draw the image itself.
119 canvas->translate(height * 2, height * 2);
120 if (!fPremul) {
121 // A premultiplied bitmap cannot currently be drawn.
122 SkAutoLockPixels alp(fBitmap);
123 // Copy it to a bitmap which can be drawn, converting
124 // to premultiplied:
125 SkBitmap bm;
reed84825042014-09-02 12:50:45 -0700126 bm.allocN32Pixels(fBitmap.width(), fBitmap.height());
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000127 for (int i = 0; i < fBitmap.width(); ++i) {
128 for (int j = 0; j < fBitmap.height(); ++j) {
129 *bm.getAddr32(i, j) = premultiply_unpmcolor(*fBitmap.getAddr32(i, j));
130 }
131 }
132 canvas->drawBitmap(bm, 0, 0);
133 } else {
134 canvas->drawBitmap(fBitmap, 0, 0);
135 }
136 }
137
138private:
139 const SkString fResPath;
140 SkString fCurrFile;
141 bool fPremul;
142 bool fDecodeSucceeded;
143 SkBitmap fBitmap;
144 SkOSFile::Iter fFileIter;
145
146 static const char fNextImageChar = 'j';
147 static const char fTogglePremulChar = 'h';
148
149 void nextImage() {
150 if (fResPath.size() == 0) {
151 return;
152 }
153 SkString basename;
154 if (!fFileIter.next(&basename)) {
155 fFileIter.reset(fResPath.c_str());
156 if (!fFileIter.next(&basename)) {
157 // Perhaps this should draw some error message?
158 return;
159 }
160 }
tfarinaa8e2e152014-07-28 19:26:58 -0700161 fCurrFile = SkOSPath::Join(fResPath.c_str(), basename.c_str());
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000162 this->decodeCurrFile();
163 }
164
165 void decodeCurrFile() {
166 if (fCurrFile.size() == 0) {
167 fDecodeSucceeded = false;
168 return;
169 }
170 SkFILEStream stream(fCurrFile.c_str());
171 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(&stream));
172 if (NULL == decoder.get()) {
173 fDecodeSucceeded = false;
174 return;
175 }
176 if (!fPremul) {
177 decoder->setRequireUnpremultipliedColors(true);
178 }
reedbfefc7c2014-06-12 17:40:00 -0700179 fDecodeSucceeded = decoder->decode(&stream, &fBitmap, kN32_SkColorType,
scroggo2a120802014-10-22 12:07:00 -0700180 SkImageDecoder::kDecodePixels_Mode) != SkImageDecoder::kFailure;
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000181 this->inval(NULL);
182 }
183
184 void togglePremul() {
185 fPremul = !fPremul;
186 this->decodeCurrFile();
187 }
188
189 typedef SampleView INHERITED;
190};
191
192//////////////////////////////////////////////////////////////////////////////
193
194static SkView* MyFactory() {
tfarinabcbc1782014-06-18 14:32:48 -0700195 return new UnpremulView(GetResourcePath());
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000196}
197static SkViewRegister reg(MyFactory);