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
cfx_dibitmap_unittest.cpp
Go to the documentation of this file.
1// Copyright 2018 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/cfx_dibitmap.h"
6
7#include <stdint.h>
8
9#include "core/fxcrt/fx_coordinates.h"
10#include "core/fxge/dib/fx_dib.h"
11#include "testing/gmock/include/gmock/gmock.h"
12#include "testing/gtest/include/gtest/gtest.h"
13#include "third_party/base/containers/span.h"
14
15namespace {
16
17using ::testing::ElementsAre;
18
19} // namespace
20
21TEST(CFX_DIBitmap, Create) {
22 auto pBitmap = pdfium::MakeRetain<CFX_DIBitmap>();
23 EXPECT_FALSE(pBitmap->Create(400, 300, FXDIB_Format::kInvalid));
24
25 pBitmap = pdfium::MakeRetain<CFX_DIBitmap>();
26 EXPECT_TRUE(pBitmap->Create(400, 300, FXDIB_Format::k1bppRgb));
27}
28
30 // Simple case with no provided pitch.
31 absl::optional<CFX_DIBitmap::PitchAndSize> result =
32 CFX_DIBitmap::CalculatePitchAndSize(100, 200, FXDIB_Format::kArgb, 0);
33 ASSERT_TRUE(result.has_value());
34 EXPECT_EQ(400u, result.value().pitch);
35 EXPECT_EQ(80000u, result.value().size);
36
37 // Simple case with no provided pitch and different format.
38 result =
39 CFX_DIBitmap::CalculatePitchAndSize(100, 200, FXDIB_Format::k8bppRgb, 0);
40 ASSERT_TRUE(result.has_value());
41 EXPECT_EQ(100u, result.value().pitch);
42 EXPECT_EQ(20000u, result.value().size);
43
44 // Simple case with provided pitch matching width * bpp.
45 result =
46 CFX_DIBitmap::CalculatePitchAndSize(100, 200, FXDIB_Format::kArgb, 400);
47 ASSERT_TRUE(result.has_value());
48 EXPECT_EQ(400u, result.value().pitch);
49 EXPECT_EQ(80000u, result.value().size);
50
51 // Simple case with provided pitch, where pitch exceeds width * bpp.
52 result =
53 CFX_DIBitmap::CalculatePitchAndSize(100, 200, FXDIB_Format::kArgb, 455);
54 ASSERT_TRUE(result.has_value());
55 EXPECT_EQ(455u, result.value().pitch);
56 EXPECT_EQ(91000u, result.value().size);
57}
58
60 // Bad width / height.
61 static const CFX_Size kBadDimensions[] = {
62 {0, 0}, {-1, -1}, {-1, 0}, {0, -1},
63 {0, 200}, {100, 0}, {-1, 200}, {100, -1},
64 };
65 for (const auto& dimension : kBadDimensions) {
66 EXPECT_FALSE(CFX_DIBitmap::CalculatePitchAndSize(
67 dimension.width, dimension.height, FXDIB_Format::kArgb, 0));
68 EXPECT_FALSE(CFX_DIBitmap::CalculatePitchAndSize(
69 dimension.width, dimension.height, FXDIB_Format::kArgb, 1));
70 }
71
72 // Bad format.
73 EXPECT_FALSE(
74 CFX_DIBitmap::CalculatePitchAndSize(100, 200, FXDIB_Format::kInvalid, 0));
75 EXPECT_FALSE(CFX_DIBitmap::CalculatePitchAndSize(
76 100, 200, FXDIB_Format::kInvalid, 800));
77
78 // Width too wide for claimed pitch.
79 EXPECT_FALSE(
80 CFX_DIBitmap::CalculatePitchAndSize(101, 200, FXDIB_Format::kArgb, 400));
81
82 // Overflow cases with calculated pitch.
83 EXPECT_FALSE(CFX_DIBitmap::CalculatePitchAndSize(1073747000, 1,
85 EXPECT_FALSE(CFX_DIBitmap::CalculatePitchAndSize(1048576, 1024,
87 EXPECT_FALSE(CFX_DIBitmap::CalculatePitchAndSize(4194304, 1024,
89
90 // Overflow cases with provided pitch.
91 EXPECT_FALSE(CFX_DIBitmap::CalculatePitchAndSize(
92 2147484000u, 1, FXDIB_Format::kArgb, 2147484000u));
93 EXPECT_FALSE(CFX_DIBitmap::CalculatePitchAndSize(
94 1048576, 1024, FXDIB_Format::kArgb, 4194304));
95 EXPECT_FALSE(CFX_DIBitmap::CalculatePitchAndSize(
96 4194304, 1024, FXDIB_Format::k8bppRgb, 4194304));
97}
98
100 // Test boundary condition for pitch overflow.
101 absl::optional<CFX_DIBitmap::PitchAndSize> result =
102 CFX_DIBitmap::CalculatePitchAndSize(536870908, 4, FXDIB_Format::k8bppRgb,
103 0);
104 ASSERT_TRUE(result);
105 EXPECT_EQ(536870908u, result.value().pitch);
106 EXPECT_EQ(2147483632u, result.value().size);
107 EXPECT_FALSE(CFX_DIBitmap::CalculatePitchAndSize(536870909, 4,
109
110 // Test boundary condition for size overflow.
111 result = CFX_DIBitmap::CalculatePitchAndSize(68174084, 63,
112 FXDIB_Format::k8bppRgb, 0);
113 ASSERT_TRUE(result);
114 EXPECT_EQ(68174084u, result.value().pitch);
115 EXPECT_EQ(4294967292u, result.value().size);
116 EXPECT_FALSE(CFX_DIBitmap::CalculatePitchAndSize(68174085, 63,
118}
119
120#if defined(PDF_USE_SKIA)
121TEST(CFX_DIBitmap, UnPreMultiply_FromCleared) {
122 auto bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
123 ASSERT_TRUE(bitmap->Create(1, 1, FXDIB_Format::kArgb));
124 FXARGB_SETDIB(bitmap->GetBuffer().data(), 0x7f'7f'7f'7f);
125
126 bitmap->UnPreMultiply();
127
128 EXPECT_THAT(bitmap->GetBuffer(), ElementsAre(0xff, 0xff, 0xff, 0x7f));
129}
130
131TEST(CFX_DIBitmap, UnPreMultiply_FromPreMultiplied) {
132 auto bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
133 ASSERT_TRUE(bitmap->Create(1, 1, FXDIB_Format::kArgb));
134 bitmap->ForcePreMultiply();
135 FXARGB_SETDIB(bitmap->GetBuffer().data(), 0x7f'7f'7f'7f);
136
137 bitmap->UnPreMultiply();
138
139 EXPECT_THAT(bitmap->GetBuffer(), ElementsAre(0xff, 0xff, 0xff, 0x7f));
140}
141
142TEST(CFX_DIBitmap, UnPreMultiply_FromUnPreMultiplied) {
143 auto bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
144 ASSERT_TRUE(bitmap->Create(1, 1, FXDIB_Format::kArgb));
145 bitmap->UnPreMultiply();
146 FXARGB_SETDIB(bitmap->GetBuffer().data(), 0x7f'ff'ff'ff);
147
148 bitmap->UnPreMultiply();
149
150 EXPECT_THAT(bitmap->GetBuffer(), ElementsAre(0xff, 0xff, 0xff, 0x7f));
151}
152#endif // defined(PDF_USE_SKIA)
TEST(CFX_DIBitmap, Create)
FXDIB_Format
Definition fx_dib.h:19