Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
colorwheel.cpp
Go to the documentation of this file.
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/gm.h"
11#include "include/core/SkFont.h"
19#include "tools/DecodeUtils.h"
20#include "tools/GpuToolUtils.h"
21#include "tools/Resources.h"
22#include "tools/ToolUtils.h"
24
25static void draw_image(SkCanvas* canvas, const char* resource, int x, int y) {
27 if (image) {
29 } else {
30 SkDebugf("\nCould not decode file '%s'. Did you forget"
31 " to set the resourcePath?\n", resource);
32 }
33}
34
35/*
36 This GM tests whether the image decoders properly decode each color
37 channel. Four copies of the same image should appear in the GM, and
38 the letter R should be red, B should be blue, G green, C cyan, M
39 magenta, Y yellow, and K black. In all but the JPEG version of the
40 image, the letters should be on a white disc on a transparent
41 background (rendered as a checkerboard). The JPEG image has a grey
42 background and compression artifacts.
43 */
44DEF_SIMPLE_GM(colorwheel, canvas, 256, 256) {
46 draw_image(canvas, "images/color_wheel.png", 0, 0); // top left
47 draw_image(canvas, "images/color_wheel.gif", 128, 0); // top right
48 draw_image(canvas, "images/color_wheel.webp", 0, 128); // bottom left
49 draw_image(canvas, "images/color_wheel.jpg", 128, 128); // bottom right
50}
51
52DEF_SIMPLE_GM(colorwheelnative, canvas, 128, 28) {
54 font.setEdging(SkFont::Edging::kAlias);
55
56 canvas->clear(SK_ColorLTGRAY);
57 canvas->drawString("R", 8.0f, 20.0f, font, SkPaint(SkColors::kRed));
58 canvas->drawString("G", 24.0f, 20.0f, font, SkPaint(SkColors::kGreen));
59 canvas->drawString("B", 40.0f, 20.0f, font, SkPaint(SkColors::kBlue));
60 canvas->drawString("C", 56.0f, 20.0f, font, SkPaint(SkColors::kCyan));
61 canvas->drawString("M", 72.0f, 20.0f, font, SkPaint(SkColors::kMagenta));
62 canvas->drawString("Y", 88.0f, 20.0f, font, SkPaint(SkColors::kYellow));
63 canvas->drawString("K", 104.0f, 20.0f, font, SkPaint(SkColors::kBlack));
64}
65
66/**
67 * This GM tests decoding images with non-default (overridden) alpha types.
68 */
69DEF_SIMPLE_GM(colorwheel_alphatypes, canvas, 256, 128) {
70 canvas->clear(SK_ColorWHITE);
71
72 sk_sp<SkData> imgData = GetResourceAsData("images/color_wheel.png");
73
74 auto pmImg = ToolUtils::MakeTextureImage(
76 auto upmImg = ToolUtils::MakeTextureImage(
78
80
81 // We draw a tiny (8x8) section of the image that falls right on the edge of transparency,
82 // and blow it up so we can really see the impact of filtering in premul or unpremul.
83 SkRect srcRect = SkRect::MakeXYWH(12, 102, 8, 8);
84 SkRect dstRect = SkRect::MakeLTRB(0, 0, 128, 128);
85
86 // First, we draw the normal (premul-then-filter) image, which looks good. The yellow image
87 // transitions to the white background like you'd expect.
88 canvas->drawImageRect(pmImg, srcRect, dstRect,
90 // Next, we draw the unpremul (filter-then-premul) image, which looks bad. Filtering in unpremul
91 // causes the implicit black of the transparent pixels to be bleed into the filtered edge,
92 // creating a dark "fringe" at the boundary between the yellow image and white background.
93 canvas->drawImageRect(upmImg, srcRect, dstRect.makeOffset(128, 0),
95}
kUnpremul_SkAlphaType
sk_sp< SkData > GetResourceAsData(const char *resource)
Definition Resources.cpp:42
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
constexpr SkColor SK_ColorLTGRAY
Definition SkColor.h:118
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
#define SkIntToScalar(x)
Definition SkScalar.h:57
@ kFast_SrcRectConstraint
sample outside bounds; faster
Definition SkCanvas.h:1543
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition SkCanvas.h:1528
static constexpr SkFontStyle Bold()
Definition SkFontStyle.h:69
@ kAlias
no transparent pixels on glyph edges
static void draw_image(SkCanvas *canvas, const char *resource, int x, int y)
sk_sp< SkImage > image
Definition examples.cpp:29
#define DEF_SIMPLE_GM(NAME, CANVAS, W, H)
Definition gm.h:50
double y
double x
constexpr SkColor4f kMagenta
Definition SkColor.h:445
constexpr SkColor4f kGreen
Definition SkColor.h:441
constexpr SkColor4f kRed
Definition SkColor.h:440
constexpr SkColor4f kCyan
Definition SkColor.h:444
constexpr SkColor4f kBlack
Definition SkColor.h:435
constexpr SkColor4f kBlue
Definition SkColor.h:442
constexpr SkColor4f kYellow
Definition SkColor.h:443
SK_API sk_sp< SkImage > DeferredFromEncodedData(sk_sp< SkData > encoded, std::optional< SkAlphaType > alphaType=std::nullopt)
void draw_checkerboard(SkCanvas *canvas, SkColor c1, SkColor c2, int size)
sk_sp< SkImage > MakeTextureImage(SkCanvas *canvas, sk_sp< SkImage > orig)
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition DecodeUtils.h:25
sk_sp< SkTypeface > CreatePortableTypeface(const char *name, SkFontStyle style)
constexpr SkRect makeOffset(float dx, float dy) const
Definition SkRect.h:965
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition SkRect.h:646
static sk_sp< SkShader > linear(sk_sp< SkShader > shader)