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"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkFont.h"
12 #include "include/core/SkFontStyle.h"
13 #include "include/core/SkImage.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkTypeface.h"
18 #include "include/core/SkTypes.h"
19 #include "tools/DecodeUtils.h"
20 #include "tools/GpuToolUtils.h"
21 #include "tools/Resources.h"
22 #include "tools/ToolUtils.h"
23 #include "tools/fonts/FontToolUtils.h"
24
draw_image(SkCanvas * canvas,const char * resource,int x,int y)25 static void draw_image(SkCanvas* canvas, const char* resource, int x, int y) {
26 sk_sp<SkImage> image(ToolUtils::GetResourceAsImage(resource));
27 if (image) {
28 canvas->drawImage(image, SkIntToScalar(x), SkIntToScalar(y));
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 */
44 DEF_SIMPLE_GM(colorwheel, canvas, 256, 256) {
45 ToolUtils::draw_checkerboard(canvas);
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
52 DEF_SIMPLE_GM(colorwheelnative, canvas, 128, 28) {
53 SkFont font(ToolUtils::CreatePortableTypeface("sans-serif", SkFontStyle::Bold()), 18);
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 */
69 DEF_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(
75 canvas, SkImages::DeferredFromEncodedData(imgData, kPremul_SkAlphaType));
76 auto upmImg = ToolUtils::MakeTextureImage(
77 canvas, SkImages::DeferredFromEncodedData(imgData, kUnpremul_SkAlphaType));
78
79 SkSamplingOptions linear{SkFilterMode::kLinear};
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,
89 linear, nullptr, SkCanvas::kFast_SrcRectConstraint);
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),
94 linear, nullptr, SkCanvas::kFast_SrcRectConstraint);
95 }
96