blob: b4314c5a5c1850cd561658246a67291daca08fdf [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkGradientShader.h"
12#include "SkGraphics.h"
13#include "SkImageDecoder.h"
14#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++) {
38 SkImageDecoder::DecodeFile(gNames[i], &fBitmaps[i],
39 SkBitmap::kARGB_8888_Config,
reed@android.comb3ade9d2009-06-15 13:04:45 +000040 SkImageDecoder::kDecodePixels_Mode, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 }
42 for (int i = fBitmapCount/2; i < fBitmapCount; i++) {
43 SkImageDecoder::DecodeFile(gNames[i-fBitmapCount/2], &fBitmaps[i],
44 SkBitmap::kRGB_565_Config,
reed@android.comb3ade9d2009-06-15 13:04:45 +000045 SkImageDecoder::kDecodePixels_Mode, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +000046 }
47 fCurrIndex = 0;
rmistry@google.comae933ce2012-08-23 18:19:56 +000048
reed@google.com961ddb02011-05-05 14:03:48 +000049 this->setBGColor(SK_ColorGRAY);
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000051
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 virtual ~Filter2View() {
53 delete[] fBitmaps;
54 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000055
reed@android.com8a1c16f2008-12-17 15:59:43 +000056protected:
57 // overrides from SkEventSink
58 virtual bool onQuery(SkEvent* evt) {
59 if (SampleCode::TitleQ(*evt)) {
60 SkString str("Filter/Dither ");
61 str.append(gNames[fCurrIndex]);
62 SampleCode::TitleR(evt, str.c_str());
63 return true;
64 }
65 return this->INHERITED::onQuery(evt);
66 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000067
reed@google.com961ddb02011-05-05 14:03:48 +000068 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 canvas->translate(SkIntToScalar(10), SkIntToScalar(50));
rmistry@google.comae933ce2012-08-23 18:19:56 +000070
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 const SkScalar W = SkIntToScalar(fBitmaps[0].width() + 1);
72 const SkScalar H = SkIntToScalar(fBitmaps[0].height() + 1);
73 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +000074
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000075 const SkScalar scale = 0.897917f;
reed@android.com8a1c16f2008-12-17 15:59:43 +000076 canvas->scale(SK_Scalar1, scale);
77
78 for (int k = 0; k < 2; k++) {
reed@google.com44699382013-10-31 17:28:30 +000079 paint.setFilterLevel(k == 1 ? SkPaint::kLow_FilterLevel : SkPaint::kNone_FilterLevel);
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 for (int j = 0; j < 2; j++) {
81 paint.setDither(j == 1);
82 for (int i = 0; i < fBitmapCount; i++) {
83 SkScalar x = (k * fBitmapCount + j) * W;
84 SkScalar y = i * H;
85 x = SkIntToScalar(SkScalarRound(x));
86 y = SkIntToScalar(SkScalarRound(y));
87 canvas->drawBitmap(fBitmaps[i], x, y, &paint);
88 if (i == 0) {
89 SkPaint p;
90 p.setAntiAlias(true);
91 p.setTextAlign(SkPaint::kCenter_Align);
92 p.setTextSize(SkIntToScalar(18));
93 SkString s("dither=");
94 s.appendS32(paint.isDither());
95 s.append(" filter=");
reed@google.com44699382013-10-31 17:28:30 +000096 s.appendS32(paint.getFilterLevel() != SkPaint::kNone_FilterLevel);
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 canvas->drawText(s.c_str(), s.size(), x + W/2,
98 y - p.getTextSize(), p);
99 }
100 if (k+j == 2) {
101 SkPaint p;
102 p.setAntiAlias(true);
103 p.setTextSize(SkIntToScalar(18));
104 SkString s;
105 s.append(" depth=");
106 s.appendS32(fBitmaps[i].config() == SkBitmap::kRGB_565_Config ? 16 : 32);
107 canvas->drawText(s.c_str(), s.size(), x + W + SkIntToScalar(4),
108 y + H/2, p);
109 }
110 }
111 }
112 }
113 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000114
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115private:
reed@google.com961ddb02011-05-05 14:03:48 +0000116 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117};
118
119//////////////////////////////////////////////////////////////////////////////
120
121static SkView* MyFactory() { return new Filter2View; }
122static SkViewRegister reg(MyFactory);