1 #include "SampleCode.h" 2 #include "SkColorPriv.h" 3 #include "SkGradientShader.h" 4 #include "SkView.h" 5 #include "SkCanvas.h" 6 #include "SkUtils.h" 7 8 static 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 43 class ExtractAlphaView : public SampleView { 44 SkBitmap fBM8; 45 SkBitmap fBM32; 46 SkBitmap fBM4; 47 public: 48 ExtractAlphaView() { 49 fBM8 = make_bitmap(); 50 fBM8.copyTo(&fBM32, SkBitmap::kARGB_8888_Config); 51 fBM8.copyTo(&fBM4, SkBitmap::kARGB_4444_Config); 52 53 this->setBGColor(0xFFDDDDDD); 54 } 55 56 protected: 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 66 virtual void onDrawContent(SkCanvas* canvas) { 67 SkPaint paint; 68 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); 80 } 81 82 private: 83 typedef SampleView INHERITED; 84 }; 85 86 ////////////////////////////////////////////////////////////////////////////// 87 88 static SkView* MyFactory() { return new ExtractAlphaView; } 89 static SkViewRegister reg(MyFactory); 90 91