epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
reed | bfefc7c | 2014-06-12 17:40:00 -0700 | [diff] [blame] | 7 | |
msarett | d15750c | 2016-03-18 15:48:49 -0700 | [diff] [blame] | 8 | #include "DecodeFile.h" |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 9 | #include "Sample.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
Brian Osman | 8ceee43 | 2017-12-01 10:52:28 -0500 | [diff] [blame] | 11 | #include "SkColorFilter.h" |
| 12 | #include "SkColorPriv.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | #include "SkGradientShader.h" |
| 14 | #include "SkGraphics.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | #include "SkPath.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | #include "SkRegion.h" |
| 17 | #include "SkShader.h" |
Brian Osman | 8ceee43 | 2017-12-01 10:52:28 -0500 | [diff] [blame] | 18 | #include "SkString.h" |
Mike Reed | a697df9 | 2018-10-26 07:28:30 -0400 | [diff] [blame] | 19 | #include "SkTextUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | #include "SkTime.h" |
Hal Canary | ea60b95 | 2018-08-21 11:45:46 -0400 | [diff] [blame] | 21 | #include "SkUTF.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | |
| 23 | static const char* gNames[] = { |
| 24 | "/skimages/background_01.png" |
| 25 | }; |
| 26 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 27 | class Filter2View : public Sample { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | public: |
| 29 | SkBitmap* fBitmaps; |
| 30 | int fBitmapCount; |
| 31 | int fCurrIndex; |
| 32 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 33 | Filter2View() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | fBitmapCount = SK_ARRAY_COUNT(gNames)*2; |
| 35 | fBitmaps = new SkBitmap[fBitmapCount]; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 36 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 37 | for (int i = 0; i < fBitmapCount/2; i++) { |
msarett | d15750c | 2016-03-18 15:48:49 -0700 | [diff] [blame] | 38 | decode_file(gNames[i], &fBitmaps[i]); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | } |
| 40 | for (int i = fBitmapCount/2; i < fBitmapCount; i++) { |
msarett | d15750c | 2016-03-18 15:48:49 -0700 | [diff] [blame] | 41 | decode_file(gNames[i-fBitmapCount/2], &fBitmaps[i], kRGB_565_SkColorType); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 42 | } |
| 43 | fCurrIndex = 0; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 44 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 45 | this->setBGColor(SK_ColorGRAY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 47 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | virtual ~Filter2View() { |
| 49 | delete[] fBitmaps; |
| 50 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 51 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 52 | protected: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 53 | virtual bool onQuery(Sample::Event* evt) { |
| 54 | if (Sample::TitleQ(*evt)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | SkString str("Filter/Dither "); |
| 56 | str.append(gNames[fCurrIndex]); |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 57 | Sample::TitleR(evt, str.c_str()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 58 | return true; |
| 59 | } |
| 60 | return this->INHERITED::onQuery(evt); |
| 61 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 62 | |
reed@google.com | 961ddb0 | 2011-05-05 14:03:48 +0000 | [diff] [blame] | 63 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | canvas->translate(SkIntToScalar(10), SkIntToScalar(50)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 65 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 66 | const SkScalar W = SkIntToScalar(fBitmaps[0].width() + 1); |
| 67 | const SkScalar H = SkIntToScalar(fBitmaps[0].height() + 1); |
| 68 | SkPaint paint; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 69 | |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 70 | const SkScalar scale = 0.897917f; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | canvas->scale(SK_Scalar1, scale); |
| 72 | |
| 73 | for (int k = 0; k < 2; k++) { |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 74 | paint.setFilterQuality(k == 1 ? kLow_SkFilterQuality : kNone_SkFilterQuality); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | for (int j = 0; j < 2; j++) { |
| 76 | paint.setDither(j == 1); |
| 77 | for (int i = 0; i < fBitmapCount; i++) { |
| 78 | SkScalar x = (k * fBitmapCount + j) * W; |
| 79 | SkScalar y = i * H; |
reed@google.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame] | 80 | x = SkScalarRoundToScalar(x); |
| 81 | y = SkScalarRoundToScalar(y); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 82 | canvas->drawBitmap(fBitmaps[i], x, y, &paint); |
Mike Reed | b579f07 | 2019-01-03 15:45:53 -0500 | [diff] [blame] | 83 | SkFont font; |
| 84 | font.setSize(SkIntToScalar(18)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | if (i == 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 86 | SkString s("dither="); |
| 87 | s.appendS32(paint.isDither()); |
| 88 | s.append(" filter="); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 89 | s.appendS32(paint.getFilterQuality() != kNone_SkFilterQuality); |
Mike Reed | b579f07 | 2019-01-03 15:45:53 -0500 | [diff] [blame] | 90 | SkTextUtils::DrawString(canvas, s.c_str(), x + W/2, y - font.getSize(), font, SkPaint(), |
Mike Reed | 3a42ec0 | 2018-10-30 12:53:21 -0400 | [diff] [blame] | 91 | SkTextUtils::kCenter_Align); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | } |
| 93 | if (k+j == 2) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | SkString s; |
| 95 | s.append(" depth="); |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 96 | s.appendS32(fBitmaps[i].colorType() == kRGB_565_SkColorType ? 16 : 32); |
Mike Reed | b579f07 | 2019-01-03 15:45:53 -0500 | [diff] [blame] | 97 | SkTextUtils::DrawString(canvas, s.c_str(), x + W + SkIntToScalar(4), y + H/2, font, SkPaint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 103 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | private: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 105 | typedef Sample INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | ////////////////////////////////////////////////////////////////////////////// |
| 109 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 110 | DEF_SAMPLE( return new Filter2View(); ) |