diff options
author | thestig <thestig@chromium.org> | 2016-11-08 16:22:28 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-11-08 16:22:28 -0800 |
commit | 28c888b8ee6d1da93cffaa8c14833ddc4986cc79 (patch) | |
tree | 09ca8cbc09ce58ab57e5b045cabfd84634679aac /core/fpdfapi | |
parent | 2599ff7ae76a2e7514c1794281ccb0802d6df2bc (diff) | |
download | pdfium-28c888b8ee6d1da93cffaa8c14833ddc4986cc79.tar.xz |
Cleanup CPDF_DeviceCS and friends.chromium/2914
Review-Url: https://codereview.chromium.org/2482273002
Diffstat (limited to 'core/fpdfapi')
-rw-r--r-- | core/fpdfapi/page/cpdf_colorspace.cpp | 5 | ||||
-rw-r--r-- | core/fpdfapi/page/fpdf_page_colors.cpp | 206 |
2 files changed, 108 insertions, 103 deletions
diff --git a/core/fpdfapi/page/cpdf_colorspace.cpp b/core/fpdfapi/page/cpdf_colorspace.cpp index 2740169d04..0c023d19ff 100644 --- a/core/fpdfapi/page/cpdf_colorspace.cpp +++ b/core/fpdfapi/page/cpdf_colorspace.cpp @@ -839,9 +839,8 @@ bool CPDF_ICCBasedCS::v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) { if (!m_pProfile) return false; - m_nComponents = - m_pProfile - ->GetComponents(); // Try using the nComponents from ICC profile + // Try using the |nComponents| from ICC profile + m_nComponents = m_pProfile->GetComponents(); CPDF_Dictionary* pDict = pStream->GetDict(); if (!m_pProfile->m_pTransform) { // No valid ICC profile or using sRGB CPDF_Object* pAlterCSObj = diff --git a/core/fpdfapi/page/fpdf_page_colors.cpp b/core/fpdfapi/page/fpdf_page_colors.cpp index 3ee5328dbc..455233abb5 100644 --- a/core/fpdfapi/page/fpdf_page_colors.cpp +++ b/core/fpdfapi/page/fpdf_page_colors.cpp @@ -26,9 +26,14 @@ uint32_t ComponentsForFamily(int family) { return 3; if (family == PDFCS_DEVICEGRAY) return 1; + ASSERT(family == PDFCS_DEVICECMYK); return 4; } +FX_FLOAT NormalizeChannel(FX_FLOAT fVal) { + return std::min(std::max(fVal, 0.0f), 1.0f); +} + } // namespace void sRGB_to_AdobeCMYK(FX_FLOAT R, @@ -69,109 +74,103 @@ void ReverseRGB(uint8_t* pDestBuf, const uint8_t* pSrcBuf, int pixels) { } CPDF_DeviceCS::CPDF_DeviceCS(CPDF_Document* pDoc, int family) - : CPDF_ColorSpace(pDoc, family, ComponentsForFamily(family)) {} + : CPDF_ColorSpace(pDoc, family, ComponentsForFamily(family)) { + ASSERT(family == PDFCS_DEVICEGRAY || family == PDFCS_DEVICERGB || + family == PDFCS_DEVICECMYK); +} bool CPDF_DeviceCS::GetRGB(FX_FLOAT* pBuf, FX_FLOAT& R, FX_FLOAT& G, FX_FLOAT& B) const { - if (m_Family == PDFCS_DEVICERGB) { - R = pBuf[0]; - if (R < 0) { - R = 0; - } else if (R > 1) { - R = 1; - } - G = pBuf[1]; - if (G < 0) { - G = 0; - } else if (G > 1) { - G = 1; - } - B = pBuf[2]; - if (B < 0) { - B = 0; - } else if (B > 1) { - B = 1; - } - } else if (m_Family == PDFCS_DEVICEGRAY) { - R = *pBuf; - if (R < 0) { - R = 0; - } else if (R > 1) { - R = 1; - } - G = B = R; - } else if (m_Family == PDFCS_DEVICECMYK) { - if (!m_dwStdConversion) { - AdobeCMYK_to_sRGB(pBuf[0], pBuf[1], pBuf[2], pBuf[3], R, G, B); - } else { - FX_FLOAT k = pBuf[3]; - R = 1.0f - std::min(1.0f, pBuf[0] + k); - G = 1.0f - std::min(1.0f, pBuf[1] + k); - B = 1.0f - std::min(1.0f, pBuf[2] + k); - } - } else { - ASSERT(m_Family == PDFCS_PATTERN); - R = G = B = 0; - return false; + switch (m_Family) { + case PDFCS_DEVICEGRAY: + R = NormalizeChannel(*pBuf); + G = R; + B = R; + break; + case PDFCS_DEVICERGB: + R = NormalizeChannel(pBuf[0]); + G = NormalizeChannel(pBuf[1]); + B = NormalizeChannel(pBuf[2]); + break; + case PDFCS_DEVICECMYK: + if (m_dwStdConversion) { + FX_FLOAT k = pBuf[3]; + R = 1.0f - std::min(1.0f, pBuf[0] + k); + G = 1.0f - std::min(1.0f, pBuf[1] + k); + B = 1.0f - std::min(1.0f, pBuf[2] + k); + } else { + AdobeCMYK_to_sRGB(pBuf[0], pBuf[1], pBuf[2], pBuf[3], R, G, B); + } + break; + default: + ASSERT(false); + return false; } return true; } + bool CPDF_DeviceCS::v_GetCMYK(FX_FLOAT* pBuf, FX_FLOAT& c, FX_FLOAT& m, FX_FLOAT& y, FX_FLOAT& k) const { - if (m_Family != PDFCS_DEVICECMYK) { + if (m_Family != PDFCS_DEVICECMYK) return false; - } + c = pBuf[0]; m = pBuf[1]; y = pBuf[2]; k = pBuf[3]; return true; } + bool CPDF_DeviceCS::SetRGB(FX_FLOAT* pBuf, FX_FLOAT R, FX_FLOAT G, FX_FLOAT B) const { - if (m_Family == PDFCS_DEVICERGB) { - pBuf[0] = R; - pBuf[1] = G; - pBuf[2] = B; - return true; - } - if (m_Family == PDFCS_DEVICEGRAY) { - if (R == G && R == B) { + switch (m_Family) { + case PDFCS_DEVICEGRAY: + if (R != G || R != B) + return false; *pBuf = R; return true; - } - return false; - } - if (m_Family == PDFCS_DEVICECMYK) { - sRGB_to_AdobeCMYK(R, G, B, pBuf[0], pBuf[1], pBuf[2], pBuf[3]); - return true; + case PDFCS_DEVICERGB: + pBuf[0] = R; + pBuf[1] = G; + pBuf[2] = B; + return true; + case PDFCS_DEVICECMYK: + sRGB_to_AdobeCMYK(R, G, B, pBuf[0], pBuf[1], pBuf[2], pBuf[3]); + return true; + default: + ASSERT(false); + return false; } - return false; } + bool CPDF_DeviceCS::v_SetCMYK(FX_FLOAT* pBuf, FX_FLOAT c, FX_FLOAT m, FX_FLOAT y, FX_FLOAT k) const { - if (m_Family == PDFCS_DEVICERGB) { - AdobeCMYK_to_sRGB(c, m, y, k, pBuf[0], pBuf[1], pBuf[2]); - return true; - } - if (m_Family == PDFCS_DEVICECMYK) { - pBuf[0] = c; - pBuf[1] = m; - pBuf[2] = y; - pBuf[3] = k; - return true; + switch (m_Family) { + case PDFCS_DEVICEGRAY: + return false; + case PDFCS_DEVICERGB: + AdobeCMYK_to_sRGB(c, m, y, k, pBuf[0], pBuf[1], pBuf[2]); + return true; + case PDFCS_DEVICECMYK: + pBuf[0] = c; + pBuf[1] = m; + pBuf[2] = y; + pBuf[3] = k; + return true; + default: + ASSERT(false); + return false; } - return false; } void CPDF_DeviceCS::TranslateImageLine(uint8_t* pDestBuf, @@ -180,39 +179,46 @@ void CPDF_DeviceCS::TranslateImageLine(uint8_t* pDestBuf, int image_width, int image_height, bool bTransMask) const { - if (bTransMask && m_Family == PDFCS_DEVICECMYK) { - for (int i = 0; i < pixels; i++) { - int k = 255 - pSrcBuf[3]; - pDestBuf[0] = ((255 - pSrcBuf[0]) * k) / 255; - pDestBuf[1] = ((255 - pSrcBuf[1]) * k) / 255; - pDestBuf[2] = ((255 - pSrcBuf[2]) * k) / 255; - pDestBuf += 3; - pSrcBuf += 4; - } - return; - } - if (m_Family == PDFCS_DEVICERGB) { - ReverseRGB(pDestBuf, pSrcBuf, pixels); - } else if (m_Family == PDFCS_DEVICEGRAY) { - for (int i = 0; i < pixels; i++) { - *pDestBuf++ = pSrcBuf[i]; - *pDestBuf++ = pSrcBuf[i]; - *pDestBuf++ = pSrcBuf[i]; - } - } else { - for (int i = 0; i < pixels; i++) { - if (!m_dwStdConversion) { - AdobeCMYK_to_sRGB1(pSrcBuf[0], pSrcBuf[1], pSrcBuf[2], pSrcBuf[3], - pDestBuf[2], pDestBuf[1], pDestBuf[0]); + switch (m_Family) { + case PDFCS_DEVICEGRAY: + for (int i = 0; i < pixels; i++) { + *pDestBuf++ = pSrcBuf[i]; + *pDestBuf++ = pSrcBuf[i]; + *pDestBuf++ = pSrcBuf[i]; + } + break; + case PDFCS_DEVICERGB: + ReverseRGB(pDestBuf, pSrcBuf, pixels); + break; + case PDFCS_DEVICECMYK: + if (bTransMask) { + for (int i = 0; i < pixels; i++) { + int k = 255 - pSrcBuf[3]; + pDestBuf[0] = ((255 - pSrcBuf[0]) * k) / 255; + pDestBuf[1] = ((255 - pSrcBuf[1]) * k) / 255; + pDestBuf[2] = ((255 - pSrcBuf[2]) * k) / 255; + pDestBuf += 3; + pSrcBuf += 4; + } } else { - uint8_t k = pSrcBuf[3]; - pDestBuf[2] = 255 - std::min(255, pSrcBuf[0] + k); - pDestBuf[1] = 255 - std::min(255, pSrcBuf[1] + k); - pDestBuf[0] = 255 - std::min(255, pSrcBuf[2] + k); + for (int i = 0; i < pixels; i++) { + if (m_dwStdConversion) { + uint8_t k = pSrcBuf[3]; + pDestBuf[2] = 255 - std::min(255, pSrcBuf[0] + k); + pDestBuf[1] = 255 - std::min(255, pSrcBuf[1] + k); + pDestBuf[0] = 255 - std::min(255, pSrcBuf[2] + k); + } else { + AdobeCMYK_to_sRGB1(pSrcBuf[0], pSrcBuf[1], pSrcBuf[2], pSrcBuf[3], + pDestBuf[2], pDestBuf[1], pDestBuf[0]); + } + pSrcBuf += 4; + pDestBuf += 3; + } } - pSrcBuf += 4; - pDestBuf += 3; - } + break; + default: + ASSERT(false); + break; } } |