blob: 37c49a09689023b06180d7e9f31e973b786676d7 [file] [log] [blame]
reed@android.com113244f2009-08-31 21:04:24 +00001#include "SampleCode.h"
2#include "SkColorPriv.h"
3#include "SkGradientShader.h"
4#include "SkView.h"
5#include "SkCanvas.h"
6#include "SkUtils.h"
7
8static SkBitmap make_bitmap() {
9 SkBitmap bm;
10 SkColorTable* ctable = new SkColorTable(256);
11
12 SkPMColor* c = ctable->lockColors();
13 for (int i = 0; i < 256; i++) {
14 c[i] = SkPackARGB32(255 - i, 0, 0, 0);
15 }
16 ctable->unlockColors(true);
17 bm.setConfig(SkBitmap::kIndex8_Config, 256, 256);
18 bm.allocPixels(ctable);
19 ctable->unref();
20
21 bm.lockPixels();
22 const float cx = bm.width() * 0.5f;
23 const float cy = bm.height() * 0.5f;
24 for (int y = 0; y < bm.height(); y++) {
25 float dy = y - cy;
26 dy *= dy;
27 uint8_t* p = bm.getAddr8(0, y);
28 for (int x = 0; x < 256; x++) {
29 float dx = x - cx;
30 dx *= dx;
31 float d = (dx + dy) / (cx/2);
32 int id = (int)d;
33 if (id > 255) {
34 id = 255;
35 }
36 p[x] = id;
37 }
38 }
39 bm.unlockPixels();
40 return bm;
41}
42
43class ExtractAlphaView : public SkView {
44 SkBitmap fBM8;
45 SkBitmap fBM32;
46 SkBitmap fBM4;
47public:
48 ExtractAlphaView() {
49 fBM8 = make_bitmap();
50 fBM8.copyTo(&fBM32, SkBitmap::kARGB_8888_Config);
51 fBM8.copyTo(&fBM4, SkBitmap::kARGB_4444_Config);
52 }
53
54protected:
55 // overrides from SkEventSink
56 virtual bool onQuery(SkEvent* evt) {
57 if (SampleCode::TitleQ(*evt)) {
58 SampleCode::TitleR(evt, "DitherBitmap");
59 return true;
60 }
61 return this->INHERITED::onQuery(evt);
62 }
63
64 void drawBG(SkCanvas* canvas) {
65 canvas->drawColor(0xFFDDDDDD);
66 }
67
68 virtual void onDraw(SkCanvas* canvas) {
69 drawBG(canvas);
70
71 const SkBitmap* srcBM[] = { &fBM8, &fBM32, &fBM4 };
72
73 SkPaint paint;
74 paint.setColor(SK_ColorRED);
75 for (int i = 0; i < 3; i++) {
76 canvas->drawBitmap(*srcBM[i], 0, 0, &paint);
77 SkBitmap tmp;
78 srcBM[i]->extractAlpha(&tmp);
79 canvas->drawBitmap(tmp, 0, SkIntToScalar(tmp.height() + 10), &paint);
80
81 canvas->translate(SkIntToScalar(tmp.width() + 10), 0);
82 }
83 }
84
85private:
86 typedef SkView INHERITED;
87};
88
89//////////////////////////////////////////////////////////////////////////////
90
91static SkView* MyFactory() { return new ExtractAlphaView; }
92static SkViewRegister reg(MyFactory);
93