SampleFilter2.cpp revision 6fcbfcead5dc1b61fa5b4c139a1a3714e8c58091
1
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkGradientShader.h"
12#include "SkGraphics.h"
13#include "SkImageDecoder.h"
14#include "SkPath.h"
15#include "SkRegion.h"
16#include "SkShader.h"
17#include "SkUtils.h"
18#include "SkXfermode.h"
19#include "SkColorPriv.h"
20#include "SkColorFilter.h"
21#include "SkTime.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            SkImageDecoder::DecodeFile(gNames[i], &fBitmaps[i],
39                                       SkBitmap::kARGB_8888_Config,
40                                   SkImageDecoder::kDecodePixels_Mode, NULL);
41        }
42        for (int i = fBitmapCount/2; i < fBitmapCount; i++) {
43            SkImageDecoder::DecodeFile(gNames[i-fBitmapCount/2], &fBitmaps[i],
44                                       SkBitmap::kRGB_565_Config,
45                                   SkImageDecoder::kDecodePixels_Mode, NULL);
46        }
47        fCurrIndex = 0;
48
49        this->setBGColor(SK_ColorGRAY);
50    }
51
52    virtual ~Filter2View() {
53        delete[] fBitmaps;
54    }
55
56protected:
57    // overrides from SkEventSink
58    virtual bool onQuery(SkEvent* evt) {
59        if (SampleCode::TitleQ(*evt)) {
60            SkString str("Filter/Dither ");
61            str.append(gNames[fCurrIndex]);
62            SampleCode::TitleR(evt, str.c_str());
63            return true;
64        }
65        return this->INHERITED::onQuery(evt);
66    }
67
68    virtual void onDrawContent(SkCanvas* canvas) {
69        canvas->translate(SkIntToScalar(10), SkIntToScalar(50));
70
71        const SkScalar W = SkIntToScalar(fBitmaps[0].width() + 1);
72        const SkScalar H = SkIntToScalar(fBitmaps[0].height() + 1);
73        SkPaint paint;
74
75        const SkScalar scale = SkFloatToScalar(0.897917f);
76        canvas->scale(SK_Scalar1, scale);
77
78        for (int k = 0; k < 2; k++) {
79            paint.setFilterBitmap(k == 1);
80            for (int j = 0; j < 2; j++) {
81                paint.setDither(j == 1);
82                for (int i = 0; i < fBitmapCount; i++) {
83                    SkScalar x = (k * fBitmapCount + j) * W;
84                    SkScalar y = i * H;
85                    x = SkIntToScalar(SkScalarRound(x));
86                    y = SkIntToScalar(SkScalarRound(y));
87                    canvas->drawBitmap(fBitmaps[i], x, y, &paint);
88                    if (i == 0) {
89                        SkPaint p;
90                        p.setAntiAlias(true);
91                        p.setTextAlign(SkPaint::kCenter_Align);
92                        p.setTextSize(SkIntToScalar(18));
93                        SkString s("dither=");
94                        s.appendS32(paint.isDither());
95                        s.append(" filter=");
96                        s.appendS32(paint.isFilterBitmap());
97                        canvas->drawText(s.c_str(), s.size(), x + W/2,
98                                         y - p.getTextSize(), p);
99                    }
100                    if (k+j == 2) {
101                        SkPaint p;
102                        p.setAntiAlias(true);
103                        p.setTextSize(SkIntToScalar(18));
104                        SkString s;
105                        s.append(" depth=");
106                        s.appendS32(fBitmaps[i].config() == SkBitmap::kRGB_565_Config ? 16 : 32);
107                        canvas->drawText(s.c_str(), s.size(), x + W + SkIntToScalar(4),
108                                         y + H/2, p);
109                    }
110                }
111            }
112        }
113    }
114
115private:
116    typedef SampleView INHERITED;
117};
118
119//////////////////////////////////////////////////////////////////////////////
120
121static SkView* MyFactory() { return new Filter2View; }
122static SkViewRegister reg(MyFactory);
123