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