Debian Codegraph @

gm/imagedither.cpp

raw
1 /*
2 * Copyright 2024 Google LLC
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/SkAlphaType.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkColorType.h"
13 #include "include/core/SkImage.h"
14 #include "include/core/SkImageInfo.h"
15 #include "include/core/SkPoint.h"
16 #include "include/core/SkRefCnt.h"
17 #include "include/core/SkSamplingOptions.h"
18 #include "include/core/SkSurface.h"
19 #include "include/effects/SkGradient.h"
20 #include "include/effects/SkRuntimeEffect.h"
21
22 sk_sp<SkBlender> stretch_colors_blender() {
23 return SkRuntimeEffect::MakeForBlender(SkString(
24 "half4 main(half4 src, half4 dst) { return ((dst.rgb - 0.25) * 16).rgb1; }"))
25 .effect->makeBlender(nullptr);
26 }
27
28 DEF_SIMPLE_GM_CAN_FAIL(image_dither, canvas, errorMsg, 425, 110) {
29 if (!canvas->getSurface()) {
30 // This GM relies on a high-precision (F16) image to determine if image draws are dithered.
31 // Serializing configs will tend to throw away that data (compressing to PNG), so they will
32 // produce different results before/after serialization (and thus fail).
33 *errorMsg = "Not supported in recording mode";
34 return skiagm::DrawResult::kSkip;
35 }
36
37 // First, we make a non-dithered image with a shallow radial gradient. This will be our source:
38 const SkColor4f colors[] = {
39 SkColor4f::FromColor(0xFF555555), SkColor4f::FromColor(0xFF444444) };
40 const SkPoint points[] = {{0, 0}, {100, 100}};
41 sk_sp<SkShader> gradient = SkShaders::LinearGradient(
42 points, {{colors, {}, SkTileMode::kClamp}, {}});
43 SkPaint gradientPaint;
44 gradientPaint.setShader(gradient);
45
46 sk_sp<SkSurface> surface = SkSurfaces::Raster(
47 SkImageInfo::Make(100, 100, kRGBA_F16_SkColorType, kPremul_SkAlphaType));
48 surface->getCanvas()->drawPaint(gradientPaint);
49 sk_sp<SkImage> image = surface->makeImageSnapshot();
50
51 // Now, we draw it three times:
52 // 1) As-is (no dither), to ensure that our source image doesn't have any dithering included
53 // 2) Using an image shader, with dithering enabled on the paint
54 // 3) With drawImage, with dithering enabled on the paint
55 //
56 // We'd like #2 and #3 to both respect the dither flag, for consistency (b/320529640)
57 canvas->translate(5, 5);
58
59 canvas->drawImage(image, 0, 0);
60 canvas->translate(105, 0);
61
62 SkPaint imageShaderPaint;
63 imageShaderPaint.setShader(image->makeShader(SkSamplingOptions{}));
64 imageShaderPaint.setDither(true);
65 canvas->drawRect({0, 0, 100, 100}, imageShaderPaint);
66 canvas->translate(105, 0);
67
68 SkPaint drawImagePaint;
69 drawImagePaint.setDither(true);
70 canvas->drawImage(image, 0, 0, SkSamplingOptions{}, &drawImagePaint);
71 canvas->translate(105, 0);
72
73 // Also draw the actual gradient with the dither flag, to see how it should look:
74 gradientPaint.setDither(true);
75 canvas->drawRect({0, 0, 100, 100}, gradientPaint);
76
77 SkPaint colorStretchPaint;
78 colorStretchPaint.setBlender(stretch_colors_blender());
79 canvas->drawPaint(colorStretchPaint);
80
81 return skiagm::DrawResult::kOk;
82 }