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); |
| 32 | SkShader* s = SkShader::CreateBitmapShader(bitmap, |
| 33 | SkShader::kRepeat_TileMode, |
| 34 | SkShader::kRepeat_TileMode); |
| 35 | paint.setShader(s)->unref(); |
| 36 | m.setTranslate(SkIntToScalar(250), SkIntToScalar(134)); |
| 37 | s->setLocalMatrix(m); |
| 38 | |
| 39 | r.set(SkIntToScalar(250), |
| 40 | SkIntToScalar(134), |
| 41 | SkIntToScalar(250 + 449), |
| 42 | SkIntToScalar(134 + 701)); |
| 43 | paint.setFlags(2); |
| 44 | |
| 45 | canvas->drawRect(r, paint); |
| 46 | } |
| 47 | |
| 48 | static void DrawRoundRect(SkCanvas& canvas) { |
| 49 | bool ret = false; |
| 50 | SkPaint paint; |
| 51 | SkBitmap bitmap; |
| 52 | SkMatrix matrix; |
| 53 | matrix.reset(); |
| 54 | |
| 55 | bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1370, 812); |
| 56 | bitmap.allocPixels(); |
| 57 | #if 0 |
| 58 | SkCanvas canvas; |
| 59 | canvas.setBitmapDevice(bitmap); |
| 60 | #endif |
| 61 | |
| 62 | // set up clipper |
| 63 | SkRect skclip; |
| 64 | skclip.set(SkIntToFixed(284), SkIntToFixed(40), SkIntToFixed(1370), SkIntToFixed(708)); |
| 65 | |
| 66 | // ret = canvas.clipRect(skclip); |
| 67 | // SkASSERT(ret); |
| 68 | |
| 69 | matrix.set(SkMatrix::kMTransX, SkFloatToFixed(-1153.28)); |
| 70 | matrix.set(SkMatrix::kMTransY, SkFloatToFixed(1180.50)); |
| 71 | |
| 72 | matrix.set(SkMatrix::kMScaleX, SkFloatToFixed(0.177171)); |
| 73 | matrix.set(SkMatrix::kMScaleY, SkFloatToFixed(0.177043)); |
| 74 | |
| 75 | matrix.set(SkMatrix::kMSkewX, SkFloatToFixed(0.126968)); |
| 76 | matrix.set(SkMatrix::kMSkewY, SkFloatToFixed(-0.126876)); |
| 77 | |
| 78 | matrix.set(SkMatrix::kMPersp0, SkFloatToFixed(0.0)); |
| 79 | matrix.set(SkMatrix::kMPersp1, SkFloatToFixed(0.0)); |
| 80 | |
| 81 | ret = canvas.concat(matrix); |
| 82 | |
| 83 | paint.setAntiAlias(true); |
| 84 | paint.setColor(0xb2202020); |
| 85 | paint.setStyle(SkPaint::kStroke_Style); |
| 86 | paint.setStrokeWidth(SkFloatToFixed(68.13)); |
| 87 | |
| 88 | SkRect r; |
| 89 | r.set(SkFloatToFixed(-313.714417), SkFloatToFixed(-4.826389), SkFloatToFixed(18014.447266), SkFloatToFixed(1858.154541)); |
| 90 | canvas.drawRoundRect(r, SkFloatToFixed(91.756363), SkFloatToFixed(91.756363), paint); |
| 91 | } |
| 92 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 93 | static bool SetImageRef(SkBitmap* bitmap, SkStream* stream, |
| 94 | SkBitmap::Config pref, const char name[] = NULL) { |
| 95 | #if 0 |
| 96 | // test buffer streams |
| 97 | SkStream* str = new SkBufferStream(stream, 717); |
| 98 | stream->unref(); |
| 99 | stream = str; |
| 100 | #endif |
| 101 | |
| 102 | SkImageRef* ref = new SkImageRef_GlobalPool(stream, pref, 1); |
| 103 | ref->setURI(name); |
| 104 | if (!ref->getInfo(bitmap)) { |
| 105 | delete ref; |
| 106 | return false; |
| 107 | } |
| 108 | bitmap->setPixelRef(ref)->unref(); |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | //#define SPECIFIC_IMAGE "/skimages/72.jpg" |
| 113 | #define SPECIFIC_IMAGE "/Users/reed/Downloads/3elfs.jpg" |
| 114 | |
| 115 | #define IMAGE_DIR "/skimages/" |
| 116 | #define IMAGE_SUFFIX ".gif" |
| 117 | |
| 118 | class ImageDirView : public SkView { |
| 119 | public: |
| 120 | SkBitmap* fBitmaps; |
| 121 | SkString* fStrings; |
| 122 | int fBitmapCount; |
| 123 | int fCurrIndex; |
| 124 | SkScalar fSaturation; |
| 125 | SkScalar fAngle; |
| 126 | |
| 127 | ImageDirView() { |
| 128 | SkImageRef_GlobalPool::SetRAMBudget(320 * 1024); |
| 129 | |
| 130 | #ifdef SPECIFIC_IMAGE |
| 131 | fBitmaps = new SkBitmap[3]; |
| 132 | fStrings = new SkString[3]; |
| 133 | fBitmapCount = 3; |
| 134 | const SkBitmap::Config configs[] = { |
| 135 | SkBitmap::kARGB_8888_Config, |
| 136 | SkBitmap::kRGB_565_Config, |
| 137 | SkBitmap::kARGB_4444_Config |
| 138 | }; |
| 139 | for (int i = 0; i < fBitmapCount; i++) { |
| 140 | #if 1 |
| 141 | SkStream* stream = new SkFILEStream(SPECIFIC_IMAGE); |
| 142 | SetImageRef(&fBitmaps[i], stream, configs[i], SPECIFIC_IMAGE); |
reed@android.com | 791f5a1 | 2009-03-16 13:53:11 +0000 | [diff] [blame] | 143 | stream->unref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 144 | #else |
| 145 | SkImageDecoder::DecodeFile(SPECIFIC_IMAGE, &fBitmaps[i]); |
| 146 | #endif |
| 147 | } |
| 148 | #else |
| 149 | int i, N = 0; |
| 150 | SkOSFile::Iter iter(IMAGE_DIR, IMAGE_SUFFIX); |
| 151 | SkString name; |
| 152 | while (iter.next(&name)) { |
| 153 | N += 1; |
| 154 | } |
| 155 | fBitmaps = new SkBitmap[N]; |
| 156 | fStrings = new SkString[N]; |
| 157 | iter.reset(IMAGE_DIR, IMAGE_SUFFIX); |
| 158 | for (i = 0; i < N; i++) { |
| 159 | iter.next(&name); |
| 160 | SkString path(IMAGE_DIR); |
| 161 | path.append(name); |
| 162 | SkStream* stream = new SkFILEStream(path.c_str()); |
| 163 | |
| 164 | SetImageRef(&fBitmaps[i], stream, SkBitmap::kNo_Config, |
| 165 | name.c_str()); |
reed@android.com | 791f5a1 | 2009-03-16 13:53:11 +0000 | [diff] [blame] | 166 | stream->unref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | |
| 187 | protected: |
| 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 | } |
reed@android.com | 44a6312 | 2009-05-30 02:40:28 +0000 | [diff] [blame] | 236 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 237 | canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 238 | |
| 239 | SkScalar x = SkIntToScalar(32), y = SkIntToScalar(32); |
| 240 | SkPaint paint; |
reed@android.com | 44a6312 | 2009-05-30 02:40:28 +0000 | [diff] [blame] | 241 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 242 | #if 0 |
| 243 | for (int i = 0; i < fBitmapCount; i++) { |
| 244 | SkPaint p; |
| 245 | |
| 246 | #if 1 |
| 247 | const SkScalar cm[] = { |
| 248 | SkIntToScalar(2), 0, 0, 0, SkIntToScalar(-255), |
| 249 | 0, SkIntToScalar(2), 0, 0, SkIntToScalar(-255), |
| 250 | 0, 0, SkIntToScalar(2), 0, SkIntToScalar(-255), |
| 251 | 0, 0, 0, SkIntToScalar(1), 0 |
| 252 | }; |
| 253 | SkColorFilter* cf = new SkColorMatrixFilter(cm); |
| 254 | p.setColorFilter(cf)->unref(); |
| 255 | #endif |
| 256 | |
| 257 | canvas->drawBitmap(fBitmaps[i], x, y, &p); |
| 258 | x += SkIntToScalar(fBitmaps[i].width() + 10); |
| 259 | } |
| 260 | return; |
| 261 | #endif |
| 262 | |
| 263 | canvas->drawBitmap(fBitmaps[fCurrIndex], x, y, &paint); |
| 264 | #ifndef SPECIFIC_IMAGE |
| 265 | if (true) { |
| 266 | fCurrIndex += 1; |
| 267 | if (fCurrIndex >= fBitmapCount) { |
| 268 | fCurrIndex = 0; |
| 269 | } |
| 270 | this->inval(NULL); |
| 271 | } |
| 272 | #endif |
| 273 | } |
| 274 | |
| 275 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { |
| 276 | if (true) { |
| 277 | fCurrIndex += 1; |
| 278 | if (fCurrIndex >= fBitmapCount) |
| 279 | fCurrIndex = 0; |
| 280 | this->inval(NULL); |
| 281 | } |
| 282 | return new Click(this); |
| 283 | } |
| 284 | |
| 285 | virtual bool onClick(Click* click) { |
| 286 | SkScalar center = this->width()/2; |
| 287 | fSaturation = SkScalarDiv(click->fCurr.fX - center, center/2); |
| 288 | center = this->height()/2; |
| 289 | fAngle = SkScalarDiv(click->fCurr.fY - center, center) * 180; |
| 290 | |
| 291 | fDX += click->fCurr.fX - click->fPrev.fX; |
| 292 | fDY += click->fCurr.fY - click->fPrev.fY; |
| 293 | |
| 294 | fScale = SkScalarDiv(click->fCurr.fX, this->width()); |
| 295 | |
| 296 | this->inval(NULL); |
| 297 | return true; |
| 298 | return this->INHERITED::onClick(click); |
| 299 | } |
| 300 | |
| 301 | private: |
| 302 | SkScalar fDX, fDY; |
| 303 | typedef SkView INHERITED; |
| 304 | }; |
| 305 | |
| 306 | ////////////////////////////////////////////////////////////////////////////// |
| 307 | |
| 308 | static SkView* MyFactory() { return new ImageDirView; } |
| 309 | static SkViewRegister reg(MyFactory); |
| 310 | |