reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 1 | #include "SampleCode.h" |
| 2 | #include "SkColorPriv.h" |
| 3 | #include "SkView.h" |
| 4 | #include "SkCanvas.h" |
| 5 | #include "SkUtils.h" |
| 6 | |
| 7 | static SkBitmap make_bitmap() { |
| 8 | SkBitmap bm; |
| 9 | SkColorTable* ctable = new SkColorTable(256); |
| 10 | |
| 11 | SkPMColor* c = ctable->lockColors(); |
| 12 | for (int i = 0; i < 256; i++) { |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame^] | 13 | c[i] = SkPackARGB32(0xFF, i, 0, 0); |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 14 | } |
| 15 | ctable->unlockColors(true); |
| 16 | bm.setConfig(SkBitmap::kIndex8_Config, 256, 32); |
| 17 | bm.allocPixels(ctable); |
| 18 | ctable->unref(); |
| 19 | |
| 20 | bm.lockPixels(); |
| 21 | for (int y = 0; y < bm.height(); y++) { |
| 22 | uint8_t* p = bm.getAddr8(0, y); |
| 23 | for (int x = 0; x < 256; x++) { |
| 24 | p[x] = x; |
| 25 | } |
| 26 | } |
| 27 | bm.unlockPixels(); |
| 28 | return bm; |
| 29 | } |
| 30 | |
| 31 | class DitherBitmapView : public SkView { |
| 32 | SkBitmap fBM8; |
| 33 | SkBitmap fBM32; |
| 34 | public: |
| 35 | DitherBitmapView() { |
| 36 | fBM8 = make_bitmap(); |
| 37 | fBM8.copyTo(&fBM32, SkBitmap::kARGB_8888_Config); |
| 38 | } |
| 39 | |
| 40 | protected: |
| 41 | // overrides from SkEventSink |
| 42 | virtual bool onQuery(SkEvent* evt) { |
| 43 | if (SampleCode::TitleQ(*evt)) { |
| 44 | SampleCode::TitleR(evt, "DitherBitmap"); |
| 45 | return true; |
| 46 | } |
| 47 | return this->INHERITED::onQuery(evt); |
| 48 | } |
| 49 | |
| 50 | void drawBG(SkCanvas* canvas) { |
| 51 | canvas->drawColor(0xFFDDDDDD); |
| 52 | } |
| 53 | |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame^] | 54 | static void setBitmapOpaque(SkBitmap* bm, bool isOpaque) { |
| 55 | SkAutoLockPixels alp(*bm); // needed for ctable |
| 56 | bm->setIsOpaque(isOpaque); |
| 57 | SkColorTable* ctable = bm->getColorTable(); |
| 58 | if (ctable) { |
| 59 | ctable->setIsOpaque(isOpaque); |
| 60 | } |
| 61 | } |
| 62 | |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 63 | static void draw2(SkCanvas* canvas, const SkBitmap& bm) { |
| 64 | SkPaint paint; |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame^] | 65 | SkBitmap bitmap(bm); |
| 66 | |
| 67 | setBitmapOpaque(&bitmap, false); |
| 68 | paint.setDither(false); |
| 69 | canvas->drawBitmap(bitmap, 0, 0, &paint); |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 70 | paint.setDither(true); |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame^] | 71 | canvas->drawBitmap(bitmap, 0, SkIntToScalar(bm.height() + 10), &paint); |
| 72 | |
| 73 | setBitmapOpaque(&bitmap, true); |
| 74 | SkScalar x = SkIntToScalar(bm.width() + 10); |
| 75 | paint.setDither(false); |
| 76 | canvas->drawBitmap(bitmap, x, 0, &paint); |
| 77 | paint.setDither(true); |
| 78 | canvas->drawBitmap(bitmap, x, SkIntToScalar(bm.height() + 10), &paint); |
reed@android.com | 755dd47 | 2009-08-20 21:29:45 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | virtual void onDraw(SkCanvas* canvas) { |
| 82 | drawBG(canvas); |
| 83 | |
| 84 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 85 | |
| 86 | draw2(canvas, fBM8); |
| 87 | canvas->translate(0, SkIntToScalar(fBM8.height() *3)); |
| 88 | draw2(canvas, fBM32); |
| 89 | } |
| 90 | |
| 91 | private: |
| 92 | typedef SkView INHERITED; |
| 93 | }; |
| 94 | |
| 95 | ////////////////////////////////////////////////////////////////////////////// |
| 96 | |
| 97 | static SkView* MyFactory() { return new DitherBitmapView; } |
| 98 | static SkViewRegister reg(MyFactory); |
| 99 | |