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