blob: e6134bdb2255432ab71a2b3dafe43201b5faba34 [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"
reed@android.com8a1c16f2008-12-17 15:59:43 +000018#include "SkColorPriv.h"
19#include "SkColorFilter.h"
20#include "SkTime.h"
21
22static const char* gNames[] = {
23 "/skimages/background_01.png"
24};
25
reed@google.com961ddb02011-05-05 14:03:48 +000026class Filter2View : public SampleView {
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:
52 // overrides from SkEventSink
53 virtual bool onQuery(SkEvent* evt) {
54 if (SampleCode::TitleQ(*evt)) {
55 SkString str("Filter/Dither ");
56 str.append(gNames[fCurrIndex]);
57 SampleCode::TitleR(evt, str.c_str());
58 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);
86 p.setTextAlign(SkPaint::kCenter_Align);
87 p.setTextSize(SkIntToScalar(18));
88 SkString s("dither=");
89 s.appendS32(paint.isDither());
90 s.append(" filter=");
reed93a12152015-03-16 10:08:34 -070091 s.appendS32(paint.getFilterQuality() != kNone_SkFilterQuality);
Cary Clark2a475ea2017-04-28 15:35:12 -040092 canvas->drawString(s, x + W/2,
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 y - p.getTextSize(), p);
94 }
95 if (k+j == 2) {
96 SkPaint p;
97 p.setAntiAlias(true);
98 p.setTextSize(SkIntToScalar(18));
99 SkString s;
100 s.append(" depth=");
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000101 s.appendS32(fBitmaps[i].colorType() == kRGB_565_SkColorType ? 16 : 32);
Cary Clark2a475ea2017-04-28 15:35:12 -0400102 canvas->drawString(s, x + W + SkIntToScalar(4),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 y + H/2, p);
104 }
105 }
106 }
107 }
108 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000109
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110private:
reed@google.com961ddb02011-05-05 14:03:48 +0000111 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112};
113
114//////////////////////////////////////////////////////////////////////////////
115
116static SkView* MyFactory() { return new Filter2View; }
117static SkViewRegister reg(MyFactory);