• 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 "include/core/SkCanvas.h"
9 #include "include/core/SkFont.h"
10 #include "include/core/SkString.h"
11 #include "include/utils/SkTextUtils.h"
12 #include "samplecode/DecodeFile.h"
13 #include "samplecode/Sample.h"
14 #include "tools/Resources.h"
15 
16 #include <vector>
17 
18 static const char* gNames[] = {
19     "images/mandrill_512_q075.jpg",
20     "images/dog.jpg",
21 };
22 
23 struct Filter2View : public Sample {
24     std::vector<SkBitmap> fBitmaps;
25 
onOnceBeforeDrawFilter2View26     void onOnceBeforeDraw() override {
27         SkASSERT(fBitmaps.empty());
28         fBitmaps.reserve(SK_ARRAY_COUNT(gNames) * 2);
29         for (const char* name : gNames) {
30             SkBitmap bitmap;
31             (void)decode_file(GetResourceAsData(name), &bitmap);
32             fBitmaps.push_back(std::move(bitmap));
33         }
34         for (const char* name : gNames) {
35             SkBitmap bitmap;
36             (void)decode_file(GetResourceAsData(name), &bitmap, kRGB_565_SkColorType);
37             fBitmaps.push_back(std::move(bitmap));
38         }
39         this->setBGColor(SK_ColorGRAY);
40     }
41 
nameFilter2View42     SkString name() override { return SkString("Filter/Dither"); }
43 
onDrawContentFilter2View44     void onDrawContent(SkCanvas* canvas) override {
45         canvas->translate(SkIntToScalar(10), SkIntToScalar(50));
46 
47         const SkScalar W = SkIntToScalar(fBitmaps[0].width() + 1);
48         const SkScalar H = SkIntToScalar(fBitmaps[0].height() + 1);
49         SkPaint paint;
50 
51         const SkScalar scale = 0.897917f;
52         canvas->scale(SK_Scalar1, scale);
53 
54         for (int k = 0; k < 2; k++) {
55             paint.setFilterQuality(k == 1 ? kLow_SkFilterQuality : kNone_SkFilterQuality);
56             for (int j = 0; j < 2; j++) {
57                 paint.setDither(j == 1);
58                 for (int i = 0; i < (int)fBitmaps.size(); i++) {
59                     SkScalar x = (k * (int)fBitmaps.size() + j) * W;
60                     SkScalar y = i * H;
61                     x = SkScalarRoundToScalar(x);
62                     y = SkScalarRoundToScalar(y);
63                     canvas->drawBitmap(fBitmaps[i], x, y, &paint);
64                     SkFont font;
65                     font.setSize(SkIntToScalar(18));
66                     if (i == 0) {
67                         SkString s("dither=");
68                         s.appendS32(paint.isDither());
69                         s.append(" filter=");
70                         s.appendS32(paint.getFilterQuality() != kNone_SkFilterQuality);
71                         SkTextUtils::DrawString(canvas, s.c_str(), x + W/2, y - font.getSize(), font, SkPaint(),
72                                                 SkTextUtils::kCenter_Align);
73                     }
74                     if (k+j == 2) {
75                         SkString s;
76                         s.append(" depth=");
77                         s.appendS32(fBitmaps[i].colorType() == kRGB_565_SkColorType ? 16 : 32);
78                         SkTextUtils::DrawString(canvas, s.c_str(), x + W + SkIntToScalar(4), y + H/2, font, SkPaint());
79                     }
80                 }
81             }
82         }
83     }
84 };
85 DEF_SAMPLE( return new Filter2View(); )
86