blob: b7d0583853fd0834828b1c510ac09e1bb5658d3c [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());
61
fmalitacd56f812015-09-14 13:31:18 -070062 {
fmalitacd56f812015-09-14 13:31:18 -070063 // Draw an unscaled bitmap.
Michael Ludwig898bbfa2019-08-02 15:21:23 -040064 sk_sp<SkImageFilter> imageSource(SkImageFilters::Image(fImage));
robertphillips549c8992016-04-01 09:28:51 -070065 fill_rect_filtered(canvas, clipRect, std::move(imageSource));
fmalitacd56f812015-09-14 13:31:18 -070066 canvas->translate(SkIntToScalar(100), 0);
robertphillips549c8992016-04-01 09:28:51 -070067 }
68 {
fmalitacd56f812015-09-14 13:31:18 -070069 // Draw an unscaled subset of the source bitmap (srcRect -> srcRect).
robertphillips549c8992016-04-01 09:28:51 -070070 sk_sp<SkImageFilter> imageSourceSrcRect(
Michael Ludwig898bbfa2019-08-02 15:21:23 -040071 SkImageFilters::Image(fImage, srcRect, srcRect, kHigh_SkFilterQuality));
robertphillips549c8992016-04-01 09:28:51 -070072 fill_rect_filtered(canvas, clipRect, std::move(imageSourceSrcRect));
fmalitacd56f812015-09-14 13:31:18 -070073 canvas->translate(SkIntToScalar(100), 0);
robertphillips549c8992016-04-01 09:28:51 -070074 }
75 {
fmalitacd56f812015-09-14 13:31:18 -070076 // Draw a subset of the bitmap scaled to a destination rect (srcRect -> dstRect).
robertphillips549c8992016-04-01 09:28:51 -070077 sk_sp<SkImageFilter> imageSourceSrcRectDstRect(
Michael Ludwig898bbfa2019-08-02 15:21:23 -040078 SkImageFilters::Image(fImage, srcRect, dstRect, kHigh_SkFilterQuality));
robertphillips549c8992016-04-01 09:28:51 -070079 fill_rect_filtered(canvas, clipRect, std::move(imageSourceSrcRectDstRect));
fmalitacd56f812015-09-14 13:31:18 -070080 canvas->translate(SkIntToScalar(100), 0);
robertphillips549c8992016-04-01 09:28:51 -070081 }
82 {
fmalitacd56f812015-09-14 13:31:18 -070083 // Draw the entire bitmap scaled to a destination rect (bounds -> dstRect).
robertphillips549c8992016-04-01 09:28:51 -070084 sk_sp<SkImageFilter> imageSourceDstRectOnly(
Michael Ludwig898bbfa2019-08-02 15:21:23 -040085 SkImageFilters::Image(fImage, bounds, dstRect, kHigh_SkFilterQuality));
robertphillips549c8992016-04-01 09:28:51 -070086 fill_rect_filtered(canvas, clipRect, std::move(imageSourceDstRectOnly));
fmalitacd56f812015-09-14 13:31:18 -070087 canvas->translate(SkIntToScalar(100), 0);
88 }
89 }
90
91private:
reed9ce9d672016-03-17 10:51:11 -070092 sk_sp<SkImage> fImage;
John Stiles7571f9e2020-09-02 22:42:33 -040093 using INHERITED = GM;
fmalitacd56f812015-09-14 13:31:18 -070094};
95
96///////////////////////////////////////////////////////////////////////////////
97
98DEF_GM( return new ImageSourceGM; )