blob: 06b542efb76b0521f58dac38b905762f6f36ce35 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
reedbfefc7c2014-06-12 17:40:00 -07007
msarettd15750c2016-03-18 15:48:49 -07008#include "DecodeFile.h"
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04009#include "Sample.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkCanvas.h"
Brian Osman8ceee432017-12-01 10:52:28 -050011#include "SkColorFilter.h"
12#include "SkColorPriv.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkGradientShader.h"
14#include "SkGraphics.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkPath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016#include "SkRegion.h"
17#include "SkShader.h"
Brian Osman8ceee432017-12-01 10:52:28 -050018#include "SkString.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000019#include "SkTime.h"
Hal Canaryea60b952018-08-21 11:45:46 -040020#include "SkUTF.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000021
22static const char* gNames[] = {
23 "/skimages/background_01.png"
24};
25
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040026class Filter2View : public Sample {
reed@android.com8a1c16f2008-12-17 15:59:43 +000027public:
28 SkBitmap* fBitmaps;
29 int fBitmapCount;
30 int fCurrIndex;
31
rmistry@google.comae933ce2012-08-23 18:19:56 +000032 Filter2View() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000033 fBitmapCount = SK_ARRAY_COUNT(gNames)*2;
34 fBitmaps = new SkBitmap[fBitmapCount];
rmistry@google.comae933ce2012-08-23 18:19:56 +000035
reed@android.com8a1c16f2008-12-17 15:59:43 +000036 for (int i = 0; i < fBitmapCount/2; i++) {
msarettd15750c2016-03-18 15:48:49 -070037 decode_file(gNames[i], &fBitmaps[i]);
reed@android.com8a1c16f2008-12-17 15:59:43 +000038 }
39 for (int i = fBitmapCount/2; i < fBitmapCount; i++) {
msarettd15750c2016-03-18 15:48:49 -070040 decode_file(gNames[i-fBitmapCount/2], &fBitmaps[i], kRGB_565_SkColorType);
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 }
42 fCurrIndex = 0;
rmistry@google.comae933ce2012-08-23 18:19:56 +000043
reed@google.com961ddb02011-05-05 14:03:48 +000044 this->setBGColor(SK_ColorGRAY);
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000046
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 virtual ~Filter2View() {
48 delete[] fBitmaps;
49 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000050
reed@android.com8a1c16f2008-12-17 15:59:43 +000051protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040052 virtual bool onQuery(Sample::Event* evt) {
53 if (Sample::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 SkString str("Filter/Dither ");
55 str.append(gNames[fCurrIndex]);
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040056 Sample::TitleR(evt, str.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +000057 return true;
58 }
59 return this->INHERITED::onQuery(evt);
60 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000061
reed@google.com961ddb02011-05-05 14:03:48 +000062 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 canvas->translate(SkIntToScalar(10), SkIntToScalar(50));
rmistry@google.comae933ce2012-08-23 18:19:56 +000064
reed@android.com8a1c16f2008-12-17 15:59:43 +000065 const SkScalar W = SkIntToScalar(fBitmaps[0].width() + 1);
66 const SkScalar H = SkIntToScalar(fBitmaps[0].height() + 1);
67 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +000068
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000069 const SkScalar scale = 0.897917f;
reed@android.com8a1c16f2008-12-17 15:59:43 +000070 canvas->scale(SK_Scalar1, scale);
71
72 for (int k = 0; k < 2; k++) {
reed93a12152015-03-16 10:08:34 -070073 paint.setFilterQuality(k == 1 ? kLow_SkFilterQuality : kNone_SkFilterQuality);
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 for (int j = 0; j < 2; j++) {
75 paint.setDither(j == 1);
76 for (int i = 0; i < fBitmapCount; i++) {
77 SkScalar x = (k * fBitmapCount + j) * W;
78 SkScalar y = i * H;
reed@google.come1ca7052013-12-17 19:22:07 +000079 x = SkScalarRoundToScalar(x);
80 y = SkScalarRoundToScalar(y);
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 canvas->drawBitmap(fBitmaps[i], x, y, &paint);
82 if (i == 0) {
83 SkPaint p;
84 p.setAntiAlias(true);
85 p.setTextAlign(SkPaint::kCenter_Align);
86 p.setTextSize(SkIntToScalar(18));
87 SkString s("dither=");
88 s.appendS32(paint.isDither());
89 s.append(" filter=");
reed93a12152015-03-16 10:08:34 -070090 s.appendS32(paint.getFilterQuality() != kNone_SkFilterQuality);
Cary Clark2a475ea2017-04-28 15:35:12 -040091 canvas->drawString(s, x + W/2,
reed@android.com8a1c16f2008-12-17 15:59:43 +000092 y - p.getTextSize(), p);
93 }
94 if (k+j == 2) {
95 SkPaint p;
96 p.setAntiAlias(true);
97 p.setTextSize(SkIntToScalar(18));
98 SkString s;
99 s.append(" depth=");
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000100 s.appendS32(fBitmaps[i].colorType() == kRGB_565_SkColorType ? 16 : 32);
Cary Clark2a475ea2017-04-28 15:35:12 -0400101 canvas->drawString(s, x + W + SkIntToScalar(4),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 y + H/2, p);
103 }
104 }
105 }
106 }
107 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000108
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400110 typedef Sample INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111};
112
113//////////////////////////////////////////////////////////////////////////////
114
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400115DEF_SAMPLE( return new Filter2View(); )