summaryrefslogtreecommitdiffstats
path: root/image/test/gtest/TestDownscalingFilter.cpp
blob: 596becab0be50c84332ce8bca86e1da9f4960dfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "gtest/gtest.h"

#include "mozilla/gfx/2D.h"
#include "Common.h"
#include "Decoder.h"
#include "DecoderFactory.h"
#include "SourceBuffer.h"
#include "SurfaceFilters.h"
#include "SurfacePipe.h"

using namespace mozilla;
using namespace mozilla::gfx;
using namespace mozilla::image;

template <typename Func> void
WithDownscalingFilter(const IntSize& aInputSize,
                      const IntSize& aOutputSize,
                      Func aFunc)
{
  RefPtr<Decoder> decoder = CreateTrivialDecoder();
  ASSERT_TRUE(decoder != nullptr);

  WithFilterPipeline(decoder, Forward<Func>(aFunc),
                     DownscalingConfig { aInputSize,
                                         SurfaceFormat::B8G8R8A8 },
                     SurfaceConfig { decoder, 0, aOutputSize,
                                     SurfaceFormat::B8G8R8A8, false });
}

void
AssertConfiguringDownscalingFilterFails(const IntSize& aInputSize,
                                        const IntSize& aOutputSize)
{
  RefPtr<Decoder> decoder = CreateTrivialDecoder();
  ASSERT_TRUE(decoder != nullptr);

  AssertConfiguringPipelineFails(decoder,
                                 DownscalingConfig { aInputSize,
                                                     SurfaceFormat::B8G8R8A8 },
                                 SurfaceConfig { decoder, 0, aOutputSize,
                                                 SurfaceFormat::B8G8R8A8, false });
}

TEST(ImageDownscalingFilter, WritePixels100_100to99_99)
{
  WithDownscalingFilter(IntSize(100, 100), IntSize(99, 99),
                        [](Decoder* aDecoder, SurfaceFilter* aFilter) {
    CheckWritePixels(aDecoder, aFilter,
                     /* aOutputRect = */ Some(IntRect(0, 0, 99, 99)));
  });
}

TEST(ImageDownscalingFilter, WritePixels100_100to33_33)
{
  WithDownscalingFilter(IntSize(100, 100), IntSize(33, 33),
                        [](Decoder* aDecoder, SurfaceFilter* aFilter) {
    CheckWritePixels(aDecoder, aFilter,
                     /* aOutputRect = */ Some(IntRect(0, 0, 33, 33)));
  });
}

TEST(ImageDownscalingFilter, WritePixels100_100to1_1)
{
  WithDownscalingFilter(IntSize(100, 100), IntSize(1, 1),
                        [](Decoder* aDecoder, SurfaceFilter* aFilter) {
    CheckWritePixels(aDecoder, aFilter,
                     /* aOutputRect = */ Some(IntRect(0, 0, 1, 1)));
  });
}

TEST(ImageDownscalingFilter, WritePixels100_100to33_99)
{
  WithDownscalingFilter(IntSize(100, 100), IntSize(33, 99),
                        [](Decoder* aDecoder, SurfaceFilter* aFilter) {
    CheckWritePixels(aDecoder, aFilter,
                     /* aOutputRect = */ Some(IntRect(0, 0, 33, 99)));
  });
}

TEST(ImageDownscalingFilter, WritePixels100_100to99_33)
{
  WithDownscalingFilter(IntSize(100, 100), IntSize(99, 33),
                        [](Decoder* aDecoder, SurfaceFilter* aFilter) {
    CheckWritePixels(aDecoder, aFilter,
                     /* aOutputRect = */ Some(IntRect(0, 0, 99, 33)));
  });
}

TEST(ImageDownscalingFilter, WritePixels100_100to99_1)
{
  WithDownscalingFilter(IntSize(100, 100), IntSize(99, 1),
                        [](Decoder* aDecoder, SurfaceFilter* aFilter) {
    CheckWritePixels(aDecoder, aFilter,
                     /* aOutputRect = */ Some(IntRect(0, 0, 99, 1)));
  });
}

TEST(ImageDownscalingFilter, WritePixels100_100to1_99)
{
  WithDownscalingFilter(IntSize(100, 100), IntSize(1, 99),
                        [](Decoder* aDecoder, SurfaceFilter* aFilter) {
    CheckWritePixels(aDecoder, aFilter,
                     /* aOutputRect = */ Some(IntRect(0, 0, 1, 99)));
  });
}

TEST(ImageDownscalingFilter, DownscalingFailsFor100_100to101_101)
{
  // Upscaling is disallowed.
  AssertConfiguringDownscalingFilterFails(IntSize(100, 100), IntSize(101, 101));
}

TEST(ImageDownscalingFilter, DownscalingFailsFor100_100to100_100)
{
  // "Scaling" to the same size is disallowed.
  AssertConfiguringDownscalingFilterFails(IntSize(100, 100), IntSize(100, 100));
}

TEST(ImageDownscalingFilter, DownscalingFailsFor0_0toMinus1_Minus1)
{
  // A 0x0 input size is disallowed.
  AssertConfiguringDownscalingFilterFails(IntSize(0, 0), IntSize(-1, -1));
}

TEST(ImageDownscalingFilter, DownscalingFailsForMinus1_Minus1toMinus2_Minus2)
{
  // A negative input size is disallowed.
  AssertConfiguringDownscalingFilterFails(IntSize(-1, -1), IntSize(-2, -2));
}

TEST(ImageDownscalingFilter, DownscalingFailsFor100_100to0_0)
{
  // A 0x0 output size is disallowed.
  AssertConfiguringDownscalingFilterFails(IntSize(100, 100), IntSize(0, 0));
}

TEST(ImageDownscalingFilter, DownscalingFailsFor100_100toMinus1_Minus1)
{
  // A negative output size is disallowed.
  AssertConfiguringDownscalingFilterFails(IntSize(100, 100), IntSize(-1, -1));
}

TEST(ImageDownscalingFilter, WritePixelsOutput100_100to20_20)
{
  WithDownscalingFilter(IntSize(100, 100), IntSize(20, 20),
                        [](Decoder* aDecoder, SurfaceFilter* aFilter) {
    // Fill the image. It consists of 25 lines of green, followed by 25 lines of
    // red, followed by 25 lines of green, followed by 25 more lines of red.
    uint32_t count = 0;
    auto result = aFilter->WritePixels<uint32_t>([&]() -> NextPixel<uint32_t> {
      uint32_t color = (count <= 25 * 100) || (count > 50 * 100 && count <= 75 * 100)
                     ? BGRAColor::Green().AsPixel()
                     : BGRAColor::Red().AsPixel();
      ++count;
      return AsVariant(color);
    });
    EXPECT_EQ(WriteState::FINISHED, result);
    EXPECT_EQ(100u * 100u, count);

    AssertCorrectPipelineFinalState(aFilter,
                                    IntRect(0, 0, 100, 100),
                                    IntRect(0, 0, 20, 20));

    // Check that the generated image is correct. Note that we skip rows near
    // the transitions between colors, since the downscaler does not produce a
    // sharp boundary at these points. Even some of the rows we test need a
    // small amount of fuzz; this is just the nature of Lanczos downscaling.
    RawAccessFrameRef currentFrame = aDecoder->GetCurrentFrameRef();
    RefPtr<SourceSurface> surface = currentFrame->GetSourceSurface();
    EXPECT_TRUE(RowsAreSolidColor(surface, 0, 4, BGRAColor::Green(), /* aFuzz = */ 2));
    EXPECT_TRUE(RowsAreSolidColor(surface, 6, 3, BGRAColor::Red(), /* aFuzz = */ 3));
    EXPECT_TRUE(RowsAreSolidColor(surface, 11, 3, BGRAColor::Green(), /* aFuzz = */ 3));
    EXPECT_TRUE(RowsAreSolidColor(surface, 16, 4, BGRAColor::Red(), /* aFuzz = */ 3));
  });
}

TEST(ImageDownscalingFilter, WritePixelsOutput100_100to10_20)
{
  WithDownscalingFilter(IntSize(100, 100), IntSize(10, 20),
                        [](Decoder* aDecoder, SurfaceFilter* aFilter) {
    // Fill the image. It consists of 25 lines of green, followed by 25 lines of
    // red, followed by 25 lines of green, followed by 25 more lines of red.
    uint32_t count = 0;
    auto result = aFilter->WritePixels<uint32_t>([&]() -> NextPixel<uint32_t> {
      uint32_t color = (count <= 25 * 100) || (count > 50 * 100 && count <= 75 * 100)
                     ? BGRAColor::Green().AsPixel()
                     : BGRAColor::Red().AsPixel();
      ++count;
      return AsVariant(color);
    });
    EXPECT_EQ(WriteState::FINISHED, result);
    EXPECT_EQ(100u * 100u, count);

    AssertCorrectPipelineFinalState(aFilter,
                                    IntRect(0, 0, 100, 100),
                                    IntRect(0, 0, 10, 20));

    // Check that the generated image is correct. Note that we skip rows near
    // the transitions between colors, since the downscaler does not produce a
    // sharp boundary at these points. Even some of the rows we test need a
    // small amount of fuzz; this is just the nature of Lanczos downscaling.
    RawAccessFrameRef currentFrame = aDecoder->GetCurrentFrameRef();
    RefPtr<SourceSurface> surface = currentFrame->GetSourceSurface();
    EXPECT_TRUE(RowsAreSolidColor(surface, 0, 4, BGRAColor::Green(), /* aFuzz = */ 2));
    EXPECT_TRUE(RowsAreSolidColor(surface, 6, 3, BGRAColor::Red(), /* aFuzz = */ 3));
    EXPECT_TRUE(RowsAreSolidColor(surface, 11, 3, BGRAColor::Green(), /* aFuzz = */ 3));
    EXPECT_TRUE(RowsAreSolidColor(surface, 16, 4, BGRAColor::Red(), /* aFuzz = */ 3));
  });
}

TEST(ImageDownscalingFilter, ConfiguringPalettedDownscaleFails)
{
  RefPtr<Decoder> decoder = CreateTrivialDecoder();
  ASSERT_TRUE(decoder != nullptr);

  // DownscalingFilter does not support paletted images, so configuration should
  // fail.
  AssertConfiguringPipelineFails(decoder,
                                 DownscalingConfig { IntSize(100, 100),
                                                     SurfaceFormat::B8G8R8A8 },
                                 PalettedSurfaceConfig { decoder, 0, IntSize(20, 20),
                                                         IntRect(0, 0, 20, 20),
                                                         SurfaceFormat::B8G8R8A8, 8,
                                                         false });
}