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