reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame^] | 1 | #include "SampleCode.h" |
| 2 | #include "SkView.h" |
| 3 | #include "SkCanvas.h" |
| 4 | #include "SkGradientShader.h" |
| 5 | #include "SkPath.h" |
| 6 | #include "SkRegion.h" |
| 7 | #include "SkShader.h" |
| 8 | #include "SkUtils.h" |
| 9 | #include "SkShaderExtras.h" |
| 10 | #include "Sk1DPathEffect.h" |
| 11 | #include "SkCornerPathEffect.h" |
| 12 | #include "SkPathMeasure.h" |
| 13 | #include "SkRandom.h" |
| 14 | #include "SkColorPriv.h" |
| 15 | #include "SkColorFilter.h" |
| 16 | #include "SkDither.h" |
| 17 | |
| 18 | static void draw_sweep(SkCanvas* c, int width, int height, SkScalar angle) { |
| 19 | SkRect r; |
| 20 | SkPaint p; |
| 21 | |
| 22 | p.setAntiAlias(true); |
| 23 | // p.setDither(true); |
| 24 | p.setStrokeWidth(SkIntToScalar(width/10)); |
| 25 | p.setStyle(SkPaint::kStroke_Style); |
| 26 | |
| 27 | r.set(0, 0, SkIntToScalar(width), SkIntToScalar(height)); |
| 28 | |
| 29 | // SkColor colors[] = { SK_ColorRED, SK_ColorBLUE, SK_ColorGREEN, SK_ColorCYAN }; |
| 30 | SkColor colors[] = { 0x4c737373, 0x4c737373, 0xffffd300 }; |
| 31 | SkShader* s = SkGradientShader::CreateSweep(r.centerX(), r.centerY(), |
| 32 | colors, NULL, SK_ARRAY_COUNT(colors)); |
| 33 | p.setShader(s)->unref(); |
| 34 | |
| 35 | SkAutoCanvasRestore acr(c, true); |
| 36 | |
| 37 | c->translate(r.centerX(), r.centerY()); |
| 38 | c->rotate(angle); |
| 39 | c->translate(-r.centerX(), -r.centerY()); |
| 40 | |
| 41 | SkRect bounds = r; |
| 42 | r.inset(p.getStrokeWidth(), p.getStrokeWidth()); |
| 43 | SkRect innerBounds = r; |
| 44 | |
| 45 | if (true) { |
| 46 | c->drawOval(r, p); |
| 47 | } else { |
| 48 | SkScalar x = r.centerX(); |
| 49 | SkScalar y = r.centerY(); |
| 50 | SkScalar radius = r.width() / 2; |
| 51 | SkScalar thickness = p.getStrokeWidth(); |
| 52 | SkScalar sweep = SkFloatToScalar(360.0f); |
| 53 | SkPath path; |
| 54 | |
| 55 | path.moveTo(x + radius, y); |
| 56 | // outer top |
| 57 | path.lineTo(x + radius + thickness, y); |
| 58 | // outer arc |
| 59 | path.arcTo(bounds, 0, sweep, false); |
| 60 | // inner arc |
| 61 | path.arcTo(innerBounds, sweep, -sweep, false); |
| 62 | path.close(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | static void make_bm(SkBitmap* bm) |
| 67 | { |
| 68 | bm->setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 69 | bm->allocPixels(); |
| 70 | #if 0 |
| 71 | bm->eraseColor(SK_ColorBLUE); |
| 72 | return; |
| 73 | #else |
| 74 | bm->eraseColor(0); |
| 75 | #endif |
| 76 | |
| 77 | SkCanvas c(*bm); |
| 78 | draw_sweep(&c, bm->width(), bm->height(), 0); |
| 79 | } |
| 80 | |
| 81 | static void pre_dither(const SkBitmap& bm) |
| 82 | { |
| 83 | SkAutoLockPixels alp(bm); |
| 84 | |
| 85 | for (unsigned y = 0; y < bm.height(); y++) { |
| 86 | DITHER_4444_SCAN(y); |
| 87 | |
| 88 | SkPMColor* p = bm.getAddr32(0, y); |
| 89 | for (unsigned x = 0; x < bm.width(); x++) { |
| 90 | SkPMColor c = *p; |
| 91 | |
| 92 | unsigned a = SkGetPackedA32(c); |
| 93 | unsigned r = SkGetPackedR32(c); |
| 94 | unsigned g = SkGetPackedG32(c); |
| 95 | unsigned b = SkGetPackedB32(c); |
| 96 | |
| 97 | unsigned d = DITHER_VALUE(x); |
| 98 | |
| 99 | a = SkDITHER_A32To4444(a, d); |
| 100 | r = SkDITHER_R32To4444(r, d); |
| 101 | g = SkDITHER_G32To4444(g, d); |
| 102 | b = SkDITHER_B32To4444(b, d); |
| 103 | |
| 104 | a = SkA4444ToA32(a); |
| 105 | r = SkR4444ToR32(r); |
| 106 | g = SkG4444ToG32(g); |
| 107 | b = SkB4444ToB32(b); |
| 108 | |
| 109 | *p++ = SkPackARGB32(a, r, g, b); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | class DitherView : public SkView { |
| 115 | public: |
| 116 | SkBitmap fBM, fBMPreDither, fBM16; |
| 117 | SkScalar fAngle; |
| 118 | |
| 119 | DitherView() { |
| 120 | make_bm(&fBM); |
| 121 | make_bm(&fBMPreDither); |
| 122 | pre_dither(fBMPreDither); |
| 123 | fBM.copyTo(&fBM16, SkBitmap::kARGB_4444_Config); |
| 124 | |
| 125 | fAngle = 0; |
| 126 | } |
| 127 | |
| 128 | protected: |
| 129 | // overrides from SkEventSink |
| 130 | virtual bool onQuery(SkEvent* evt) { |
| 131 | if (SampleCode::TitleQ(*evt)) { |
| 132 | SampleCode::TitleR(evt, "Dither"); |
| 133 | return true; |
| 134 | } |
| 135 | return this->INHERITED::onQuery(evt); |
| 136 | } |
| 137 | |
| 138 | void drawBG(SkCanvas* canvas) { |
| 139 | // canvas->drawColor(0xFFDDDDDD); |
| 140 | canvas->drawColor(0xFF181818); |
| 141 | } |
| 142 | |
| 143 | virtual void onDraw(SkCanvas* canvas) { |
| 144 | this->drawBG(canvas); |
| 145 | |
| 146 | SkPaint paint; |
| 147 | SkScalar x = SkIntToScalar(10); |
| 148 | SkScalar y = SkIntToScalar(10); |
| 149 | const SkScalar DX = SkIntToScalar(fBM.width() + 10); |
| 150 | |
| 151 | paint.setAntiAlias(true); |
| 152 | |
| 153 | if (true) { |
| 154 | canvas->drawBitmap(fBM, x, y, &paint); |
| 155 | x += DX; |
| 156 | paint.setDither(true); |
| 157 | canvas->drawBitmap(fBM, x, y, &paint); |
| 158 | |
| 159 | x += DX; |
| 160 | paint.setDither(false); |
| 161 | canvas->drawBitmap(fBMPreDither, x, y, &paint); |
| 162 | |
| 163 | x += DX; |
| 164 | canvas->drawBitmap(fBM16, x, y, &paint); |
| 165 | } |
| 166 | |
| 167 | canvas->translate(DX, DX*2); |
| 168 | draw_sweep(canvas, fBM.width(), fBM.height(), fAngle); |
| 169 | canvas->translate(DX, 0); |
| 170 | draw_sweep(canvas, fBM.width()>>1, fBM.height()>>1, fAngle); |
| 171 | canvas->translate(DX, 0); |
| 172 | draw_sweep(canvas, fBM.width()>>2, fBM.height()>>2, fAngle); |
| 173 | |
| 174 | fAngle += SK_Scalar1/2; |
| 175 | this->inval(NULL); |
| 176 | } |
| 177 | |
| 178 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) |
| 179 | { |
| 180 | // fSweep += SK_Scalar1; |
| 181 | this->inval(NULL); |
| 182 | return this->INHERITED::onFindClickHandler(x, y); |
| 183 | } |
| 184 | |
| 185 | virtual bool onClick(Click* click) |
| 186 | { |
| 187 | return this->INHERITED::onClick(click); |
| 188 | } |
| 189 | |
| 190 | private: |
| 191 | typedef SkView INHERITED; |
| 192 | }; |
| 193 | |
| 194 | ////////////////////////////////////////////////////////////////////////////// |
| 195 | |
| 196 | static SkView* MyFactory() { return new DitherView; } |
| 197 | static SkViewRegister reg(MyFactory); |
| 198 | |