blob: 84de781bec7d4d4c2d91ceac148a1264a3aa84d8 [file] [log] [blame]
fmalitacd56f812015-09-14 13:31:18 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBitmap.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkColor.h"
12#include "include/core/SkFilterQuality.h"
13#include "include/core/SkImage.h"
14#include "include/core/SkImageFilter.h"
15#include "include/core/SkPaint.h"
16#include "include/core/SkRect.h"
17#include "include/core/SkRefCnt.h"
18#include "include/core/SkScalar.h"
19#include "include/core/SkSize.h"
20#include "include/core/SkString.h"
Michael Ludwig898bbfa2019-08-02 15:21:23 -040021#include "include/effects/SkImageFilters.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "tools/ToolUtils.h"
fmalitacd56f812015-09-14 13:31:18 -070023
Ben Wagner7fde8e12019-05-01 17:28:53 -040024#include <utility>
fmalitacd56f812015-09-14 13:31:18 -070025
26// This GM exercises the SkImageSource ImageFilter class.
27
robertphillips549c8992016-04-01 09:28:51 -070028static void fill_rect_filtered(SkCanvas* canvas,
29 const SkRect& clipRect,
30 sk_sp<SkImageFilter> filter) {
31 SkPaint paint;
32 paint.setImageFilter(std::move(filter));
33 canvas->save();
34 canvas->clipRect(clipRect);
35 canvas->drawPaint(paint);
36 canvas->restore();
37}
38
fmalitacd56f812015-09-14 13:31:18 -070039class ImageSourceGM : public skiagm::GM {
40public:
41 ImageSourceGM() { }
42
43protected:
44 SkString onShortName() override {
45 return SkString("imagesource");
46 }
47
48 SkISize onISize() override { return SkISize::Make(500, 150); }
49
50 void onOnceBeforeDraw() override {
Mike Reedac9f0c92020-12-23 10:11:33 -050051 fImage = ToolUtils::create_string_image(100, 100, 0xFFFFFFFF, 20, 70, 96, "e");
fmalitacd56f812015-09-14 13:31:18 -070052 }
53
fmalitacd56f812015-09-14 13:31:18 -070054 void onDraw(SkCanvas* canvas) override {
55 canvas->clear(SK_ColorBLACK);
robertphillips549c8992016-04-01 09:28:51 -070056
57 const SkRect srcRect = SkRect::MakeXYWH(20, 20, 30, 30);
58 const SkRect dstRect = SkRect::MakeXYWH(0, 10, 60, 60);
59 const SkRect clipRect = SkRect::MakeXYWH(0, 0, 100, 100);
60 const SkRect bounds = SkRect::MakeIWH(fImage->width(), fImage->height());
Mike Reed708dc4f2021-02-13 21:04:58 -050061 const SkSamplingOptions sampling({1/3.0f, 1/3.0f});
robertphillips549c8992016-04-01 09:28:51 -070062
fmalitacd56f812015-09-14 13:31:18 -070063 {
fmalitacd56f812015-09-14 13:31:18 -070064 // Draw an unscaled bitmap.
Michael Ludwig898bbfa2019-08-02 15:21:23 -040065 sk_sp<SkImageFilter> imageSource(SkImageFilters::Image(fImage));
robertphillips549c8992016-04-01 09:28:51 -070066 fill_rect_filtered(canvas, clipRect, std::move(imageSource));
fmalitacd56f812015-09-14 13:31:18 -070067 canvas->translate(SkIntToScalar(100), 0);
robertphillips549c8992016-04-01 09:28:51 -070068 }
69 {
fmalitacd56f812015-09-14 13:31:18 -070070 // Draw an unscaled subset of the source bitmap (srcRect -> srcRect).
robertphillips549c8992016-04-01 09:28:51 -070071 sk_sp<SkImageFilter> imageSourceSrcRect(
Mike Reed708dc4f2021-02-13 21:04:58 -050072 SkImageFilters::Image(fImage, srcRect, srcRect, sampling));
robertphillips549c8992016-04-01 09:28:51 -070073 fill_rect_filtered(canvas, clipRect, std::move(imageSourceSrcRect));
fmalitacd56f812015-09-14 13:31:18 -070074 canvas->translate(SkIntToScalar(100), 0);
robertphillips549c8992016-04-01 09:28:51 -070075 }
76 {
fmalitacd56f812015-09-14 13:31:18 -070077 // Draw a subset of the bitmap scaled to a destination rect (srcRect -> dstRect).
robertphillips549c8992016-04-01 09:28:51 -070078 sk_sp<SkImageFilter> imageSourceSrcRectDstRect(
Mike Reed708dc4f2021-02-13 21:04:58 -050079 SkImageFilters::Image(fImage, srcRect, dstRect, sampling));
robertphillips549c8992016-04-01 09:28:51 -070080 fill_rect_filtered(canvas, clipRect, std::move(imageSourceSrcRectDstRect));
fmalitacd56f812015-09-14 13:31:18 -070081 canvas->translate(SkIntToScalar(100), 0);
robertphillips549c8992016-04-01 09:28:51 -070082 }
83 {
fmalitacd56f812015-09-14 13:31:18 -070084 // Draw the entire bitmap scaled to a destination rect (bounds -> dstRect).
robertphillips549c8992016-04-01 09:28:51 -070085 sk_sp<SkImageFilter> imageSourceDstRectOnly(
Mike Reed708dc4f2021-02-13 21:04:58 -050086 SkImageFilters::Image(fImage, bounds, dstRect, sampling));
robertphillips549c8992016-04-01 09:28:51 -070087 fill_rect_filtered(canvas, clipRect, std::move(imageSourceDstRectOnly));
fmalitacd56f812015-09-14 13:31:18 -070088 canvas->translate(SkIntToScalar(100), 0);
89 }
90 }
91
92private:
reed9ce9d672016-03-17 10:51:11 -070093 sk_sp<SkImage> fImage;
John Stiles7571f9e2020-09-02 22:42:33 -040094 using INHERITED = GM;
fmalitacd56f812015-09-14 13:31:18 -070095};
96
97///////////////////////////////////////////////////////////////////////////////
98
99DEF_GM( return new ImageSourceGM; )