colormatrix.cpp revision 7b6c19392cd980462908764b5ea17c4796610427
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 14class SkOnce { 15public: 16 SkOnce() : fOnce(false) {}; 17 18 bool once() const { 19 if (fOnce) { 20 return false; 21 } 22 fOnce = true; 23 return true; 24 } 25 26private: 27 mutable bool fOnce; 28}; 29 30static void setColorMatrix(SkPaint* paint, const SkColorMatrix& matrix) { 31 paint->setColorFilter(SkNEW_ARGS(SkColorMatrixFilter, (matrix)))->unref(); 32} 33 34static void setArray(SkPaint* paint, const SkScalar array[]) { 35 paint->setColorFilter(SkNEW_ARGS(SkColorMatrixFilter, (array)))->unref(); 36} 37 38namespace skiagm { 39 40class ColorMatrixGM : public GM { 41 SkOnce fOnce; 42 void init() { 43 if (fOnce.once()) { 44 fBitmap = createBitmap(64, 64); 45 } 46 } 47 48public: 49 ColorMatrixGM() { 50 this->setBGColor(0xFF808080); 51 } 52 53protected: 54 virtual SkString onShortName() { 55 return SkString("colormatrix"); 56 } 57 58 virtual SkISize onISize() { 59 return make_isize(WIDTH, HEIGHT); 60 } 61 62 SkBitmap createBitmap(int width, int height) { 63 SkBitmap bm; 64 bm.setConfig(SkBitmap::kARGB_8888_Config, width, height); 65 bm.allocPixels(); 66 SkCanvas canvas(bm); 67 canvas.clear(0x0); 68 for (int y = 0; y < height; ++y) { 69 for (int x = 0; x < width; ++x) { 70 SkPaint paint; 71 paint.setColor(SkColorSetARGB(255, x * 255 / width, y * 255 / height, 0)); 72 canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(x), 73 SkIntToScalar(y), SK_Scalar1, SK_Scalar1), paint); 74 } 75 } 76 return bm; 77 } 78 virtual void onDraw(SkCanvas* canvas) { 79 this->init(); 80 81 SkPaint paint; 82 SkColorMatrix matrix; 83 84 matrix.setIdentity(); 85 setColorMatrix(&paint, matrix); 86 canvas->drawBitmap(fBitmap, 0, 0, &paint); 87 88 matrix.setRotate(SkColorMatrix::kR_Axis, 90); 89 setColorMatrix(&paint, matrix); 90 canvas->drawBitmap(fBitmap, 80, 0, &paint); 91 92 matrix.setRotate(SkColorMatrix::kG_Axis, 90); 93 setColorMatrix(&paint, matrix); 94 canvas->drawBitmap(fBitmap, 160, 0, &paint); 95 96 matrix.setRotate(SkColorMatrix::kB_Axis, 90); 97 setColorMatrix(&paint, matrix); 98 canvas->drawBitmap(fBitmap, 240, 0, &paint); 99 100 matrix.setSaturation(SkFloatToScalar(0.0f)); 101 setColorMatrix(&paint, matrix); 102 canvas->drawBitmap(fBitmap, 0, 80, &paint); 103 104 matrix.setSaturation(SkFloatToScalar(0.5f)); 105 setColorMatrix(&paint, matrix); 106 canvas->drawBitmap(fBitmap, 80, 80, &paint); 107 108 matrix.setSaturation(SkFloatToScalar(1.0f)); 109 setColorMatrix(&paint, matrix); 110 canvas->drawBitmap(fBitmap, 160, 80, &paint); 111 112 matrix.setSaturation(SkFloatToScalar(2.0f)); 113 setColorMatrix(&paint, matrix); 114 canvas->drawBitmap(fBitmap, 240, 80, &paint); 115 116 matrix.setRGB2YUV(); 117 setColorMatrix(&paint, matrix); 118 canvas->drawBitmap(fBitmap, 0, 160, &paint); 119 120 matrix.setYUV2RGB(); 121 setColorMatrix(&paint, matrix); 122 canvas->drawBitmap(fBitmap, 80, 160, &paint); 123 124 SkScalar s1 = SK_Scalar1; 125 SkScalar s255 = SkIntToScalar(255); 126 // Move red into alpha, set color to white 127 SkScalar data[20] = { 128 0, 0, 0, 0, s255, 129 0, 0, 0, 0, s255, 130 0, 0, 0, 0, s255, 131 s1, 0, 0, 0, 0, 132 }; 133 134 setArray(&paint, data); 135 canvas->drawBitmap(fBitmap, 160, 160, &paint); 136 } 137 138private: 139 SkBitmap fBitmap; 140 typedef GM INHERITED; 141}; 142 143////////////////////////////////////////////////////////////////////////////// 144 145static GM* MyFactory(void*) { return new ColorMatrixGM; } 146static GMRegistry reg(MyFactory); 147 148} 149