blob: 860272d892999e73c7d295e4faadb60e4ea2430f [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
reed@google.com0faac1e2011-05-11 05:58:58 +000043class ExtractAlphaView : public SampleView {
reed@android.com113244f2009-08-31 21:04:24 +000044 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);
reed@google.com0faac1e2011-05-11 05:58:58 +000052
53 this->setBGColor(0xFFDDDDDD);
reed@android.com113244f2009-08-31 21:04:24 +000054 }
55
56protected:
57 // overrides from SkEventSink
58 virtual bool onQuery(SkEvent* evt) {
59 if (SampleCode::TitleQ(*evt)) {
60 SampleCode::TitleR(evt, "DitherBitmap");
61 return true;
62 }
63 return this->INHERITED::onQuery(evt);
64 }
65
reed@google.com0faac1e2011-05-11 05:58:58 +000066 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com113244f2009-08-31 21:04:24 +000067 SkPaint paint;
reed@android.com34e85802009-09-02 21:11:44 +000068 paint.setAntiAlias(true);
69 paint.setStyle(SkPaint::kStroke_Style);
70
71 SkMatrix matrix;
72 matrix.setScale(3.55f, 80.f);
73 canvas->setMatrix(matrix);
74
75 paint.setStrokeWidth(0.0588f);
76 canvas->drawLine(10, 5, 30, 4.8f, paint);
77
78 paint.setStrokeWidth(0.06f);
79 canvas->drawLine(20, 5, 40, 4.8f, paint);
reed@android.com113244f2009-08-31 21:04:24 +000080 }
81
82private:
reed@google.com0faac1e2011-05-11 05:58:58 +000083 typedef SampleView INHERITED;
reed@android.com113244f2009-08-31 21:04:24 +000084};
85
86//////////////////////////////////////////////////////////////////////////////
87
88static SkView* MyFactory() { return new ExtractAlphaView; }
89static SkViewRegister reg(MyFactory);
90