Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cstretchengine_unittest.cpp
Go to the documentation of this file.
1// Copyright 2017 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "core/fxge/dib/cstretchengine.h"
6
7#include <utility>
8
9#include "core/fpdfapi/page/cpdf_dib.h"
10#include "core/fpdfapi/parser/cpdf_dictionary.h"
11#include "core/fpdfapi/parser/cpdf_number.h"
12#include "core/fpdfapi/parser/cpdf_stream.h"
13#include "core/fxge/dib/fx_dib.h"
14#include "testing/gtest/include/gtest/gtest.h"
15
16namespace {
17
18// Discovered experimentally
19constexpr uint32_t kTooBigSrcLen = 20;
20constexpr uint32_t kTooBigDestLen = 32 * 1024 * 1024 + 1;
21
22uint32_t PixelWeightSum(const CStretchEngine::PixelWeight* weights) {
23 uint32_t sum = 0;
24 for (int i = weights->m_SrcStart; i <= weights->m_SrcEnd; ++i) {
25 sum += weights->GetWeightForPosition(i);
26 }
27 return sum;
28}
29
30void ExecuteOneStretchTest(int32_t dest_width,
31 int32_t src_width,
32 const FXDIB_ResampleOptions& options) {
33 constexpr uint32_t kExpectedSum = CStretchEngine::kFixedPointOne;
35 ASSERT_TRUE(table.CalculateWeights(dest_width, 0, dest_width, src_width, 0,
36 src_width, options));
37 for (int32_t i = 0; i < dest_width; ++i) {
38 EXPECT_EQ(kExpectedSum, PixelWeightSum(table.GetPixelWeight(i)))
39 << "for { " << src_width << ", " << dest_width << " } at " << i;
40 }
41}
42
43void ExecuteOneReversedStretchTest(int32_t dest_width,
44 int32_t src_width,
45 const FXDIB_ResampleOptions& options) {
46 constexpr uint32_t kExpectedSum = CStretchEngine::kFixedPointOne;
48 ASSERT_TRUE(table.CalculateWeights(-dest_width, 0, dest_width, src_width, 0,
49 src_width, options));
50 for (int32_t i = 0; i < dest_width; ++i) {
51 EXPECT_EQ(kExpectedSum, PixelWeightSum(table.GetPixelWeight(i)))
52 << "for { " << src_width << ", " << dest_width << " } at " << i
53 << " (reversed)";
54 }
55}
56
57void ExecuteStretchTests(const FXDIB_ResampleOptions& options) {
58 // Can't test everything, few random values chosen.
59 constexpr int32_t kDestWidths[] = {1, 2, 337, 512, 808, 2550};
60 constexpr int32_t kSrcWidths[] = {1, 2, 187, 256, 809, 1110};
61 for (int32_t src_width : kSrcWidths) {
62 for (int32_t dest_width : kDestWidths) {
63 ExecuteOneStretchTest(dest_width, src_width, options);
64 ExecuteOneReversedStretchTest(dest_width, src_width, options);
65 }
66 }
67}
68
69} // namespace
70
72 FX_RECT clip_rect;
73 RetainPtr<CPDF_Dictionary> dict_obj = pdfium::MakeRetain<CPDF_Dictionary>();
74 dict_obj->SetNewFor<CPDF_Number>("Width", 71000);
75 dict_obj->SetNewFor<CPDF_Number>("Height", 12500);
76 RetainPtr<CPDF_Stream> stream =
77 pdfium::MakeRetain<CPDF_Stream>(std::move(dict_obj));
78 auto dib_source = pdfium::MakeRetain<CPDF_DIB>(nullptr, stream);
79 dib_source->Load();
80 CStretchEngine engine(nullptr, FXDIB_Format::k8bppRgb, 500, 500, clip_rect,
81 dib_source, FXDIB_ResampleOptions());
85 EXPECT_FALSE(engine.GetResampleOptionsForTest().bLossy);
86}
87
90 ExecuteStretchTests(options);
91}
92
95 options.bNoSmoothing = true;
96 ExecuteStretchTests(options);
97}
98
100 FXDIB_ResampleOptions options;
101 options.bInterpolateBilinear = true;
102 ExecuteStretchTests(options);
103}
104
106 FXDIB_ResampleOptions options;
107 options.bNoSmoothing = true;
108 options.bInterpolateBilinear = true;
109 ExecuteStretchTests(options);
110}
111
113 FXDIB_ResampleOptions options;
115 ASSERT_TRUE(table.CalculateWeights(100, 0, 100, 0, 0, 0, options));
116}
117
119 FXDIB_ResampleOptions options;
120 options.bNoSmoothing = true;
122 ASSERT_TRUE(table.CalculateWeights(100, 0, 100, 0, 0, 0, options));
123}
124
126 FXDIB_ResampleOptions options;
127 options.bInterpolateBilinear = true;
129 ASSERT_TRUE(table.CalculateWeights(100, 0, 100, 0, 0, 0, options));
130}
131
133 FXDIB_ResampleOptions options;
134 options.bNoSmoothing = true;
135 options.bInterpolateBilinear = true;
137 ASSERT_TRUE(table.CalculateWeights(100, 0, 100, 0, 0, 0, options));
138}
139
141 FXDIB_ResampleOptions options;
143 ASSERT_TRUE(table.CalculateWeights(0, 0, 0, 100, 0, 100, options));
144}
145
147 FXDIB_ResampleOptions options;
149 ASSERT_FALSE(table.CalculateWeights(kTooBigDestLen, 0, kTooBigDestLen,
150 kTooBigSrcLen, 0, kTooBigSrcLen,
151 options));
152}
PixelWeight * GetPixelWeight(int pixel)
bool CalculateWeights(int dest_len, int dest_min, int dest_max, int src_len, int src_min, int src_max, const FXDIB_ResampleOptions &options)
static constexpr uint32_t kFixedPointOne
const FXDIB_ResampleOptions & GetResampleOptionsForTest() const
TEST(CStretchEngine, OverflowInCtor)
FXDIB_Format
Definition fx_dib.h:19
uint32_t GetWeightForPosition(int position) const
bool bInterpolateBilinear
Definition fx_dib.h:41