blob: e4a43cada93ed3e7b6aed52b8d04ce8222dc5506 [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"
Mike Reeda697df92018-10-26 07:28:30 -040019#include "SkTextUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000020#include "SkTime.h"
Hal Canaryea60b952018-08-21 11:45:46 -040021#include "SkUTF.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022
23static const char* gNames[] = {
24 "/skimages/background_01.png"
25};
26
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040027class Filter2View : public Sample {
reed@android.com8a1c16f2008-12-17 15:59:43 +000028public:
29 SkBitmap* fBitmaps;
30 int fBitmapCount;
31 int fCurrIndex;
32
rmistry@google.comae933ce2012-08-23 18:19:56 +000033 Filter2View() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000034 fBitmapCount = SK_ARRAY_COUNT(gNames)*2;
35 fBitmaps = new SkBitmap[fBitmapCount];
rmistry@google.comae933ce2012-08-23 18:19:56 +000036
reed@android.com8a1c16f2008-12-17 15:59:43 +000037 for (int i = 0; i < fBitmapCount/2; i++) {
msarettd15750c2016-03-18 15:48:49 -070038 decode_file(gNames[i], &fBitmaps[i]);
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 }
40 for (int i = fBitmapCount/2; i < fBitmapCount; i++) {
msarettd15750c2016-03-18 15:48:49 -070041 decode_file(gNames[i-fBitmapCount/2], &fBitmaps[i], kRGB_565_SkColorType);
reed@android.com8a1c16f2008-12-17 15:59:43 +000042 }
43 fCurrIndex = 0;
rmistry@google.comae933ce2012-08-23 18:19:56 +000044
reed@google.com961ddb02011-05-05 14:03:48 +000045 this->setBGColor(SK_ColorGRAY);
reed@android.com8a1c16f2008-12-17 15:59:43 +000046 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000047
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 virtual ~Filter2View() {
49 delete[] fBitmaps;
50 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000051
reed@android.com8a1c16f2008-12-17 15:59:43 +000052protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040053 virtual bool onQuery(Sample::Event* evt) {
54 if (Sample::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 SkString str("Filter/Dither ");
56 str.append(gNames[fCurrIndex]);
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040057 Sample::TitleR(evt, str.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +000058 return true;
59 }
60 return this->INHERITED::onQuery(evt);
61 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000062
reed@google.com961ddb02011-05-05 14:03:48 +000063 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 canvas->translate(SkIntToScalar(10), SkIntToScalar(50));
rmistry@google.comae933ce2012-08-23 18:19:56 +000065
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 const SkScalar W = SkIntToScalar(fBitmaps[0].width() + 1);
67 const SkScalar H = SkIntToScalar(fBitmaps[0].height() + 1);
68 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +000069
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000070 const SkScalar scale = 0.897917f;
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 canvas->scale(SK_Scalar1, scale);
72
73 for (int k = 0; k < 2; k++) {
reed93a12152015-03-16 10:08:34 -070074 paint.setFilterQuality(k == 1 ? kLow_SkFilterQuality : kNone_SkFilterQuality);
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 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.come1ca7052013-12-17 19:22:07 +000080 x = SkScalarRoundToScalar(x);
81 y = SkScalarRoundToScalar(y);
reed@android.com8a1c16f2008-12-17 15:59:43 +000082 canvas->drawBitmap(fBitmaps[i], x, y, &paint);
83 if (i == 0) {
84 SkPaint p;
85 p.setAntiAlias(true);
reed@android.com8a1c16f2008-12-17 15:59:43 +000086 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);
Mike Reeda697df92018-10-26 07:28:30 -040091 SkTextUtils::DrawString(canvas, s, x + W/2, y - p.getTextSize(), p,
Mike Reed3a42ec02018-10-30 12:53:21 -040092 SkTextUtils::kCenter_Align);
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 }
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);
Mike Reeda697df92018-10-26 07:28:30 -0400101 SkTextUtils::DrawString(canvas, s, x + W + SkIntToScalar(4), y + H/2, p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 }
103 }
104 }
105 }
106 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000107
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400109 typedef Sample INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110};
111
112//////////////////////////////////////////////////////////////////////////////
113
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400114DEF_SAMPLE( return new Filter2View(); )