• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/Resources.h"
20 #include "tools/ToolUtils.h"
21 
draw_image(SkCanvas * canvas,const char * resource,int x,int y)22 static void draw_image(SkCanvas* canvas, const char* resource, int x, int y) {
23     sk_sp<SkImage> image(GetResourceAsImage(resource));
24     if (image) {
25         canvas->drawImage(image, SkIntToScalar(x), SkIntToScalar(y));
26     } else {
27         SkDebugf("\nCould not decode file '%s'. Did you forget"
28                  " to set the resourcePath?\n", resource);
29     }
30 }
31 
32 /*
33   This GM tests whether the image decoders properly decode each color
34   channel.  Four copies of the same image should appear in the GM, and
35   the letter R should be red, B should be blue, G green, C cyan, M
36   magenta, Y yellow, and K black. In all but the JPEG version of the
37   image, the letters should be on a white disc on a transparent
38   background (rendered as a checkerboard).  The JPEG image has a grey
39   background and compression artifacts.
40  */
41 DEF_SIMPLE_GM(colorwheel, canvas, 256, 256) {
42     ToolUtils::draw_checkerboard(canvas);
43     draw_image(canvas, "images/color_wheel.png", 0, 0);  // top left
44     draw_image(canvas, "images/color_wheel.gif", 128, 0);  // top right
45     draw_image(canvas, "images/color_wheel.webp", 0, 128);  // bottom left
46     draw_image(canvas, "images/color_wheel.jpg", 128, 128);  // bottom right
47 }
48 
49 DEF_SIMPLE_GM(colorwheelnative, canvas, 128, 28) {
50     SkFont font(ToolUtils::create_portable_typeface("sans-serif", SkFontStyle::Bold()), 18);
51     font.setEdging(SkFont::Edging::kAlias);
52 
53     canvas->clear(SK_ColorLTGRAY);
54     canvas->drawString("R", 8.0f,   20.0f, font, SkPaint(SkColors::kRed));
55     canvas->drawString("G", 24.0f,  20.0f, font, SkPaint(SkColors::kGreen));
56     canvas->drawString("B", 40.0f,  20.0f, font, SkPaint(SkColors::kBlue));
57     canvas->drawString("C", 56.0f,  20.0f, font, SkPaint(SkColors::kCyan));
58     canvas->drawString("M", 72.0f,  20.0f, font, SkPaint(SkColors::kMagenta));
59     canvas->drawString("Y", 88.0f,  20.0f, font, SkPaint(SkColors::kYellow));
60     canvas->drawString("K", 104.0f, 20.0f, font, SkPaint(SkColors::kBlack));
61 }
62 
63 /**
64  * This GM tests decoding images with non-default (overridden) alpha types.
65  */
66 DEF_SIMPLE_GM(colorwheel_alphatypes, canvas, 256, 128) {
67     canvas->clear(SK_ColorWHITE);
68 
69     sk_sp<SkData> imgData = GetResourceAsData("images/color_wheel.png");
70 
71     auto pmImg = SkImage::MakeFromEncoded(imgData, kPremul_SkAlphaType);
72     auto upmImg = SkImage::MakeFromEncoded(imgData, kUnpremul_SkAlphaType);
73 
74     SkSamplingOptions linear{SkFilterMode::kLinear};
75 
76     // We draw a tiny (8x8) section of the image that falls right on the edge of transparency,
77     // and blow it up so we can really see the impact of filtering in premul or unpremul.
78     SkRect srcRect = SkRect::MakeXYWH(12, 102, 8, 8);
79     SkRect dstRect = SkRect::MakeLTRB(0, 0, 128, 128);
80 
81     // First, we draw the normal (premul-then-filter) image, which looks good. The yellow image
82     // transitions to the white background like you'd expect.
83     canvas->drawImageRect(pmImg, srcRect, dstRect,
84                           linear, nullptr, SkCanvas::kFast_SrcRectConstraint);
85     // Next, we draw the unpremul (filter-then-premul) image, which looks bad. Filtering in unpremul
86     // causes the implicit black of the transparent pixels to be bleed into the filtered edge,
87     // creating a dark "fringe" at the boundary between the yellow image and white background.
88     canvas->drawImageRect(upmImg, srcRect, dstRect.makeOffset(128, 0),
89                           linear, nullptr, SkCanvas::kFast_SrcRectConstraint);
90 }
91