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