blob: 30ac2bd0015b80032116533a6afea1489e864bc1 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001#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 "SkPorterDuff.h"
9#include "SkRegion.h"
10#include "SkShader.h"
11#include "SkUtils.h"
12#include "SkXfermode.h"
13#include "SkShaderExtras.h"
14#include "SkColorPriv.h"
15#include "SkColorFilter.h"
16#include "SkTime.h"
17
18static const char* gNames[] = {
19 "/skimages/background_01.png"
20};
21
22class Filter2View : public SkView {
23public:
24 SkBitmap* fBitmaps;
25 int fBitmapCount;
26 int fCurrIndex;
27
28 Filter2View() {
29 fBitmapCount = SK_ARRAY_COUNT(gNames)*2;
30 fBitmaps = new SkBitmap[fBitmapCount];
31
32 for (int i = 0; i < fBitmapCount/2; i++) {
33 SkImageDecoder::DecodeFile(gNames[i], &fBitmaps[i],
34 SkBitmap::kARGB_8888_Config,
35 SkImageDecoder::kDecodePixels_Mode);
36 }
37 for (int i = fBitmapCount/2; i < fBitmapCount; i++) {
38 SkImageDecoder::DecodeFile(gNames[i-fBitmapCount/2], &fBitmaps[i],
39 SkBitmap::kRGB_565_Config,
40 SkImageDecoder::kDecodePixels_Mode);
41 }
42 fCurrIndex = 0;
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 void drawBG(SkCanvas* canvas) {
62// canvas->drawColor(0xFFDDDDDD);
63 canvas->drawColor(SK_ColorGRAY);
64// canvas->drawColor(SK_ColorWHITE);
65 }
66
67 virtual void onDraw(SkCanvas* canvas) {
68 this->drawBG(canvas);
69
70 canvas->translate(SkIntToScalar(10), SkIntToScalar(50));
71
72 const SkScalar W = SkIntToScalar(fBitmaps[0].width() + 1);
73 const SkScalar H = SkIntToScalar(fBitmaps[0].height() + 1);
74 SkPaint paint;
75
76 const SkScalar scale = SkFloatToScalar(0.897917f);
77 canvas->scale(SK_Scalar1, scale);
78
79 for (int k = 0; k < 2; k++) {
80 paint.setFilterBitmap(k == 1);
81 for (int j = 0; j < 2; j++) {
82 paint.setDither(j == 1);
83 for (int i = 0; i < fBitmapCount; i++) {
84 SkScalar x = (k * fBitmapCount + j) * W;
85 SkScalar y = i * H;
86 x = SkIntToScalar(SkScalarRound(x));
87 y = SkIntToScalar(SkScalarRound(y));
88 canvas->drawBitmap(fBitmaps[i], x, y, &paint);
89 if (i == 0) {
90 SkPaint p;
91 p.setAntiAlias(true);
92 p.setTextAlign(SkPaint::kCenter_Align);
93 p.setTextSize(SkIntToScalar(18));
94 SkString s("dither=");
95 s.appendS32(paint.isDither());
96 s.append(" filter=");
97 s.appendS32(paint.isFilterBitmap());
98 canvas->drawText(s.c_str(), s.size(), x + W/2,
99 y - p.getTextSize(), p);
100 }
101 if (k+j == 2) {
102 SkPaint p;
103 p.setAntiAlias(true);
104 p.setTextSize(SkIntToScalar(18));
105 SkString s;
106 s.append(" depth=");
107 s.appendS32(fBitmaps[i].config() == SkBitmap::kRGB_565_Config ? 16 : 32);
108 canvas->drawText(s.c_str(), s.size(), x + W + SkIntToScalar(4),
109 y + H/2, p);
110 }
111 }
112 }
113 }
114 }
115
116private:
117 typedef SkView INHERITED;
118};
119
120//////////////////////////////////////////////////////////////////////////////
121
122static SkView* MyFactory() { return new Filter2View; }
123static SkViewRegister reg(MyFactory);
124