Move CPDF_ColorSpace::IsValidIccComponents() to IccTransform

Make the IsValidIccComponents() method available at a lower level, so
IccTransform can use it as well.

Change-Id: I8f779d32fb5317347a286309c56359518934834b
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/111691
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_colorspace.cpp b/core/fpdfapi/page/cpdf_colorspace.cpp
index 2a9d2f4..6bc7033 100644
--- a/core/fpdfapi/page/cpdf_colorspace.cpp
+++ b/core/fpdfapi/page/cpdf_colorspace.cpp
@@ -32,6 +32,7 @@
 #include "core/fpdfapi/parser/cpdf_stream.h"
 #include "core/fpdfapi/parser/fpdf_parser_utility.h"
 #include "core/fxcodec/fx_codec.h"
+#include "core/fxcodec/icc/icc_transform.h"
 #include "core/fxcrt/data_vector.h"
 #include "core/fxcrt/fx_2d_size.h"
 #include "core/fxcrt/fx_memory_wrappers.h"
@@ -553,11 +554,6 @@
   }
 }
 
-// static
-bool CPDF_ColorSpace::IsValidIccComponents(int components) {
-  return components == 1 || components == 3 || components == 4;
-}
-
 std::vector<float> CPDF_ColorSpace::CreateBufAndSetDefaultColor() const {
   DCHECK(m_Family != Family::kPattern);
 
@@ -899,8 +895,9 @@
   // with Acrobat and reject bad values.
   RetainPtr<const CPDF_Dictionary> pDict = pStream->GetDict();
   int32_t nDictComponents = pDict ? pDict->GetIntegerFor("N") : 0;
-  if (!IsValidIccComponents(nDictComponents))
+  if (!fxcodec::IccTransform::IsValidIccComponents(nDictComponents)) {
     return 0;
+  }
 
   uint32_t nComponents = static_cast<uint32_t>(nDictComponents);
   m_pProfile = CPDF_DocPageData::FromDocument(pDoc)->GetIccProfile(pStream);
@@ -977,7 +974,7 @@
 
   // |nMaxColors| will not overflow since |nComponents| is limited in size.
   const uint32_t nComponents = CountComponents();
-  DCHECK(IsValidIccComponents(nComponents));
+  DCHECK(fxcodec::IccTransform::IsValidIccComponents(nComponents));
   int nMaxColors = 1;
   for (uint32_t i = 0; i < nComponents; i++)
     nMaxColors *= 52;
@@ -1074,7 +1071,7 @@
 // static
 std::vector<float> CPDF_ICCBasedCS::GetRanges(const CPDF_Dictionary* pDict,
                                               uint32_t nComponents) {
-  DCHECK(IsValidIccComponents(nComponents));
+  DCHECK(fxcodec::IccTransform::IsValidIccComponents(nComponents));
   RetainPtr<const CPDF_Array> pRanges = pDict->GetArrayFor("Range");
   if (pRanges && pRanges->size() >= nComponents * 2)
     return ReadArrayElementsToVector(pRanges.Get(), nComponents * 2);
diff --git a/core/fpdfapi/page/cpdf_colorspace.h b/core/fpdfapi/page/cpdf_colorspace.h
index 39f84ec..127b9f6 100644
--- a/core/fpdfapi/page/cpdf_colorspace.h
+++ b/core/fpdfapi/page/cpdf_colorspace.h
@@ -81,7 +81,6 @@
       uint32_t family_id);
 
   static uint32_t ComponentsForFamily(Family family);
-  static bool IsValidIccComponents(int components);
 
   // Should only be called if this colorspace is not a pattern.
   std::vector<float> CreateBufAndSetDefaultColor() const;
diff --git a/core/fpdfapi/page/cpdf_dib.cpp b/core/fpdfapi/page/cpdf_dib.cpp
index 30f1dcf..c478d37 100644
--- a/core/fpdfapi/page/cpdf_dib.cpp
+++ b/core/fpdfapi/page/cpdf_dib.cpp
@@ -28,6 +28,7 @@
 #include "core/fpdfapi/parser/fpdf_parser_decode.h"
 #include "core/fpdfapi/parser/fpdf_parser_utility.h"
 #include "core/fxcodec/basic/basicmodule.h"
+#include "core/fxcodec/icc/icc_transform.h"
 #include "core/fxcodec/jbig2/jbig2_decoder.h"
 #include "core/fxcodec/jpeg/jpegmodule.h"
 #include "core/fxcodec/jpx/cjpx_decoder.h"
@@ -603,8 +604,8 @@
         break;
       }
       case CPDF_ColorSpace::Family::kICCBased: {
-        if (!CPDF_ColorSpace::IsValidIccComponents(colorspace_comps) ||
-            !CPDF_ColorSpace::IsValidIccComponents(m_nComponents) ||
+        if (!fxcodec::IccTransform::IsValidIccComponents(colorspace_comps) ||
+            !fxcodec::IccTransform::IsValidIccComponents(m_nComponents) ||
             colorspace_comps < m_nComponents) {
           return false;
         }
diff --git a/core/fxcodec/icc/icc_transform.cpp b/core/fxcodec/icc/icc_transform.cpp
index 485988d..be81e89 100644
--- a/core/fxcodec/icc/icc_transform.cpp
+++ b/core/fxcodec/icc/icc_transform.cpp
@@ -67,9 +67,9 @@
   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)
+  if (!IsValidIccComponents(nSrcComponents)) {
     return nullptr;
+  }
 
   int srcFormat;
   bool bLab = false;
@@ -142,4 +142,10 @@
   cmsDoTransform(m_hTransform, pSrc.data(), pDest.data(), pixels);
 }
 
+// static
+bool IccTransform::IsValidIccComponents(int components) {
+  // According to PDF spec, number of components must be 1, 3, or 4.
+  return components == 1 || components == 3 || components == 4;
+}
+
 }  // namespace fxcodec
diff --git a/core/fxcodec/icc/icc_transform.h b/core/fxcodec/icc/icc_transform.h
index 47cda07..53d6adb 100644
--- a/core/fxcodec/icc/icc_transform.h
+++ b/core/fxcodec/icc/icc_transform.h
@@ -38,6 +38,8 @@
   int components() const { return m_nSrcComponents; }
   bool IsNormal() const { return m_bNormal; }
 
+  static bool IsValidIccComponents(int components);
+
  private:
   IccTransform(cmsHTRANSFORM transform,
                int srcComponents,