• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
8 #include "gm.h"
9 
10 #include "sk_tool_utils.h"
11 #include "DecodeFile.h"
12 #include "Resources.h"
13 #include "SampleCode.h"
14 #include "SkBlurMask.h"
15 #include "SkBlurDrawLooper.h"
16 #include "SkCanvas.h"
17 #include "SkColorPriv.h"
18 #include "SkOSFile.h"
19 #include "SkOSPath.h"
20 #include "SkStream.h"
21 #include "SkString.h"
22 #include "SkSystemEventTypes.h"
23 #include "SkTypes.h"
24 #include "SkUtils.h"
25 #include "SkView.h"
26 
27 /**
28  *  Interprets c as an unpremultiplied color, and returns the
29  *  premultiplied equivalent.
30  */
premultiply_unpmcolor(SkPMColor c)31 static 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 
39 class UnpremulView : public SampleView {
40 public:
UnpremulView(SkString res)41     UnpremulView(SkString res)
42     : fResPath(res)
43     , fPremul(true)
44     , fDecodeSucceeded(false) {
45         this->nextImage();
46     }
47 
48 protected:
49     // overrides from SkEventSink
onQuery(SkEvent * evt)50     bool onQuery(SkEvent* evt) 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 
onDrawBackground(SkCanvas * canvas)76     void onDrawBackground(SkCanvas* canvas) override {
77         sk_tool_utils::draw_checkerboard(canvas, 0xFFCCCCCC, 0xFFFFFFFF, 12);
78     }
79 
onDrawContent(SkCanvas * canvas)80     void onDrawContent(SkCanvas* canvas) override {
81         SkPaint paint;
82         paint.setAntiAlias(true);
83         paint.setTextSize(SkIntToScalar(24));
84         auto looper(
85             SkBlurDrawLooper::Make(SK_ColorBLUE, SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(2)),
86                                    0, 0));
87         paint.setLooper(looper);
88         SkScalar height = paint.getFontMetrics(nullptr);
89         if (!fDecodeSucceeded) {
90             SkString failure;
91             if (fResPath.size() == 0) {
92                 failure.printf("resource path is required!");
93             } else {
94                 failure.printf("Failed to decode %s", fCurrFile.c_str());
95             }
96             canvas->drawString(failure, 0, height, paint);
97             return;
98         }
99 
100         // Name, size of the file, and whether or not it is premultiplied.
101         SkString header(SkOSPath::Basename(fCurrFile.c_str()));
102         header.appendf("     [%dx%d]     %s", fBitmap.width(), fBitmap.height(),
103                        (fPremul ? "premultiplied" : "unpremultiplied"));
104         canvas->drawString(header, 0, height, paint);
105         canvas->translate(0, height);
106 
107         // Help messages
108         header.printf("Press '%c' to move to the next image.'", fNextImageChar);
109         canvas->drawString(header, 0, height, paint);
110         canvas->translate(0, height);
111 
112         header.printf("Press '%c' to toggle premultiplied decode.", fTogglePremulChar);
113         canvas->drawString(header, 0, height, paint);
114 
115         // Now draw the image itself.
116         canvas->translate(height * 2, height * 2);
117         if (!fPremul) {
118             // A premultiplied bitmap cannot currently be drawn.
119             // Copy it to a bitmap which can be drawn, converting
120             // to premultiplied:
121             SkBitmap bm;
122             bm.allocN32Pixels(fBitmap.width(), fBitmap.height());
123             for (int i = 0; i < fBitmap.width(); ++i) {
124                 for (int j = 0; j < fBitmap.height(); ++j) {
125                     *bm.getAddr32(i, j) = premultiply_unpmcolor(*fBitmap.getAddr32(i, j));
126                 }
127             }
128             canvas->drawBitmap(bm, 0, 0);
129         } else {
130             canvas->drawBitmap(fBitmap, 0, 0);
131         }
132     }
133 
134 private:
135     const SkString  fResPath;
136     SkString        fCurrFile;
137     bool            fPremul;
138     bool            fDecodeSucceeded;
139     SkBitmap        fBitmap;
140     SkOSFile::Iter  fFileIter;
141 
142     static const char   fNextImageChar      = 'j';
143     static const char   fTogglePremulChar   = 'h';
144 
nextImage()145     void nextImage() {
146         if (fResPath.size() == 0) {
147             return;
148         }
149         SkString basename;
150         if (!fFileIter.next(&basename)) {
151             fFileIter.reset(fResPath.c_str());
152             if (!fFileIter.next(&basename)) {
153                 // Perhaps this should draw some error message?
154                 return;
155             }
156         }
157         fCurrFile = SkOSPath::Join(fResPath.c_str(), basename.c_str());
158         this->decodeCurrFile();
159     }
160 
decodeCurrFile()161     void decodeCurrFile() {
162         if (fCurrFile.size() == 0) {
163             fDecodeSucceeded = false;
164             return;
165         }
166         fDecodeSucceeded = decode_file(fCurrFile.c_str(), &fBitmap, kN32_SkColorType, !fPremul);
167         this->inval(nullptr);
168     }
169 
togglePremul()170     void togglePremul() {
171         fPremul = !fPremul;
172         this->decodeCurrFile();
173     }
174 
175     typedef SampleView INHERITED;
176 };
177 
178 //////////////////////////////////////////////////////////////////////////////
179 
MyFactory()180 static SkView* MyFactory() {
181     return new UnpremulView(GetResourcePath());
182 }
183 static SkViewRegister reg(MyFactory);
184