1 /*
2  * Copyright 2011 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 "DecodeFile.h"
9 #include "SampleCode.h"
10 #include "SkView.h"
11 #include "SkCanvas.h"
12 #include "SkGradientShader.h"
13 #include "SkGraphics.h"
14 #include "SkPath.h"
15 #include "SkRegion.h"
16 #include "SkShader.h"
17 #include "SkUtils.h"
18 #include "SkColorPriv.h"
19 #include "SkColorFilter.h"
20 #include "SkTime.h"
21 
22 static const char* gNames[] = {
23     "/skimages/background_01.png"
24 };
25 
26 class Filter2View : public SampleView {
27 public:
28     SkBitmap*   fBitmaps;
29     int         fBitmapCount;
30     int         fCurrIndex;
31 
Filter2View()32     Filter2View() {
33         fBitmapCount = SK_ARRAY_COUNT(gNames)*2;
34         fBitmaps = new SkBitmap[fBitmapCount];
35 
36         for (int i = 0; i < fBitmapCount/2; i++) {
37             decode_file(gNames[i], &fBitmaps[i]);
38         }
39         for (int i = fBitmapCount/2; i < fBitmapCount; i++) {
40             decode_file(gNames[i-fBitmapCount/2], &fBitmaps[i], kRGB_565_SkColorType);
41         }
42         fCurrIndex = 0;
43 
44         this->setBGColor(SK_ColorGRAY);
45     }
46 
~Filter2View()47     virtual ~Filter2View() {
48         delete[] fBitmaps;
49     }
50 
51 protected:
52     // overrides from SkEventSink
onQuery(SkEvent * evt)53     virtual bool onQuery(SkEvent* evt) {
54         if (SampleCode::TitleQ(*evt)) {
55             SkString str("Filter/Dither ");
56             str.append(gNames[fCurrIndex]);
57             SampleCode::TitleR(evt, str.c_str());
58             return true;
59         }
60         return this->INHERITED::onQuery(evt);
61     }
62 
onDrawContent(SkCanvas * canvas)63     virtual void onDrawContent(SkCanvas* canvas) {
64         canvas->translate(SkIntToScalar(10), SkIntToScalar(50));
65 
66         const SkScalar W = SkIntToScalar(fBitmaps[0].width() + 1);
67         const SkScalar H = SkIntToScalar(fBitmaps[0].height() + 1);
68         SkPaint paint;
69 
70         const SkScalar scale = 0.897917f;
71         canvas->scale(SK_Scalar1, scale);
72 
73         for (int k = 0; k < 2; k++) {
74             paint.setFilterQuality(k == 1 ? kLow_SkFilterQuality : kNone_SkFilterQuality);
75             for (int j = 0; j < 2; j++) {
76                 paint.setDither(j == 1);
77                 for (int i = 0; i < fBitmapCount; i++) {
78                     SkScalar x = (k * fBitmapCount + j) * W;
79                     SkScalar y = i * H;
80                     x = SkScalarRoundToScalar(x);
81                     y = SkScalarRoundToScalar(y);
82                     canvas->drawBitmap(fBitmaps[i], x, y, &paint);
83                     if (i == 0) {
84                         SkPaint p;
85                         p.setAntiAlias(true);
86                         p.setTextAlign(SkPaint::kCenter_Align);
87                         p.setTextSize(SkIntToScalar(18));
88                         SkString s("dither=");
89                         s.appendS32(paint.isDither());
90                         s.append(" filter=");
91                         s.appendS32(paint.getFilterQuality() != kNone_SkFilterQuality);
92                         canvas->drawText(s.c_str(), s.size(), x + W/2,
93                                          y - p.getTextSize(), p);
94                     }
95                     if (k+j == 2) {
96                         SkPaint p;
97                         p.setAntiAlias(true);
98                         p.setTextSize(SkIntToScalar(18));
99                         SkString s;
100                         s.append(" depth=");
101                         s.appendS32(fBitmaps[i].colorType() == kRGB_565_SkColorType ? 16 : 32);
102                         canvas->drawText(s.c_str(), s.size(), x + W + SkIntToScalar(4),
103                                          y + H/2, p);
104                     }
105                 }
106             }
107         }
108     }
109 
110 private:
111     typedef SampleView INHERITED;
112 };
113 
114 //////////////////////////////////////////////////////////////////////////////
115 
MyFactory()116 static SkView* MyFactory() { return new Filter2View; }
117 static SkViewRegister reg(MyFactory);
118