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 "gm/gm.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkMatrix.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkRect.h"
14 #include "include/core/SkScalar.h"
15 #include "include/core/SkSize.h"
16 #include "include/core/SkString.h"
17 #include "include/core/SkTypes.h"
18 #include "tools/Resources.h"
19 #include "tools/ToolUtils.h"
20
21 namespace {
computeSize(const SkBitmap & bm,const SkMatrix & mat)22 static SkSize computeSize(const SkBitmap& bm, const SkMatrix& mat) {
23 SkRect bounds = SkRect::MakeWH(SkIntToScalar(bm.width()),
24 SkIntToScalar(bm.height()));
25 mat.mapRect(&bounds);
26 return SkSize::Make(bounds.width(), bounds.height());
27 }
28
draw_cell(SkCanvas * canvas,const SkBitmap & bm,const SkMatrix & mat,SkScalar dx,const SkSamplingOptions & sampling)29 static void draw_cell(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat, SkScalar dx,
30 const SkSamplingOptions& sampling) {
31 SkAutoCanvasRestore acr(canvas, true);
32
33 canvas->translate(dx, 0);
34 canvas->concat(mat);
35 canvas->drawImage(bm.asImage(), 0, 0, sampling);
36 }
37
draw_row(SkCanvas * canvas,const SkBitmap & bm,const SkMatrix & mat,SkScalar dx)38 static void draw_row(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat, SkScalar dx) {
39 draw_cell(canvas, bm, mat, 0 * dx, SkSamplingOptions());
40 draw_cell(canvas, bm, mat, 1 * dx, SkSamplingOptions(SkFilterMode::kLinear));
41 draw_cell(canvas, bm, mat, 2 * dx, SkSamplingOptions(SkFilterMode::kLinear,
42 SkMipmapMode::kLinear));
43 draw_cell(canvas, bm, mat, 3 * dx, SkSamplingOptions(SkCubicResampler::Mitchell()));
44 }
45
46 class FilterIndiaBoxGM : public skiagm::GM {
47 SkBitmap fBM;
48 SkMatrix fMatrix[2];
49
onOnceBeforeDraw()50 void onOnceBeforeDraw() override {
51 constexpr char kResource[] = "images/box.gif";
52 if (!GetResourceAsBitmap(kResource, &fBM)) {
53 fBM.allocN32Pixels(1, 1);
54 fBM.eraseARGB(255, 255, 0 , 0); // red == bad
55 }
56 fBM.setImmutable();
57
58 SkScalar cx = SkScalarHalf(fBM.width());
59 SkScalar cy = SkScalarHalf(fBM.height());
60
61 float vertScale = 30.0f/55.0f;
62 float horizScale = 150.0f/200.0f;
63
64 fMatrix[0].setScale(horizScale, vertScale);
65 fMatrix[1].setRotate(30, cx, cy); fMatrix[1].postScale(horizScale, vertScale);
66 }
67
onShortName()68 SkString onShortName() override { return SkString("filterindiabox"); }
69
onISize()70 SkISize onISize() override { return {680, 130}; }
71
onDraw(SkCanvas * canvas)72 void onDraw(SkCanvas* canvas) override {
73 canvas->translate(10, 10);
74 for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrix); ++i) {
75 SkSize size = computeSize(fBM, fMatrix[i]);
76 size.fWidth += 20;
77 size.fHeight += 20;
78
79 draw_row(canvas, fBM, fMatrix[i], size.fWidth);
80 canvas->translate(0, size.fHeight);
81 }
82 }
83 };
84 } // namespace
85
86 DEF_GM( return new FilterIndiaBoxGM(); )
87