blob: 928f2ca8fa0ea231bf3bec68ff53a50ce0ff2026 [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);
reed@android.com34e85802009-09-02 21:11:44 +000070
reed@android.com113244f2009-08-31 21:04:24 +000071 SkPaint paint;
reed@android.com34e85802009-09-02 21:11:44 +000072 paint.setAntiAlias(true);
73 paint.setStyle(SkPaint::kStroke_Style);
74
75 SkMatrix matrix;
76 matrix.setScale(3.55f, 80.f);
77 canvas->setMatrix(matrix);
78
79 paint.setStrokeWidth(0.0588f);
80 canvas->drawLine(10, 5, 30, 4.8f, paint);
81
82 paint.setStrokeWidth(0.06f);
83 canvas->drawLine(20, 5, 40, 4.8f, paint);
reed@android.com113244f2009-08-31 21:04:24 +000084 }
85
86private:
87 typedef SkView INHERITED;
88};
89
90//////////////////////////////////////////////////////////////////////////////
91
92static SkView* MyFactory() { return new ExtractAlphaView; }
93static SkViewRegister reg(MyFactory);
94