Flutter Engine
The Flutter Engine
SamplingTest.cpp
Go to the documentation of this file.
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
15#include "src/base/SkRandom.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//
26DEF_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) {
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
43 REPORTER_ASSERT(r, ToolUtils::equal_pixels(src.get(), dst.get()) == expect_same);
44 };
45
46 // Exercise all non-cubics -- expecting no changes
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}
DEF_TEST(sampling_with_identity_matrix, r)
#define SkASSERT(cond)
Definition: SkAssert.h:116
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
#define N
Definition: beziers.cpp:19
float nextF()
Definition: SkRandom.h:55
static bool NoChangeWithIdentityMatrix(const SkSamplingOptions &sampling)
#define B
SkSamplingOptions sampling
Definition: SkRecords.h:337
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition: DecodeUtils.h:25
bool equal_pixels(const SkPixmap &a, const SkPixmap &b)
Definition: ToolUtils.cpp:456
static const char *const names[]
Definition: symbols.cc:24
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
dst
Definition: cp.py:12
SkSamplingOptions(SkFilterMode::kLinear))
static SkImageInfo MakeN32Premul(int width, int height)