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 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "core/fpdfapi/page/cpdf_devicecs.h"
8
9 #include "core/fxcrt/retain_ptr.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
TEST(CPDF_DeviceCSTest,GetRGBFromGray)12 TEST(CPDF_DeviceCSTest, GetRGBFromGray) {
13 float R;
14 float G;
15 float B;
16 auto device_gray =
17 pdfium::MakeRetain<CPDF_DeviceCS>(CPDF_ColorSpace::Family::kDeviceGray);
18
19 // Test normal values. For gray, only first value from buf should be used.
20 float buf[3] = {0.43f, 0.11f, 0.34f};
21 ASSERT_TRUE(device_gray->GetRGB(buf, &R, &G, &B));
22 EXPECT_FLOAT_EQ(0.43f, R);
23 EXPECT_FLOAT_EQ(0.43f, G);
24 EXPECT_FLOAT_EQ(0.43f, B);
25 buf[0] = 0.872f;
26 ASSERT_TRUE(device_gray->GetRGB(buf, &R, &G, &B));
27 EXPECT_FLOAT_EQ(0.872f, R);
28 EXPECT_FLOAT_EQ(0.872f, G);
29 EXPECT_FLOAT_EQ(0.872f, B);
30
31 // Test boundary values
32 buf[0] = {0.0f};
33 ASSERT_TRUE(device_gray->GetRGB(buf, &R, &G, &B));
34 EXPECT_FLOAT_EQ(0.0f, R);
35 EXPECT_FLOAT_EQ(0.0f, G);
36 EXPECT_FLOAT_EQ(0.0f, B);
37 buf[0] = 1.0f;
38 ASSERT_TRUE(device_gray->GetRGB(buf, &R, &G, &B));
39 EXPECT_FLOAT_EQ(1.0f, R);
40 EXPECT_FLOAT_EQ(1.0f, G);
41 EXPECT_FLOAT_EQ(1.0f, B);
42
43 // Test out of range values
44 buf[0] = -0.01f;
45 ASSERT_TRUE(device_gray->GetRGB(buf, &R, &G, &B));
46 EXPECT_FLOAT_EQ(0.0f, R);
47 EXPECT_FLOAT_EQ(0.0f, G);
48 EXPECT_FLOAT_EQ(0.0f, B);
49 buf[0] = 12.5f;
50 ASSERT_TRUE(device_gray->GetRGB(buf, &R, &G, &B));
51 EXPECT_FLOAT_EQ(1.0f, R);
52 EXPECT_FLOAT_EQ(1.0f, G);
53 EXPECT_FLOAT_EQ(1.0f, B);
54 }
55
TEST(CPDF_DeviceCSTest,GetRGBFromRGB)56 TEST(CPDF_DeviceCSTest, GetRGBFromRGB) {
57 float R;
58 float G;
59 float B;
60 auto device_rgb =
61 pdfium::MakeRetain<CPDF_DeviceCS>(CPDF_ColorSpace::Family::kDeviceRGB);
62
63 // Test normal values
64 float buf[3] = {0.13f, 1.0f, 0.652f};
65 ASSERT_TRUE(device_rgb->GetRGB(buf, &R, &G, &B));
66 EXPECT_FLOAT_EQ(0.13f, R);
67 EXPECT_FLOAT_EQ(1.0f, G);
68 EXPECT_FLOAT_EQ(0.652f, B);
69 buf[0] = 0.0f;
70 buf[1] = 0.52f;
71 buf[2] = 0.78f;
72 ASSERT_TRUE(device_rgb->GetRGB(buf, &R, &G, &B));
73 EXPECT_FLOAT_EQ(0.0f, R);
74 EXPECT_FLOAT_EQ(0.52f, G);
75 EXPECT_FLOAT_EQ(0.78f, B);
76
77 // Test out of range values
78 buf[0] = -10.5f;
79 buf[1] = 100.0f;
80 ASSERT_TRUE(device_rgb->GetRGB(buf, &R, &G, &B));
81 EXPECT_FLOAT_EQ(0.0f, R);
82 EXPECT_FLOAT_EQ(1.0f, G);
83 EXPECT_FLOAT_EQ(0.78f, B);
84 }
85
TEST(CPDF_DeviceCSTest,GetRGBFromCMYK)86 TEST(CPDF_DeviceCSTest, GetRGBFromCMYK) {
87 float R;
88 float G;
89 float B;
90 auto device_cmyk =
91 pdfium::MakeRetain<CPDF_DeviceCS>(CPDF_ColorSpace::Family::kDeviceCMYK);
92
93 // Test normal values
94 float buf[4] = {0.6f, 0.5f, 0.3f, 0.9f};
95 ASSERT_TRUE(device_cmyk->GetRGB(buf, &R, &G, &B));
96 EXPECT_FLOAT_EQ(0.0627451f, R);
97 EXPECT_FLOAT_EQ(0.0627451f, G);
98 EXPECT_FLOAT_EQ(0.10588236f, B);
99 buf[0] = 0.15f;
100 buf[2] = 0.0f;
101 ASSERT_TRUE(device_cmyk->GetRGB(buf, &R, &G, &B));
102 EXPECT_FLOAT_EQ(0.2f, R);
103 EXPECT_FLOAT_EQ(0.0862745f, G);
104 EXPECT_FLOAT_EQ(0.16470589f, B);
105 buf[2] = 1.0f;
106 buf[3] = 0.0f;
107 ASSERT_TRUE(device_cmyk->GetRGB(buf, &R, &G, &B));
108 EXPECT_FLOAT_EQ(0.85098046f, R);
109 EXPECT_FLOAT_EQ(0.552941f, G);
110 EXPECT_FLOAT_EQ(0.15686275f, B);
111
112 // Test out of range values
113 buf[2] = 1.5f;
114 buf[3] = -0.6f;
115 ASSERT_TRUE(device_cmyk->GetRGB(buf, &R, &G, &B));
116 EXPECT_FLOAT_EQ(0.85098046f, R);
117 EXPECT_FLOAT_EQ(0.552941f, G);
118 EXPECT_FLOAT_EQ(0.15686275f, B);
119 }
120