blob: 2c55ab05fa3813a8153b5f15a14b18d5e6bd4e7b [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001#include "SampleCode.h"
2#include "SkView.h"
3#include "SkCanvas.h"
4#include "SkGradientShader.h"
5#include "SkGraphics.h"
6#include "SkImageDecoder.h"
7#include "SkPath.h"
8#include "SkPorterDuff.h"
9#include "SkRegion.h"
10#include "SkShader.h"
11#include "SkUtils.h"
12#include "SkXfermode.h"
13#include "SkShaderExtras.h"
14#include "SkColorPriv.h"
15#include "SkColorFilter.h"
16#include "SkTime.h"
17#include "SkTypeface.h"
18
19#include "SkImageRef_GlobalPool.h"
20#include "SkOSFile.h"
21#include "SkStream.h"
22
23#include "SkBlurDrawLooper.h"
24#include "SkColorMatrixFilter.h"
25
26static void drawmarshmallow(SkCanvas* canvas) {
27 SkBitmap bitmap;
28 SkPaint paint;
29 SkRect r;
30 SkMatrix m;
31
32 SkImageDecoder::DecodeFile("/Users/reed/Downloads/3elfs.jpg", &bitmap);
33 SkShader* s = SkShader::CreateBitmapShader(bitmap,
34 SkShader::kRepeat_TileMode,
35 SkShader::kRepeat_TileMode);
36 paint.setShader(s)->unref();
37 m.setTranslate(SkIntToScalar(250), SkIntToScalar(134));
38 s->setLocalMatrix(m);
39
40 r.set(SkIntToScalar(250),
41 SkIntToScalar(134),
42 SkIntToScalar(250 + 449),
43 SkIntToScalar(134 + 701));
44 paint.setFlags(2);
45
46 canvas->drawRect(r, paint);
47}
48
49static void DrawRoundRect(SkCanvas& canvas) {
50 bool ret = false;
51 SkPaint paint;
52 SkBitmap bitmap;
53 SkMatrix matrix;
54 matrix.reset();
55
56 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1370, 812);
57 bitmap.allocPixels();
58#if 0
59 SkCanvas canvas;
60 canvas.setBitmapDevice(bitmap);
61#endif
62
63 // set up clipper
64 SkRect skclip;
65 skclip.set(SkIntToFixed(284), SkIntToFixed(40), SkIntToFixed(1370), SkIntToFixed(708));
66
67// ret = canvas.clipRect(skclip);
68// SkASSERT(ret);
69
70 matrix.set(SkMatrix::kMTransX, SkFloatToFixed(-1153.28));
71 matrix.set(SkMatrix::kMTransY, SkFloatToFixed(1180.50));
72
73 matrix.set(SkMatrix::kMScaleX, SkFloatToFixed(0.177171));
74 matrix.set(SkMatrix::kMScaleY, SkFloatToFixed(0.177043));
75
76 matrix.set(SkMatrix::kMSkewX, SkFloatToFixed(0.126968));
77 matrix.set(SkMatrix::kMSkewY, SkFloatToFixed(-0.126876));
78
79 matrix.set(SkMatrix::kMPersp0, SkFloatToFixed(0.0));
80 matrix.set(SkMatrix::kMPersp1, SkFloatToFixed(0.0));
81
82 ret = canvas.concat(matrix);
83
84 paint.setAntiAlias(true);
85 paint.setColor(0xb2202020);
86 paint.setStyle(SkPaint::kStroke_Style);
87 paint.setStrokeWidth(SkFloatToFixed(68.13));
88
89 SkRect r;
90 r.set(SkFloatToFixed(-313.714417), SkFloatToFixed(-4.826389), SkFloatToFixed(18014.447266), SkFloatToFixed(1858.154541));
91 canvas.drawRoundRect(r, SkFloatToFixed(91.756363), SkFloatToFixed(91.756363), paint);
92}
93
94// ownership of the stream is transferred
95static bool SetImageRef(SkBitmap* bitmap, SkStream* stream,
96 SkBitmap::Config pref, const char name[] = NULL) {
97#if 0
98 // test buffer streams
99 SkStream* str = new SkBufferStream(stream, 717);
100 stream->unref();
101 stream = str;
102#endif
103
104 SkImageRef* ref = new SkImageRef_GlobalPool(stream, pref, 1);
105 ref->setURI(name);
106 if (!ref->getInfo(bitmap)) {
107 delete ref;
108 return false;
109 }
110 bitmap->setPixelRef(ref)->unref();
111 return true;
112}
113
114//#define SPECIFIC_IMAGE "/skimages/72.jpg"
115#define SPECIFIC_IMAGE "/Users/reed/Downloads/3elfs.jpg"
116
117#define IMAGE_DIR "/skimages/"
118#define IMAGE_SUFFIX ".gif"
119
120class ImageDirView : public SkView {
121public:
122 SkBitmap* fBitmaps;
123 SkString* fStrings;
124 int fBitmapCount;
125 int fCurrIndex;
126 SkScalar fSaturation;
127 SkScalar fAngle;
128
129 ImageDirView() {
130 SkImageRef_GlobalPool::SetRAMBudget(320 * 1024);
131
132#ifdef SPECIFIC_IMAGE
133 fBitmaps = new SkBitmap[3];
134 fStrings = new SkString[3];
135 fBitmapCount = 3;
136 const SkBitmap::Config configs[] = {
137 SkBitmap::kARGB_8888_Config,
138 SkBitmap::kRGB_565_Config,
139 SkBitmap::kARGB_4444_Config
140 };
141 for (int i = 0; i < fBitmapCount; i++) {
142#if 1
143 SkStream* stream = new SkFILEStream(SPECIFIC_IMAGE);
144 SetImageRef(&fBitmaps[i], stream, configs[i], SPECIFIC_IMAGE);
145#else
146 SkImageDecoder::DecodeFile(SPECIFIC_IMAGE, &fBitmaps[i]);
147#endif
148 }
149#else
150 int i, N = 0;
151 SkOSFile::Iter iter(IMAGE_DIR, IMAGE_SUFFIX);
152 SkString name;
153 while (iter.next(&name)) {
154 N += 1;
155 }
156 fBitmaps = new SkBitmap[N];
157 fStrings = new SkString[N];
158 iter.reset(IMAGE_DIR, IMAGE_SUFFIX);
159 for (i = 0; i < N; i++) {
160 iter.next(&name);
161 SkString path(IMAGE_DIR);
162 path.append(name);
163 SkStream* stream = new SkFILEStream(path.c_str());
164
165 SetImageRef(&fBitmaps[i], stream, SkBitmap::kNo_Config,
166 name.c_str());
167 fStrings[i] = name;
168 }
169 fBitmapCount = N;
170#endif
171 fCurrIndex = 0;
172 fDX = fDY = 0;
173
174 fSaturation = SK_Scalar1;
175 fAngle = 0;
176
177 fScale = SK_Scalar1;
178 }
179
180 virtual ~ImageDirView() {
181 delete[] fBitmaps;
182 delete[] fStrings;
183
184 SkImageRef_GlobalPool::DumpPool();
185 }
186
187protected:
188 // overrides from SkEventSink
189 virtual bool onQuery(SkEvent* evt) {
190 if (SampleCode::TitleQ(*evt)) {
191 SkString str("ImageDir: ");
192#ifdef SPECIFIC_IMAGE
193 str.append(SPECIFIC_IMAGE);
194#else
195 str.append(IMAGE_DIR);
196#endif
197 SampleCode::TitleR(evt, str.c_str());
198 return true;
199 }
200 return this->INHERITED::onQuery(evt);
201 }
202
203 void drawBG(SkCanvas* canvas) {
204// canvas->drawColor(0xFFDDDDDD);
205 canvas->drawColor(SK_ColorGRAY);
206 canvas->drawColor(SK_ColorWHITE);
207 }
208
209 SkScalar fScale;
210 virtual void onDraw(SkCanvas* canvas) {
211 this->drawBG(canvas);
212
213 if (true) {
214 canvas->scale(SkIntToScalar(2), SkIntToScalar(2));
215 drawmarshmallow(canvas);
216 return;
217 }
218
219 if (false) {
220 SkPaint p;
221 p.setStyle(SkPaint::kStroke_Style);
222 p.setStrokeWidth(SkIntToScalar(4));
223 canvas->drawCircle(SkIntToScalar(100), SkIntToScalar(100), SkIntToScalar(50), p);
224 p.setAntiAlias(true);
225 canvas->drawCircle(SkIntToScalar(300), SkIntToScalar(100), SkIntToScalar(50), p);
226 }
227 if (false) {
228 SkScalar cx = this->width()/2;
229 SkScalar cy = this->height()/2;
230 canvas->translate(cx, cy);
231 canvas->scale(fScale, fScale);
232 canvas->translate(-cx, -cy);
233 DrawRoundRect(*canvas);
234 return;
235 }
236
237 SkScalar scale = SK_Scalar1 * 999/1000;
238// scale = SK_Scalar1/2;
239
240 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
241 // canvas->scale(scale, scale);
242
243 SkScalar x = SkIntToScalar(32), y = SkIntToScalar(32);
244 SkPaint paint;
245
246 // x += fDX;
247 // y += fDY;
248
249// paint.setLooper(new SkBlurDrawLooper(SkIntToScalar(12), 0, 0, 0xDD000000))->unref();
250
251#if 0
252 for (int i = 0; i < fBitmapCount; i++) {
253 SkPaint p;
254
255#if 1
256 const SkScalar cm[] = {
257 SkIntToScalar(2), 0, 0, 0, SkIntToScalar(-255),
258 0, SkIntToScalar(2), 0, 0, SkIntToScalar(-255),
259 0, 0, SkIntToScalar(2), 0, SkIntToScalar(-255),
260 0, 0, 0, SkIntToScalar(1), 0
261 };
262 SkColorFilter* cf = new SkColorMatrixFilter(cm);
263 p.setColorFilter(cf)->unref();
264#endif
265
266 canvas->drawBitmap(fBitmaps[i], x, y, &p);
267 x += SkIntToScalar(fBitmaps[i].width() + 10);
268 }
269 return;
270#endif
271
272 canvas->drawBitmap(fBitmaps[fCurrIndex], x, y, &paint);
273#ifndef SPECIFIC_IMAGE
274 if (true) {
275 fCurrIndex += 1;
276 if (fCurrIndex >= fBitmapCount) {
277 fCurrIndex = 0;
278 }
279 this->inval(NULL);
280 }
281#endif
282 }
283
284 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
285 if (true) {
286 fCurrIndex += 1;
287 if (fCurrIndex >= fBitmapCount)
288 fCurrIndex = 0;
289 this->inval(NULL);
290 }
291 return new Click(this);
292 }
293
294 virtual bool onClick(Click* click) {
295 SkScalar center = this->width()/2;
296 fSaturation = SkScalarDiv(click->fCurr.fX - center, center/2);
297 center = this->height()/2;
298 fAngle = SkScalarDiv(click->fCurr.fY - center, center) * 180;
299
300 fDX += click->fCurr.fX - click->fPrev.fX;
301 fDY += click->fCurr.fY - click->fPrev.fY;
302
303 fScale = SkScalarDiv(click->fCurr.fX, this->width());
304
305 this->inval(NULL);
306 return true;
307 return this->INHERITED::onClick(click);
308 }
309
310private:
311 SkScalar fDX, fDY;
312 typedef SkView INHERITED;
313};
314
315//////////////////////////////////////////////////////////////////////////////
316
317static SkView* MyFactory() { return new ImageDirView; }
318static SkViewRegister reg(MyFactory);
319