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 "SkColorFilterImageFilter.h" 9 #include "SkColorMatrixFilter.h" 10 #include "SkImage.h" 11 #include "SkImageSource.h" 12 #include "SkTileImageFilter.h" 13 #include "gm.h" 14 #include "sk_tool_utils.h" 15 16 #define WIDTH 400 17 #define HEIGHT 200 18 #define MARGIN 12 19 20 namespace skiagm { 21 22 class TileImageFilterGM : public GM { 23 public: TileImageFilterGM()24 TileImageFilterGM() { 25 this->setBGColor(0xFF000000); 26 } 27 28 protected: onShortName()29 SkString onShortName() override { 30 return SkString("tileimagefilter"); 31 } 32 onISize()33 SkISize onISize() override{ 34 return SkISize::Make(WIDTH, HEIGHT); 35 } 36 onOnceBeforeDraw()37 void onOnceBeforeDraw() override { 38 fBitmap = SkImage::MakeFromBitmap( 39 sk_tool_utils::create_string_bitmap(50, 50, 0xD000D000, 10, 45, 50, "e")); 40 41 fCheckerboard = SkImage::MakeFromBitmap( 42 sk_tool_utils::create_checkerboard_bitmap(80, 80, 43 0xFFA0A0A0, 44 0xFF404040, 45 8)); 46 } 47 onDraw(SkCanvas * canvas)48 void onDraw(SkCanvas* canvas) override { 49 canvas->clear(SK_ColorBLACK); 50 SkPaint red; 51 red.setColor(SK_ColorRED); 52 red.setStyle(SkPaint::kStroke_Style); 53 SkPaint blue; 54 blue.setColor(SK_ColorBLUE); 55 blue.setStyle(SkPaint::kStroke_Style); 56 57 int x = 0, y = 0; 58 for (size_t i = 0; i < 4; i++) { 59 sk_sp<SkImage> image = (i & 0x01) ? fCheckerboard : fBitmap; 60 SkRect srcRect = SkRect::MakeXYWH(SkIntToScalar(image->width()/4), 61 SkIntToScalar(image->height()/4), 62 SkIntToScalar(image->width()/(i+1)), 63 SkIntToScalar(image->height()/(i+1))); 64 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(i * 8), 65 SkIntToScalar(i * 4), 66 SkIntToScalar(image->width() - i * 12), 67 SkIntToScalar(image->height()) - i * 12); 68 sk_sp<SkImageFilter> tileInput(SkImageSource::Make(image)); 69 sk_sp<SkImageFilter> filter(SkTileImageFilter::Make(srcRect, 70 dstRect, 71 std::move(tileInput))); 72 canvas->save(); 73 canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); 74 SkPaint paint; 75 paint.setImageFilter(std::move(filter)); 76 canvas->drawImage(fBitmap.get(), 0, 0, &paint); 77 canvas->drawRect(srcRect, red); 78 canvas->drawRect(dstRect, blue); 79 canvas->restore(); 80 x += image->width() + MARGIN; 81 if (x + image->width() > WIDTH) { 82 x = 0; 83 y += image->height() + MARGIN; 84 } 85 } 86 87 SkScalar matrix[20] = { SK_Scalar1, 0, 0, 0, 0, 88 0, SK_Scalar1, 0, 0, 0, 89 0, 0, SK_Scalar1, 0, 0, 90 0, 0, 0, SK_Scalar1, 0 }; 91 92 SkRect srcRect = SkRect::MakeWH(SkIntToScalar(fBitmap->width()), 93 SkIntToScalar(fBitmap->height())); 94 SkRect dstRect = SkRect::MakeWH(SkIntToScalar(fBitmap->width() * 2), 95 SkIntToScalar(fBitmap->height() * 2)); 96 sk_sp<SkImageFilter> tile(SkTileImageFilter::Make(srcRect, dstRect, nullptr)); 97 sk_sp<SkColorFilter> cf(SkColorFilter::MakeMatrixFilterRowMajor255(matrix)); 98 99 SkPaint paint; 100 paint.setImageFilter(SkColorFilterImageFilter::Make(std::move(cf), std::move(tile))); 101 canvas->save(); 102 canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); 103 canvas->clipRect(dstRect); 104 canvas->saveLayer(&dstRect, &paint); 105 canvas->drawImage(fBitmap.get(), 0, 0); 106 canvas->restore(); 107 canvas->drawRect(srcRect, red); 108 canvas->drawRect(dstRect, blue); 109 canvas->restore(); 110 111 canvas->translate(0, SkIntToScalar(100)); 112 113 srcRect = SkRect::MakeXYWH(0, 0, 50, 50); 114 dstRect = SkRect::MakeXYWH(0, 0, 100, 100); 115 SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(5, 5, 40, 40)); 116 sk_sp<SkColorFilter> greenCF = SkColorFilter::MakeModeFilter(SK_ColorGREEN, 117 SkBlendMode::kSrc); 118 sk_sp<SkImageFilter> green(SkColorFilterImageFilter::Make(std::move(greenCF), 119 nullptr, 120 &cropRect)); 121 tile = SkTileImageFilter::Make(srcRect, dstRect, std::move(green)); 122 paint.setColor(SK_ColorRED); 123 paint.setImageFilter(std::move(tile)); 124 canvas->drawRect(dstRect, paint); 125 } 126 private: 127 sk_sp<SkImage> fBitmap, fCheckerboard; 128 129 typedef GM INHERITED; 130 }; 131 132 ////////////////////////////////////////////////////////////////////////////// 133 134 DEF_GM(return new TileImageFilterGM;) 135 } 136