summaryrefslogtreecommitdiff
path: root/core/fpdfapi/page/cpdf_devicecs_unittest.cpp
blob: 742f4a0f15cf3e08a2b3a3307230b9a4e6ea64da (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
// Copyright 2017 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#include "core/fpdfapi/page/pageint.h"
#include "testing/gtest/include/gtest/gtest.h"

TEST(CPDF_DeviceCSTest, GetRGBFromGray) {
  float R;
  float G;
  float B;
  CPDF_DeviceCS deviceGray(nullptr, PDFCS_DEVICEGRAY);

  // Test normal values. For gray, only first value from buf should be used.
  float buf[3] = {0.43f, 0.11f, 0.34f};
  ASSERT_TRUE(deviceGray.GetRGB(buf, &R, &G, &B));
  EXPECT_FLOAT_EQ(0.43f, R);
  EXPECT_FLOAT_EQ(0.43f, G);
  EXPECT_FLOAT_EQ(0.43f, B);
  buf[0] = 0.872f;
  ASSERT_TRUE(deviceGray.GetRGB(buf, &R, &G, &B));
  EXPECT_FLOAT_EQ(0.872f, R);
  EXPECT_FLOAT_EQ(0.872f, G);
  EXPECT_FLOAT_EQ(0.872f, B);

  // Test boundary values
  buf[0] = {0.0f};
  ASSERT_TRUE(deviceGray.GetRGB(buf, &R, &G, &B));
  EXPECT_FLOAT_EQ(0.0f, R);
  EXPECT_FLOAT_EQ(0.0f, G);
  EXPECT_FLOAT_EQ(0.0f, B);
  buf[0] = 1.0f;
  ASSERT_TRUE(deviceGray.GetRGB(buf, &R, &G, &B));
  EXPECT_FLOAT_EQ(1.0f, R);
  EXPECT_FLOAT_EQ(1.0f, G);
  EXPECT_FLOAT_EQ(1.0f, B);

  // Test out of range values
  buf[0] = -0.01f;
  ASSERT_TRUE(deviceGray.GetRGB(buf, &R, &G, &B));
  EXPECT_FLOAT_EQ(0.0f, R);
  EXPECT_FLOAT_EQ(0.0f, G);
  EXPECT_FLOAT_EQ(0.0f, B);
  buf[0] = 12.5f;
  ASSERT_TRUE(deviceGray.GetRGB(buf, &R, &G, &B));
  EXPECT_FLOAT_EQ(1.0f, R);
  EXPECT_FLOAT_EQ(1.0f, G);
  EXPECT_FLOAT_EQ(1.0f, B);
}

TEST(CPDF_DeviceCSTest, GetRGBFromRGB) {
  float R;
  float G;
  float B;
  CPDF_DeviceCS deviceRGB(nullptr, PDFCS_DEVICERGB);

  // Test normal values
  float buf[3] = {0.13f, 1.0f, 0.652f};
  ASSERT_TRUE(deviceRGB.GetRGB(buf, &R, &G, &B));
  EXPECT_FLOAT_EQ(0.13f, R);
  EXPECT_FLOAT_EQ(1.0f, G);
  EXPECT_FLOAT_EQ(0.652f, B);
  buf[0] = 0.0f;
  buf[1] = 0.52f;
  buf[2] = 0.78f;
  ASSERT_TRUE(deviceRGB.GetRGB(buf, &R, &G, &B));
  EXPECT_FLOAT_EQ(0.0f, R);
  EXPECT_FLOAT_EQ(0.52f, G);
  EXPECT_FLOAT_EQ(0.78f, B);

  // Test out of range values
  buf[0] = -10.5f;
  buf[1] = 100.0f;
  ASSERT_TRUE(deviceRGB.GetRGB(buf, &R, &G, &B));
  EXPECT_FLOAT_EQ(0.0f, R);
  EXPECT_FLOAT_EQ(1.0f, G);
  EXPECT_FLOAT_EQ(0.78f, B);
}

TEST(CPDF_DeviceCSTest, GetRGBFromCMYK) {
  float R;
  float G;
  float B;
  CPDF_DeviceCS deviceCMYK(nullptr, PDFCS_DEVICECMYK);

  // Test normal values
  float buf[4] = {0.6f, 0.5f, 0.3f, 0.9f};
  ASSERT_TRUE(deviceCMYK.GetRGB(buf, &R, &G, &B));
  EXPECT_FLOAT_EQ(0.0627451f, R);
  EXPECT_FLOAT_EQ(0.0627451f, G);
  EXPECT_FLOAT_EQ(0.10588236f, B);
  buf[0] = 0.15f;
  buf[2] = 0.0f;
  ASSERT_TRUE(deviceCMYK.GetRGB(buf, &R, &G, &B));
  EXPECT_FLOAT_EQ(0.2f, R);
  EXPECT_FLOAT_EQ(0.0862745f, G);
  EXPECT_FLOAT_EQ(0.16470589f, B);
  buf[2] = 1.0f;
  buf[3] = 0.0f;
  ASSERT_TRUE(deviceCMYK.GetRGB(buf, &R, &G, &B));
  EXPECT_FLOAT_EQ(0.85098046f, R);
  EXPECT_FLOAT_EQ(0.552941f, G);
  EXPECT_FLOAT_EQ(0.15686275f, B);

  // Test out of range values
  buf[2] = 1.5f;
  buf[3] = -0.6f;
  ASSERT_TRUE(deviceCMYK.GetRGB(buf, &R, &G, &B));
  EXPECT_FLOAT_EQ(0.85098046f, R);
  EXPECT_FLOAT_EQ(0.552941f, G);
  EXPECT_FLOAT_EQ(0.15686275f, B);
}