blob: 519f88a6a5e4cfedf2a10b5def6862dbf633e55f [file] [log] [blame]
sugoice686272014-10-09 05:27:23 -07001/*
2 * Copyright 2014 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 "SkColorCubeFilter.h"
sugoice686272014-10-09 05:27:23 -070010#include "SkData.h"
11#include "SkGradientShader.h"
12
13namespace skiagm {
14
15static SkShader* MakeLinear() {
16 static const SkPoint pts[2] = {
17 { 0, 0 },
18 { SkIntToScalar(80), SkIntToScalar(80) }
19 };
20 static const SkColor colors[] = { SK_ColorYELLOW, SK_ColorBLUE };
21 return SkGradientShader::CreateLinear(
halcanary96fcdcc2015-08-27 07:41:13 -070022 pts, colors, nullptr, 2, SkShader::kRepeat_TileMode, 0, &SkMatrix::I());
sugoice686272014-10-09 05:27:23 -070023}
24
25class ColorCubeGM : public GM {
26public:
27 ColorCubeGM()
28 : fInitialized(false)
halcanary96fcdcc2015-08-27 07:41:13 -070029 , f3DLut4(nullptr)
30 , f3DLut8(nullptr)
31 , f3DLut16(nullptr)
32 , f3DLut32(nullptr)
33 , f3DLut64(nullptr)
sugoice686272014-10-09 05:27:23 -070034 {
35 this->setBGColor(0xFF000000);
36 }
37
38 ~ColorCubeGM() {
39 SkSafeUnref(f3DLut4);
40 SkSafeUnref(f3DLut8);
41 SkSafeUnref(f3DLut16);
42 SkSafeUnref(f3DLut32);
43 SkSafeUnref(f3DLut64);
44 }
45
46protected:
47 virtual SkString onShortName() {
48 return SkString("colorcube");
49 }
50
51 void make_3Dluts() {
52 make_3Dlut(&f3DLut4, 4, true, false, false);
53 make_3Dlut(&f3DLut8, 8, false, true, false);
54 make_3Dlut(&f3DLut16, 16, false, true, true);
55 make_3Dlut(&f3DLut32, 32, true, true, false);
56 make_3Dlut(&f3DLut64, 64, true, false, true);
57 }
58
59 void make_bitmap() {
60 fBitmap.allocN32Pixels(80, 80);
61 SkCanvas canvas(fBitmap);
62 canvas.clear(0x00000000);
63 SkPaint paint;
64 paint.setAntiAlias(true);
65 SkShader* shader = MakeLinear();
66 paint.setShader(shader);
67 SkRect r = { 0, 0, SkIntToScalar(80), SkIntToScalar(80) };
68 canvas.drawRect(r, paint);
69 shader->unref();
70 }
71
72 void make_3Dlut(SkData** data, int size, bool invR, bool invG, bool invB) {
73 *data = SkData::NewUninitialized(sizeof(SkColor) * size * size * size);
74 SkColor* pixels = (SkColor*)((*data)->writable_data());
75 SkAutoMalloc lutMemory(size);
76 SkAutoMalloc invLutMemory(size);
77 uint8_t* lut = (uint8_t*)lutMemory.get();
78 uint8_t* invLut = (uint8_t*)invLutMemory.get();
79 const int maxIndex = size - 1;
80 for (int i = 0; i < size; i++) {
81 lut[i] = (i * 255) / maxIndex;
82 invLut[i] = ((maxIndex - i) * 255) / maxIndex;
83 }
84 for (int r = 0; r < size; ++r) {
85 for (int g = 0; g < size; ++g) {
86 for (int b = 0; b < size; ++b) {
caryclark12596012015-07-29 05:27:47 -070087 pixels[(size * ((size * b) + g)) + r] = sk_tool_utils::color_to_565(
88 SkColorSetARGB(0xFF,
sugoice686272014-10-09 05:27:23 -070089 invR ? invLut[r] : lut[r],
90 invG ? invLut[g] : lut[g],
caryclark12596012015-07-29 05:27:47 -070091 invB ? invLut[b] : lut[b]));
sugoice686272014-10-09 05:27:23 -070092 }
93 }
94 }
95 }
96
97 virtual SkISize onISize() {
98 return SkISize::Make(500, 100);
99 }
100
101 virtual void onDraw(SkCanvas* canvas) {
102 if (!fInitialized) {
103 this->make_bitmap();
104 this->make_3Dluts();
105 fInitialized = true;
106 }
107 canvas->clear(0x00000000);
108 SkPaint paint;
109 paint.setColorFilter(SkColorCubeFilter::Create(f3DLut4, 4))->unref();
110 canvas->drawBitmap(fBitmap, 10, 10, &paint);
111
112 paint.setColorFilter(SkColorCubeFilter::Create(f3DLut8, 8))->unref();
113 canvas->drawBitmap(fBitmap, 110, 10, &paint);
114
115 paint.setColorFilter(SkColorCubeFilter::Create(f3DLut16, 16))->unref();
116 canvas->drawBitmap(fBitmap, 210, 10, &paint);
117
118 paint.setColorFilter(SkColorCubeFilter::Create(f3DLut32, 32))->unref();
119 canvas->drawBitmap(fBitmap, 310, 10, &paint);
120
121 paint.setColorFilter(SkColorCubeFilter::Create(f3DLut64, 64))->unref();
122 canvas->drawBitmap(fBitmap, 410, 10, &paint);
123 }
124
125private:
126 typedef GM INHERITED;
127 bool fInitialized;
128 SkBitmap fBitmap;
129 SkData* f3DLut4;
130 SkData* f3DLut8;
131 SkData* f3DLut16;
132 SkData* f3DLut32;
133 SkData* f3DLut64;
134};
135
136//////////////////////////////////////////////////////////////////////////////
137
138static GM* MyFactory(void*) { return new ColorCubeGM; }
139static GMRegistry reg(MyFactory);
140
141}