blob: 8c93563c409e629b696b3e7e19c4aeb3d54992fd [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"
reed@android.com8a1c16f2008-12-17 15:59:43 +00009#include "SampleCode.h"
10#include "SkView.h"
11#include "SkCanvas.h"
12#include "SkGradientShader.h"
13#include "SkGraphics.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkPath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkRegion.h"
16#include "SkShader.h"
17#include "SkUtils.h"
18#include "SkXfermode.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000019#include "SkColorPriv.h"
20#include "SkColorFilter.h"
21#include "SkTime.h"
22
23static const char* gNames[] = {
24 "/skimages/background_01.png"
25};
26
reed@google.com961ddb02011-05-05 14:03:48 +000027class Filter2View : public SampleView {
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:
53 // overrides from SkEventSink
54 virtual bool onQuery(SkEvent* evt) {
55 if (SampleCode::TitleQ(*evt)) {
56 SkString str("Filter/Dither ");
57 str.append(gNames[fCurrIndex]);
58 SampleCode::TitleR(evt, str.c_str());
59 return true;
60 }
61 return this->INHERITED::onQuery(evt);
62 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000063
reed@google.com961ddb02011-05-05 14:03:48 +000064 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000065 canvas->translate(SkIntToScalar(10), SkIntToScalar(50));
rmistry@google.comae933ce2012-08-23 18:19:56 +000066
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 const SkScalar W = SkIntToScalar(fBitmaps[0].width() + 1);
68 const SkScalar H = SkIntToScalar(fBitmaps[0].height() + 1);
69 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +000070
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000071 const SkScalar scale = 0.897917f;
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 canvas->scale(SK_Scalar1, scale);
73
74 for (int k = 0; k < 2; k++) {
reed93a12152015-03-16 10:08:34 -070075 paint.setFilterQuality(k == 1 ? kLow_SkFilterQuality : kNone_SkFilterQuality);
reed@android.com8a1c16f2008-12-17 15:59:43 +000076 for (int j = 0; j < 2; j++) {
77 paint.setDither(j == 1);
78 for (int i = 0; i < fBitmapCount; i++) {
79 SkScalar x = (k * fBitmapCount + j) * W;
80 SkScalar y = i * H;
reed@google.come1ca7052013-12-17 19:22:07 +000081 x = SkScalarRoundToScalar(x);
82 y = SkScalarRoundToScalar(y);
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 canvas->drawBitmap(fBitmaps[i], x, y, &paint);
84 if (i == 0) {
85 SkPaint p;
86 p.setAntiAlias(true);
87 p.setTextAlign(SkPaint::kCenter_Align);
88 p.setTextSize(SkIntToScalar(18));
89 SkString s("dither=");
90 s.appendS32(paint.isDither());
91 s.append(" filter=");
reed93a12152015-03-16 10:08:34 -070092 s.appendS32(paint.getFilterQuality() != kNone_SkFilterQuality);
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 canvas->drawText(s.c_str(), s.size(), x + W/2,
94 y - p.getTextSize(), p);
95 }
96 if (k+j == 2) {
97 SkPaint p;
98 p.setAntiAlias(true);
99 p.setTextSize(SkIntToScalar(18));
100 SkString s;
101 s.append(" depth=");
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000102 s.appendS32(fBitmaps[i].colorType() == kRGB_565_SkColorType ? 16 : 32);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 canvas->drawText(s.c_str(), s.size(), x + W + SkIntToScalar(4),
104 y + H/2, p);
105 }
106 }
107 }
108 }
109 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000110
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111private:
reed@google.com961ddb02011-05-05 14:03:48 +0000112 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113};
114
115//////////////////////////////////////////////////////////////////////////////
116
117static SkView* MyFactory() { return new Filter2View; }
118static SkViewRegister reg(MyFactory);