blob: ffe4428b484949bce73e5214394489fd2500c045 [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
10#include "Resources.h"
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000011#include "SampleCode.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000012#include "SkBlurMask.h"
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000013#include "SkBlurDrawLooper.h"
14#include "SkCanvas.h"
15#include "SkColorPriv.h"
16#include "SkForceLinking.h"
17#include "SkImageDecoder.h"
18#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
26__SK_FORCE_IMAGE_DECODER_LINKING;
27
28// Defined in SampleColorFilter.cpp
29extern SkShader* createChecker();
30
31/**
32 * Interprets c as an unpremultiplied color, and returns the
33 * premultiplied equivalent.
34 */
35static SkPMColor premultiply_unpmcolor(SkPMColor c) {
36 U8CPU a = SkGetPackedA32(c);
37 U8CPU r = SkGetPackedR32(c);
38 U8CPU g = SkGetPackedG32(c);
39 U8CPU b = SkGetPackedB32(c);
40 return SkPreMultiplyARGB(a, r, g, b);
41}
42
43class UnpremulView : public SampleView {
44public:
45 UnpremulView(SkString res)
46 : fResPath(res)
47 , fPremul(true)
48 , fDecodeSucceeded(false) {
49 this->nextImage();
50 }
51
52protected:
53 // overrides from SkEventSink
54 virtual bool onQuery(SkEvent* evt) SK_OVERRIDE {
55 if (SampleCode::TitleQ(*evt)) {
56 SampleCode::TitleR(evt, "unpremul");
57 return true;
58 }
59 SkUnichar uni;
60 if (SampleCode::CharQ(*evt, &uni)) {
61 char utf8[kMaxBytesInUTF8Sequence];
62 size_t size = SkUTF8_FromUnichar(uni, utf8);
63 // Only consider events for single char keys
64 if (1 == size) {
65 switch (utf8[0]) {
66 case fNextImageChar:
67 this->nextImage();
68 return true;
69 case fTogglePremulChar:
70 this->togglePremul();
71 return true;
72 default:
73 break;
74 }
75 }
76 }
77 return this->INHERITED::onQuery(evt);
78 }
79
80 virtual void onDrawBackground(SkCanvas* canvas) SK_OVERRIDE {
81 SkPaint paint;
82 SkAutoTUnref<SkShader> shader(createChecker());
83 paint.setShader(shader.get());
84 canvas->drawPaint(paint);
85 }
86
87 virtual void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
88 SkPaint paint;
89 paint.setAntiAlias(true);
90 paint.setTextSize(SkIntToScalar(24));
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000091 SkAutoTUnref<SkBlurDrawLooper> looper(
92 SkBlurDrawLooper::Create(SK_ColorBLUE,
93 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(2)),
94 0, 0));
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000095 paint.setLooper(looper);
96 SkScalar height = paint.getFontMetrics(NULL);
97 if (!fDecodeSucceeded) {
98 SkString failure;
99 if (fResPath.size() == 0) {
100 failure.printf("resource path is required!");
101 } else {
102 failure.printf("Failed to decode %s", fCurrFile.c_str());
103 }
104 canvas->drawText(failure.c_str(), failure.size(), 0, height, paint);
105 return;
106 }
107
108 // Name, size of the file, and whether or not it is premultiplied.
tfarinaa8e2e152014-07-28 19:26:58 -0700109 SkString header(SkOSPath::Basename(fCurrFile.c_str()));
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000110 header.appendf(" [%dx%d] %s", fBitmap.width(), fBitmap.height(),
111 (fPremul ? "premultiplied" : "unpremultiplied"));
112 canvas->drawText(header.c_str(), header.size(), 0, height, paint);
113 canvas->translate(0, height);
114
115 // Help messages
116 header.printf("Press '%c' to move to the next image.'", fNextImageChar);
117 canvas->drawText(header.c_str(), header.size(), 0, height, paint);
118 canvas->translate(0, height);
119
120 header.printf("Press '%c' to toggle premultiplied decode.", fTogglePremulChar);
121 canvas->drawText(header.c_str(), header.size(), 0, height, paint);
122
123 // Now draw the image itself.
124 canvas->translate(height * 2, height * 2);
125 if (!fPremul) {
126 // A premultiplied bitmap cannot currently be drawn.
127 SkAutoLockPixels alp(fBitmap);
128 // Copy it to a bitmap which can be drawn, converting
129 // to premultiplied:
130 SkBitmap bm;
reed84825042014-09-02 12:50:45 -0700131 bm.allocN32Pixels(fBitmap.width(), fBitmap.height());
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000132 for (int i = 0; i < fBitmap.width(); ++i) {
133 for (int j = 0; j < fBitmap.height(); ++j) {
134 *bm.getAddr32(i, j) = premultiply_unpmcolor(*fBitmap.getAddr32(i, j));
135 }
136 }
137 canvas->drawBitmap(bm, 0, 0);
138 } else {
139 canvas->drawBitmap(fBitmap, 0, 0);
140 }
141 }
142
143private:
144 const SkString fResPath;
145 SkString fCurrFile;
146 bool fPremul;
147 bool fDecodeSucceeded;
148 SkBitmap fBitmap;
149 SkOSFile::Iter fFileIter;
150
151 static const char fNextImageChar = 'j';
152 static const char fTogglePremulChar = 'h';
153
154 void nextImage() {
155 if (fResPath.size() == 0) {
156 return;
157 }
158 SkString basename;
159 if (!fFileIter.next(&basename)) {
160 fFileIter.reset(fResPath.c_str());
161 if (!fFileIter.next(&basename)) {
162 // Perhaps this should draw some error message?
163 return;
164 }
165 }
tfarinaa8e2e152014-07-28 19:26:58 -0700166 fCurrFile = SkOSPath::Join(fResPath.c_str(), basename.c_str());
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000167 this->decodeCurrFile();
168 }
169
170 void decodeCurrFile() {
171 if (fCurrFile.size() == 0) {
172 fDecodeSucceeded = false;
173 return;
174 }
175 SkFILEStream stream(fCurrFile.c_str());
176 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(&stream));
177 if (NULL == decoder.get()) {
178 fDecodeSucceeded = false;
179 return;
180 }
181 if (!fPremul) {
182 decoder->setRequireUnpremultipliedColors(true);
183 }
reedbfefc7c2014-06-12 17:40:00 -0700184 fDecodeSucceeded = decoder->decode(&stream, &fBitmap, kN32_SkColorType,
scroggo2a120802014-10-22 12:07:00 -0700185 SkImageDecoder::kDecodePixels_Mode) != SkImageDecoder::kFailure;
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000186 this->inval(NULL);
187 }
188
189 void togglePremul() {
190 fPremul = !fPremul;
191 this->decodeCurrFile();
192 }
193
194 typedef SampleView INHERITED;
195};
196
197//////////////////////////////////////////////////////////////////////////////
198
199static SkView* MyFactory() {
tfarinabcbc1782014-06-18 14:32:48 -0700200 return new UnpremulView(GetResourcePath());
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000201}
202static SkViewRegister reg(MyFactory);