blob: 9ae56ce40f796f8eabab1383247ea2c49de4fac3 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com113244f2009-08-31 21:04:24 +00008#include "SampleCode.h"
9#include "SkColorPriv.h"
10#include "SkGradientShader.h"
11#include "SkView.h"
12#include "SkCanvas.h"
13#include "SkUtils.h"
14
15static SkBitmap make_bitmap() {
16 SkBitmap bm;
17 SkColorTable* ctable = new SkColorTable(256);
18
19 SkPMColor* c = ctable->lockColors();
20 for (int i = 0; i < 256; i++) {
21 c[i] = SkPackARGB32(255 - i, 0, 0, 0);
22 }
23 ctable->unlockColors(true);
24 bm.setConfig(SkBitmap::kIndex8_Config, 256, 256);
25 bm.allocPixels(ctable);
26 ctable->unref();
27
28 bm.lockPixels();
29 const float cx = bm.width() * 0.5f;
30 const float cy = bm.height() * 0.5f;
31 for (int y = 0; y < bm.height(); y++) {
32 float dy = y - cy;
33 dy *= dy;
34 uint8_t* p = bm.getAddr8(0, y);
35 for (int x = 0; x < 256; x++) {
36 float dx = x - cx;
37 dx *= dx;
38 float d = (dx + dy) / (cx/2);
39 int id = (int)d;
40 if (id > 255) {
41 id = 255;
42 }
43 p[x] = id;
44 }
45 }
46 bm.unlockPixels();
47 return bm;
48}
49
reed@google.com0faac1e2011-05-11 05:58:58 +000050class ExtractAlphaView : public SampleView {
reed@android.com113244f2009-08-31 21:04:24 +000051 SkBitmap fBM8;
52 SkBitmap fBM32;
53 SkBitmap fBM4;
54public:
55 ExtractAlphaView() {
56 fBM8 = make_bitmap();
57 fBM8.copyTo(&fBM32, SkBitmap::kARGB_8888_Config);
58 fBM8.copyTo(&fBM4, SkBitmap::kARGB_4444_Config);
reed@google.com0faac1e2011-05-11 05:58:58 +000059
60 this->setBGColor(0xFFDDDDDD);
reed@android.com113244f2009-08-31 21:04:24 +000061 }
62
63protected:
64 // overrides from SkEventSink
65 virtual bool onQuery(SkEvent* evt) {
66 if (SampleCode::TitleQ(*evt)) {
67 SampleCode::TitleR(evt, "DitherBitmap");
68 return true;
69 }
70 return this->INHERITED::onQuery(evt);
71 }
72
reed@google.com0faac1e2011-05-11 05:58:58 +000073 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com113244f2009-08-31 21:04:24 +000074 SkPaint paint;
reed@android.com34e85802009-09-02 21:11:44 +000075 paint.setAntiAlias(true);
76 paint.setStyle(SkPaint::kStroke_Style);
77
78 SkMatrix matrix;
79 matrix.setScale(3.55f, 80.f);
80 canvas->setMatrix(matrix);
81
82 paint.setStrokeWidth(0.0588f);
83 canvas->drawLine(10, 5, 30, 4.8f, paint);
84
85 paint.setStrokeWidth(0.06f);
86 canvas->drawLine(20, 5, 40, 4.8f, paint);
reed@android.com113244f2009-08-31 21:04:24 +000087 }
88
89private:
reed@google.com0faac1e2011-05-11 05:58:58 +000090 typedef SampleView INHERITED;
reed@android.com113244f2009-08-31 21:04:24 +000091};
92
93//////////////////////////////////////////////////////////////////////////////
94
95static SkView* MyFactory() { return new ExtractAlphaView; }
96static SkViewRegister reg(MyFactory);
97