Debian Codegraph @

gm/resizeimagefilter.cpp

raw
1 /*
2 * Copyright 2013 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 "gm/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkImage.h"
12 #include "include/core/SkImageFilter.h"
13 #include "include/core/SkMatrix.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkRefCnt.h"
17 #include "include/core/SkScalar.h"
18 #include "include/core/SkSize.h"
19 #include "include/core/SkString.h"
20 #include "include/core/SkSurface.h"
21 #include "include/effects/SkImageFilters.h"
22
23 #include <utility>
24
25 namespace skiagm {
26
27 class ResizeGM : public GM {
28 public:
29 ResizeGM() {
30 this->setBGColor(0x00000000);
31 }
32
33 protected:
34 SkString getName() const override { return SkString("resizeimagefilter"); }
35
36 void draw(SkCanvas* canvas,
37 const SkRect& rect,
38 const SkSize& deviceSize,
39 const SkSamplingOptions& sampling,
40 sk_sp<SkImageFilter> input) {
41 SkRect dstRect;
42 canvas->getLocalToDeviceAs3x3().mapRect(&dstRect, rect);
43 canvas->save();
44 SkScalar deviceScaleX = deviceSize.width() / dstRect.width();
45 SkScalar deviceScaleY = deviceSize.height() / dstRect.height();
46 canvas->translate(rect.x(), rect.y());
47 canvas->scale(deviceScaleX, deviceScaleY);
48 canvas->translate(-rect.x(), -rect.y());
49 SkMatrix matrix;
50 matrix.setScale(SkScalarInvert(deviceScaleX), SkScalarInvert(deviceScaleY));
51 sk_sp<SkImageFilter> filter(SkImageFilters::MatrixTransform(matrix,
52 sampling,
53 std::move(input)));
54 SkPaint filteredPaint;
55 filteredPaint.setImageFilter(std::move(filter));
56 canvas->saveLayer(&rect, &filteredPaint);
57 SkPaint paint;
58 paint.setColor(0xFF00FF00);
59 SkRect ovalRect = rect;
60 ovalRect.inset(SkIntToScalar(4), SkIntToScalar(4));
61 canvas->drawOval(ovalRect, paint);
62 canvas->restore(); // for saveLayer
63 canvas->restore();
64 }
65
66 SkISize getISize() override { return SkISize::Make(630, 100); }
67
68 void onDraw(SkCanvas* canvas) override {
69 canvas->clear(SK_ColorBLACK);
70
71 const SkSamplingOptions samplings[] = {
72 SkSamplingOptions(),
73 SkSamplingOptions(SkFilterMode::kLinear),
74 SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear),
75 SkSamplingOptions(SkCubicResampler::Mitchell()),
76 SkSamplingOptions::Aniso(16),
77 };
78 const SkRect srcRect = SkRect::MakeWH(96, 96);
79 const SkSize deviceSize = SkSize::Make(16, 16);
80
81 for (const auto& sampling : samplings) {
82 this->draw(canvas, srcRect, deviceSize, sampling, nullptr);
83 canvas->translate(srcRect.width() + SkIntToScalar(10), 0);
84 }
85
86 {
87 sk_sp<SkSurface> surface(SkSurfaces::Raster(SkImageInfo::MakeN32Premul(16, 16)));
88 SkCanvas* surfaceCanvas = surface->getCanvas();
89 surfaceCanvas->clear(0x000000);
90 {
91 SkPaint paint;
92 paint.setColor(0xFF00FF00);
93 SkRect ovalRect = SkRect::MakeWH(16, 16);
94 ovalRect.inset(SkIntToScalar(2)/3, SkIntToScalar(2)/3);
95 surfaceCanvas->drawOval(ovalRect, paint);
96 }
97 sk_sp<SkImage> image(surface->makeImageSnapshot());
98 SkRect inRect = SkRect::MakeXYWH(-4, -4, 20, 20);
99 SkRect outRect = SkRect::MakeXYWH(-24, -24, 120, 120);
100 sk_sp<SkImageFilter> source(
101 SkImageFilters::Image(std::move(image), inRect, outRect,
102 SkSamplingOptions({1/3.0f, 1/3.0f})));
103 this->draw(canvas, srcRect, deviceSize, samplings[3], std::move(source));
104 }
105 }
106
107 private:
108 using INHERITED = GM;
109 };
110
111 //////////////////////////////////////////////////////////////////////////////
112
113 DEF_GM(return new ResizeGM; )
114
115 } // namespace skiagm