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