blob: df727c42033867ddbda6039dc27a0c4a1eb0ee32 [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.com755dd472009-08-20 21:29:45 +00008#include "SampleCode.h"
9#include "SkColorPriv.h"
reed@android.com3c9b2a42009-08-27 19:28:37 +000010#include "SkGradientShader.h"
reed@android.com755dd472009-08-20 21:29:45 +000011#include "SkView.h"
12#include "SkCanvas.h"
13#include "SkUtils.h"
14
reed@android.com3c9b2a42009-08-27 19:28:37 +000015static void draw_rect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
16 canvas->drawRect(r, p);
17
18 SkPaint frame(p);
19 frame.setShader(NULL);
20 frame.setStyle(SkPaint::kStroke_Style);
21 canvas->drawRect(r, frame);
22}
23
24static void draw_gradient(SkCanvas* canvas) {
25 SkRect r = { 0, 0, SkIntToScalar(256), SkIntToScalar(32) };
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000026 SkPoint pts[] = { { r.fLeft, r.fTop }, { r.fRight, r.fTop } };
reed@android.com3c9b2a42009-08-27 19:28:37 +000027 SkColor colors[] = { 0xFF000000, 0xFFFF0000 };
28 SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, 2,
29 SkShader::kClamp_TileMode);
30
31 SkPaint p;
32 p.setShader(s)->unref();
33 draw_rect(canvas, r, p);
34
35 canvas->translate(0, SkIntToScalar(40));
36 p.setDither(true);
37 draw_rect(canvas, r, p);
38}
39
reed@android.comc6459962009-08-25 19:15:31 +000040static void test_pathregion() {
41 SkPath path;
42 SkRegion region;
rmistry@google.comae933ce2012-08-23 18:19:56 +000043 path.moveTo(25071800.f, -141823808.f);
reed@android.comc6459962009-08-25 19:15:31 +000044 path.lineTo(25075500.f, -141824000.f);
45 path.lineTo(25075400.f, -141827712.f);
46 path.lineTo(25071810.f, -141827600.f);
47 path.close();
48
49 SkIRect bounds;
50 path.getBounds().round(&bounds);
51 SkRegion clip(bounds);
52 bool result = region.setPath(path, clip); // <-- !! DOWN !!
53 SkDebugf("----- result %d\n", result);
54}
55
reed@android.com755dd472009-08-20 21:29:45 +000056static SkBitmap make_bitmap() {
57 SkBitmap bm;
58 SkColorTable* ctable = new SkColorTable(256);
59
60 SkPMColor* c = ctable->lockColors();
61 for (int i = 0; i < 256; i++) {
reed@android.comcafc9f92009-08-22 03:44:57 +000062 c[i] = SkPackARGB32(0xFF, i, 0, 0);
reed@android.com755dd472009-08-20 21:29:45 +000063 }
64 ctable->unlockColors(true);
65 bm.setConfig(SkBitmap::kIndex8_Config, 256, 32);
66 bm.allocPixels(ctable);
67 ctable->unref();
68
69 bm.lockPixels();
70 for (int y = 0; y < bm.height(); y++) {
71 uint8_t* p = bm.getAddr8(0, y);
72 for (int x = 0; x < 256; x++) {
73 p[x] = x;
74 }
75 }
76 bm.unlockPixels();
77 return bm;
78}
79
reed@google.com0faac1e2011-05-11 05:58:58 +000080class DitherBitmapView : public SampleView {
reed@android.com755dd472009-08-20 21:29:45 +000081 SkBitmap fBM8;
82 SkBitmap fBM32;
83public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000084 DitherBitmapView() {
reed@android.comc6459962009-08-25 19:15:31 +000085 test_pathregion();
reed@android.com755dd472009-08-20 21:29:45 +000086 fBM8 = make_bitmap();
87 fBM8.copyTo(&fBM32, SkBitmap::kARGB_8888_Config);
rmistry@google.comae933ce2012-08-23 18:19:56 +000088
reed@google.com0faac1e2011-05-11 05:58:58 +000089 this->setBGColor(0xFFDDDDDD);
reed@android.com755dd472009-08-20 21:29:45 +000090 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000091
reed@android.com755dd472009-08-20 21:29:45 +000092protected:
93 // overrides from SkEventSink
94 virtual bool onQuery(SkEvent* evt) {
95 if (SampleCode::TitleQ(*evt)) {
96 SampleCode::TitleR(evt, "DitherBitmap");
97 return true;
98 }
99 return this->INHERITED::onQuery(evt);
100 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000101
reed@android.comcafc9f92009-08-22 03:44:57 +0000102 static void setBitmapOpaque(SkBitmap* bm, bool isOpaque) {
103 SkAutoLockPixels alp(*bm); // needed for ctable
104 bm->setIsOpaque(isOpaque);
105 SkColorTable* ctable = bm->getColorTable();
106 if (ctable) {
107 ctable->setIsOpaque(isOpaque);
108 }
109 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000110
reed@android.com755dd472009-08-20 21:29:45 +0000111 static void draw2(SkCanvas* canvas, const SkBitmap& bm) {
112 SkPaint paint;
reed@android.comcafc9f92009-08-22 03:44:57 +0000113 SkBitmap bitmap(bm);
114
115 setBitmapOpaque(&bitmap, false);
116 paint.setDither(false);
117 canvas->drawBitmap(bitmap, 0, 0, &paint);
reed@android.com755dd472009-08-20 21:29:45 +0000118 paint.setDither(true);
reed@android.comcafc9f92009-08-22 03:44:57 +0000119 canvas->drawBitmap(bitmap, 0, SkIntToScalar(bm.height() + 10), &paint);
120
121 setBitmapOpaque(&bitmap, true);
122 SkScalar x = SkIntToScalar(bm.width() + 10);
123 paint.setDither(false);
124 canvas->drawBitmap(bitmap, x, 0, &paint);
125 paint.setDither(true);
126 canvas->drawBitmap(bitmap, x, SkIntToScalar(bm.height() + 10), &paint);
reed@android.com755dd472009-08-20 21:29:45 +0000127 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000128
reed@google.com0faac1e2011-05-11 05:58:58 +0000129 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com755dd472009-08-20 21:29:45 +0000130 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
131
132 draw2(canvas, fBM8);
133 canvas->translate(0, SkIntToScalar(fBM8.height() *3));
134 draw2(canvas, fBM32);
reed@android.com3c9b2a42009-08-27 19:28:37 +0000135
136 canvas->translate(0, SkIntToScalar(fBM8.height() *3));
137 draw_gradient(canvas);
reed@android.com755dd472009-08-20 21:29:45 +0000138 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000139
reed@android.com755dd472009-08-20 21:29:45 +0000140private:
reed@google.com0faac1e2011-05-11 05:58:58 +0000141 typedef SampleView INHERITED;
reed@android.com755dd472009-08-20 21:29:45 +0000142};
143
144//////////////////////////////////////////////////////////////////////////////
145
146static SkView* MyFactory() { return new DitherBitmapView; }
147static SkViewRegister reg(MyFactory);