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