Replace IccModule with IccTransform.
The module class, with its static methods, serves no purpose, and the
actions it performs are better served as methods of the LCmsCmm class.
-- Rename from LCmsCmm for clarity.
Change-Id: I7f8ada3c41d43b73f1f59c4280af412e7f98e774
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/84633
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_colorspace.cpp b/core/fpdfapi/page/cpdf_colorspace.cpp
index 53ab29c..170d61e 100644
--- a/core/fpdfapi/page/cpdf_colorspace.cpp
+++ b/core/fpdfapi/page/cpdf_colorspace.cpp
@@ -32,7 +32,7 @@
#include "core/fpdfapi/parser/cpdf_string.h"
#include "core/fpdfapi/parser/fpdf_parser_utility.h"
#include "core/fxcodec/fx_codec.h"
-#include "core/fxcodec/icc/iccmodule.h"
+#include "core/fxcodec/icc/icc_transform.h"
#include "core/fxcrt/fx_safe_types.h"
#include "core/fxcrt/maybe_owned.h"
#include "core/fxcrt/scoped_set_insertion.h"
@@ -986,14 +986,12 @@
}
if (m_pProfile->transform()) {
float rgb[3];
- IccModule::Translate(m_pProfile->transform(), pBuf.first(CountComponents()),
- rgb);
+ m_pProfile->transform()->Translate(pBuf.first(CountComponents()), rgb);
*R = rgb[0];
*G = rgb[1];
*B = rgb[2];
return true;
}
-
if (m_pAlterCS)
return m_pAlterCS->GetRGB(pBuf, R, G, B);
@@ -1041,12 +1039,10 @@
if (nPixelCount.IsValid())
bTranslate = nPixelCount.ValueOrDie() < nMaxColors * 3 / 2;
}
- if (bTranslate) {
- IccModule::TranslateScanline(m_pProfile->transform(), pDestBuf, pSrcBuf,
- pixels);
+ if (bTranslate && m_pProfile->transform()) {
+ m_pProfile->transform()->TranslateScanline(pDestBuf, pSrcBuf, pixels);
return;
}
-
if (m_pCache.empty()) {
m_pCache =
fxcrt::Vector2D<uint8_t, FxAllocAllocator<uint8_t>>(nMaxColors, 3);
@@ -1062,8 +1058,10 @@
order /= 52;
}
}
- IccModule::TranslateScanline(m_pProfile->transform(), m_pCache.data(),
- temp_src.data(), nMaxColors);
+ if (m_pProfile->transform()) {
+ m_pProfile->transform()->TranslateScanline(m_pCache.data(),
+ temp_src.data(), nMaxColors);
+ }
}
for (int i = 0; i < pixels; i++) {
int index = 0;
diff --git a/core/fpdfapi/page/cpdf_iccprofile.cpp b/core/fpdfapi/page/cpdf_iccprofile.cpp
index f8d40e4..d962b6d 100644
--- a/core/fpdfapi/page/cpdf_iccprofile.cpp
+++ b/core/fpdfapi/page/cpdf_iccprofile.cpp
@@ -7,7 +7,7 @@
#include "core/fpdfapi/page/cpdf_iccprofile.h"
#include "core/fpdfapi/parser/cpdf_stream.h"
-#include "core/fxcodec/icc/iccmodule.h"
+#include "core/fxcodec/icc/icc_transform.h"
namespace {
@@ -26,7 +26,7 @@
return;
}
- m_Transform = IccModule::CreateTransformSRGB(span);
+ m_Transform = fxcodec::IccTransform::CreateTransformSRGB(span);
if (m_Transform)
m_nSrcComponents = m_Transform->components();
}
diff --git a/core/fpdfapi/page/cpdf_iccprofile.h b/core/fpdfapi/page/cpdf_iccprofile.h
index a8ee297..aee817e 100644
--- a/core/fpdfapi/page/cpdf_iccprofile.h
+++ b/core/fpdfapi/page/cpdf_iccprofile.h
@@ -16,7 +16,7 @@
class CPDF_Stream;
namespace fxcodec {
-class CLcmsCmm;
+class IccTransform;
} // namespace fxcodec
class CPDF_IccProfile final : public Retainable, public Observable {
@@ -27,7 +27,7 @@
bool IsValid() const { return IsSRGB() || IsSupported(); }
bool IsSRGB() const { return m_bsRGB; }
bool IsSupported() const { return !!m_Transform; }
- fxcodec::CLcmsCmm* transform() { return m_Transform.get(); }
+ fxcodec::IccTransform* transform() { return m_Transform.get(); }
uint32_t GetComponents() const { return m_nSrcComponents; }
private:
@@ -37,7 +37,7 @@
const bool m_bsRGB;
uint32_t m_nSrcComponents = 0;
RetainPtr<const CPDF_Stream> const m_pStream;
- std::unique_ptr<fxcodec::CLcmsCmm> m_Transform;
+ std::unique_ptr<fxcodec::IccTransform> m_Transform;
};
#endif // CORE_FPDFAPI_PAGE_CPDF_ICCPROFILE_H_
diff --git a/core/fxcodec/BUILD.gn b/core/fxcodec/BUILD.gn
index 784dcce..4564510 100644
--- a/core/fxcodec/BUILD.gn
+++ b/core/fxcodec/BUILD.gn
@@ -18,8 +18,8 @@
"fx_codec.cpp",
"fx_codec.h",
"fx_codec_def.h",
- "icc/iccmodule.cpp",
- "icc/iccmodule.h",
+ "icc/icc_transform.cpp",
+ "icc/icc_transform.h",
"jbig2/JBig2_ArithDecoder.cpp",
"jbig2/JBig2_ArithDecoder.h",
"jbig2/JBig2_ArithIntDecoder.cpp",
diff --git a/core/fxcodec/icc/iccmodule.cpp b/core/fxcodec/icc/icc_transform.cpp
similarity index 77%
rename from core/fxcodec/icc/iccmodule.cpp
rename to core/fxcodec/icc/icc_transform.cpp
index 9cf0c4f..b8483ba 100644
--- a/core/fxcodec/icc/iccmodule.cpp
+++ b/core/fxcodec/icc/icc_transform.cpp
@@ -4,7 +4,7 @@
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-#include "core/fxcodec/icc/iccmodule.h"
+#include "core/fxcodec/icc/icc_transform.h"
#include <algorithm>
#include <memory>
@@ -13,6 +13,7 @@
#include "core/fxcrt/fx_memory_wrappers.h"
#include "third_party/base/cxx17_backports.h"
#include "third_party/base/notreached.h"
+#include "third_party/base/ptr_util.h"
namespace fxcodec {
@@ -37,21 +38,21 @@
} // namespace
-CLcmsCmm::CLcmsCmm(cmsHTRANSFORM hTransform,
- int srcComponents,
- bool bIsLab,
- bool bNormal)
+IccTransform::IccTransform(cmsHTRANSFORM hTransform,
+ int srcComponents,
+ bool bIsLab,
+ bool bNormal)
: m_hTransform(hTransform),
m_nSrcComponents(srcComponents),
m_bLab(bIsLab),
m_bNormal(bNormal) {}
-CLcmsCmm::~CLcmsCmm() {
+IccTransform::~IccTransform() {
cmsDeleteTransform(m_hTransform);
}
// static
-std::unique_ptr<CLcmsCmm> IccModule::CreateTransformSRGB(
+std::unique_ptr<IccTransform> IccTransform::CreateTransformSRGB(
pdfium::span<const uint8_t> span) {
ScopedCmsProfile srcProfile(cmsOpenProfileFromMem(span.data(), span.size()));
if (!srcProfile)
@@ -62,8 +63,8 @@
return nullptr;
cmsColorSpaceSignature srcCS = cmsGetColorSpace(srcProfile.get());
-
uint32_t nSrcComponents = cmsChannelsOf(srcCS);
+
// According to PDF spec, number of components must be 1, 3, or 4.
if (nSrcComponents != 1 && nSrcComponents != 3 && nSrcComponents != 4)
return nullptr;
@@ -105,27 +106,24 @@
if (!hTransform)
return nullptr;
- return std::make_unique<CLcmsCmm>(hTransform, nSrcComponents, bLab, bNormal);
+ // Private ctor.
+ return pdfium::WrapUnique(
+ new IccTransform(hTransform, nSrcComponents, bLab, bNormal));
}
-// static
-void IccModule::Translate(CLcmsCmm* pTransform,
- pdfium::span<const float> pSrcValues,
- pdfium::span<float> pDestValues) {
- if (!pTransform)
- return;
-
+void IccTransform::Translate(pdfium::span<const float> pSrcValues,
+ pdfium::span<float> pDestValues) {
uint8_t output[4];
// TODO(npm): Currently the CmsDoTransform method is part of LCMS and it will
// apply some member of m_hTransform to the input. We need to go over all the
// places which set transform to verify that only `pSrcValues.size()`
// components are used.
- if (pTransform->IsLab()) {
+ if (m_bLab) {
std::vector<double, FxAllocAllocator<double>> inputs(
std::max<size_t>(pSrcValues.size(), 16));
for (uint32_t i = 0; i < pSrcValues.size(); ++i)
inputs[i] = pSrcValues[i];
- cmsDoTransform(pTransform->transform(), inputs.data(), output, 1);
+ cmsDoTransform(m_hTransform, inputs.data(), output, 1);
} else {
std::vector<uint8_t, FxAllocAllocator<uint8_t>> inputs(
std::max<size_t>(pSrcValues.size(), 16));
@@ -133,20 +131,17 @@
inputs[i] =
pdfium::clamp(static_cast<int>(pSrcValues[i] * 255.0f), 0, 255);
}
- cmsDoTransform(pTransform->transform(), inputs.data(), output, 1);
+ cmsDoTransform(m_hTransform, inputs.data(), output, 1);
}
pDestValues[0] = output[2] / 255.0f;
pDestValues[1] = output[1] / 255.0f;
pDestValues[2] = output[0] / 255.0f;
}
-// static
-void IccModule::TranslateScanline(CLcmsCmm* pTransform,
- unsigned char* pDest,
- const unsigned char* pSrc,
- int32_t pixels) {
- if (pTransform)
- cmsDoTransform(pTransform->transform(), pSrc, pDest, pixels);
+void IccTransform::TranslateScanline(unsigned char* pDest,
+ const unsigned char* pSrc,
+ int32_t pixels) {
+ cmsDoTransform(m_hTransform, pSrc, pDest, pixels);
}
} // namespace fxcodec
diff --git a/core/fxcodec/icc/icc_transform.h b/core/fxcodec/icc/icc_transform.h
new file mode 100644
index 0000000..9ea0c34
--- /dev/null
+++ b/core/fxcodec/icc/icc_transform.h
@@ -0,0 +1,53 @@
+// Copyright 2016 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
+
+#ifndef CORE_FXCODEC_ICC_ICC_TRANSFORM_H_
+#define CORE_FXCODEC_ICC_ICC_TRANSFORM_H_
+
+#include <stdint.h>
+
+#include <memory>
+
+#include "core/fxcodec/fx_codec_def.h"
+#include "third_party/base/span.h"
+
+#if defined(USE_SYSTEM_LCMS2)
+#include <lcms2.h>
+#else
+#include "third_party/lcms/include/lcms2.h"
+#endif
+
+namespace fxcodec {
+
+class IccTransform {
+ public:
+ static std::unique_ptr<IccTransform> CreateTransformSRGB(
+ pdfium::span<const uint8_t> span);
+
+ ~IccTransform();
+
+ void Translate(pdfium::span<const float> pSrcValues,
+ pdfium::span<float> pDestValues);
+ void TranslateScanline(uint8_t* pDest, const uint8_t* pSrc, int pixels);
+
+ int components() const { return m_nSrcComponents; }
+ bool IsNormal() const { return m_bNormal; }
+
+ private:
+ IccTransform(cmsHTRANSFORM transform,
+ int srcComponents,
+ bool bIsLab,
+ bool bNormal);
+
+ const cmsHTRANSFORM m_hTransform;
+ const int m_nSrcComponents;
+ const bool m_bLab;
+ const bool m_bNormal;
+};
+
+} // namespace fxcodec
+
+#endif // CORE_FXCODEC_ICC_ICC_TRANSFORM_H_
diff --git a/core/fxcodec/icc/iccmodule.h b/core/fxcodec/icc/iccmodule.h
deleted file mode 100644
index 0f2fa7f..0000000
--- a/core/fxcodec/icc/iccmodule.h
+++ /dev/null
@@ -1,67 +0,0 @@
-// Copyright 2016 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
-
-#ifndef CORE_FXCODEC_ICC_ICCMODULE_H_
-#define CORE_FXCODEC_ICC_ICCMODULE_H_
-
-#include <stdint.h>
-
-#include <memory>
-
-#include "core/fxcodec/fx_codec_def.h"
-#include "third_party/base/span.h"
-
-#if defined(USE_SYSTEM_LCMS2)
-#include <lcms2.h>
-#else
-#include "third_party/lcms/include/lcms2.h"
-#endif
-
-namespace fxcodec {
-
-class CLcmsCmm {
- public:
- CLcmsCmm(cmsHTRANSFORM transform,
- int srcComponents,
- bool bIsLab,
- bool bNormal);
- ~CLcmsCmm();
-
- cmsHTRANSFORM transform() const { return m_hTransform; }
- int components() const { return m_nSrcComponents; }
- bool IsLab() const { return m_bLab; }
- bool IsNormal() const { return m_bNormal; }
-
- private:
- const cmsHTRANSFORM m_hTransform;
- const int m_nSrcComponents;
- const bool m_bLab;
- const bool m_bNormal;
-};
-
-class IccModule {
- public:
- static std::unique_ptr<CLcmsCmm> CreateTransformSRGB(
- pdfium::span<const uint8_t> span);
- static void Translate(CLcmsCmm* pTransform,
- pdfium::span<const float> pSrcValues,
- pdfium::span<float> pDestValues);
- static void TranslateScanline(CLcmsCmm* pTransform,
- uint8_t* pDest,
- const uint8_t* pSrc,
- int pixels);
-
- IccModule() = delete;
- IccModule(const IccModule&) = delete;
- IccModule& operator=(const IccModule&) = delete;
-};
-
-} // namespace fxcodec
-
-using CLcmsCmm = fxcodec::CLcmsCmm;
-using IccModule = fxcodec::IccModule;
-
-#endif // CORE_FXCODEC_ICC_ICCMODULE_H_
diff --git a/testing/fuzzers/pdf_codec_icc_fuzzer.cc b/testing/fuzzers/pdf_codec_icc_fuzzer.cc
index 298adbb..7020fe0 100644
--- a/testing/fuzzers/pdf_codec_icc_fuzzer.cc
+++ b/testing/fuzzers/pdf_codec_icc_fuzzer.cc
@@ -4,21 +4,18 @@
#include <cstdint>
-#include "core/fxcodec/icc/iccmodule.h"
+#include "core/fxcodec/icc/icc_transform.h"
#include "third_party/base/span.h"
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
- std::unique_ptr<CLcmsCmm> transform =
- IccModule::CreateTransformSRGB(pdfium::make_span(data, size));
-
+ std::unique_ptr<fxcodec::IccTransform> transform =
+ fxcodec::IccTransform::CreateTransformSRGB(pdfium::make_span(data, size));
if (!transform)
return 0;
const float src[4] = {0.5f, 0.5f, 0.5f, 0.5f};
float dst[4];
- IccModule::Translate(transform.get(),
- pdfium::make_span(src).first(transform->components()),
+ transform->Translate(pdfium::make_span(src).first(transform->components()),
pdfium::make_span(dst));
-
return 0;
}