Home | History | Annotate | Download | only in gm
      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 #include "SkCanvas.h"
     10 #include "SkColorMatrixFilter.h"
     11 #include "SkColorPriv.h"
     12 #include "SkShader.h"
     13 
     14 #include "SkBlurImageFilter.h"
     15 #include "SkColorFilterImageFilter.h"
     16 
     17 #define FILTER_WIDTH    SkIntToScalar(30)
     18 #define FILTER_HEIGHT   SkIntToScalar(30)
     19 #define MARGIN          SkIntToScalar(10)
     20 
     21 static SkImageFilter* make_blur(float amount, SkImageFilter* input = NULL) {
     22     return new SkBlurImageFilter(amount, amount, input);
     23 }
     24 
     25 static SkImageFilter* make_brightness(float amount, SkImageFilter* input = NULL) {
     26     SkScalar amount255 = SkScalarMul(SkFloatToScalar(amount), SkIntToScalar(255));
     27     SkScalar matrix[20] = { 1, 0, 0, 0, amount255,
     28                             0, 1, 0, 0, amount255,
     29                             0, 0, 1, 0, amount255,
     30                             0, 0, 0, 1, 0 };
     31     SkAutoTUnref<SkColorFilter> filter(new SkColorMatrixFilter(matrix));
     32     return SkColorFilterImageFilter::Create(filter, input);
     33 }
     34 
     35 static SkImageFilter* make_grayscale(SkImageFilter* input = NULL) {
     36     SkScalar matrix[20];
     37     memset(matrix, 0, 20 * sizeof(SkScalar));
     38     matrix[0] = matrix[5] = matrix[10] = SkFloatToScalar(0.2126f);
     39     matrix[1] = matrix[6] = matrix[11] = SkFloatToScalar(0.7152f);
     40     matrix[2] = matrix[7] = matrix[12] = SkFloatToScalar(0.0722f);
     41     matrix[18] = SkFloatToScalar(1.0f);
     42     SkAutoTUnref<SkColorFilter> filter(new SkColorMatrixFilter(matrix));
     43     return SkColorFilterImageFilter::Create(filter, input);
     44 }
     45 
     46 static SkImageFilter* make_mode_blue(SkImageFilter* input = NULL) {
     47     SkAutoTUnref<SkColorFilter> filter(
     48         SkColorFilter::CreateModeFilter(SK_ColorBLUE, SkXfermode::kSrcIn_Mode));
     49     return SkColorFilterImageFilter::Create(filter, input);
     50 }
     51 
     52 class ColorFilterImageFilterGM : public skiagm::GM {
     53 public:
     54     ColorFilterImageFilterGM () {}
     55 
     56 protected:
     57 
     58     virtual SkString onShortName() {
     59         return SkString("colorfilterimagefilter");
     60     }
     61 
     62     void drawClippedRect(SkCanvas* canvas, const SkRect& r, const SkPaint& paint, float outset = 0.0f) {
     63         canvas->save();
     64         SkRect clip(r);
     65         clip.outset(SkFloatToScalar(outset), SkFloatToScalar(outset));
     66         canvas->clipRect(clip);
     67         canvas->drawRect(r, paint);
     68         canvas->restore();
     69     }
     70 
     71     virtual SkISize onISize() { return SkISize::Make(400, 100); }
     72 
     73     virtual void onDraw(SkCanvas* canvas) {
     74 
     75         SkRect r = SkRect::MakeWH(FILTER_WIDTH, FILTER_HEIGHT);
     76         SkPaint paint;
     77         paint.setColor(SK_ColorRED);
     78         canvas->save();
     79         for (float brightness = -1.0f; brightness <= 1.0f; brightness += 0.2f) {
     80             SkAutoTUnref<SkImageFilter> dim(make_brightness(-brightness));
     81             SkAutoTUnref<SkImageFilter> bright(make_brightness(brightness, dim));
     82             paint.setImageFilter(bright);
     83             drawClippedRect(canvas, r, paint);
     84             canvas->translate(FILTER_WIDTH + MARGIN, 0);
     85         }
     86         canvas->restore();
     87         canvas->translate(0, FILTER_HEIGHT + MARGIN);
     88         {
     89             SkAutoTUnref<SkImageFilter> brightness(make_brightness(0.9f));
     90             SkAutoTUnref<SkImageFilter> grayscale(make_grayscale(brightness));
     91             paint.setImageFilter(grayscale);
     92             drawClippedRect(canvas, r, paint);
     93             canvas->translate(FILTER_WIDTH + MARGIN, 0);
     94         }
     95         {
     96             SkAutoTUnref<SkImageFilter> grayscale(make_grayscale());
     97             SkAutoTUnref<SkImageFilter> brightness(make_brightness(0.9f, grayscale));
     98             paint.setImageFilter(brightness);
     99             drawClippedRect(canvas, r, paint);
    100             canvas->translate(FILTER_WIDTH + MARGIN, 0);
    101         }
    102         {
    103             SkAutoTUnref<SkImageFilter> blue(make_mode_blue());
    104             SkAutoTUnref<SkImageFilter> brightness(make_brightness(1.0f, blue));
    105             paint.setImageFilter(brightness);
    106             drawClippedRect(canvas, r, paint);
    107             canvas->translate(FILTER_WIDTH + MARGIN, 0);
    108         }
    109         {
    110             SkAutoTUnref<SkImageFilter> brightness(make_brightness(1.0f));
    111             SkAutoTUnref<SkImageFilter> blue(make_mode_blue(brightness));
    112             paint.setImageFilter(blue);
    113             drawClippedRect(canvas, r, paint);
    114             canvas->translate(FILTER_WIDTH + MARGIN, 0);
    115         }
    116         {
    117             SkAutoTUnref<SkImageFilter> blur(make_blur(3.0f));
    118             SkAutoTUnref<SkImageFilter> brightness(make_brightness(0.5f, blur));
    119             paint.setImageFilter(brightness);
    120             drawClippedRect(canvas, r, paint, 3);
    121             canvas->translate(FILTER_WIDTH + MARGIN, 0);
    122         }
    123     }
    124 
    125 private:
    126     typedef GM INHERITED;
    127 };
    128 
    129 //////////////////////////////////////////////////////////////////////////////
    130 
    131 static skiagm::GM* MyFactory(void*) { return new ColorFilterImageFilterGM; }
    132 static skiagm::GMRegistry reg(MyFactory);
    133