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#include "SkGradientShader.h" 11 12#define WIDTH 500 13#define HEIGHT 500 14 15class SkOnce { 16public: 17 SkOnce() : fOnce(false) {}; 18 19 bool once() const { 20 if (fOnce) { 21 return false; 22 } 23 fOnce = true; 24 return true; 25 } 26 27private: 28 mutable bool fOnce; 29}; 30 31static void setColorMatrix(SkPaint* paint, const SkColorMatrix& matrix) { 32 paint->setColorFilter(SkNEW_ARGS(SkColorMatrixFilter, (matrix)))->unref(); 33} 34 35static void setArray(SkPaint* paint, const SkScalar array[]) { 36 paint->setColorFilter(SkNEW_ARGS(SkColorMatrixFilter, (array)))->unref(); 37} 38 39namespace skiagm { 40 41class ColorMatrixGM : public GM { 42 SkOnce fOnce; 43 void init() { 44 if (fOnce.once()) { 45 fSolidBitmap = this->createSolidBitmap(64, 64); 46 fTransparentBitmap = this->createTransparentBitmap(64, 64); 47 } 48 } 49 50public: 51 ColorMatrixGM() { 52 this->setBGColor(0xFF808080); 53 } 54 55protected: 56 virtual SkString onShortName() { 57 return SkString("colormatrix"); 58 } 59 60 virtual SkISize onISize() { 61 return make_isize(WIDTH, HEIGHT); 62 } 63 64 SkBitmap createSolidBitmap(int width, int height) { 65 SkBitmap bm; 66 bm.setConfig(SkBitmap::kARGB_8888_Config, width, height); 67 bm.allocPixels(); 68 SkCanvas canvas(bm); 69 canvas.clear(0x0); 70 for (int y = 0; y < height; ++y) { 71 for (int x = 0; x < width; ++x) { 72 SkPaint paint; 73 paint.setColor(SkColorSetARGB(255, x * 255 / width, y * 255 / height, 0)); 74 canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(x), 75 SkIntToScalar(y), SK_Scalar1, SK_Scalar1), paint); 76 } 77 } 78 return bm; 79 } 80 81 // creates a bitmap with shades of transparent gray. 82 SkBitmap createTransparentBitmap(int width, int height) { 83 SkBitmap bm; 84 bm.setConfig(SkBitmap::kARGB_8888_Config, width, height); 85 bm.allocPixels(); 86 SkCanvas canvas(bm); 87 canvas.clear(0x0); 88 89 SkPoint pts[] = {{0, 0}, {SkIntToScalar(width), SkIntToScalar(height)}}; 90 SkColor colors[] = {0x00000000, 0xFFFFFFFF}; 91 SkPaint paint; 92 paint.setShader(SkGradientShader::CreateLinear(pts, colors, NULL, 2, 93 SkShader::kClamp_TileMode))->unref(); 94 canvas.drawRect(SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)), paint); 95 return bm; 96 } 97 98 virtual void onDraw(SkCanvas* canvas) { 99 this->init(); 100 101 SkPaint paint; 102 SkColorMatrix matrix; 103 104 paint.setXfermodeMode(SkXfermode::kSrc_Mode); 105 const SkBitmap bmps[] = { fSolidBitmap, fTransparentBitmap }; 106 107 for (size_t i = 0; i < SK_ARRAY_COUNT(bmps); ++i) { 108 109 matrix.setIdentity(); 110 setColorMatrix(&paint, matrix); 111 canvas->drawBitmap(bmps[i], 0, 0, &paint); 112 113 matrix.setRotate(SkColorMatrix::kR_Axis, 90); 114 setColorMatrix(&paint, matrix); 115 canvas->drawBitmap(bmps[i], 80, 0, &paint); 116 117 matrix.setRotate(SkColorMatrix::kG_Axis, 90); 118 setColorMatrix(&paint, matrix); 119 canvas->drawBitmap(bmps[i], 160, 0, &paint); 120 121 matrix.setRotate(SkColorMatrix::kB_Axis, 90); 122 setColorMatrix(&paint, matrix); 123 canvas->drawBitmap(bmps[i], 240, 0, &paint); 124 125 matrix.setSaturation(0.0f); 126 setColorMatrix(&paint, matrix); 127 canvas->drawBitmap(bmps[i], 0, 80, &paint); 128 129 matrix.setSaturation(0.5f); 130 setColorMatrix(&paint, matrix); 131 canvas->drawBitmap(bmps[i], 80, 80, &paint); 132 133 matrix.setSaturation(1.0f); 134 setColorMatrix(&paint, matrix); 135 canvas->drawBitmap(bmps[i], 160, 80, &paint); 136 137 matrix.setSaturation(2.0f); 138 setColorMatrix(&paint, matrix); 139 canvas->drawBitmap(bmps[i], 240, 80, &paint); 140 141 matrix.setRGB2YUV(); 142 setColorMatrix(&paint, matrix); 143 canvas->drawBitmap(bmps[i], 0, 160, &paint); 144 145 matrix.setYUV2RGB(); 146 setColorMatrix(&paint, matrix); 147 canvas->drawBitmap(bmps[i], 80, 160, &paint); 148 149 SkScalar s1 = SK_Scalar1; 150 SkScalar s255 = SkIntToScalar(255); 151 // Move red into alpha, set color to white 152 SkScalar data[20] = { 153 0, 0, 0, 0, s255, 154 0, 0, 0, 0, s255, 155 0, 0, 0, 0, s255, 156 s1, 0, 0, 0, 0, 157 }; 158 159 setArray(&paint, data); 160 canvas->drawBitmap(bmps[i], 160, 160, &paint); 161 162 canvas->translate(0, 240); 163 } 164 } 165 166private: 167 SkBitmap fSolidBitmap; 168 SkBitmap fTransparentBitmap; 169 typedef GM INHERITED; 170}; 171 172////////////////////////////////////////////////////////////////////////////// 173 174static GM* MyFactory(void*) { return new ColorMatrixGM; } 175static GMRegistry reg(MyFactory); 176 177} 178