OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2015 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 "sk_tool_utils.h" |
| 9 #include "SkSurface.h" |
| 10 #include "Resources.h" |
| 11 #include "gm.h" |
| 12 |
| 13 #include "SkMath.h" |
| 14 #include "SkColorPriv.h" |
| 15 |
| 16 static SkBitmap copy_bitmap(const SkBitmap& src, SkColorType colorType) { |
| 17 SkBitmap copy; |
| 18 src.copyTo(©, colorType); |
| 19 copy.setImmutable(); |
| 20 return copy; |
| 21 } |
| 22 |
| 23 #define SCALE 128 |
| 24 |
| 25 // Make either A8 or gray8 bitmap. |
| 26 static SkBitmap make_bitmap(SkColorType ct) { |
| 27 SkBitmap bm; |
| 28 switch (ct) { |
| 29 case kAlpha_8_SkColorType: |
| 30 bm.allocPixels(SkImageInfo::MakeA8(SCALE, SCALE)); |
| 31 break; |
| 32 case kGray_8_SkColorType: |
| 33 bm.allocPixels( |
| 34 SkImageInfo::Make(SCALE, SCALE, ct, kOpaque_SkAlphaType)); |
| 35 break; |
| 36 default: |
| 37 SkASSERT(false); |
| 38 return bm; |
| 39 } |
| 40 SkAutoLockPixels autoLockPixels(bm); |
| 41 uint8_t spectrum[256]; |
| 42 for (int y = 0; y < 256; ++y) { |
| 43 spectrum[y] = y; |
| 44 } |
| 45 for (int y = 0; y < 128; ++y) { |
| 46 // Shift over one byte each scanline. |
| 47 memcpy(bm.getAddr8(0, y), &spectrum[y], 128); |
| 48 } |
| 49 bm.setImmutable(); |
| 50 return bm; |
| 51 } |
| 52 |
| 53 static void draw_center_letter(char c, |
| 54 SkPaint* p, |
| 55 SkColor color, |
| 56 SkScalar x, |
| 57 SkScalar y, |
| 58 SkCanvas* canvas) { |
| 59 SkRect bounds; |
| 60 p->setColor(color); |
| 61 p->measureText(&c, 1, &bounds); |
| 62 canvas->drawText(&c, 1, x - bounds.centerX(), y - bounds.centerY(), *p); |
| 63 } |
| 64 |
| 65 static void color_wheel_native(SkCanvas* canvas) { |
| 66 SkAutoCanvasRestore autoCanvasRestore(canvas, true); |
| 67 canvas->translate(0.5f * SCALE, 0.5f * SCALE); |
| 68 SkPaint p; |
| 69 p.setAntiAlias(false); |
| 70 p.setColor(SK_ColorWHITE); |
| 71 canvas->drawCircle(0.0f, 0.0f, SCALE * 0.5f, p); |
| 72 |
| 73 const double sqrt_3_over_2 = 0.8660254037844387; |
| 74 const SkScalar Z = 0.0f; |
| 75 const SkScalar D = 0.3f * SkIntToScalar(SCALE); |
| 76 const SkScalar X = SkDoubleToScalar(D * sqrt_3_over_2); |
| 77 const SkScalar Y = D * SK_ScalarHalf; |
| 78 sk_tool_utils::set_portable_typeface(&p, NULL, SkTypeface::kBold); |
| 79 p.setTextSize(0.28125f * SCALE); |
| 80 draw_center_letter('K', &p, SK_ColorBLACK, Z, Z, canvas); |
| 81 draw_center_letter('R', &p, SK_ColorRED, Z, D, canvas); |
| 82 draw_center_letter('G', &p, SK_ColorGREEN, -X, -Y, canvas); |
| 83 draw_center_letter('B', &p, SK_ColorBLUE, X, -Y, canvas); |
| 84 draw_center_letter('C', &p, SK_ColorCYAN, Z, -D, canvas); |
| 85 draw_center_letter('M', &p, SK_ColorMAGENTA, X, Y, canvas); |
| 86 draw_center_letter('Y', &p, SK_ColorYELLOW, -X, Y, canvas); |
| 87 } |
| 88 |
| 89 template <typename T> |
| 90 int find(T* array, int N, T item) { |
| 91 for (int i = 0; i < N; ++i) { |
| 92 if (array[i] == item) { |
| 93 return i; |
| 94 } |
| 95 } |
| 96 return -1; |
| 97 } |
| 98 |
| 99 static SkPMColor premultiply_color(SkColor c) { |
| 100 return SkPremultiplyARGBInline(SkColorGetA(c), SkColorGetR(c), |
| 101 SkColorGetG(c), SkColorGetB(c)); |
| 102 } |
| 103 |
| 104 static SkBitmap indexed_bitmap() { |
| 105 SkBitmap n32bitmap; |
| 106 n32bitmap.allocN32Pixels(SCALE, SCALE); |
| 107 n32bitmap.eraseColor(SK_ColorTRANSPARENT); |
| 108 |
| 109 SkCanvas canvas(n32bitmap); |
| 110 color_wheel_native(&canvas); |
| 111 const SkColor colors[] = { |
| 112 SK_ColorTRANSPARENT, |
| 113 SK_ColorWHITE, |
| 114 SK_ColorBLACK, |
| 115 SK_ColorRED, |
| 116 SK_ColorGREEN, |
| 117 SK_ColorBLUE, |
| 118 SK_ColorCYAN, |
| 119 SK_ColorMAGENTA, |
| 120 SK_ColorYELLOW, |
| 121 }; |
| 122 SkPMColor pmColors[SK_ARRAY_COUNT(colors)]; |
| 123 for (size_t i = 0; i < SK_ARRAY_COUNT(colors); ++i) { |
| 124 pmColors[i] = premultiply_color(colors[i]); |
| 125 } |
| 126 SkBitmap bm; |
| 127 SkAutoTUnref<SkColorTable> ctable( |
| 128 SkNEW_ARGS(SkColorTable, (pmColors, SK_ARRAY_COUNT(pmColors)))); |
| 129 SkImageInfo info = SkImageInfo::Make(SCALE, SCALE, kIndex_8_SkColorType, |
| 130 kPremul_SkAlphaType); |
| 131 bm.allocPixels(info, NULL, ctable); |
| 132 SkAutoLockPixels autoLockPixels1(n32bitmap); |
| 133 SkAutoLockPixels autoLockPixels2(bm); |
| 134 for (int y = 0; y < SCALE; ++y) { |
| 135 for (int x = 0; x < SCALE; ++x) { |
| 136 SkPMColor c = *n32bitmap.getAddr32(x, y); |
| 137 int idx = find(pmColors, SK_ARRAY_COUNT(pmColors), c); |
| 138 *bm.getAddr8(x, y) = SkClampMax(idx, SK_ARRAY_COUNT(pmColors) - 1); |
| 139 } |
| 140 } |
| 141 return bm; |
| 142 } |
| 143 |
| 144 static void draw(SkCanvas* canvas, |
| 145 const SkPaint& p, |
| 146 const SkBitmap& src, |
| 147 SkColorType colorType, |
| 148 const char text[]) { |
| 149 SkASSERT(src.colorType() == colorType); |
| 150 canvas->drawBitmap(src, 0.0f, 0.0f); |
| 151 canvas->drawText(text, strlen(text), 0.0f, 12.0f, p); |
| 152 } |
| 153 |
| 154 DEF_SIMPLE_GM(all_bitmap_configs, canvas, SCALE, 6 * SCALE) { |
| 155 SkAutoCanvasRestore autoCanvasRestore(canvas, true); |
| 156 SkPaint p; |
| 157 p.setColor(SK_ColorBLACK); |
| 158 p.setAntiAlias(true); |
| 159 sk_tool_utils::set_portable_typeface(&p, NULL); |
| 160 |
| 161 sk_tool_utils::draw_checkerboard(canvas, SK_ColorLTGRAY, SK_ColorWHITE, 8); |
| 162 |
| 163 SkBitmap bitmap; |
| 164 if (GetResourceAsBitmap("color_wheel.png", &bitmap)) { |
| 165 bitmap.setImmutable(); |
| 166 draw(canvas, p, bitmap, kN32_SkColorType, "Native 32"); |
| 167 |
| 168 canvas->translate(0.0f, SkIntToScalar(SCALE)); |
| 169 SkBitmap copy565 = copy_bitmap(bitmap, kRGB_565_SkColorType); |
| 170 p.setColor(SK_ColorRED); |
| 171 draw(canvas, p, copy565, kRGB_565_SkColorType, "RGB 565"); |
| 172 p.setColor(SK_ColorBLACK); |
| 173 |
| 174 canvas->translate(0.0f, SkIntToScalar(SCALE)); |
| 175 SkBitmap copy4444 = copy_bitmap(bitmap, kARGB_4444_SkColorType); |
| 176 draw(canvas, p, copy4444, kARGB_4444_SkColorType, "ARGB 4444"); |
| 177 } else { |
| 178 canvas->translate(0.0f, SkIntToScalar(2 * SCALE)); |
| 179 } |
| 180 |
| 181 canvas->translate(0.0f, SkIntToScalar(SCALE)); |
| 182 SkBitmap bitmapIndexed = indexed_bitmap(); |
| 183 draw(canvas, p, bitmapIndexed, kIndex_8_SkColorType, "Index 8"); |
| 184 |
| 185 canvas->translate(0.0f, SkIntToScalar(SCALE)); |
| 186 SkBitmap bitmapA8 = make_bitmap(kAlpha_8_SkColorType); |
| 187 draw(canvas, p, bitmapA8, kAlpha_8_SkColorType, "Alpha 8"); |
| 188 |
| 189 p.setColor(SK_ColorRED); |
| 190 canvas->translate(0.0f, SkIntToScalar(SCALE)); |
| 191 SkBitmap bitmapG8 = make_bitmap(kGray_8_SkColorType); |
| 192 draw(canvas, p, bitmapG8, kGray_8_SkColorType, "Gray 8"); |
| 193 } |
OLD | NEW |