1 /* 2 * Copyright 2012 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.h" 9 10 #include "SkBitmapSource.h" 11 #include "SkBlendImageFilter.h" 12 #include "SkBlurImageFilter.h" 13 #include "SkColorFilter.h" 14 #include "SkColorMatrixFilter.h" 15 #include "SkColorFilterImageFilter.h" 16 #include "SkMergeImageFilter.h" 17 #include "SkMorphologyImageFilter.h" 18 19 #include "SkTestImageFilters.h" 20 21 /////////////////////////////////////////////////////////////////////////////// 22 23 class ImageFiltersGraphGM : public skiagm::GM { 24 public: 25 ImageFiltersGraphGM() : fInitialized(false) {} 26 27 protected: 28 virtual SkString onShortName() { 29 return SkString("imagefiltersgraph"); 30 } 31 32 void make_bitmap() { 33 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); 34 fBitmap.allocPixels(); 35 SkDevice device(fBitmap); 36 SkCanvas canvas(&device); 37 canvas.clear(0x00000000); 38 SkPaint paint; 39 paint.setAntiAlias(true); 40 paint.setColor(0xFFFFFFFF); 41 paint.setTextSize(SkIntToScalar(96)); 42 const char* str = "e"; 43 canvas.drawText(str, strlen(str), SkIntToScalar(20), SkIntToScalar(70), paint); 44 } 45 46 virtual SkISize onISize() { return SkISize::Make(200, 100); } 47 48 virtual void onDraw(SkCanvas* canvas) { 49 if (!fInitialized) { 50 this->make_bitmap(); 51 fInitialized = true; 52 } 53 canvas->clear(0x00000000); 54 { 55 SkAutoTUnref<SkImageFilter> bitmapSource(new SkBitmapSource(fBitmap)); 56 SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(SK_ColorRED, 57 SkXfermode::kSrcIn_Mode)); 58 SkAutoTUnref<SkImageFilter> blur(new SkBlurImageFilter(4.0f, 4.0f, bitmapSource)); 59 SkAutoTUnref<SkImageFilter> erode(new SkErodeImageFilter(4, 4, blur)); 60 SkAutoTUnref<SkImageFilter> color(SkColorFilterImageFilter::Create(cf, erode)); 61 SkAutoTUnref<SkImageFilter> merge(new SkMergeImageFilter(blur, color)); 62 63 SkPaint paint; 64 paint.setImageFilter(merge); 65 canvas->drawPaint(paint); 66 } 67 { 68 SkAutoTUnref<SkImageFilter> morph(new SkDilateImageFilter(5, 5)); 69 70 SkScalar matrix[20] = { SK_Scalar1, 0, 0, 0, 0, 71 0, SK_Scalar1, 0, 0, 0, 72 0, 0, SK_Scalar1, 0, 0, 73 0, 0, 0, SkFloatToScalar(0.5f), 0 }; 74 75 SkAutoTUnref<SkColorFilter> matrixFilter(new SkColorMatrixFilter(matrix)); 76 SkAutoTUnref<SkImageFilter> colorMorph(SkColorFilterImageFilter::Create(matrixFilter, morph)); 77 SkAutoTUnref<SkImageFilter> blendColor(new SkBlendImageFilter(SkBlendImageFilter::kNormal_Mode, colorMorph)); 78 79 SkPaint paint; 80 paint.setImageFilter(blendColor); 81 canvas->drawBitmap(fBitmap, 100, 0, &paint); 82 } 83 } 84 85 private: 86 typedef GM INHERITED; 87 SkBitmap fBitmap; 88 bool fInitialized; 89 }; 90 91 /////////////////////////////////////////////////////////////////////////////// 92 93 static skiagm::GM* MyFactory(void*) { return new ImageFiltersGraphGM; } 94 static skiagm::GMRegistry reg(MyFactory); 95