blob: 35816a213a1a9bee73db294aba0549546e143a14 [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
7static SkBitmap make_bitmap() {
8 SkBitmap bm;
9 SkColorTable* ctable = new SkColorTable(256);
10
11 SkPMColor* c = ctable->lockColors();
12 for (int i = 0; i < 256; i++) {
reed@android.comcafc9f92009-08-22 03:44:57 +000013 c[i] = SkPackARGB32(0xFF, i, 0, 0);
reed@android.com755dd472009-08-20 21:29:45 +000014 }
15 ctable->unlockColors(true);
16 bm.setConfig(SkBitmap::kIndex8_Config, 256, 32);
17 bm.allocPixels(ctable);
18 ctable->unref();
19
20 bm.lockPixels();
21 for (int y = 0; y < bm.height(); y++) {
22 uint8_t* p = bm.getAddr8(0, y);
23 for (int x = 0; x < 256; x++) {
24 p[x] = x;
25 }
26 }
27 bm.unlockPixels();
28 return bm;
29}
30
31class DitherBitmapView : public SkView {
32 SkBitmap fBM8;
33 SkBitmap fBM32;
34public:
35 DitherBitmapView() {
36 fBM8 = make_bitmap();
37 fBM8.copyTo(&fBM32, SkBitmap::kARGB_8888_Config);
38 }
39
40protected:
41 // overrides from SkEventSink
42 virtual bool onQuery(SkEvent* evt) {
43 if (SampleCode::TitleQ(*evt)) {
44 SampleCode::TitleR(evt, "DitherBitmap");
45 return true;
46 }
47 return this->INHERITED::onQuery(evt);
48 }
49
50 void drawBG(SkCanvas* canvas) {
51 canvas->drawColor(0xFFDDDDDD);
52 }
53
reed@android.comcafc9f92009-08-22 03:44:57 +000054 static void setBitmapOpaque(SkBitmap* bm, bool isOpaque) {
55 SkAutoLockPixels alp(*bm); // needed for ctable
56 bm->setIsOpaque(isOpaque);
57 SkColorTable* ctable = bm->getColorTable();
58 if (ctable) {
59 ctable->setIsOpaque(isOpaque);
60 }
61 }
62
reed@android.com755dd472009-08-20 21:29:45 +000063 static void draw2(SkCanvas* canvas, const SkBitmap& bm) {
64 SkPaint paint;
reed@android.comcafc9f92009-08-22 03:44:57 +000065 SkBitmap bitmap(bm);
66
67 setBitmapOpaque(&bitmap, false);
68 paint.setDither(false);
69 canvas->drawBitmap(bitmap, 0, 0, &paint);
reed@android.com755dd472009-08-20 21:29:45 +000070 paint.setDither(true);
reed@android.comcafc9f92009-08-22 03:44:57 +000071 canvas->drawBitmap(bitmap, 0, SkIntToScalar(bm.height() + 10), &paint);
72
73 setBitmapOpaque(&bitmap, true);
74 SkScalar x = SkIntToScalar(bm.width() + 10);
75 paint.setDither(false);
76 canvas->drawBitmap(bitmap, x, 0, &paint);
77 paint.setDither(true);
78 canvas->drawBitmap(bitmap, x, SkIntToScalar(bm.height() + 10), &paint);
reed@android.com755dd472009-08-20 21:29:45 +000079 }
80
81 virtual void onDraw(SkCanvas* canvas) {
82 drawBG(canvas);
83
84 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
85
86 draw2(canvas, fBM8);
87 canvas->translate(0, SkIntToScalar(fBM8.height() *3));
88 draw2(canvas, fBM32);
89 }
90
91private:
92 typedef SkView INHERITED;
93};
94
95//////////////////////////////////////////////////////////////////////////////
96
97static SkView* MyFactory() { return new DitherBitmapView; }
98static SkViewRegister reg(MyFactory);
99