1 /* 2 * Copyright 2011 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 "SkColorMatrixFilter.h" 10 11 #define WIDTH 500 12 #define HEIGHT 500 13 14 namespace skiagm { 15 16 class ColorMatrixGM : public GM { 17 public: 18 ColorMatrixGM() { 19 this->setBGColor(0xFF808080); 20 fBitmap = createBitmap(64, 64); 21 } 22 23 protected: 24 virtual SkString onShortName() { 25 return SkString("colormatrix"); 26 } 27 28 virtual SkISize onISize() { 29 return make_isize(WIDTH, HEIGHT); 30 } 31 32 SkBitmap createBitmap(int width, int height) { 33 SkBitmap bm; 34 bm.setConfig(SkBitmap::kARGB_8888_Config, width, height); 35 bm.allocPixels(); 36 SkCanvas canvas(bm); 37 canvas.clear(0x0); 38 for (int y = 0; y < height; ++y) { 39 for (int x = 0; x < width; ++x) { 40 SkPaint paint; 41 paint.setColor(SkColorSetARGB(255, x * 255 / width, y * 255 / height, 0)); 42 canvas.drawRect(SkRect::MakeXYWH(x, y, 1, 1), paint); 43 } 44 } 45 return bm; 46 } 47 virtual void onDraw(SkCanvas* canvas) { 48 49 SkPaint paint; 50 SkColorMatrix matrix; 51 SkColorMatrixFilter* filter = new SkColorMatrixFilter(); 52 paint.setColorFilter(filter)->unref(); 53 54 matrix.setIdentity(); 55 filter->setMatrix(matrix); 56 canvas->drawBitmap(fBitmap, 0, 0, &paint); 57 58 matrix.setRotate(SkColorMatrix::kR_Axis, 90); 59 filter->setMatrix(matrix); 60 canvas->drawBitmap(fBitmap, 80, 0, &paint); 61 62 matrix.setRotate(SkColorMatrix::kG_Axis, 90); 63 filter->setMatrix(matrix); 64 canvas->drawBitmap(fBitmap, 160, 0, &paint); 65 66 matrix.setRotate(SkColorMatrix::kB_Axis, 90); 67 filter->setMatrix(matrix); 68 canvas->drawBitmap(fBitmap, 240, 0, &paint); 69 70 matrix.setSaturation(SkFloatToScalar(0.0f)); 71 filter->setMatrix(matrix); 72 canvas->drawBitmap(fBitmap, 0, 80, &paint); 73 74 matrix.setSaturation(SkFloatToScalar(0.5f)); 75 filter->setMatrix(matrix); 76 canvas->drawBitmap(fBitmap, 80, 80, &paint); 77 78 matrix.setSaturation(SkFloatToScalar(1.0f)); 79 filter->setMatrix(matrix); 80 canvas->drawBitmap(fBitmap, 160, 80, &paint); 81 82 matrix.setSaturation(SkFloatToScalar(2.0f)); 83 filter->setMatrix(matrix); 84 canvas->drawBitmap(fBitmap, 240, 80, &paint); 85 86 matrix.setRGB2YUV(); 87 filter->setMatrix(matrix); 88 canvas->drawBitmap(fBitmap, 0, 160, &paint); 89 90 matrix.setYUV2RGB(); 91 filter->setMatrix(matrix); 92 canvas->drawBitmap(fBitmap, 80, 160, &paint); 93 94 SkScalar s1 = SK_Scalar1; 95 SkScalar s255 = SkIntToScalar(255); 96 // Move red into alpha, set color to white 97 SkScalar data[20] = { 98 0, 0, 0, 0, s255, 99 0, 0, 0, 0, s255, 100 0, 0, 0, 0, s255, 101 s1, 0, 0, 0, 0, 102 }; 103 104 filter->setArray(data); 105 canvas->drawBitmap(fBitmap, 160, 160, &paint); 106 } 107 108 private: 109 SkBitmap fBitmap; 110 typedef GM INHERITED; 111 }; 112 113 ////////////////////////////////////////////////////////////////////////////// 114 115 static GM* MyFactory(void*) { return new ColorMatrixGM; } 116 static GMRegistry reg(MyFactory); 117 118 } 119