xref: /aosp_15_r20/external/pdfium/core/fpdfapi/page/cpdf_colorspace_unittest.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2021 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/fpdfapi/page/cpdf_colorspace.h"
6 
7 #include <stdint.h>
8 #include <string.h>
9 
10 #include "core/fxcrt/retain_ptr.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 
TEST(CPDF_CalGray,TranslateImageLine)13 TEST(CPDF_CalGray, TranslateImageLine) {
14   const uint8_t kSrc[12] = {255, 0, 0, 0, 255, 0, 0, 0, 255, 128, 128, 128};
15   const uint8_t kExpect[12] = {255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0};
16 
17   RetainPtr<CPDF_ColorSpace> pCal = CPDF_ColorSpace::AllocateColorSpace("CalG");
18   ASSERT_TRUE(pCal);
19 
20   uint8_t dst[12];
21   memset(dst, 0xbd, sizeof(dst));
22   pCal->TranslateImageLine(dst, kSrc, 4, 4, 1, true);
23   for (size_t i = 0; i < 12; ++i)
24     EXPECT_EQ(dst[i], kExpect[i]) << " at " << i;
25 
26   memset(dst, 0xbd, sizeof(dst));
27   pCal->TranslateImageLine(dst, kSrc, 4, 4, 1, false);
28   for (size_t i = 0; i < 12; ++i)
29     EXPECT_EQ(dst[i], kExpect[i]) << " at " << i;
30 }
31 
TEST(CPDF_CalRGB,TranslateImageLine)32 TEST(CPDF_CalRGB, TranslateImageLine) {
33   const uint8_t kSrc[12] = {255, 0, 0, 0, 255, 0, 0, 0, 255, 128, 128, 128};
34   const uint8_t kExpectMask[12] = {255, 58, 0,   0,   255, 0,
35                                    70,  0,  255, 188, 188, 188};
36   const uint8_t kExpectNomask[12] = {0,   0, 255, 0,   255, 0,
37                                      255, 0, 0,   128, 128, 128};
38 
39   RetainPtr<CPDF_ColorSpace> pCal = CPDF_ColorSpace::AllocateColorSpace("CalR");
40   ASSERT_TRUE(pCal);
41 
42   uint8_t dst[12];
43   memset(dst, 0xbd, sizeof(dst));
44   pCal->TranslateImageLine(dst, kSrc, 4, 4, 1, true);
45   for (size_t i = 0; i < 12; ++i)
46     EXPECT_EQ(dst[i], kExpectMask[i]) << " at " << i;
47 
48   memset(dst, 0xbd, sizeof(dst));
49   pCal->TranslateImageLine(dst, kSrc, 4, 4, 1, false);
50   for (size_t i = 0; i < 12; ++i)
51     EXPECT_EQ(dst[i], kExpectNomask[i]) << " at " << i;
52 }
53