OLD | NEW |
---|---|
(Empty) | |
1 /* | |
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 "SkColorProfileFilter.h" | |
10 #include "SkBitmapSource.h" | |
11 #include "SkData.h" | |
12 #include "SkGradientShader.h" | |
13 | |
14 namespace skiagm { | |
15 | |
16 static SkShader* MakeLinear() { | |
17 static const SkPoint pts[2] = { | |
18 { 0, 0 }, | |
19 { SkIntToScalar(80), SkIntToScalar(80) } | |
20 }; | |
21 static const SkColor colors[] = { SK_ColorYELLOW, SK_ColorBLUE }; | |
22 return SkGradientShader::CreateLinear( | |
23 pts, colors, NULL, 2, SkShader::kRepeat_TileMode, 0, &SkMatrix::I()); | |
24 } | |
25 | |
26 class ColorProfileGM : public GM { | |
27 public: | |
28 ColorProfileGM() | |
29 : fInitialized(false) | |
30 , f3DLut4(NULL) | |
31 , f3DLut8(NULL) | |
32 , f3DLut16(NULL) | |
33 , f3DLut32(NULL) | |
34 , f3DLut64(NULL) | |
35 { | |
36 this->setBGColor(0xFF000000); | |
37 } | |
38 | |
39 ~ColorProfileGM() { | |
40 SkSafeUnref(f3DLut4); | |
41 SkSafeUnref(f3DLut8); | |
42 SkSafeUnref(f3DLut16); | |
43 SkSafeUnref(f3DLut32); | |
44 SkSafeUnref(f3DLut64); | |
45 } | |
46 | |
47 protected: | |
48 virtual SkString onShortName() { | |
49 return SkString("colorprofile"); | |
50 } | |
51 | |
52 void make_3Dluts() { | |
53 make_3Dlut(&f3DLut4, 4, true, false, false); | |
54 make_3Dlut(&f3DLut8, 8, false, true, false); | |
55 make_3Dlut(&f3DLut16, 16, false, true, true); | |
56 make_3Dlut(&f3DLut32, 32, true, true, false); | |
57 make_3Dlut(&f3DLut64, 64, true, false, true); | |
58 } | |
59 | |
60 void make_bitmap() { | |
61 fBitmap.allocN32Pixels(80, 80); | |
62 SkCanvas canvas(fBitmap); | |
63 canvas.clear(0x00000000); | |
64 SkPaint paint; | |
65 paint.setAntiAlias(true); | |
66 SkShader* shader = MakeLinear(); | |
67 paint.setShader(shader); | |
68 SkRect r = { 0, 0, SkIntToScalar(80), SkIntToScalar(80) }; | |
69 canvas.drawRect(r, paint); | |
70 shader->unref(); | |
71 } | |
72 | |
73 void make_3Dlut(SkData** data, int size, bool invR, bool invG, bool invB) { | |
74 *data = SkData::NewUninitialized(sizeof(SkColor) * size * size * size); | |
75 SkColor* pixels = (SkColor*)((*data)->writable_data()); | |
76 SkAutoMalloc lutMemory(size); | |
77 SkAutoMalloc invLutMemory(size); | |
78 uint8_t* lut = (uint8_t*)lutMemory.get(); | |
79 uint8_t* invLut = (uint8_t*)invLutMemory.get(); | |
80 const int maxIndex = size - 1; | |
81 for (int i = 0; i < size; i++) { | |
82 lut[i] = (i * 255) / maxIndex; | |
83 invLut[i] = ((maxIndex - i) * 255) / maxIndex; | |
84 } | |
85 for (int r = 0; r < size; ++r) { | |
86 for (int g = 0; g < size; ++g) { | |
87 for (int b = 0; b < size; ++b) { | |
88 pixels[(size * ((size * b) + g)) + r] = SkColorSetARGB(0xFF, | |
89 invR ? invLut[r] : lut[r], | |
90 invG ? invLut[g] : lut[g], | |
91 invB ? invLut[b] : lut[b]); | |
92 } | |
93 } | |
94 } | |
95 } | |
96 | |
97 virtual SkISize onISize() { | |
98 return SkISize::Make(500, 100); | |
99 } | |
100 | |
101 void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) { | |
102 canvas->save(); | |
103 canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); | |
104 canvas->clipRect(SkRect::MakeWH(SkIntToScalar(fBitmap.width()), | |
105 SkIntToScalar(fBitmap.height()))); | |
106 canvas->drawBitmap(fBitmap, 0, 0, &paint); | |
107 canvas->restore(); | |
108 } | |
109 | |
110 virtual void onDraw(SkCanvas* canvas) { | |
111 if (!fInitialized) { | |
112 this->make_bitmap(); | |
113 this->make_3Dluts(); | |
114 fInitialized = true; | |
115 } | |
116 canvas->clear(0x00000000); | |
117 SkPaint paint; | |
118 paint.setColorFilter(SkColorProfileFilter::Create(f3DLut4, 4))->unref(); | |
119 drawClippedBitmap(canvas, 10, 10, paint); | |
Stephen White
2014/10/06 16:40:40
Not sure if drawClippedBitmap() is necessary here
sugoi1
2014/10/06 17:18:41
Done.
| |
120 | |
121 paint.setColorFilter(SkColorProfileFilter::Create(f3DLut8, 8))->unref(); | |
122 drawClippedBitmap(canvas, 110, 10, paint); | |
123 | |
124 paint.setColorFilter(SkColorProfileFilter::Create(f3DLut16, 16))->unref( ); | |
125 drawClippedBitmap(canvas, 210, 10, paint); | |
126 | |
127 paint.setColorFilter(SkColorProfileFilter::Create(f3DLut32, 32))->unref( ); | |
128 drawClippedBitmap(canvas, 310, 10, paint); | |
129 | |
130 paint.setColorFilter(SkColorProfileFilter::Create(f3DLut64, 64))->unref( ); | |
131 drawClippedBitmap(canvas, 410, 10, paint); | |
132 } | |
133 | |
134 private: | |
135 typedef GM INHERITED; | |
136 bool fInitialized; | |
137 SkBitmap fBitmap; | |
138 SkData* f3DLut4; | |
139 SkData* f3DLut8; | |
140 SkData* f3DLut16; | |
141 SkData* f3DLut32; | |
142 SkData* f3DLut64; | |
143 }; | |
144 | |
145 ////////////////////////////////////////////////////////////////////////////// | |
146 | |
147 static GM* MyFactory(void*) { return new ColorProfileGM; } | |
148 static GMRegistry reg(MyFactory); | |
149 | |
150 } | |
OLD | NEW |