blob: e859cdaf44a90bc9a61b1a7917e621b0118d2e1b [file] [log] [blame]
reed@android.com755dd472009-08-20 21:29:45 +00001#include "SampleCode.h"
2#include "SkColorPriv.h"
3#include "SkView.h"
4#include "SkCanvas.h"
5#include "SkUtils.h"
6
reed@android.comc6459962009-08-25 19:15:31 +00007static void test_pathregion() {
8 SkPath path;
9 SkRegion region;
10 path.moveTo(25071800.f, -141823808.f);
11 path.lineTo(25075500.f, -141824000.f);
12 path.lineTo(25075400.f, -141827712.f);
13 path.lineTo(25071810.f, -141827600.f);
14 path.close();
15
16 SkIRect bounds;
17 path.getBounds().round(&bounds);
18 SkRegion clip(bounds);
19 bool result = region.setPath(path, clip); // <-- !! DOWN !!
20 SkDebugf("----- result %d\n", result);
21}
22
reed@android.com755dd472009-08-20 21:29:45 +000023static SkBitmap make_bitmap() {
24 SkBitmap bm;
25 SkColorTable* ctable = new SkColorTable(256);
26
27 SkPMColor* c = ctable->lockColors();
28 for (int i = 0; i < 256; i++) {
reed@android.comcafc9f92009-08-22 03:44:57 +000029 c[i] = SkPackARGB32(0xFF, i, 0, 0);
reed@android.com755dd472009-08-20 21:29:45 +000030 }
31 ctable->unlockColors(true);
32 bm.setConfig(SkBitmap::kIndex8_Config, 256, 32);
33 bm.allocPixels(ctable);
34 ctable->unref();
35
36 bm.lockPixels();
37 for (int y = 0; y < bm.height(); y++) {
38 uint8_t* p = bm.getAddr8(0, y);
39 for (int x = 0; x < 256; x++) {
40 p[x] = x;
41 }
42 }
43 bm.unlockPixels();
44 return bm;
45}
46
47class DitherBitmapView : public SkView {
48 SkBitmap fBM8;
49 SkBitmap fBM32;
50public:
51 DitherBitmapView() {
reed@android.comc6459962009-08-25 19:15:31 +000052 test_pathregion();
reed@android.com755dd472009-08-20 21:29:45 +000053 fBM8 = make_bitmap();
54 fBM8.copyTo(&fBM32, SkBitmap::kARGB_8888_Config);
55 }
56
57protected:
58 // overrides from SkEventSink
59 virtual bool onQuery(SkEvent* evt) {
60 if (SampleCode::TitleQ(*evt)) {
61 SampleCode::TitleR(evt, "DitherBitmap");
62 return true;
63 }
64 return this->INHERITED::onQuery(evt);
65 }
66
67 void drawBG(SkCanvas* canvas) {
68 canvas->drawColor(0xFFDDDDDD);
69 }
70
reed@android.comcafc9f92009-08-22 03:44:57 +000071 static void setBitmapOpaque(SkBitmap* bm, bool isOpaque) {
72 SkAutoLockPixels alp(*bm); // needed for ctable
73 bm->setIsOpaque(isOpaque);
74 SkColorTable* ctable = bm->getColorTable();
75 if (ctable) {
76 ctable->setIsOpaque(isOpaque);
77 }
78 }
79
reed@android.com755dd472009-08-20 21:29:45 +000080 static void draw2(SkCanvas* canvas, const SkBitmap& bm) {
81 SkPaint paint;
reed@android.comcafc9f92009-08-22 03:44:57 +000082 SkBitmap bitmap(bm);
83
84 setBitmapOpaque(&bitmap, false);
85 paint.setDither(false);
86 canvas->drawBitmap(bitmap, 0, 0, &paint);
reed@android.com755dd472009-08-20 21:29:45 +000087 paint.setDither(true);
reed@android.comcafc9f92009-08-22 03:44:57 +000088 canvas->drawBitmap(bitmap, 0, SkIntToScalar(bm.height() + 10), &paint);
89
90 setBitmapOpaque(&bitmap, true);
91 SkScalar x = SkIntToScalar(bm.width() + 10);
92 paint.setDither(false);
93 canvas->drawBitmap(bitmap, x, 0, &paint);
94 paint.setDither(true);
95 canvas->drawBitmap(bitmap, x, SkIntToScalar(bm.height() + 10), &paint);
reed@android.com755dd472009-08-20 21:29:45 +000096 }
97
98 virtual void onDraw(SkCanvas* canvas) {
99 drawBG(canvas);
100
101 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
102
103 draw2(canvas, fBM8);
104 canvas->translate(0, SkIntToScalar(fBM8.height() *3));
105 draw2(canvas, fBM32);
106 }
107
108private:
109 typedef SkView INHERITED;
110};
111
112//////////////////////////////////////////////////////////////////////////////
113
114static SkView* MyFactory() { return new DitherBitmapView; }
115static SkViewRegister reg(MyFactory);
116