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