blob: 922910ce32f3fc3f05d51db8706a5270eaaf5bc2 [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"
reed@android.com0767e472008-12-23 16:06:51 +000013#include "SkComposeShader.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#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
reed@android.com8a1c16f2008-12-17 15:59:43 +000094static bool SetImageRef(SkBitmap* bitmap, SkStream* stream,
95 SkBitmap::Config pref, const char name[] = NULL) {
96#if 0
97 // test buffer streams
98 SkStream* str = new SkBufferStream(stream, 717);
99 stream->unref();
100 stream = str;
101#endif
102
103 SkImageRef* ref = new SkImageRef_GlobalPool(stream, pref, 1);
104 ref->setURI(name);
105 if (!ref->getInfo(bitmap)) {
106 delete ref;
107 return false;
108 }
109 bitmap->setPixelRef(ref)->unref();
110 return true;
111}
112
113//#define SPECIFIC_IMAGE "/skimages/72.jpg"
114#define SPECIFIC_IMAGE "/Users/reed/Downloads/3elfs.jpg"
115
116#define IMAGE_DIR "/skimages/"
117#define IMAGE_SUFFIX ".gif"
118
119class ImageDirView : public SkView {
120public:
121 SkBitmap* fBitmaps;
122 SkString* fStrings;
123 int fBitmapCount;
124 int fCurrIndex;
125 SkScalar fSaturation;
126 SkScalar fAngle;
127
128 ImageDirView() {
129 SkImageRef_GlobalPool::SetRAMBudget(320 * 1024);
130
131#ifdef SPECIFIC_IMAGE
132 fBitmaps = new SkBitmap[3];
133 fStrings = new SkString[3];
134 fBitmapCount = 3;
135 const SkBitmap::Config configs[] = {
136 SkBitmap::kARGB_8888_Config,
137 SkBitmap::kRGB_565_Config,
138 SkBitmap::kARGB_4444_Config
139 };
140 for (int i = 0; i < fBitmapCount; i++) {
141#if 1
142 SkStream* stream = new SkFILEStream(SPECIFIC_IMAGE);
143 SetImageRef(&fBitmaps[i], stream, configs[i], SPECIFIC_IMAGE);
reed@android.com791f5a12009-03-16 13:53:11 +0000144 stream->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145#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());
reed@android.com791f5a12009-03-16 13:53:11 +0000167 stream->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168 fStrings[i] = name;
169 }
170 fBitmapCount = N;
171#endif
172 fCurrIndex = 0;
173 fDX = fDY = 0;
174
175 fSaturation = SK_Scalar1;
176 fAngle = 0;
177
178 fScale = SK_Scalar1;
179 }
180
181 virtual ~ImageDirView() {
182 delete[] fBitmaps;
183 delete[] fStrings;
184
185 SkImageRef_GlobalPool::DumpPool();
186 }
187
188protected:
189 // overrides from SkEventSink
190 virtual bool onQuery(SkEvent* evt) {
191 if (SampleCode::TitleQ(*evt)) {
192 SkString str("ImageDir: ");
193#ifdef SPECIFIC_IMAGE
194 str.append(SPECIFIC_IMAGE);
195#else
196 str.append(IMAGE_DIR);
197#endif
198 SampleCode::TitleR(evt, str.c_str());
199 return true;
200 }
201 return this->INHERITED::onQuery(evt);
202 }
203
204 void drawBG(SkCanvas* canvas) {
205// canvas->drawColor(0xFFDDDDDD);
206 canvas->drawColor(SK_ColorGRAY);
207 canvas->drawColor(SK_ColorWHITE);
208 }
209
210 SkScalar fScale;
211 virtual void onDraw(SkCanvas* canvas) {
212 this->drawBG(canvas);
213
214 if (true) {
215 canvas->scale(SkIntToScalar(2), SkIntToScalar(2));
216 drawmarshmallow(canvas);
217 return;
218 }
219
220 if (false) {
221 SkPaint p;
222 p.setStyle(SkPaint::kStroke_Style);
223 p.setStrokeWidth(SkIntToScalar(4));
224 canvas->drawCircle(SkIntToScalar(100), SkIntToScalar(100), SkIntToScalar(50), p);
225 p.setAntiAlias(true);
226 canvas->drawCircle(SkIntToScalar(300), SkIntToScalar(100), SkIntToScalar(50), p);
227 }
228 if (false) {
229 SkScalar cx = this->width()/2;
230 SkScalar cy = this->height()/2;
231 canvas->translate(cx, cy);
232 canvas->scale(fScale, fScale);
233 canvas->translate(-cx, -cy);
234 DrawRoundRect(*canvas);
235 return;
236 }
237
238 SkScalar scale = SK_Scalar1 * 999/1000;
239// scale = SK_Scalar1/2;
240
241 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
242 // canvas->scale(scale, scale);
243
244 SkScalar x = SkIntToScalar(32), y = SkIntToScalar(32);
245 SkPaint paint;
246
247 // x += fDX;
248 // y += fDY;
249
250// paint.setLooper(new SkBlurDrawLooper(SkIntToScalar(12), 0, 0, 0xDD000000))->unref();
251
252#if 0
253 for (int i = 0; i < fBitmapCount; i++) {
254 SkPaint p;
255
256#if 1
257 const SkScalar cm[] = {
258 SkIntToScalar(2), 0, 0, 0, SkIntToScalar(-255),
259 0, SkIntToScalar(2), 0, 0, SkIntToScalar(-255),
260 0, 0, SkIntToScalar(2), 0, SkIntToScalar(-255),
261 0, 0, 0, SkIntToScalar(1), 0
262 };
263 SkColorFilter* cf = new SkColorMatrixFilter(cm);
264 p.setColorFilter(cf)->unref();
265#endif
266
267 canvas->drawBitmap(fBitmaps[i], x, y, &p);
268 x += SkIntToScalar(fBitmaps[i].width() + 10);
269 }
270 return;
271#endif
272
273 canvas->drawBitmap(fBitmaps[fCurrIndex], x, y, &paint);
274#ifndef SPECIFIC_IMAGE
275 if (true) {
276 fCurrIndex += 1;
277 if (fCurrIndex >= fBitmapCount) {
278 fCurrIndex = 0;
279 }
280 this->inval(NULL);
281 }
282#endif
283 }
284
285 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
286 if (true) {
287 fCurrIndex += 1;
288 if (fCurrIndex >= fBitmapCount)
289 fCurrIndex = 0;
290 this->inval(NULL);
291 }
292 return new Click(this);
293 }
294
295 virtual bool onClick(Click* click) {
296 SkScalar center = this->width()/2;
297 fSaturation = SkScalarDiv(click->fCurr.fX - center, center/2);
298 center = this->height()/2;
299 fAngle = SkScalarDiv(click->fCurr.fY - center, center) * 180;
300
301 fDX += click->fCurr.fX - click->fPrev.fX;
302 fDY += click->fCurr.fY - click->fPrev.fY;
303
304 fScale = SkScalarDiv(click->fCurr.fX, this->width());
305
306 this->inval(NULL);
307 return true;
308 return this->INHERITED::onClick(click);
309 }
310
311private:
312 SkScalar fDX, fDY;
313 typedef SkView INHERITED;
314};
315
316//////////////////////////////////////////////////////////////////////////////
317
318static SkView* MyFactory() { return new ImageDirView; }
319static SkViewRegister reg(MyFactory);
320