SampleFilter2.cpp revision 961ddb04a0a7aba843032d829ab867518e52559e
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 SampleView {
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        this->setBGColor(SK_ColorGRAY);
43    }
44
45    virtual ~Filter2View() {
46        delete[] fBitmaps;
47    }
48
49protected:
50    // overrides from SkEventSink
51    virtual bool onQuery(SkEvent* evt) {
52        if (SampleCode::TitleQ(*evt)) {
53            SkString str("Filter/Dither ");
54            str.append(gNames[fCurrIndex]);
55            SampleCode::TitleR(evt, str.c_str());
56            return true;
57        }
58        return this->INHERITED::onQuery(evt);
59    }
60
61    virtual void onDrawContent(SkCanvas* canvas) {
62        canvas->translate(SkIntToScalar(10), SkIntToScalar(50));
63
64        const SkScalar W = SkIntToScalar(fBitmaps[0].width() + 1);
65        const SkScalar H = SkIntToScalar(fBitmaps[0].height() + 1);
66        SkPaint paint;
67
68        const SkScalar scale = SkFloatToScalar(0.897917f);
69        canvas->scale(SK_Scalar1, scale);
70
71        for (int k = 0; k < 2; k++) {
72            paint.setFilterBitmap(k == 1);
73            for (int j = 0; j < 2; j++) {
74                paint.setDither(j == 1);
75                for (int i = 0; i < fBitmapCount; i++) {
76                    SkScalar x = (k * fBitmapCount + j) * W;
77                    SkScalar y = i * H;
78                    x = SkIntToScalar(SkScalarRound(x));
79                    y = SkIntToScalar(SkScalarRound(y));
80                    canvas->drawBitmap(fBitmaps[i], x, y, &paint);
81                    if (i == 0) {
82                        SkPaint p;
83                        p.setAntiAlias(true);
84                        p.setTextAlign(SkPaint::kCenter_Align);
85                        p.setTextSize(SkIntToScalar(18));
86                        SkString s("dither=");
87                        s.appendS32(paint.isDither());
88                        s.append(" filter=");
89                        s.appendS32(paint.isFilterBitmap());
90                        canvas->drawText(s.c_str(), s.size(), x + W/2,
91                                         y - p.getTextSize(), p);
92                    }
93                    if (k+j == 2) {
94                        SkPaint p;
95                        p.setAntiAlias(true);
96                        p.setTextSize(SkIntToScalar(18));
97                        SkString s;
98                        s.append(" depth=");
99                        s.appendS32(fBitmaps[i].config() == SkBitmap::kRGB_565_Config ? 16 : 32);
100                        canvas->drawText(s.c_str(), s.size(), x + W + SkIntToScalar(4),
101                                         y + H/2, p);
102                    }
103                }
104            }
105        }
106    }
107
108private:
109    typedef SampleView INHERITED;
110};
111
112//////////////////////////////////////////////////////////////////////////////
113
114static SkView* MyFactory() { return new Filter2View; }
115static SkViewRegister reg(MyFactory);
116
117