• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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  */
7 
8 #include "DecodeFile.h"
9 #include "Sample.h"
10 #include "SkCanvas.h"
11 #include "SkColorFilter.h"
12 #include "SkColorPriv.h"
13 #include "SkGradientShader.h"
14 #include "SkGraphics.h"
15 #include "SkPath.h"
16 #include "SkRegion.h"
17 #include "SkShader.h"
18 #include "SkString.h"
19 #include "SkTextUtils.h"
20 #include "SkTime.h"
21 #include "SkUTF.h"
22 
23 static const char* gNames[] = {
24     "/skimages/background_01.png"
25 };
26 
27 class Filter2View : public Sample {
28 public:
29     SkBitmap*   fBitmaps;
30     int         fBitmapCount;
31     int         fCurrIndex;
32 
Filter2View()33     Filter2View() {
34         fBitmapCount = SK_ARRAY_COUNT(gNames)*2;
35         fBitmaps = new SkBitmap[fBitmapCount];
36 
37         for (int i = 0; i < fBitmapCount/2; i++) {
38             decode_file(gNames[i], &fBitmaps[i]);
39         }
40         for (int i = fBitmapCount/2; i < fBitmapCount; i++) {
41             decode_file(gNames[i-fBitmapCount/2], &fBitmaps[i], kRGB_565_SkColorType);
42         }
43         fCurrIndex = 0;
44 
45         this->setBGColor(SK_ColorGRAY);
46     }
47 
~Filter2View()48     virtual ~Filter2View() {
49         delete[] fBitmaps;
50     }
51 
52 protected:
onQuery(Sample::Event * evt)53     virtual bool onQuery(Sample::Event* evt) {
54         if (Sample::TitleQ(*evt)) {
55             SkString str("Filter/Dither ");
56             str.append(gNames[fCurrIndex]);
57             Sample::TitleR(evt, str.c_str());
58             return true;
59         }
60         return this->INHERITED::onQuery(evt);
61     }
62 
onDrawContent(SkCanvas * canvas)63     virtual void onDrawContent(SkCanvas* canvas) {
64         canvas->translate(SkIntToScalar(10), SkIntToScalar(50));
65 
66         const SkScalar W = SkIntToScalar(fBitmaps[0].width() + 1);
67         const SkScalar H = SkIntToScalar(fBitmaps[0].height() + 1);
68         SkPaint paint;
69 
70         const SkScalar scale = 0.897917f;
71         canvas->scale(SK_Scalar1, scale);
72 
73         for (int k = 0; k < 2; k++) {
74             paint.setFilterQuality(k == 1 ? kLow_SkFilterQuality : kNone_SkFilterQuality);
75             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;
80                     x = SkScalarRoundToScalar(x);
81                     y = SkScalarRoundToScalar(y);
82                     canvas->drawBitmap(fBitmaps[i], x, y, &paint);
83                     SkFont font;
84                     font.setSize(SkIntToScalar(18));
85                     if (i == 0) {
86                         SkString s("dither=");
87                         s.appendS32(paint.isDither());
88                         s.append(" filter=");
89                         s.appendS32(paint.getFilterQuality() != kNone_SkFilterQuality);
90                         SkTextUtils::DrawString(canvas, s.c_str(), x + W/2, y - font.getSize(), font, SkPaint(),
91                                                 SkTextUtils::kCenter_Align);
92                     }
93                     if (k+j == 2) {
94                         SkString s;
95                         s.append(" depth=");
96                         s.appendS32(fBitmaps[i].colorType() == kRGB_565_SkColorType ? 16 : 32);
97                         SkTextUtils::DrawString(canvas, s.c_str(), x + W + SkIntToScalar(4), y + H/2, font, SkPaint());
98                     }
99                 }
100             }
101         }
102     }
103 
104 private:
105     typedef Sample INHERITED;
106 };
107 
108 //////////////////////////////////////////////////////////////////////////////
109 
110 DEF_SAMPLE( return new Filter2View(); )
111