Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Color_Type_RGBA_F16.cpp
Go to the documentation of this file.
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.
4// HASH=dd81527bbdf5eaae7dd21ac04ab84f9e
5REG_FIDDLE(Color_Type_RGBA_F16, 256, 96, false, 0) {
6union FloatUIntUnion {
7 uint32_t fUInt;
8 float fFloat;
9};
10uint16_t FloatToHalf(float f) {
11 static const FloatUIntUnion magic = { 15 << 23 };
12 static const uint32_t round_mask = ~0xfffu;
13 FloatUIntUnion floatUnion;
14 floatUnion.fFloat = f;
15 uint32_t sign = floatUnion.fUInt & 0x80000000u;
16 floatUnion.fUInt ^= sign;
17 floatUnion.fUInt &= round_mask;
18 floatUnion.fFloat *= magic.fFloat;
19 floatUnion.fUInt -= round_mask;
20 return (floatUnion.fUInt >> 13) | (sign >> 16);
21}
22
23void draw(SkCanvas* canvas) {
24 canvas->scale(16, 16);
27 bitmap.allocPixels(imageInfo);
28 SkCanvas offscreen(bitmap);
29 offscreen.clear(SK_ColorGREEN);
30 canvas->drawImage(bitmap.asImage(), 0, 0);
31 auto H = [](float c) -> uint16_t {
32 return FloatToHalf(c);
33 };
34 // R G B A
35 uint16_t red_f16[][4] = { { H(1.0), H(0.0), H(0.0), H(1.0) },
36 { H(.75), H(0.0), H(0.0), H(1.0) },
37 { H(.50), H(0.0), H(0.0), H(1.0) },
38 { H(.25), H(0.0), H(0.0), H(1.0) } };
39 uint16_t blue_f16[][4] = { { H(0.0), H(0.0), H(1.0), H(1.0) },
40 { H(0.0), H(0.0), H(.75), H(1.0) },
41 { H(0.0), H(0.0), H(.50), H(1.0) },
42 { H(0.0), H(0.0), H(.25), H(1.0) } };
43 SkPixmap redPixmap(imageInfo, red_f16, imageInfo.minRowBytes());
44 if (bitmap.writePixels(redPixmap, 0, 0)) {
45 canvas->drawImage(bitmap.asImage(), 2, 2);
46 }
47 SkPixmap bluePixmap(imageInfo, blue_f16, imageInfo.minRowBytes());
48 if (bitmap.writePixels(bluePixmap, 0, 0)) {
49 canvas->drawImage(bitmap.asImage(), 4, 4);
50 }
51}
52} // END FIDDLE
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
@ kRGBA_F16_SkColorType
pixel with half floats for red, green, blue, alpha;
Definition SkColorType.h:38
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
static int sign(SkScalar x)
Definition SkPath.cpp:2141
static void draw(SkCanvas *canvas, SkRect &target, int x, int y)
Definition aaclip.cpp:27
void clear(SkColor color)
Definition SkCanvas.h:1199
void scale(SkScalar sx, SkScalar sy)
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition SkCanvas.h:1528
#define H
#define REG_FIDDLE(NAME, W, H, TEXT, I)
Definition examples.h:60
Definition SkMD5.cpp:130
size_t minRowBytes() const
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)