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