blob: 897012efe8bf5bd9142fd8ef5d5ebb136a9f9b76 [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 Kleinea3f0142019-03-20 11:12:10 -050051 SkBitmap bm = ToolUtils::create_string_bitmap(100, 100, 0xFFFFFFFF, 20, 70, 96, "e");
reed9ce9d672016-03-17 10:51:11 -070052 fImage = SkImage::MakeFromBitmap(bm);
fmalitacd56f812015-09-14 13:31:18 -070053 }
54
fmalitacd56f812015-09-14 13:31:18 -070055 void onDraw(SkCanvas* canvas) override {
56 canvas->clear(SK_ColorBLACK);
robertphillips549c8992016-04-01 09:28:51 -070057
58 const SkRect srcRect = SkRect::MakeXYWH(20, 20, 30, 30);
59 const SkRect dstRect = SkRect::MakeXYWH(0, 10, 60, 60);
60 const SkRect clipRect = SkRect::MakeXYWH(0, 0, 100, 100);
61 const SkRect bounds = SkRect::MakeIWH(fImage->width(), fImage->height());
62
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(
Michael Ludwig898bbfa2019-08-02 15:21:23 -040072 SkImageFilters::Image(fImage, srcRect, srcRect, kHigh_SkFilterQuality));
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(
Michael Ludwig898bbfa2019-08-02 15:21:23 -040079 SkImageFilters::Image(fImage, srcRect, dstRect, kHigh_SkFilterQuality));
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(
Michael Ludwig898bbfa2019-08-02 15:21:23 -040086 SkImageFilters::Image(fImage, bounds, dstRect, kHigh_SkFilterQuality));
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;
fmalitacd56f812015-09-14 13:31:18 -070094 typedef GM INHERITED;
95};
96
97///////////////////////////////////////////////////////////////////////////////
98
99DEF_GM( return new ImageSourceGM; )