blob: da7e42dbbe161ac22296306910b07a6707a94ad9 [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);
Mike Reed91919132019-01-02 12:21:01 -050078
79 SkFont font;
80 font.setSize(24);
reed7b380d02016-03-21 13:25:16 -070081 auto looper(
82 SkBlurDrawLooper::Make(SK_ColorBLUE, SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(2)),
83 0, 0));
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000084 paint.setLooper(looper);
Mike Reed91919132019-01-02 12:21:01 -050085 SkScalar height = font.getMetrics(nullptr);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000086 if (!fDecodeSucceeded) {
87 SkString failure;
88 if (fResPath.size() == 0) {
89 failure.printf("resource path is required!");
90 } else {
91 failure.printf("Failed to decode %s", fCurrFile.c_str());
92 }
Hal Canary89a644b2019-01-07 09:36:09 -050093 canvas->drawString(failure, 0, height, font, paint);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000094 return;
95 }
96
97 // Name, size of the file, and whether or not it is premultiplied.
tfarinaa8e2e152014-07-28 19:26:58 -070098 SkString header(SkOSPath::Basename(fCurrFile.c_str()));
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000099 header.appendf(" [%dx%d] %s", fBitmap.width(), fBitmap.height(),
100 (fPremul ? "premultiplied" : "unpremultiplied"));
Hal Canary89a644b2019-01-07 09:36:09 -0500101 canvas->drawString(header, 0, height, font, paint);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000102 canvas->translate(0, height);
103
104 // Help messages
105 header.printf("Press '%c' to move to the next image.'", fNextImageChar);
Hal Canary89a644b2019-01-07 09:36:09 -0500106 canvas->drawString(header, 0, height, font, paint);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000107 canvas->translate(0, height);
108
109 header.printf("Press '%c' to toggle premultiplied decode.", fTogglePremulChar);
Hal Canary89a644b2019-01-07 09:36:09 -0500110 canvas->drawString(header, 0, height, font, paint);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000111
112 // Now draw the image itself.
113 canvas->translate(height * 2, height * 2);
114 if (!fPremul) {
115 // A premultiplied bitmap cannot currently be drawn.
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000116 // Copy it to a bitmap which can be drawn, converting
117 // to premultiplied:
118 SkBitmap bm;
reed84825042014-09-02 12:50:45 -0700119 bm.allocN32Pixels(fBitmap.width(), fBitmap.height());
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000120 for (int i = 0; i < fBitmap.width(); ++i) {
121 for (int j = 0; j < fBitmap.height(); ++j) {
122 *bm.getAddr32(i, j) = premultiply_unpmcolor(*fBitmap.getAddr32(i, j));
123 }
124 }
125 canvas->drawBitmap(bm, 0, 0);
126 } else {
127 canvas->drawBitmap(fBitmap, 0, 0);
128 }
129 }
130
131private:
132 const SkString fResPath;
133 SkString fCurrFile;
134 bool fPremul;
135 bool fDecodeSucceeded;
136 SkBitmap fBitmap;
137 SkOSFile::Iter fFileIter;
138
139 static const char fNextImageChar = 'j';
140 static const char fTogglePremulChar = 'h';
141
142 void nextImage() {
143 if (fResPath.size() == 0) {
144 return;
145 }
146 SkString basename;
147 if (!fFileIter.next(&basename)) {
148 fFileIter.reset(fResPath.c_str());
149 if (!fFileIter.next(&basename)) {
150 // Perhaps this should draw some error message?
151 return;
152 }
153 }
tfarinaa8e2e152014-07-28 19:26:58 -0700154 fCurrFile = SkOSPath::Join(fResPath.c_str(), basename.c_str());
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000155 this->decodeCurrFile();
156 }
157
158 void decodeCurrFile() {
159 if (fCurrFile.size() == 0) {
160 fDecodeSucceeded = false;
161 return;
162 }
msarettd15750c2016-03-18 15:48:49 -0700163 fDecodeSucceeded = decode_file(fCurrFile.c_str(), &fBitmap, kN32_SkColorType, !fPremul);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000164 }
165
166 void togglePremul() {
167 fPremul = !fPremul;
168 this->decodeCurrFile();
169 }
170
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400171 typedef Sample INHERITED;
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000172};
173
174//////////////////////////////////////////////////////////////////////////////
175
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400176DEF_SAMPLE( return new UnpremulView(GetResourcePath("images")); )