blob: c84e1d44e8b9fff1a15c131bae18e5e9ecc8317e [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"
9#include "SampleCode.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000010#include "SkBlurMask.h"
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000011#include "SkBlurDrawLooper.h"
12#include "SkCanvas.h"
13#include "SkColorPriv.h"
14#include "SkForceLinking.h"
15#include "SkImageDecoder.h"
16#include "SkOSFile.h"
17#include "SkStream.h"
18#include "SkString.h"
19#include "SkSystemEventTypes.h"
20#include "SkTypes.h"
21#include "SkUtils.h"
22#include "SkView.h"
23
24__SK_FORCE_IMAGE_DECODER_LINKING;
25
26// Defined in SampleColorFilter.cpp
27extern SkShader* createChecker();
28
29/**
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
52 virtual bool onQuery(SkEvent* evt) SK_OVERRIDE {
53 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
78 virtual void onDrawBackground(SkCanvas* canvas) SK_OVERRIDE {
79 SkPaint paint;
80 SkAutoTUnref<SkShader> shader(createChecker());
81 paint.setShader(shader.get());
82 canvas->drawPaint(paint);
83 }
84
85 virtual void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
86 SkPaint paint;
87 paint.setAntiAlias(true);
88 paint.setTextSize(SkIntToScalar(24));
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000089 SkAutoTUnref<SkBlurDrawLooper> looper(
90 SkBlurDrawLooper::Create(SK_ColorBLUE,
91 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(2)),
92 0, 0));
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000093 paint.setLooper(looper);
94 SkScalar height = paint.getFontMetrics(NULL);
95 if (!fDecodeSucceeded) {
96 SkString failure;
97 if (fResPath.size() == 0) {
98 failure.printf("resource path is required!");
99 } else {
100 failure.printf("Failed to decode %s", fCurrFile.c_str());
101 }
102 canvas->drawText(failure.c_str(), failure.size(), 0, height, paint);
103 return;
104 }
105
106 // Name, size of the file, and whether or not it is premultiplied.
107 SkString header(SkOSPath::SkBasename(fCurrFile.c_str()));
108 header.appendf(" [%dx%d] %s", fBitmap.width(), fBitmap.height(),
109 (fPremul ? "premultiplied" : "unpremultiplied"));
110 canvas->drawText(header.c_str(), header.size(), 0, height, paint);
111 canvas->translate(0, height);
112
113 // Help messages
114 header.printf("Press '%c' to move to the next image.'", fNextImageChar);
115 canvas->drawText(header.c_str(), header.size(), 0, height, paint);
116 canvas->translate(0, height);
117
118 header.printf("Press '%c' to toggle premultiplied decode.", fTogglePremulChar);
119 canvas->drawText(header.c_str(), header.size(), 0, height, paint);
120
121 // Now draw the image itself.
122 canvas->translate(height * 2, height * 2);
123 if (!fPremul) {
124 // A premultiplied bitmap cannot currently be drawn.
125 SkAutoLockPixels alp(fBitmap);
126 // Copy it to a bitmap which can be drawn, converting
127 // to premultiplied:
128 SkBitmap bm;
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000129 if (!bm.allocN32Pixels(fBitmap.width(), fBitmap.height())) {
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000130 SkString errMsg("allocPixels failed");
131 canvas->drawText(errMsg.c_str(), errMsg.size(), 0, height, paint);
132 return;
133 }
134 for (int i = 0; i < fBitmap.width(); ++i) {
135 for (int j = 0; j < fBitmap.height(); ++j) {
136 *bm.getAddr32(i, j) = premultiply_unpmcolor(*fBitmap.getAddr32(i, j));
137 }
138 }
139 canvas->drawBitmap(bm, 0, 0);
140 } else {
141 canvas->drawBitmap(fBitmap, 0, 0);
142 }
143 }
144
145private:
146 const SkString fResPath;
147 SkString fCurrFile;
148 bool fPremul;
149 bool fDecodeSucceeded;
150 SkBitmap fBitmap;
151 SkOSFile::Iter fFileIter;
152
153 static const char fNextImageChar = 'j';
154 static const char fTogglePremulChar = 'h';
155
156 void nextImage() {
157 if (fResPath.size() == 0) {
158 return;
159 }
160 SkString basename;
161 if (!fFileIter.next(&basename)) {
162 fFileIter.reset(fResPath.c_str());
163 if (!fFileIter.next(&basename)) {
164 // Perhaps this should draw some error message?
165 return;
166 }
167 }
168 fCurrFile = SkOSPath::SkPathJoin(fResPath.c_str(), basename.c_str());
169 this->decodeCurrFile();
170 }
171
172 void decodeCurrFile() {
173 if (fCurrFile.size() == 0) {
174 fDecodeSucceeded = false;
175 return;
176 }
177 SkFILEStream stream(fCurrFile.c_str());
178 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(&stream));
179 if (NULL == decoder.get()) {
180 fDecodeSucceeded = false;
181 return;
182 }
183 if (!fPremul) {
184 decoder->setRequireUnpremultipliedColors(true);
185 }
reedbfefc7c2014-06-12 17:40:00 -0700186 fDecodeSucceeded = decoder->decode(&stream, &fBitmap, kN32_SkColorType,
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000187 SkImageDecoder::kDecodePixels_Mode);
188 this->inval(NULL);
189 }
190
191 void togglePremul() {
192 fPremul = !fPremul;
193 this->decodeCurrFile();
194 }
195
196 typedef SampleView INHERITED;
197};
198
199//////////////////////////////////////////////////////////////////////////////
200
201static SkView* MyFactory() {
202 return new UnpremulView(skiagm::GM::GetResourcePath());
203}
204static SkViewRegister reg(MyFactory);