Hal Canary | 8751512 | 2019-03-15 14:22:51 -0400 | [diff] [blame] | 1 | // Copyright 2019 Google LLC. |
| 2 | // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. |
| 3 | #include "fiddle/examples.h" |
| 4 | // HASH=e72db006f1bea26feceaef8727ff9818 |
Hal Canary | a7181e7c | 2019-03-18 16:06:34 -0400 | [diff] [blame^] | 5 | REG_FIDDLE(ImageInfo_makeAlphaType, 256, 256, false, 3) { |
Hal Canary | 8751512 | 2019-03-15 14:22:51 -0400 | [diff] [blame] | 6 | void draw(SkCanvas* canvas) { |
| 7 | const int width = 256; |
| 8 | const int height = 128; |
| 9 | SkColor pixels[height][width]; |
| 10 | for (int y = 0; y < height; ++y) { |
| 11 | for (int x = 0; x < width; ++x) { |
| 12 | int red = SkScalarRoundToInt(255 * SkScalarAbs(SkScalarSin((x * 4 + y) * 0.03f))); |
| 13 | int blue = SkScalarRoundToInt(255 * SkScalarAbs(SkScalarCos((x * 3 + y) * 0.04f))); |
| 14 | int green = SkScalarRoundToInt(255 * SkScalarAbs(SkScalarSin((x * 2 + y) * 0.05f))); |
| 15 | int alpha = SkScalarRoundToInt(255 * SkScalarAbs(SkScalarCos((x * 1 + y) * 0.006f))); |
| 16 | pixels[y][x] = |
| 17 | SkColorSetARGB(alpha, red * alpha / 255, green * alpha / 255, blue * alpha / 255); |
| 18 | } |
| 19 | } |
| 20 | SkBitmap bitmap; |
| 21 | SkImageInfo info = SkImageInfo::Make(width, height, kBGRA_8888_SkColorType, kPremul_SkAlphaType); |
| 22 | bitmap.installPixels(info, (void*) pixels, sizeof(SkColor) * width); |
| 23 | canvas->drawBitmap(source, 0, 0); |
| 24 | canvas->drawBitmap(bitmap, 0, 0); |
| 25 | SkImageInfo unpremulInfo = info.makeAlphaType(kUnpremul_SkAlphaType); |
| 26 | bitmap.installPixels(unpremulInfo, (void*) pixels, sizeof(SkColor) * width); |
| 27 | canvas->drawBitmap(bitmap, 0, 128); |
| 28 | } |
| 29 | } // END FIDDLE |