blob: 3e670be05c2737d3249839f3e6e789d87a2d4e3f [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
halcanaryb0cce2c2015-01-26 12:49:00 -08008#include "sk_tool_utils.h"
msarettd15750c2016-03-18 15:48:49 -07009#include "DecodeFile.h"
tfarinabcbc1782014-06-18 14:32:48 -070010#include "Resources.h"
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040011#include "Sample.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"
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000016#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050017#include "SkOSPath.h"
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000018#include "SkStream.h"
19#include "SkString.h"
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000020#include "SkTypes.h"
Hal Canaryea60b952018-08-21 11:45:46 -040021#include "SkUTF.h"
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000022
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000023/**
24 * Interprets c as an unpremultiplied color, and returns the
25 * premultiplied equivalent.
26 */
27static SkPMColor premultiply_unpmcolor(SkPMColor c) {
28 U8CPU a = SkGetPackedA32(c);
29 U8CPU r = SkGetPackedR32(c);
30 U8CPU g = SkGetPackedG32(c);
31 U8CPU b = SkGetPackedB32(c);
32 return SkPreMultiplyARGB(a, r, g, b);
33}
34
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040035class UnpremulView : public Sample {
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000036public:
37 UnpremulView(SkString res)
38 : fResPath(res)
39 , fPremul(true)
40 , fDecodeSucceeded(false) {
41 this->nextImage();
42 }
43
44protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040045 bool onQuery(Sample::Event* evt) override {
46 if (Sample::TitleQ(*evt)) {
47 Sample::TitleR(evt, "unpremul");
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000048 return true;
49 }
50 SkUnichar uni;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040051 if (Sample::CharQ(*evt, &uni)) {
Hal Canaryf107a2f2018-07-25 16:52:48 -040052 char utf8[SkUTF::kMaxBytesInUTF8Sequence];
53 size_t size = SkUTF::ToUTF8(uni, utf8);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000054 // Only consider events for single char keys
55 if (1 == size) {
56 switch (utf8[0]) {
57 case fNextImageChar:
58 this->nextImage();
59 return true;
60 case fTogglePremulChar:
61 this->togglePremul();
62 return true;
63 default:
64 break;
65 }
66 }
67 }
68 return this->INHERITED::onQuery(evt);
69 }
70
mtklein36352bf2015-03-25 18:17:31 -070071 void onDrawBackground(SkCanvas* canvas) override {
halcanaryb0cce2c2015-01-26 12:49:00 -080072 sk_tool_utils::draw_checkerboard(canvas, 0xFFCCCCCC, 0xFFFFFFFF, 12);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000073 }
74
mtklein36352bf2015-03-25 18:17:31 -070075 void onDrawContent(SkCanvas* canvas) override {
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000076 SkPaint paint;
77 paint.setAntiAlias(true);
78 paint.setTextSize(SkIntToScalar(24));
reed7b380d02016-03-21 13:25:16 -070079 auto looper(
80 SkBlurDrawLooper::Make(SK_ColorBLUE, SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(2)),
81 0, 0));
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000082 paint.setLooper(looper);
halcanary96fcdcc2015-08-27 07:41:13 -070083 SkScalar height = paint.getFontMetrics(nullptr);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000084 if (!fDecodeSucceeded) {
85 SkString failure;
86 if (fResPath.size() == 0) {
87 failure.printf("resource path is required!");
88 } else {
89 failure.printf("Failed to decode %s", fCurrFile.c_str());
90 }
Cary Clark2a475ea2017-04-28 15:35:12 -040091 canvas->drawString(failure, 0, height, paint);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000092 return;
93 }
94
95 // Name, size of the file, and whether or not it is premultiplied.
tfarinaa8e2e152014-07-28 19:26:58 -070096 SkString header(SkOSPath::Basename(fCurrFile.c_str()));
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000097 header.appendf(" [%dx%d] %s", fBitmap.width(), fBitmap.height(),
98 (fPremul ? "premultiplied" : "unpremultiplied"));
Cary Clark2a475ea2017-04-28 15:35:12 -040099 canvas->drawString(header, 0, height, paint);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000100 canvas->translate(0, height);
101
102 // Help messages
103 header.printf("Press '%c' to move to the next image.'", fNextImageChar);
Cary Clark2a475ea2017-04-28 15:35:12 -0400104 canvas->drawString(header, 0, height, paint);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000105 canvas->translate(0, height);
106
107 header.printf("Press '%c' to toggle premultiplied decode.", fTogglePremulChar);
Cary Clark2a475ea2017-04-28 15:35:12 -0400108 canvas->drawString(header, 0, height, paint);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000109
110 // Now draw the image itself.
111 canvas->translate(height * 2, height * 2);
112 if (!fPremul) {
113 // A premultiplied bitmap cannot currently be drawn.
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000114 // Copy it to a bitmap which can be drawn, converting
115 // to premultiplied:
116 SkBitmap bm;
reed84825042014-09-02 12:50:45 -0700117 bm.allocN32Pixels(fBitmap.width(), fBitmap.height());
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000118 for (int i = 0; i < fBitmap.width(); ++i) {
119 for (int j = 0; j < fBitmap.height(); ++j) {
120 *bm.getAddr32(i, j) = premultiply_unpmcolor(*fBitmap.getAddr32(i, j));
121 }
122 }
123 canvas->drawBitmap(bm, 0, 0);
124 } else {
125 canvas->drawBitmap(fBitmap, 0, 0);
126 }
127 }
128
129private:
130 const SkString fResPath;
131 SkString fCurrFile;
132 bool fPremul;
133 bool fDecodeSucceeded;
134 SkBitmap fBitmap;
135 SkOSFile::Iter fFileIter;
136
137 static const char fNextImageChar = 'j';
138 static const char fTogglePremulChar = 'h';
139
140 void nextImage() {
141 if (fResPath.size() == 0) {
142 return;
143 }
144 SkString basename;
145 if (!fFileIter.next(&basename)) {
146 fFileIter.reset(fResPath.c_str());
147 if (!fFileIter.next(&basename)) {
148 // Perhaps this should draw some error message?
149 return;
150 }
151 }
tfarinaa8e2e152014-07-28 19:26:58 -0700152 fCurrFile = SkOSPath::Join(fResPath.c_str(), basename.c_str());
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000153 this->decodeCurrFile();
154 }
155
156 void decodeCurrFile() {
157 if (fCurrFile.size() == 0) {
158 fDecodeSucceeded = false;
159 return;
160 }
msarettd15750c2016-03-18 15:48:49 -0700161 fDecodeSucceeded = decode_file(fCurrFile.c_str(), &fBitmap, kN32_SkColorType, !fPremul);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000162 }
163
164 void togglePremul() {
165 fPremul = !fPremul;
166 this->decodeCurrFile();
167 }
168
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400169 typedef Sample INHERITED;
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000170};
171
172//////////////////////////////////////////////////////////////////////////////
173
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400174DEF_SAMPLE( return new UnpremulView(GetResourcePath("images")); )