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_iccprofile.h"
8
9 #include <utility>
10
11 #include "core/fpdfapi/parser/cpdf_stream_acc.h"
12 #include "core/fxcodec/icc/icc_transform.h"
13
14 namespace {
15
DetectSRGB(pdfium::span<const uint8_t> span)16 bool DetectSRGB(pdfium::span<const uint8_t> span) {
17 static const char kSRGB[] = "sRGB IEC61966-2.1";
18 return span.size() == 3144 && memcmp(&span[400], kSRGB, strlen(kSRGB)) == 0;
19 }
20
21 } // namespace
22
CPDF_IccProfile(RetainPtr<const CPDF_StreamAcc> stream_acc,uint32_t expected_components)23 CPDF_IccProfile::CPDF_IccProfile(RetainPtr<const CPDF_StreamAcc> stream_acc,
24 uint32_t expected_components)
25 : m_pStreamAcc(std::move(stream_acc)),
26 m_bsRGB(expected_components == 3 && DetectSRGB(m_pStreamAcc->GetSpan())) {
27 if (m_bsRGB) {
28 m_nSrcComponents = 3;
29 return;
30 }
31
32 auto transform =
33 fxcodec::IccTransform::CreateTransformSRGB(m_pStreamAcc->GetSpan());
34 if (!transform) {
35 return;
36 }
37
38 uint32_t components = transform->components();
39 if (components != expected_components) {
40 return;
41 }
42
43 m_nSrcComponents = components;
44 m_Transform = std::move(transform);
45 }
46
47 CPDF_IccProfile::~CPDF_IccProfile() = default;
48
IsNormal() const49 bool CPDF_IccProfile::IsNormal() const {
50 return m_Transform->IsNormal();
51 }
52
Translate(pdfium::span<const float> pSrcValues,pdfium::span<float> pDestValues)53 void CPDF_IccProfile::Translate(pdfium::span<const float> pSrcValues,
54 pdfium::span<float> pDestValues) {
55 m_Transform->Translate(pSrcValues, pDestValues);
56 }
57
TranslateScanline(pdfium::span<uint8_t> pDest,pdfium::span<const uint8_t> pSrc,int pixels)58 void CPDF_IccProfile::TranslateScanline(pdfium::span<uint8_t> pDest,
59 pdfium::span<const uint8_t> pSrc,
60 int pixels) {
61 m_Transform->TranslateScanline(pDest, pSrc, pixels);
62 }
63