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