1 /*
2 * Copyright 2020 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 "include/core/SkCanvas.h"
9 #include "include/core/SkImage.h"
10 #include "include/core/SkImageInfo.h"
11 #include "include/core/SkRefCnt.h"
12 #include "include/core/SkSamplingOptions.h"
13 #include "include/core/SkSurface.h"
14 #include "include/core/SkTypes.h"
15 #include "src/base/SkRandom.h"
16 #include "src/core/SkSamplingPriv.h"
17 #include "tests/Test.h"
18 #include "tools/DecodeUtils.h"
19 #include "tools/ToolUtils.h"
20
21 #include <initializer_list>
22
23 // In general, sampling under identity matrix should not affect the pixels. However,
24 // cubic resampling when B != 0 is expected to change pixels.
25 //
DEF_TEST(sampling_with_identity_matrix,r)26 DEF_TEST(sampling_with_identity_matrix, r) {
27 const char* names[] = {
28 "images/mandrill_128.png", "images/color_wheel.jpg",
29 };
30
31 SkRandom rand;
32 for (auto name : names) {
33 auto src = ToolUtils::GetResourceAsImage(name);
34 auto surf = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(src->width(), src->height()));
35 auto canvas = surf->getCanvas();
36
37 auto dotest = [&](const SkSamplingOptions& sampling, bool expect_same) {
38 canvas->clear(0);
39 canvas->drawImage(src.get(), 0, 0, sampling, nullptr);
40 auto dst = surf->makeImageSnapshot();
41
42 REPORTER_ASSERT(r, SkSamplingPriv::NoChangeWithIdentityMatrix(sampling) == expect_same);
43 REPORTER_ASSERT(r, ToolUtils::equal_pixels(src.get(), dst.get()) == expect_same);
44 };
45
46 // Exercise all non-cubics -- expecting no changes
47 for (auto m : {SkMipmapMode::kNone, SkMipmapMode::kNearest, SkMipmapMode::kLinear}) {
48 for (auto f : {SkFilterMode::kNearest, SkFilterMode::kLinear}) {
49 dotest(SkSamplingOptions(f, m), true);
50 }
51 }
52
53 // Exercise cubic variants with B zero and non-zero
54 constexpr int N = 30; // try a bunch of random values
55 for (int i = 0; i < N; ++i) {
56 float C = rand.nextF();
57 dotest(SkSamplingOptions({0, C}), true);
58
59 float B = rand.nextF() * 0.9f + 0.05f; // non-zero but still within (0,,,1]
60 SkASSERT(B != 0);
61 dotest(SkSamplingOptions({B, C}), false);
62 }
63 }
64 }
65