diff options
Diffstat (limited to 'core/fpdfapi/fpdf_page')
34 files changed, 2756 insertions, 1638 deletions
diff --git a/core/fpdfapi/fpdf_page/cpdf_allstates.cpp b/core/fpdfapi/fpdf_page/cpdf_allstates.cpp index 96ccc77f81..5d02381d2e 100644 --- a/core/fpdfapi/fpdf_page/cpdf_allstates.cpp +++ b/core/fpdfapi/fpdf_page/cpdf_allstates.cpp @@ -8,6 +8,7 @@ #include "core/fpdfapi/fpdf_page/pageint.h" #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" +#include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" namespace { diff --git a/core/fpdfapi/fpdf_page/cpdf_color.cpp b/core/fpdfapi/fpdf_page/cpdf_color.cpp new file mode 100644 index 0000000000..d174660d4a --- /dev/null +++ b/core/fpdfapi/fpdf_page/cpdf_color.cpp @@ -0,0 +1,180 @@ +// 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 + +#include "core/fpdfapi/fpdf_page/include/cpdf_color.h" + +#include "core/fpdfapi/fpdf_page/pageint.h" +#include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" +#include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" +#include "core/fxcrt/include/fx_system.h" + +CPDF_Color::CPDF_Color(int family) { + m_pCS = CPDF_ColorSpace::GetStockCS(family); + int nComps = 3; + if (family == PDFCS_DEVICEGRAY) + nComps = 1; + else if (family == PDFCS_DEVICECMYK) + nComps = 4; + + m_pBuffer = FX_Alloc(FX_FLOAT, nComps); + for (int i = 0; i < nComps; i++) + m_pBuffer[i] = 0; +} + +CPDF_Color::~CPDF_Color() { + ReleaseBuffer(); + ReleaseColorSpace(); +} + +void CPDF_Color::ReleaseBuffer() { + if (!m_pBuffer) + return; + + if (m_pCS->GetFamily() == PDFCS_PATTERN) { + PatternValue* pvalue = (PatternValue*)m_pBuffer; + CPDF_Pattern* pPattern = + pvalue->m_pCountedPattern ? pvalue->m_pCountedPattern->get() : nullptr; + if (pPattern && pPattern->m_pDocument) { + CPDF_DocPageData* pPageData = pPattern->m_pDocument->GetPageData(); + if (pPageData) + pPageData->ReleasePattern(pPattern->m_pPatternObj); + } + } + FX_Free(m_pBuffer); + m_pBuffer = nullptr; +} + +void CPDF_Color::ReleaseColorSpace() { + if (m_pCS && m_pCS->m_pDocument && m_pCS->GetArray()) { + m_pCS->m_pDocument->GetPageData()->ReleaseColorSpace(m_pCS->GetArray()); + m_pCS = nullptr; + } +} + +void CPDF_Color::SetColorSpace(CPDF_ColorSpace* pCS) { + if (m_pCS == pCS) { + if (!m_pBuffer) + m_pBuffer = pCS->CreateBuf(); + + ReleaseColorSpace(); + m_pCS = pCS; + return; + } + ReleaseBuffer(); + ReleaseColorSpace(); + + m_pCS = pCS; + if (m_pCS) { + m_pBuffer = pCS->CreateBuf(); + pCS->GetDefaultColor(m_pBuffer); + } +} + +void CPDF_Color::SetValue(FX_FLOAT* comps) { + if (!m_pBuffer) + return; + if (m_pCS->GetFamily() != PDFCS_PATTERN) + FXSYS_memcpy(m_pBuffer, comps, m_pCS->CountComponents() * sizeof(FX_FLOAT)); +} + +void CPDF_Color::SetValue(CPDF_Pattern* pPattern, FX_FLOAT* comps, int ncomps) { + if (ncomps > MAX_PATTERN_COLORCOMPS) + return; + + if (!m_pCS || m_pCS->GetFamily() != PDFCS_PATTERN) { + FX_Free(m_pBuffer); + m_pCS = CPDF_ColorSpace::GetStockCS(PDFCS_PATTERN); + m_pBuffer = m_pCS->CreateBuf(); + } + + CPDF_DocPageData* pDocPageData = nullptr; + PatternValue* pvalue = (PatternValue*)m_pBuffer; + if (pvalue->m_pPattern && pvalue->m_pPattern->m_pDocument) { + pDocPageData = pvalue->m_pPattern->m_pDocument->GetPageData(); + if (pDocPageData) + pDocPageData->ReleasePattern(pvalue->m_pPattern->m_pPatternObj); + } + pvalue->m_nComps = ncomps; + pvalue->m_pPattern = pPattern; + if (ncomps) + FXSYS_memcpy(pvalue->m_Comps, comps, ncomps * sizeof(FX_FLOAT)); + + pvalue->m_pCountedPattern = nullptr; + if (pPattern && pPattern->m_pDocument) { + if (!pDocPageData) + pDocPageData = pPattern->m_pDocument->GetPageData(); + + pvalue->m_pCountedPattern = + pDocPageData->FindPatternPtr(pPattern->m_pPatternObj); + } +} + +void CPDF_Color::Copy(const CPDF_Color* pSrc) { + ReleaseBuffer(); + ReleaseColorSpace(); + + m_pCS = pSrc->m_pCS; + if (m_pCS && m_pCS->m_pDocument) { + CPDF_Array* pArray = m_pCS->GetArray(); + if (pArray) + m_pCS = m_pCS->m_pDocument->GetPageData()->GetCopiedColorSpace(pArray); + } + if (!m_pCS) + return; + + m_pBuffer = m_pCS->CreateBuf(); + FXSYS_memcpy(m_pBuffer, pSrc->m_pBuffer, m_pCS->GetBufSize()); + if (m_pCS->GetFamily() == PDFCS_PATTERN) { + PatternValue* pvalue = (PatternValue*)m_pBuffer; + if (pvalue->m_pPattern && pvalue->m_pPattern->m_pDocument) { + pvalue->m_pPattern = + pvalue->m_pPattern->m_pDocument->GetPageData()->GetPattern( + pvalue->m_pPattern->m_pPatternObj, FALSE, + &pvalue->m_pPattern->m_ParentMatrix); + } + } +} + +FX_BOOL CPDF_Color::GetRGB(int& R, int& G, int& B) const { + if (!m_pCS || !m_pBuffer) + return FALSE; + + FX_FLOAT r = 0.0f, g = 0.0f, b = 0.0f; + if (!m_pCS->GetRGB(m_pBuffer, r, g, b)) + return FALSE; + + R = (int32_t)(r * 255 + 0.5f); + G = (int32_t)(g * 255 + 0.5f); + B = (int32_t)(b * 255 + 0.5f); + return TRUE; +} + +CPDF_Pattern* CPDF_Color::GetPattern() const { + if (!m_pBuffer || m_pCS->GetFamily() != PDFCS_PATTERN) + return nullptr; + + PatternValue* pvalue = (PatternValue*)m_pBuffer; + return pvalue->m_pPattern; +} + +CPDF_ColorSpace* CPDF_Color::GetPatternCS() const { + if (!m_pBuffer || m_pCS->GetFamily() != PDFCS_PATTERN) + return nullptr; + return m_pCS->GetBaseCS(); +} + +FX_FLOAT* CPDF_Color::GetPatternColor() const { + if (!m_pBuffer || m_pCS->GetFamily() != PDFCS_PATTERN) + return nullptr; + + PatternValue* pvalue = (PatternValue*)m_pBuffer; + return pvalue->m_nComps ? pvalue->m_Comps : nullptr; +} + +FX_BOOL CPDF_Color::IsEqual(const CPDF_Color& other) const { + return m_pCS && m_pCS == other.m_pCS && + FXSYS_memcmp(m_pBuffer, other.m_pBuffer, m_pCS->GetBufSize()) == 0; +} diff --git a/core/fpdfapi/fpdf_page/cpdf_colorspace.cpp b/core/fpdfapi/fpdf_page/cpdf_colorspace.cpp new file mode 100644 index 0000000000..b1b464471d --- /dev/null +++ b/core/fpdfapi/fpdf_page/cpdf_colorspace.cpp @@ -0,0 +1,1253 @@ +// 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 + +#include "core/fpdfapi/fpdf_page/include/cpdf_colorspace.h" + +#include "core/fpdfapi/fpdf_page/pageint.h" +#include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" +#include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" +#include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" +#include "core/fpdfapi/fpdf_parser/include/cpdf_object.h" +#include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h" +#include "core/fpdfapi/fpdf_parser/include/cpdf_stream_acc.h" +#include "core/fpdfapi/fpdf_parser/include/cpdf_string.h" +#include "core/fpdfapi/include/cpdf_modulemgr.h" +#include "core/include/fxcodec/fx_codec.h" + +namespace { + +const uint8_t g_sRGBSamples1[] = { + 0, 3, 6, 10, 13, 15, 18, 20, 22, 23, 25, 27, 28, 30, 31, + 32, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, + 48, 49, 49, 50, 51, 52, 53, 53, 54, 55, 56, 56, 57, 58, 58, + 59, 60, 61, 61, 62, 62, 63, 64, 64, 65, 66, 66, 67, 67, 68, + 68, 69, 70, 70, 71, 71, 72, 72, 73, 73, 74, 74, 75, 76, 76, + 77, 77, 78, 78, 79, 79, 79, 80, 80, 81, 81, 82, 82, 83, 83, + 84, 84, 85, 85, 85, 86, 86, 87, 87, 88, 88, 88, 89, 89, 90, + 90, 91, 91, 91, 92, 92, 93, 93, 93, 94, 94, 95, 95, 95, 96, + 96, 97, 97, 97, 98, 98, 98, 99, 99, 99, 100, 100, 101, 101, 101, + 102, 102, 102, 103, 103, 103, 104, 104, 104, 105, 105, 106, 106, 106, 107, + 107, 107, 108, 108, 108, 109, 109, 109, 110, 110, 110, 110, 111, 111, 111, + 112, 112, 112, 113, 113, 113, 114, 114, 114, 115, 115, 115, 115, 116, 116, + 116, 117, 117, 117, 118, 118, 118, 118, 119, 119, 119, 120, +}; + +const uint8_t g_sRGBSamples2[] = { + 120, 121, 122, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, + 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 148, 149, + 150, 151, 152, 153, 154, 155, 155, 156, 157, 158, 159, 159, 160, 161, 162, + 163, 163, 164, 165, 166, 167, 167, 168, 169, 170, 170, 171, 172, 173, 173, + 174, 175, 175, 176, 177, 178, 178, 179, 180, 180, 181, 182, 182, 183, 184, + 185, 185, 186, 187, 187, 188, 189, 189, 190, 190, 191, 192, 192, 193, 194, + 194, 195, 196, 196, 197, 197, 198, 199, 199, 200, 200, 201, 202, 202, 203, + 203, 204, 205, 205, 206, 206, 207, 208, 208, 209, 209, 210, 210, 211, 212, + 212, 213, 213, 214, 214, 215, 215, 216, 216, 217, 218, 218, 219, 219, 220, + 220, 221, 221, 222, 222, 223, 223, 224, 224, 225, 226, 226, 227, 227, 228, + 228, 229, 229, 230, 230, 231, 231, 232, 232, 233, 233, 234, 234, 235, 235, + 236, 236, 237, 237, 238, 238, 238, 239, 239, 240, 240, 241, 241, 242, 242, + 243, 243, 244, 244, 245, 245, 246, 246, 246, 247, 247, 248, 248, 249, 249, + 250, 250, 251, 251, 251, 252, 252, 253, 253, 254, 254, 255, 255, +}; + +class CPDF_CalGray : public CPDF_ColorSpace { + public: + explicit CPDF_CalGray(CPDF_Document* pDoc); + + FX_BOOL v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) override; + + FX_BOOL GetRGB(FX_FLOAT* pBuf, + FX_FLOAT& R, + FX_FLOAT& G, + FX_FLOAT& B) const override; + FX_BOOL SetRGB(FX_FLOAT* pBuf, + FX_FLOAT R, + FX_FLOAT G, + FX_FLOAT B) const override; + + void TranslateImageLine(uint8_t* pDestBuf, + const uint8_t* pSrcBuf, + int pixels, + int image_width, + int image_height, + FX_BOOL bTransMask = FALSE) const override; + + private: + FX_FLOAT m_WhitePoint[3]; + FX_FLOAT m_BlackPoint[3]; + FX_FLOAT m_Gamma; +}; + +class CPDF_CalRGB : public CPDF_ColorSpace { + public: + explicit CPDF_CalRGB(CPDF_Document* pDoc); + + FX_BOOL v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) override; + + FX_BOOL GetRGB(FX_FLOAT* pBuf, + FX_FLOAT& R, + FX_FLOAT& G, + FX_FLOAT& B) const override; + FX_BOOL SetRGB(FX_FLOAT* pBuf, + FX_FLOAT R, + FX_FLOAT G, + FX_FLOAT B) const override; + + void TranslateImageLine(uint8_t* pDestBuf, + const uint8_t* pSrcBuf, + int pixels, + int image_width, + int image_height, + FX_BOOL bTransMask = FALSE) const override; + + FX_FLOAT m_WhitePoint[3]; + FX_FLOAT m_BlackPoint[3]; + FX_FLOAT m_Gamma[3]; + FX_FLOAT m_Matrix[9]; + FX_BOOL m_bGamma; + FX_BOOL m_bMatrix; +}; + +class CPDF_LabCS : public CPDF_ColorSpace { + public: + explicit CPDF_LabCS(CPDF_Document* pDoc); + + FX_BOOL v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) override; + + void GetDefaultValue(int iComponent, + FX_FLOAT& value, + FX_FLOAT& min, + FX_FLOAT& max) const override; + FX_BOOL GetRGB(FX_FLOAT* pBuf, + FX_FLOAT& R, + FX_FLOAT& G, + FX_FLOAT& B) const override; + FX_BOOL SetRGB(FX_FLOAT* pBuf, + FX_FLOAT R, + FX_FLOAT G, + FX_FLOAT B) const override; + + void TranslateImageLine(uint8_t* pDestBuf, + const uint8_t* pSrcBuf, + int pixels, + int image_width, + int image_height, + FX_BOOL bTransMask = FALSE) const override; + + FX_FLOAT m_WhitePoint[3]; + FX_FLOAT m_BlackPoint[3]; + FX_FLOAT m_Ranges[4]; +}; + +class CPDF_ICCBasedCS : public CPDF_ColorSpace { + public: + explicit CPDF_ICCBasedCS(CPDF_Document* pDoc); + ~CPDF_ICCBasedCS() override; + + FX_BOOL v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) override; + + FX_BOOL GetRGB(FX_FLOAT* pBuf, + FX_FLOAT& R, + FX_FLOAT& G, + FX_FLOAT& B) const override; + FX_BOOL SetRGB(FX_FLOAT* pBuf, + FX_FLOAT R, + FX_FLOAT G, + FX_FLOAT B) const override; + + FX_BOOL v_GetCMYK(FX_FLOAT* pBuf, + FX_FLOAT& c, + FX_FLOAT& m, + FX_FLOAT& y, + FX_FLOAT& k) const override; + + void EnableStdConversion(FX_BOOL bEnabled) override; + void TranslateImageLine(uint8_t* pDestBuf, + const uint8_t* pSrcBuf, + int pixels, + int image_width, + int image_height, + FX_BOOL bTransMask = FALSE) const override; + + CPDF_ColorSpace* m_pAlterCS; + CPDF_IccProfile* m_pProfile; + uint8_t* m_pCache; + FX_FLOAT* m_pRanges; + FX_BOOL m_bOwn; +}; + +class CPDF_IndexedCS : public CPDF_ColorSpace { + public: + explicit CPDF_IndexedCS(CPDF_Document* pDoc); + ~CPDF_IndexedCS() override; + + FX_BOOL v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) override; + + FX_BOOL GetRGB(FX_FLOAT* pBuf, + FX_FLOAT& R, + FX_FLOAT& G, + FX_FLOAT& B) const override; + CPDF_ColorSpace* GetBaseCS() const override; + + void EnableStdConversion(FX_BOOL bEnabled) override; + + CPDF_ColorSpace* m_pBaseCS; + CPDF_CountedColorSpace* m_pCountedBaseCS; + int m_nBaseComponents; + int m_MaxIndex; + CFX_ByteString m_Table; + FX_FLOAT* m_pCompMinMax; +}; + +class CPDF_SeparationCS : public CPDF_ColorSpace { + public: + explicit CPDF_SeparationCS(CPDF_Document* pDoc); + ~CPDF_SeparationCS() override; + + // CPDF_ColorSpace: + void GetDefaultValue(int iComponent, + FX_FLOAT& value, + FX_FLOAT& min, + FX_FLOAT& max) const override; + FX_BOOL v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) override; + FX_BOOL GetRGB(FX_FLOAT* pBuf, + FX_FLOAT& R, + FX_FLOAT& G, + FX_FLOAT& B) const override; + void EnableStdConversion(FX_BOOL bEnabled) override; + + CPDF_ColorSpace* m_pAltCS; + CPDF_Function* m_pFunc; + enum { None, All, Colorant } m_Type; +}; + +class CPDF_DeviceNCS : public CPDF_ColorSpace { + public: + explicit CPDF_DeviceNCS(CPDF_Document* pDoc); + ~CPDF_DeviceNCS() override; + + // CPDF_ColorSpace: + void GetDefaultValue(int iComponent, + FX_FLOAT& value, + FX_FLOAT& min, + FX_FLOAT& max) const override; + FX_BOOL v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) override; + FX_BOOL GetRGB(FX_FLOAT* pBuf, + FX_FLOAT& R, + FX_FLOAT& G, + FX_FLOAT& B) const override; + void EnableStdConversion(FX_BOOL bEnabled) override; + + CPDF_ColorSpace* m_pAltCS; + CPDF_Function* m_pFunc; +}; + +FX_FLOAT RGB_Conversion(FX_FLOAT colorComponent) { + if (colorComponent > 1) + colorComponent = 1; + if (colorComponent < 0) + colorComponent = 0; + + int scale = (int)(colorComponent * 1023); + if (scale < 0) + scale = 0; + if (scale < 192) + colorComponent = (g_sRGBSamples1[scale] / 255.0f); + else + colorComponent = (g_sRGBSamples2[scale / 4 - 48] / 255.0f); + return colorComponent; +} + +void XYZ_to_sRGB(FX_FLOAT X, + FX_FLOAT Y, + FX_FLOAT Z, + FX_FLOAT& R, + FX_FLOAT& G, + FX_FLOAT& B) { + FX_FLOAT R1 = 3.2410f * X - 1.5374f * Y - 0.4986f * Z; + FX_FLOAT G1 = -0.9692f * X + 1.8760f * Y + 0.0416f * Z; + FX_FLOAT B1 = 0.0556f * X - 0.2040f * Y + 1.0570f * Z; + + R = RGB_Conversion(R1); + G = RGB_Conversion(G1); + B = RGB_Conversion(B1); +} + +void XYZ_to_sRGB_WhitePoint(FX_FLOAT X, + FX_FLOAT Y, + FX_FLOAT Z, + FX_FLOAT& R, + FX_FLOAT& G, + FX_FLOAT& B, + FX_FLOAT Xw, + FX_FLOAT Yw, + FX_FLOAT Zw) { + // The following RGB_xyz is based on + // sRGB value {Rx,Ry}={0.64, 0.33}, {Gx,Gy}={0.30, 0.60}, {Bx,By}={0.15, 0.06} + + FX_FLOAT Rx = 0.64f, Ry = 0.33f; + FX_FLOAT Gx = 0.30f, Gy = 0.60f; + FX_FLOAT Bx = 0.15f, By = 0.06f; + CFX_Matrix_3by3 RGB_xyz(Rx, Gx, Bx, Ry, Gy, By, 1 - Rx - Ry, 1 - Gx - Gy, + 1 - Bx - By); + CFX_Vector_3by1 whitePoint(Xw, Yw, Zw); + CFX_Vector_3by1 XYZ(X, Y, Z); + + CFX_Vector_3by1 RGB_Sum_XYZ = RGB_xyz.Inverse().TransformVector(whitePoint); + CFX_Matrix_3by3 RGB_SUM_XYZ_DIAG(RGB_Sum_XYZ.a, 0, 0, 0, RGB_Sum_XYZ.b, 0, 0, + 0, RGB_Sum_XYZ.c); + CFX_Matrix_3by3 M = RGB_xyz.Multiply(RGB_SUM_XYZ_DIAG); + CFX_Vector_3by1 RGB = M.Inverse().TransformVector(XYZ); + + R = RGB_Conversion(RGB.a); + G = RGB_Conversion(RGB.b); + B = RGB_Conversion(RGB.c); +} + +} // namespace + +CPDF_ColorSpace* CPDF_ColorSpace::ColorspaceFromName( + const CFX_ByteString& name) { + if (name == "DeviceRGB" || name == "RGB") { + return CPDF_ColorSpace::GetStockCS(PDFCS_DEVICERGB); + } + if (name == "DeviceGray" || name == "G") { + return CPDF_ColorSpace::GetStockCS(PDFCS_DEVICEGRAY); + } + if (name == "DeviceCMYK" || name == "CMYK") { + return CPDF_ColorSpace::GetStockCS(PDFCS_DEVICECMYK); + } + if (name == "Pattern") { + return CPDF_ColorSpace::GetStockCS(PDFCS_PATTERN); + } + return NULL; +} + +CPDF_ColorSpace* CPDF_ColorSpace::GetStockCS(int family) { + return CPDF_ModuleMgr::Get()->GetPageModule()->GetStockCS(family); +} + +CPDF_ColorSpace* CPDF_ColorSpace::Load(CPDF_Document* pDoc, CPDF_Object* pObj) { + if (!pObj) + return nullptr; + if (pObj->IsName()) + return ColorspaceFromName(pObj->GetString()); + + if (CPDF_Stream* pStream = pObj->AsStream()) { + CPDF_Dictionary* pDict = pStream->GetDict(); + if (!pDict) + return nullptr; + + for (const auto& it : *pDict) { + CPDF_ColorSpace* pRet = nullptr; + CPDF_Object* pValue = it.second; + if (ToName(pValue)) + pRet = ColorspaceFromName(pValue->GetString()); + if (pRet) + return pRet; + } + return nullptr; + } + + CPDF_Array* pArray = pObj->AsArray(); + if (!pArray || pArray->GetCount() == 0) + return nullptr; + + CPDF_Object* pFamilyObj = pArray->GetElementValue(0); + if (!pFamilyObj) + return nullptr; + + CFX_ByteString familyname = pFamilyObj->GetString(); + if (pArray->GetCount() == 1) + return ColorspaceFromName(familyname); + + CPDF_ColorSpace* pCS = NULL; + FX_DWORD id = familyname.GetID(); + if (id == FXBSTR_ID('C', 'a', 'l', 'G')) { + pCS = new CPDF_CalGray(pDoc); + } else if (id == FXBSTR_ID('C', 'a', 'l', 'R')) { + pCS = new CPDF_CalRGB(pDoc); + } else if (id == FXBSTR_ID('L', 'a', 'b', 0)) { + pCS = new CPDF_LabCS(pDoc); + } else if (id == FXBSTR_ID('I', 'C', 'C', 'B')) { + pCS = new CPDF_ICCBasedCS(pDoc); + } else if (id == FXBSTR_ID('I', 'n', 'd', 'e') || + id == FXBSTR_ID('I', 0, 0, 0)) { + pCS = new CPDF_IndexedCS(pDoc); + } else if (id == FXBSTR_ID('S', 'e', 'p', 'a')) { + pCS = new CPDF_SeparationCS(pDoc); + } else if (id == FXBSTR_ID('D', 'e', 'v', 'i')) { + pCS = new CPDF_DeviceNCS(pDoc); + } else if (id == FXBSTR_ID('P', 'a', 't', 't')) { + pCS = new CPDF_PatternCS(pDoc); + } else { + return NULL; + } + pCS->m_pArray = pArray; + if (!pCS->v_Load(pDoc, pArray)) { + pCS->ReleaseCS(); + return NULL; + } + return pCS; +} + +void CPDF_ColorSpace::ReleaseCS() { + if (this == GetStockCS(PDFCS_DEVICERGB)) { + return; + } + if (this == GetStockCS(PDFCS_DEVICEGRAY)) { + return; + } + if (this == GetStockCS(PDFCS_DEVICECMYK)) { + return; + } + if (this == GetStockCS(PDFCS_PATTERN)) { + return; + } + delete this; +} + +int CPDF_ColorSpace::GetBufSize() const { + if (m_Family == PDFCS_PATTERN) { + return sizeof(PatternValue); + } + return m_nComponents * sizeof(FX_FLOAT); +} + +FX_FLOAT* CPDF_ColorSpace::CreateBuf() { + int size = GetBufSize(); + uint8_t* pBuf = FX_Alloc(uint8_t, size); + return (FX_FLOAT*)pBuf; +} + +FX_BOOL CPDF_ColorSpace::sRGB() const { + if (m_Family == PDFCS_DEVICERGB) { + return TRUE; + } + if (m_Family != PDFCS_ICCBASED) { + return FALSE; + } + CPDF_ICCBasedCS* pCS = (CPDF_ICCBasedCS*)this; + return pCS->m_pProfile->m_bsRGB; +} + +FX_BOOL CPDF_ColorSpace::GetCMYK(FX_FLOAT* pBuf, + FX_FLOAT& c, + FX_FLOAT& m, + FX_FLOAT& y, + FX_FLOAT& k) const { + if (v_GetCMYK(pBuf, c, m, y, k)) { + return TRUE; + } + FX_FLOAT R, G, B; + if (!GetRGB(pBuf, R, G, B)) { + return FALSE; + } + sRGB_to_AdobeCMYK(R, G, B, c, m, y, k); + return TRUE; +} + +FX_BOOL CPDF_ColorSpace::SetCMYK(FX_FLOAT* pBuf, + FX_FLOAT c, + FX_FLOAT m, + FX_FLOAT y, + FX_FLOAT k) const { + if (v_SetCMYK(pBuf, c, m, y, k)) { + return TRUE; + } + FX_FLOAT R, G, B; + AdobeCMYK_to_sRGB(c, m, y, k, R, G, B); + return SetRGB(pBuf, R, G, B); +} + +void CPDF_ColorSpace::GetDefaultColor(FX_FLOAT* buf) const { + if (!buf || m_Family == PDFCS_PATTERN) { + return; + } + FX_FLOAT min, max; + for (int i = 0; i < m_nComponents; i++) { + GetDefaultValue(i, buf[i], min, max); + } +} + +int CPDF_ColorSpace::GetMaxIndex() const { + if (m_Family != PDFCS_INDEXED) { + return 0; + } + CPDF_IndexedCS* pCS = (CPDF_IndexedCS*)this; + return pCS->m_MaxIndex; +} + +void CPDF_ColorSpace::TranslateImageLine(uint8_t* dest_buf, + const uint8_t* src_buf, + int pixels, + int image_width, + int image_height, + FX_BOOL bTransMask) const { + CFX_FixedBufGrow<FX_FLOAT, 16> srcbuf(m_nComponents); + FX_FLOAT* src = srcbuf; + FX_FLOAT R, G, B; + for (int i = 0; i < pixels; i++) { + for (int j = 0; j < m_nComponents; j++) + if (m_Family == PDFCS_INDEXED) { + src[j] = (FX_FLOAT)(*src_buf++); + } else { + src[j] = (FX_FLOAT)(*src_buf++) / 255; + } + GetRGB(src, R, G, B); + *dest_buf++ = (int32_t)(B * 255); + *dest_buf++ = (int32_t)(G * 255); + *dest_buf++ = (int32_t)(R * 255); + } +} + +void CPDF_ColorSpace::EnableStdConversion(FX_BOOL bEnabled) { + if (bEnabled) + m_dwStdConversion++; + else if (m_dwStdConversion) + m_dwStdConversion--; +} + +CPDF_CalGray::CPDF_CalGray(CPDF_Document* pDoc) + : CPDF_ColorSpace(pDoc, PDFCS_CALGRAY, 1) {} + +FX_BOOL CPDF_CalGray::v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) { + CPDF_Dictionary* pDict = pArray->GetDictAt(1); + if (!pDict) + return FALSE; + + CPDF_Array* pParam = pDict->GetArrayBy("WhitePoint"); + int i; + for (i = 0; i < 3; i++) + m_WhitePoint[i] = pParam ? pParam->GetNumberAt(i) : 0; + + pParam = pDict->GetArrayBy("BlackPoint"); + for (i = 0; i < 3; i++) + m_BlackPoint[i] = pParam ? pParam->GetNumberAt(i) : 0; + + m_Gamma = pDict->GetNumberBy("Gamma"); + if (m_Gamma == 0) + m_Gamma = 1.0f; + return TRUE; +} + +FX_BOOL CPDF_CalGray::GetRGB(FX_FLOAT* pBuf, + FX_FLOAT& R, + FX_FLOAT& G, + FX_FLOAT& B) const { + R = G = B = *pBuf; + return TRUE; +} + +FX_BOOL CPDF_CalGray::SetRGB(FX_FLOAT* pBuf, + FX_FLOAT R, + FX_FLOAT G, + FX_FLOAT B) const { + if (R == G && R == B) { + *pBuf = R; + return TRUE; + } + return FALSE; +} + +void CPDF_CalGray::TranslateImageLine(uint8_t* pDestBuf, + const uint8_t* pSrcBuf, + int pixels, + int image_width, + int image_height, + FX_BOOL bTransMask) const { + for (int i = 0; i < pixels; i++) { + *pDestBuf++ = pSrcBuf[i]; + *pDestBuf++ = pSrcBuf[i]; + *pDestBuf++ = pSrcBuf[i]; + } +} + +CPDF_CalRGB::CPDF_CalRGB(CPDF_Document* pDoc) + : CPDF_ColorSpace(pDoc, PDFCS_CALRGB, 3) {} + +FX_BOOL CPDF_CalRGB::v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) { + CPDF_Dictionary* pDict = pArray->GetDictAt(1); + if (!pDict) + return FALSE; + + CPDF_Array* pParam = pDict->GetArrayBy("WhitePoint"); + int i; + for (i = 0; i < 3; i++) + m_WhitePoint[i] = pParam ? pParam->GetNumberAt(i) : 0; + + pParam = pDict->GetArrayBy("BlackPoint"); + for (i = 0; i < 3; i++) + m_BlackPoint[i] = pParam ? pParam->GetNumberAt(i) : 0; + + pParam = pDict->GetArrayBy("Gamma"); + if (pParam) { + m_bGamma = TRUE; + for (i = 0; i < 3; i++) + m_Gamma[i] = pParam->GetNumberAt(i); + } else { + m_bGamma = FALSE; + } + + pParam = pDict->GetArrayBy("Matrix"); + if (pParam) { + m_bMatrix = TRUE; + for (i = 0; i < 9; i++) + m_Matrix[i] = pParam->GetNumberAt(i); + } else { + m_bMatrix = FALSE; + } + return TRUE; +} + +FX_BOOL CPDF_CalRGB::GetRGB(FX_FLOAT* pBuf, + FX_FLOAT& R, + FX_FLOAT& G, + FX_FLOAT& B) const { + FX_FLOAT A_ = pBuf[0]; + FX_FLOAT B_ = pBuf[1]; + FX_FLOAT C_ = pBuf[2]; + if (m_bGamma) { + A_ = (FX_FLOAT)FXSYS_pow(A_, m_Gamma[0]); + B_ = (FX_FLOAT)FXSYS_pow(B_, m_Gamma[1]); + C_ = (FX_FLOAT)FXSYS_pow(C_, m_Gamma[2]); + } + + FX_FLOAT X; + FX_FLOAT Y; + FX_FLOAT Z; + if (m_bMatrix) { + X = m_Matrix[0] * A_ + m_Matrix[3] * B_ + m_Matrix[6] * C_; + Y = m_Matrix[1] * A_ + m_Matrix[4] * B_ + m_Matrix[7] * C_; + Z = m_Matrix[2] * A_ + m_Matrix[5] * B_ + m_Matrix[8] * C_; + } else { + X = A_; + Y = B_; + Z = C_; + } + XYZ_to_sRGB_WhitePoint(X, Y, Z, R, G, B, m_WhitePoint[0], m_WhitePoint[1], + m_WhitePoint[2]); + return TRUE; +} + +FX_BOOL CPDF_CalRGB::SetRGB(FX_FLOAT* pBuf, + FX_FLOAT R, + FX_FLOAT G, + FX_FLOAT B) const { + pBuf[0] = R; + pBuf[1] = G; + pBuf[2] = B; + return TRUE; +} + +void CPDF_CalRGB::TranslateImageLine(uint8_t* pDestBuf, + const uint8_t* pSrcBuf, + int pixels, + int image_width, + int image_height, + FX_BOOL bTransMask) const { + if (bTransMask) { + FX_FLOAT Cal[3]; + FX_FLOAT R; + FX_FLOAT G; + FX_FLOAT B; + for (int i = 0; i < pixels; i++) { + Cal[0] = ((FX_FLOAT)pSrcBuf[2]) / 255; + Cal[1] = ((FX_FLOAT)pSrcBuf[1]) / 255; + Cal[2] = ((FX_FLOAT)pSrcBuf[0]) / 255; + GetRGB(Cal, R, G, B); + pDestBuf[0] = FXSYS_round(B * 255); + pDestBuf[1] = FXSYS_round(G * 255); + pDestBuf[2] = FXSYS_round(R * 255); + pSrcBuf += 3; + pDestBuf += 3; + } + } + ReverseRGB(pDestBuf, pSrcBuf, pixels); +} + +CPDF_LabCS::CPDF_LabCS(CPDF_Document* pDoc) + : CPDF_ColorSpace(pDoc, PDFCS_LAB, 3) {} + +void CPDF_LabCS::GetDefaultValue(int iComponent, + FX_FLOAT& value, + FX_FLOAT& min, + FX_FLOAT& max) const { + assert(iComponent < 3); + value = 0; + if (iComponent == 0) { + min = 0; + max = 100 * 1.0f; + } else { + min = m_Ranges[iComponent * 2 - 2]; + max = m_Ranges[iComponent * 2 - 1]; + if (value < min) + value = min; + else if (value > max) + value = max; + } +} + +FX_BOOL CPDF_LabCS::v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) { + CPDF_Dictionary* pDict = pArray->GetDictAt(1); + if (!pDict) + return FALSE; + + CPDF_Array* pParam = pDict->GetArrayBy("WhitePoint"); + int i; + for (i = 0; i < 3; i++) + m_WhitePoint[i] = pParam ? pParam->GetNumberAt(i) : 0; + + pParam = pDict->GetArrayBy("BlackPoint"); + for (i = 0; i < 3; i++) + m_BlackPoint[i] = pParam ? pParam->GetNumberAt(i) : 0; + + pParam = pDict->GetArrayBy("Range"); + const FX_FLOAT def_ranges[4] = {-100 * 1.0f, 100 * 1.0f, -100 * 1.0f, + 100 * 1.0f}; + for (i = 0; i < 4; i++) + m_Ranges[i] = pParam ? pParam->GetNumberAt(i) : def_ranges[i]; + return TRUE; +} + +FX_BOOL CPDF_LabCS::GetRGB(FX_FLOAT* pBuf, + FX_FLOAT& R, + FX_FLOAT& G, + FX_FLOAT& B) const { + FX_FLOAT Lstar = pBuf[0]; + FX_FLOAT astar = pBuf[1]; + FX_FLOAT bstar = pBuf[2]; + FX_FLOAT M = (Lstar + 16.0f) / 116.0f; + FX_FLOAT L = M + astar / 500.0f; + FX_FLOAT N = M - bstar / 200.0f; + FX_FLOAT X, Y, Z; + if (L < 0.2069f) + X = 0.957f * 0.12842f * (L - 0.1379f); + else + X = 0.957f * L * L * L; + + if (M < 0.2069f) + Y = 0.12842f * (M - 0.1379f); + else + Y = M * M * M; + + if (N < 0.2069f) + Z = 1.0889f * 0.12842f * (N - 0.1379f); + else + Z = 1.0889f * N * N * N; + + XYZ_to_sRGB(X, Y, Z, R, G, B); + return TRUE; +} + +FX_BOOL CPDF_LabCS::SetRGB(FX_FLOAT* pBuf, + FX_FLOAT R, + FX_FLOAT G, + FX_FLOAT B) const { + return FALSE; +} + +void CPDF_LabCS::TranslateImageLine(uint8_t* pDestBuf, + const uint8_t* pSrcBuf, + int pixels, + int image_width, + int image_height, + FX_BOOL bTransMask) const { + for (int i = 0; i < pixels; i++) { + FX_FLOAT lab[3]; + FX_FLOAT R, G, B; + lab[0] = (pSrcBuf[0] * 100 / 255.0f); + lab[1] = (FX_FLOAT)(pSrcBuf[1] - 128); + lab[2] = (FX_FLOAT)(pSrcBuf[2] - 128); + GetRGB(lab, R, G, B); + pDestBuf[0] = (int32_t)(B * 255); + pDestBuf[1] = (int32_t)(G * 255); + pDestBuf[2] = (int32_t)(R * 255); + pDestBuf += 3; + pSrcBuf += 3; + } +} + +CPDF_ICCBasedCS::CPDF_ICCBasedCS(CPDF_Document* pDoc) + : CPDF_ColorSpace(pDoc, PDFCS_ICCBASED, 0), + m_pAlterCS(nullptr), + m_pProfile(nullptr), + m_pCache(nullptr), + m_pRanges(nullptr), + m_bOwn(FALSE) {} + +CPDF_ICCBasedCS::~CPDF_ICCBasedCS() { + FX_Free(m_pCache); + FX_Free(m_pRanges); + if (m_pAlterCS && m_bOwn) + m_pAlterCS->ReleaseCS(); + if (m_pProfile && m_pDocument) + m_pDocument->GetPageData()->ReleaseIccProfile(m_pProfile); +} + +FX_BOOL CPDF_ICCBasedCS::v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) { + CPDF_Stream* pStream = pArray->GetStreamAt(1); + if (!pStream) + return FALSE; + + m_pProfile = pDoc->LoadIccProfile(pStream); + if (!m_pProfile) + return FALSE; + + m_nComponents = + m_pProfile + ->GetComponents(); // Try using the nComponents from ICC profile + CPDF_Dictionary* pDict = pStream->GetDict(); + if (!m_pProfile->m_pTransform) { // No valid ICC profile or using sRGB + CPDF_Object* pAlterCSObj = + pDict ? pDict->GetElementValue("Alternate") : nullptr; + if (pAlterCSObj) { + CPDF_ColorSpace* pAlterCS = CPDF_ColorSpace::Load(pDoc, pAlterCSObj); + if (pAlterCS) { + if (m_nComponents == 0) { // NO valid ICC profile + if (pAlterCS->CountComponents() > 0) { // Use Alternative colorspace + m_nComponents = pAlterCS->CountComponents(); + m_pAlterCS = pAlterCS; + m_bOwn = TRUE; + } else { // No valid alternative colorspace + pAlterCS->ReleaseCS(); + int32_t nDictComponents = pDict ? pDict->GetIntegerBy("N") : 0; + if (nDictComponents != 1 && nDictComponents != 3 && + nDictComponents != 4) { + return FALSE; + } + m_nComponents = nDictComponents; + } + + } else { // Using sRGB + if (pAlterCS->CountComponents() != m_nComponents) { + pAlterCS->ReleaseCS(); + } else { + m_pAlterCS = pAlterCS; + m_bOwn = TRUE; + } + } + } + } + if (!m_pAlterCS) { + if (m_nComponents == 1) + m_pAlterCS = GetStockCS(PDFCS_DEVICEGRAY); + else if (m_nComponents == 3) + m_pAlterCS = GetStockCS(PDFCS_DEVICERGB); + else if (m_nComponents == 4) + m_pAlterCS = GetStockCS(PDFCS_DEVICECMYK); + } + } + CPDF_Array* pRanges = pDict->GetArrayBy("Range"); + m_pRanges = FX_Alloc2D(FX_FLOAT, m_nComponents, 2); + for (int i = 0; i < m_nComponents * 2; i++) { + if (pRanges) + m_pRanges[i] = pRanges->GetNumberAt(i); + else if (i % 2) + m_pRanges[i] = 1.0f; + else + m_pRanges[i] = 0; + } + return TRUE; +} + +FX_BOOL CPDF_ICCBasedCS::GetRGB(FX_FLOAT* pBuf, + FX_FLOAT& R, + FX_FLOAT& G, + FX_FLOAT& B) const { + if (m_pProfile && m_pProfile->m_bsRGB) { + R = pBuf[0]; + G = pBuf[1]; + B = pBuf[2]; + return TRUE; + } + ICodec_IccModule* pIccModule = CPDF_ModuleMgr::Get()->GetIccModule(); + if (!m_pProfile->m_pTransform || !pIccModule) { + if (m_pAlterCS) { + return m_pAlterCS->GetRGB(pBuf, R, G, B); + } + R = G = B = 0.0f; + return TRUE; + } + FX_FLOAT rgb[3]; + pIccModule->SetComponents(m_nComponents); + pIccModule->Translate(m_pProfile->m_pTransform, pBuf, rgb); + R = rgb[0]; + G = rgb[1]; + B = rgb[2]; + return TRUE; +} + +FX_BOOL CPDF_ICCBasedCS::SetRGB(FX_FLOAT* pBuf, + FX_FLOAT R, + FX_FLOAT G, + FX_FLOAT B) const { + return FALSE; +} + +FX_BOOL CPDF_ICCBasedCS::v_GetCMYK(FX_FLOAT* pBuf, + FX_FLOAT& c, + FX_FLOAT& m, + FX_FLOAT& y, + FX_FLOAT& k) const { + if (m_nComponents != 4) + return FALSE; + + c = pBuf[0]; + m = pBuf[1]; + y = pBuf[2]; + k = pBuf[3]; + return TRUE; +} + +void CPDF_ICCBasedCS::EnableStdConversion(FX_BOOL bEnabled) { + CPDF_ColorSpace::EnableStdConversion(bEnabled); + if (m_pAlterCS) + m_pAlterCS->EnableStdConversion(bEnabled); +} + +void CPDF_ICCBasedCS::TranslateImageLine(uint8_t* pDestBuf, + const uint8_t* pSrcBuf, + int pixels, + int image_width, + int image_height, + FX_BOOL bTransMask) const { + if (m_pProfile->m_bsRGB) { + ReverseRGB(pDestBuf, pSrcBuf, pixels); + } else if (m_pProfile->m_pTransform) { + int nMaxColors = 1; + for (int i = 0; i < m_nComponents; i++) { + nMaxColors *= 52; + } + if (m_nComponents > 3 || image_width * image_height < nMaxColors * 3 / 2) { + CPDF_ModuleMgr::Get()->GetIccModule()->TranslateScanline( + m_pProfile->m_pTransform, pDestBuf, pSrcBuf, pixels); + } else { + if (!m_pCache) { + ((CPDF_ICCBasedCS*)this)->m_pCache = FX_Alloc2D(uint8_t, nMaxColors, 3); + uint8_t* temp_src = FX_Alloc2D(uint8_t, nMaxColors, m_nComponents); + uint8_t* pSrc = temp_src; + for (int i = 0; i < nMaxColors; i++) { + FX_DWORD color = i; + FX_DWORD order = nMaxColors / 52; + for (int c = 0; c < m_nComponents; c++) { + *pSrc++ = (uint8_t)(color / order * 5); + color %= order; + order /= 52; + } + } + CPDF_ModuleMgr::Get()->GetIccModule()->TranslateScanline( + m_pProfile->m_pTransform, m_pCache, temp_src, nMaxColors); + FX_Free(temp_src); + } + for (int i = 0; i < pixels; i++) { + int index = 0; + for (int c = 0; c < m_nComponents; c++) { + index = index * 52 + (*pSrcBuf) / 5; + pSrcBuf++; + } + index *= 3; + *pDestBuf++ = m_pCache[index]; + *pDestBuf++ = m_pCache[index + 1]; + *pDestBuf++ = m_pCache[index + 2]; + } + } + } else if (m_pAlterCS) { + m_pAlterCS->TranslateImageLine(pDestBuf, pSrcBuf, pixels, image_width, + image_height); + } +} + +CPDF_IndexedCS::CPDF_IndexedCS(CPDF_Document* pDoc) + : CPDF_ColorSpace(pDoc, PDFCS_INDEXED, 1), + m_pBaseCS(nullptr), + m_pCountedBaseCS(nullptr), + m_pCompMinMax(nullptr) {} + +CPDF_IndexedCS::~CPDF_IndexedCS() { + FX_Free(m_pCompMinMax); + CPDF_ColorSpace* pCS = m_pCountedBaseCS ? m_pCountedBaseCS->get() : NULL; + if (pCS && m_pDocument) { + m_pDocument->GetPageData()->ReleaseColorSpace(pCS->GetArray()); + } +} + +FX_BOOL CPDF_IndexedCS::v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) { + if (pArray->GetCount() < 4) { + return FALSE; + } + CPDF_Object* pBaseObj = pArray->GetElementValue(1); + if (pBaseObj == m_pArray) { + return FALSE; + } + CPDF_DocPageData* pDocPageData = pDoc->GetPageData(); + m_pBaseCS = pDocPageData->GetColorSpace(pBaseObj, NULL); + if (!m_pBaseCS) { + return FALSE; + } + m_pCountedBaseCS = pDocPageData->FindColorSpacePtr(m_pBaseCS->GetArray()); + m_nBaseComponents = m_pBaseCS->CountComponents(); + m_pCompMinMax = FX_Alloc2D(FX_FLOAT, m_nBaseComponents, 2); + FX_FLOAT defvalue; + for (int i = 0; i < m_nBaseComponents; i++) { + m_pBaseCS->GetDefaultValue(i, defvalue, m_pCompMinMax[i * 2], + m_pCompMinMax[i * 2 + 1]); + m_pCompMinMax[i * 2 + 1] -= m_pCompMinMax[i * 2]; + } + m_MaxIndex = pArray->GetIntegerAt(2); + + CPDF_Object* pTableObj = pArray->GetElementValue(3); + if (!pTableObj) + return FALSE; + + if (CPDF_String* pString = pTableObj->AsString()) { + m_Table = pString->GetString(); + } else if (CPDF_Stream* pStream = pTableObj->AsStream()) { + CPDF_StreamAcc acc; + acc.LoadAllData(pStream, FALSE); + m_Table = CFX_ByteStringC(acc.GetData(), acc.GetSize()); + } + return TRUE; +} + +FX_BOOL CPDF_IndexedCS::GetRGB(FX_FLOAT* pBuf, + FX_FLOAT& R, + FX_FLOAT& G, + FX_FLOAT& B) const { + int index = (int32_t)(*pBuf); + if (index < 0 || index > m_MaxIndex) { + return FALSE; + } + if (m_nBaseComponents) { + if (index == INT_MAX || (index + 1) > INT_MAX / m_nBaseComponents || + (index + 1) * m_nBaseComponents > (int)m_Table.GetLength()) { + R = G = B = 0; + return FALSE; + } + } + CFX_FixedBufGrow<FX_FLOAT, 16> Comps(m_nBaseComponents); + FX_FLOAT* comps = Comps; + const uint8_t* pTable = m_Table; + for (int i = 0; i < m_nBaseComponents; i++) { + comps[i] = + m_pCompMinMax[i * 2] + + m_pCompMinMax[i * 2 + 1] * pTable[index * m_nBaseComponents + i] / 255; + } + return m_pBaseCS->GetRGB(comps, R, G, B); +} + +CPDF_ColorSpace* CPDF_IndexedCS::GetBaseCS() const { + return m_pBaseCS; +} + +void CPDF_IndexedCS::EnableStdConversion(FX_BOOL bEnabled) { + CPDF_ColorSpace::EnableStdConversion(bEnabled); + if (m_pBaseCS) { + m_pBaseCS->EnableStdConversion(bEnabled); + } +} + +CPDF_PatternCS::CPDF_PatternCS(CPDF_Document* pDoc) + : CPDF_ColorSpace(pDoc, PDFCS_PATTERN, 1), + m_pBaseCS(nullptr), + m_pCountedBaseCS(nullptr) {} + +CPDF_PatternCS::~CPDF_PatternCS() { + CPDF_ColorSpace* pCS = m_pCountedBaseCS ? m_pCountedBaseCS->get() : NULL; + if (pCS && m_pDocument) { + m_pDocument->GetPageData()->ReleaseColorSpace(pCS->GetArray()); + } +} + +FX_BOOL CPDF_PatternCS::v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) { + CPDF_Object* pBaseCS = pArray->GetElementValue(1); + if (pBaseCS == m_pArray) { + return FALSE; + } + CPDF_DocPageData* pDocPageData = pDoc->GetPageData(); + m_pBaseCS = pDocPageData->GetColorSpace(pBaseCS, NULL); + if (m_pBaseCS) { + if (m_pBaseCS->GetFamily() == PDFCS_PATTERN) { + return FALSE; + } + m_pCountedBaseCS = pDocPageData->FindColorSpacePtr(m_pBaseCS->GetArray()); + m_nComponents = m_pBaseCS->CountComponents() + 1; + if (m_pBaseCS->CountComponents() > MAX_PATTERN_COLORCOMPS) { + return FALSE; + } + } else { + m_nComponents = 1; + } + return TRUE; +} + +FX_BOOL CPDF_PatternCS::GetRGB(FX_FLOAT* pBuf, + FX_FLOAT& R, + FX_FLOAT& G, + FX_FLOAT& B) const { + if (m_pBaseCS) { + ASSERT(m_pBaseCS->GetFamily() != PDFCS_PATTERN); + PatternValue* pvalue = (PatternValue*)pBuf; + if (m_pBaseCS->GetRGB(pvalue->m_Comps, R, G, B)) { + return TRUE; + } + } + R = G = B = 0.75f; + return FALSE; +} + +CPDF_ColorSpace* CPDF_PatternCS::GetBaseCS() const { + return m_pBaseCS; +} + +CPDF_SeparationCS::CPDF_SeparationCS(CPDF_Document* pDoc) + : CPDF_ColorSpace(pDoc, PDFCS_SEPARATION, 1), + m_pAltCS(nullptr), + m_pFunc(nullptr) {} + +CPDF_SeparationCS::~CPDF_SeparationCS() { + if (m_pAltCS) { + m_pAltCS->ReleaseCS(); + } + delete m_pFunc; +} + +void CPDF_SeparationCS::GetDefaultValue(int iComponent, + FX_FLOAT& value, + FX_FLOAT& min, + FX_FLOAT& max) const { + value = 1.0f; + min = 0; + max = 1.0f; +} + +FX_BOOL CPDF_SeparationCS::v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) { + CFX_ByteString name = pArray->GetStringAt(1); + if (name == "None") { + m_Type = None; + } else { + m_Type = Colorant; + CPDF_Object* pAltCS = pArray->GetElementValue(2); + if (pAltCS == m_pArray) { + return FALSE; + } + m_pAltCS = Load(pDoc, pAltCS); + if (!m_pAltCS) { + return FALSE; + } + CPDF_Object* pFuncObj = pArray->GetElementValue(3); + if (pFuncObj && !pFuncObj->IsName()) + m_pFunc = CPDF_Function::Load(pFuncObj); + + if (m_pFunc && m_pFunc->CountOutputs() < m_pAltCS->CountComponents()) { + delete m_pFunc; + m_pFunc = NULL; + } + } + return TRUE; +} + +FX_BOOL CPDF_SeparationCS::GetRGB(FX_FLOAT* pBuf, + FX_FLOAT& R, + FX_FLOAT& G, + FX_FLOAT& B) const { + if (m_Type == None) { + return FALSE; + } + if (!m_pFunc) { + if (!m_pAltCS) { + return FALSE; + } + int nComps = m_pAltCS->CountComponents(); + CFX_FixedBufGrow<FX_FLOAT, 16> results(nComps); + for (int i = 0; i < nComps; i++) { + results[i] = *pBuf; + } + return m_pAltCS->GetRGB(results, R, G, B); + } + CFX_FixedBufGrow<FX_FLOAT, 16> results(m_pFunc->CountOutputs()); + int nresults = 0; + m_pFunc->Call(pBuf, 1, results, nresults); + if (nresults == 0) { + return FALSE; + } + if (m_pAltCS) { + return m_pAltCS->GetRGB(results, R, G, B); + } + R = G = B = 0; + return FALSE; +} + +void CPDF_SeparationCS::EnableStdConversion(FX_BOOL bEnabled) { + CPDF_ColorSpace::EnableStdConversion(bEnabled); + if (m_pAltCS) { + m_pAltCS->EnableStdConversion(bEnabled); + } +} + +CPDF_DeviceNCS::CPDF_DeviceNCS(CPDF_Document* pDoc) + : CPDF_ColorSpace(pDoc, PDFCS_DEVICEN, 0), + m_pAltCS(nullptr), + m_pFunc(nullptr) {} + +CPDF_DeviceNCS::~CPDF_DeviceNCS() { + delete m_pFunc; + if (m_pAltCS) { + m_pAltCS->ReleaseCS(); + } +} + +void CPDF_DeviceNCS::GetDefaultValue(int iComponent, + FX_FLOAT& value, + FX_FLOAT& min, + FX_FLOAT& max) const { + value = 1.0f; + min = 0; + max = 1.0f; +} + +FX_BOOL CPDF_DeviceNCS::v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) { + CPDF_Array* pObj = ToArray(pArray->GetElementValue(1)); + if (!pObj) + return FALSE; + + m_nComponents = pObj->GetCount(); + CPDF_Object* pAltCS = pArray->GetElementValue(2); + if (!pAltCS || pAltCS == m_pArray) { + return FALSE; + } + m_pAltCS = Load(pDoc, pAltCS); + m_pFunc = CPDF_Function::Load(pArray->GetElementValue(3)); + if (!m_pAltCS || !m_pFunc) { + return FALSE; + } + if (m_pFunc->CountOutputs() < m_pAltCS->CountComponents()) { + return FALSE; + } + return TRUE; +} + +FX_BOOL CPDF_DeviceNCS::GetRGB(FX_FLOAT* pBuf, + FX_FLOAT& R, + FX_FLOAT& G, + FX_FLOAT& B) const { + if (!m_pFunc) { + return FALSE; + } + CFX_FixedBufGrow<FX_FLOAT, 16> results(m_pFunc->CountOutputs()); + int nresults = 0; + m_pFunc->Call(pBuf, m_nComponents, results, nresults); + if (nresults == 0) { + return FALSE; + } + return m_pAltCS->GetRGB(results, R, G, B); +} + +void CPDF_DeviceNCS::EnableStdConversion(FX_BOOL bEnabled) { + CPDF_ColorSpace::EnableStdConversion(bEnabled); + if (m_pAltCS) { + m_pAltCS->EnableStdConversion(bEnabled); + } +} diff --git a/core/fpdfapi/fpdf_page/cpdf_colorstate.cpp b/core/fpdfapi/fpdf_page/cpdf_colorstate.cpp index 02999c9d13..81e5e41d60 100644 --- a/core/fpdfapi/fpdf_page/cpdf_colorstate.cpp +++ b/core/fpdfapi/fpdf_page/cpdf_colorstate.cpp @@ -6,6 +6,10 @@ #include "core/fpdfapi/fpdf_page/cpdf_colorstate.h" +#include "core/fpdfapi/fpdf_page/cpdf_pattern.h" +#include "core/fpdfapi/fpdf_page/cpdf_tilingpattern.h" +#include "core/include/fxge/fx_dib.h" + void CPDF_ColorState::SetFillColor(CPDF_ColorSpace* pCS, FX_FLOAT* pValue, int nValues) { diff --git a/core/fpdfapi/fpdf_page/cpdf_colorstatedata.h b/core/fpdfapi/fpdf_page/cpdf_colorstatedata.h index 319fea51ae..96e43bd9f3 100644 --- a/core/fpdfapi/fpdf_page/cpdf_colorstatedata.h +++ b/core/fpdfapi/fpdf_page/cpdf_colorstatedata.h @@ -7,8 +7,8 @@ #ifndef CORE_FPDFAPI_FPDF_PAGE_CPDF_COLORSTATEDATA_H_ #define CORE_FPDFAPI_FPDF_PAGE_CPDF_COLORSTATEDATA_H_ +#include "core/fpdfapi/fpdf_page/include/cpdf_color.h" #include "core/fxcrt/include/fx_system.h" -#include "core/include/fpdfapi/fpdf_resource.h" class CPDF_ColorStateData { public: diff --git a/core/fpdfapi/fpdf_page/cpdf_countedobject.h b/core/fpdfapi/fpdf_page/cpdf_countedobject.h new file mode 100644 index 0000000000..da840bec60 --- /dev/null +++ b/core/fpdfapi/fpdf_page/cpdf_countedobject.h @@ -0,0 +1,45 @@ +// 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_FPDFAPI_FPDF_PAGE_CPDF_COUNTEDOBJECT_H_ +#define CORE_FPDFAPI_FPDF_PAGE_CPDF_COUNTEDOBJECT_H_ + +#include "core/fpdfapi/fpdf_page/cpdf_pattern.h" +#include "core/fpdfapi/fpdf_page/include/cpdf_colorspace.h" +#include "core/fxcrt/include/fx_system.h" + +template <class T> +class CPDF_CountedObject { + public: + explicit CPDF_CountedObject(T* ptr) : m_nCount(1), m_pObj(ptr) {} + void reset(T* ptr) { // CAUTION: tosses prior ref counts. + m_nCount = 1; + m_pObj = ptr; + } + void clear() { // Now you're all weak ptrs ... + delete m_pObj; + m_pObj = nullptr; + } + T* get() const { return m_pObj; } + T* AddRef() { + FXSYS_assert(m_pObj); + ++m_nCount; + return m_pObj; + } + void RemoveRef() { + if (m_nCount) + --m_nCount; + } + size_t use_count() const { return m_nCount; } + + protected: + size_t m_nCount; + T* m_pObj; +}; +using CPDF_CountedColorSpace = CPDF_CountedObject<CPDF_ColorSpace>; +using CPDF_CountedPattern = CPDF_CountedObject<CPDF_Pattern>; + +#endif // CORE_FPDFAPI_FPDF_PAGE_CPDF_COUNTEDOBJECT_H_ diff --git a/core/fpdfapi/fpdf_page/cpdf_image.cpp b/core/fpdfapi/fpdf_page/cpdf_image.cpp new file mode 100644 index 0000000000..d9e7d774be --- /dev/null +++ b/core/fpdfapi/fpdf_page/cpdf_image.cpp @@ -0,0 +1,461 @@ +// 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 + +#include "core/fpdfapi/fpdf_page/include/cpdf_image.h" + +#include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" +#include "core/fpdfapi/include/cpdf_modulemgr.h" +#include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" +#include "core/include/fxge/fx_dib.h" +#include "core/fpdfapi/fpdf_page/pageint.h" +#include "core/include/fxcodec/fx_codec.h" +#include "core/fpdfapi/fpdf_parser/cpdf_boolean.h" +#include "core/fpdfapi/fpdf_parser/include/cpdf_string.h" +#include "core/fpdfapi/fpdf_page/include/cpdf_page.h" +#include "core/fpdfapi/fpdf_render/cpdf_pagerendercache.h" +#include "core/fpdfapi/fpdf_render/render_int.h" + +CPDF_Image::CPDF_Image(CPDF_Document* pDoc) + : m_pDIBSource(nullptr), + m_pMask(nullptr), + m_MatteColor(0), + m_pStream(nullptr), + m_bInline(FALSE), + m_pInlineDict(nullptr), + m_pDocument(pDoc), + m_pOC(nullptr) {} + +CPDF_Image::~CPDF_Image() { + if (m_bInline) { + if (m_pStream) + m_pStream->Release(); + if (m_pInlineDict) + m_pInlineDict->Release(); + } +} + +void CPDF_Image::Release() { + if (m_bInline || (m_pStream && m_pStream->GetObjNum() == 0)) + delete this; +} + +CPDF_Image* CPDF_Image::Clone() { + if (m_pStream->GetObjNum()) + return m_pDocument->GetPageData()->GetImage(m_pStream); + + CPDF_Image* pImage = new CPDF_Image(m_pDocument); + pImage->LoadImageF(ToStream(m_pStream->Clone()), m_bInline); + if (m_bInline) + pImage->SetInlineDict(ToDictionary(m_pInlineDict->Clone(TRUE))); + + return pImage; +} + +FX_BOOL CPDF_Image::LoadImageF(CPDF_Stream* pStream, FX_BOOL bInline) { + m_pStream = pStream; + if (m_bInline && m_pInlineDict) { + m_pInlineDict->Release(); + m_pInlineDict = NULL; + } + m_bInline = bInline; + CPDF_Dictionary* pDict = pStream->GetDict(); + if (m_bInline) { + m_pInlineDict = ToDictionary(pDict->Clone()); + } + m_pOC = pDict->GetDictBy("OC"); + m_bIsMask = + !pDict->KeyExist("ColorSpace") || pDict->GetIntegerBy("ImageMask"); + m_bInterpolate = pDict->GetIntegerBy("Interpolate"); + m_Height = pDict->GetIntegerBy("Height"); + m_Width = pDict->GetIntegerBy("Width"); + return TRUE; +} + +CPDF_Dictionary* CPDF_Image::InitJPEG(uint8_t* pData, FX_DWORD size) { + int32_t width; + int32_t height; + int32_t num_comps; + int32_t bits; + FX_BOOL color_trans; + if (!CPDF_ModuleMgr::Get()->GetJpegModule()->LoadInfo( + pData, size, width, height, num_comps, bits, color_trans)) { + return NULL; + } + CPDF_Dictionary* pDict = new CPDF_Dictionary; + pDict->SetAtName("Type", "XObject"); + pDict->SetAtName("Subtype", "Image"); + pDict->SetAtInteger("Width", width); + pDict->SetAtInteger("Height", height); + const FX_CHAR* csname = NULL; + if (num_comps == 1) { + csname = "DeviceGray"; + } else if (num_comps == 3) { + csname = "DeviceRGB"; + } else if (num_comps == 4) { + csname = "DeviceCMYK"; + CPDF_Array* pDecode = new CPDF_Array; + for (int n = 0; n < 4; n++) { + pDecode->AddInteger(1); + pDecode->AddInteger(0); + } + pDict->SetAt("Decode", pDecode); + } + pDict->SetAtName("ColorSpace", csname); + pDict->SetAtInteger("BitsPerComponent", bits); + pDict->SetAtName("Filter", "DCTDecode"); + if (!color_trans) { + CPDF_Dictionary* pParms = new CPDF_Dictionary; + pDict->SetAt("DecodeParms", pParms); + pParms->SetAtInteger("ColorTransform", 0); + } + m_bIsMask = FALSE; + m_Width = width; + m_Height = height; + if (!m_pStream) { + m_pStream = new CPDF_Stream(NULL, 0, NULL); + } + return pDict; +} + +void CPDF_Image::SetJpegImage(uint8_t* pData, FX_DWORD size) { + CPDF_Dictionary* pDict = InitJPEG(pData, size); + if (!pDict) { + return; + } + m_pStream->InitStream(pData, size, pDict); +} + +void CPDF_Image::SetJpegImage(IFX_FileRead* pFile) { + FX_DWORD size = (FX_DWORD)pFile->GetSize(); + if (!size) { + return; + } + FX_DWORD dwEstimateSize = size; + if (dwEstimateSize > 8192) { + dwEstimateSize = 8192; + } + uint8_t* pData = FX_Alloc(uint8_t, dwEstimateSize); + pFile->ReadBlock(pData, 0, dwEstimateSize); + CPDF_Dictionary* pDict = InitJPEG(pData, dwEstimateSize); + FX_Free(pData); + if (!pDict && size > dwEstimateSize) { + pData = FX_Alloc(uint8_t, size); + pFile->ReadBlock(pData, 0, size); + pDict = InitJPEG(pData, size); + FX_Free(pData); + } + if (!pDict) { + return; + } + m_pStream->InitStreamFromFile(pFile, pDict); +} + +void CPDF_Image::SetImage(const CFX_DIBitmap* pBitmap, + int32_t iCompress, + IFX_FileWrite* pFileWrite, + IFX_FileRead* pFileRead, + const CFX_DIBitmap* pMask) { + int32_t BitmapWidth = pBitmap->GetWidth(); + int32_t BitmapHeight = pBitmap->GetHeight(); + if (BitmapWidth < 1 || BitmapHeight < 1) { + return; + } + uint8_t* src_buf = pBitmap->GetBuffer(); + int32_t src_pitch = pBitmap->GetPitch(); + int32_t bpp = pBitmap->GetBPP(); + + CPDF_Dictionary* pDict = new CPDF_Dictionary; + pDict->SetAtName("Type", "XObject"); + pDict->SetAtName("Subtype", "Image"); + pDict->SetAtInteger("Width", BitmapWidth); + pDict->SetAtInteger("Height", BitmapHeight); + uint8_t* dest_buf = NULL; + FX_STRSIZE dest_pitch = 0, dest_size = 0, opType = -1; + if (bpp == 1) { + int32_t reset_a = 0, reset_r = 0, reset_g = 0, reset_b = 0; + int32_t set_a = 0, set_r = 0, set_g = 0, set_b = 0; + if (!pBitmap->IsAlphaMask()) { + ArgbDecode(pBitmap->GetPaletteArgb(0), reset_a, reset_r, reset_g, + reset_b); + ArgbDecode(pBitmap->GetPaletteArgb(1), set_a, set_r, set_g, set_b); + } + if (set_a == 0 || reset_a == 0) { + pDict->SetAt("ImageMask", new CPDF_Boolean(TRUE)); + if (reset_a == 0) { + CPDF_Array* pArray = new CPDF_Array; + pArray->AddInteger(1); + pArray->AddInteger(0); + pDict->SetAt("Decode", pArray); + } + } else { + CPDF_Array* pCS = new CPDF_Array; + pCS->AddName("Indexed"); + pCS->AddName("DeviceRGB"); + pCS->AddInteger(1); + CFX_ByteString ct; + FX_CHAR* pBuf = ct.GetBuffer(6); + pBuf[0] = (FX_CHAR)reset_r; + pBuf[1] = (FX_CHAR)reset_g; + pBuf[2] = (FX_CHAR)reset_b; + pBuf[3] = (FX_CHAR)set_r; + pBuf[4] = (FX_CHAR)set_g; + pBuf[5] = (FX_CHAR)set_b; + ct.ReleaseBuffer(6); + pCS->Add(new CPDF_String(ct, TRUE)); + pDict->SetAt("ColorSpace", pCS); + } + pDict->SetAtInteger("BitsPerComponent", 1); + dest_pitch = (BitmapWidth + 7) / 8; + if ((iCompress & 0x03) == PDF_IMAGE_NO_COMPRESS) { + opType = 1; + } else { + opType = 0; + } + } else if (bpp == 8) { + int32_t iPalette = pBitmap->GetPaletteSize(); + if (iPalette > 0) { + CPDF_Array* pCS = new CPDF_Array; + m_pDocument->AddIndirectObject(pCS); + pCS->AddName("Indexed"); + pCS->AddName("DeviceRGB"); + pCS->AddInteger(iPalette - 1); + uint8_t* pColorTable = FX_Alloc2D(uint8_t, iPalette, 3); + uint8_t* ptr = pColorTable; + for (int32_t i = 0; i < iPalette; i++) { + FX_DWORD argb = pBitmap->GetPaletteArgb(i); + ptr[0] = (uint8_t)(argb >> 16); + ptr[1] = (uint8_t)(argb >> 8); + ptr[2] = (uint8_t)argb; + ptr += 3; + } + CPDF_Stream* pCTS = + new CPDF_Stream(pColorTable, iPalette * 3, new CPDF_Dictionary); + m_pDocument->AddIndirectObject(pCTS); + pCS->AddReference(m_pDocument, pCTS); + pDict->SetAtReference("ColorSpace", m_pDocument, pCS); + } else { + pDict->SetAtName("ColorSpace", "DeviceGray"); + } + pDict->SetAtInteger("BitsPerComponent", 8); + if ((iCompress & 0x03) == PDF_IMAGE_NO_COMPRESS) { + dest_pitch = BitmapWidth; + opType = 1; + } else { + opType = 0; + } + } else { + pDict->SetAtName("ColorSpace", "DeviceRGB"); + pDict->SetAtInteger("BitsPerComponent", 8); + if ((iCompress & 0x03) == PDF_IMAGE_NO_COMPRESS) { + dest_pitch = BitmapWidth * 3; + opType = 2; + } else { + opType = 0; + } + } + const CFX_DIBitmap* pMaskBitmap = NULL; + FX_BOOL bDeleteMask = FALSE; + if (pBitmap->HasAlpha()) { + pMaskBitmap = pBitmap->GetAlphaMask(); + bDeleteMask = TRUE; + } + if (!pMaskBitmap && pMask) { + FXDIB_Format maskFormat = pMask->GetFormat(); + if (maskFormat == FXDIB_1bppMask || maskFormat == FXDIB_8bppMask) { + pMaskBitmap = pMask; + } + } + if (pMaskBitmap) { + int32_t maskWidth = pMaskBitmap->GetWidth(); + int32_t maskHeight = pMaskBitmap->GetHeight(); + uint8_t* mask_buf = NULL; + FX_STRSIZE mask_size = 0; + CPDF_Dictionary* pMaskDict = new CPDF_Dictionary; + pMaskDict->SetAtName("Type", "XObject"); + pMaskDict->SetAtName("Subtype", "Image"); + pMaskDict->SetAtInteger("Width", maskWidth); + pMaskDict->SetAtInteger("Height", maskHeight); + pMaskDict->SetAtName("ColorSpace", "DeviceGray"); + pMaskDict->SetAtInteger("BitsPerComponent", 8); + if (pMaskBitmap->GetBPP() == 8 && + (iCompress & PDF_IMAGE_MASK_LOSSY_COMPRESS) != 0) { + } else if (pMaskBitmap->GetFormat() == FXDIB_1bppMask) { + } else { + mask_buf = FX_Alloc2D(uint8_t, maskHeight, maskWidth); + mask_size = maskHeight * maskWidth; // Safe since checked alloc returned. + for (int32_t a = 0; a < maskHeight; a++) { + FXSYS_memcpy(mask_buf + a * maskWidth, pMaskBitmap->GetScanline(a), + maskWidth); + } + } + pMaskDict->SetAtInteger("Length", mask_size); + + CPDF_Stream* pMaskStream = new CPDF_Stream(mask_buf, mask_size, pMaskDict); + m_pDocument->AddIndirectObject(pMaskStream); + pDict->SetAtReference("SMask", m_pDocument, pMaskStream); + if (bDeleteMask) { + delete pMaskBitmap; + } + } + FX_BOOL bStream = pFileWrite && pFileRead; + if (opType == 0) { + if (iCompress & PDF_IMAGE_LOSSLESS_COMPRESS) { + } else { + if (pBitmap->GetBPP() == 1) { + } else if (pBitmap->GetBPP() >= 8 && pBitmap->GetPalette()) { + CFX_DIBitmap* pNewBitmap = new CFX_DIBitmap(); + pNewBitmap->Copy(pBitmap); + pNewBitmap->ConvertFormat(FXDIB_Rgb); + SetImage(pNewBitmap, iCompress, pFileWrite, pFileRead); + if (pDict) { + pDict->Release(); + pDict = NULL; + } + FX_Free(dest_buf); + dest_buf = NULL; + dest_size = 0; + delete pNewBitmap; + return; + } + } + if (bStream) { + pFileWrite->WriteBlock(dest_buf, dest_size); + FX_Free(dest_buf); + dest_buf = NULL; + } + } else if (opType == 1) { + if (!bStream) { + dest_buf = FX_Alloc2D(uint8_t, dest_pitch, BitmapHeight); + dest_size = + dest_pitch * BitmapHeight; // Safe since checked alloc returned. + } + uint8_t* pDest = dest_buf; + for (int32_t i = 0; i < BitmapHeight; i++) { + if (!bStream) { + FXSYS_memcpy(pDest, src_buf, dest_pitch); + pDest += dest_pitch; + } else { + pFileWrite->WriteBlock(src_buf, dest_pitch); + } + src_buf += src_pitch; + } + } else if (opType == 2) { + if (!bStream) { + dest_buf = FX_Alloc2D(uint8_t, dest_pitch, BitmapHeight); + dest_size = + dest_pitch * BitmapHeight; // Safe since checked alloc returned. + } else { + dest_buf = FX_Alloc(uint8_t, dest_pitch); + } + uint8_t* pDest = dest_buf; + int32_t src_offset = 0; + int32_t dest_offset = 0; + for (int32_t row = 0; row < BitmapHeight; row++) { + src_offset = row * src_pitch; + for (int32_t column = 0; column < BitmapWidth; column++) { + FX_FLOAT alpha = 1; + pDest[dest_offset] = (uint8_t)(src_buf[src_offset + 2] * alpha); + pDest[dest_offset + 1] = (uint8_t)(src_buf[src_offset + 1] * alpha); + pDest[dest_offset + 2] = (uint8_t)(src_buf[src_offset] * alpha); + dest_offset += 3; + src_offset += bpp == 24 ? 3 : 4; + } + if (bStream) { + pFileWrite->WriteBlock(pDest, dest_pitch); + pDest = dest_buf; + } else { + pDest += dest_pitch; + } + dest_offset = 0; + } + if (bStream) { + FX_Free(dest_buf); + dest_buf = NULL; + } + } + if (!m_pStream) { + m_pStream = new CPDF_Stream(NULL, 0, NULL); + } + if (!bStream) { + m_pStream->InitStream(dest_buf, dest_size, pDict); + } else { + pFileWrite->Flush(); + m_pStream->InitStreamFromFile(pFileRead, pDict); + } + m_bIsMask = pBitmap->IsAlphaMask(); + m_Width = BitmapWidth; + m_Height = BitmapHeight; + FX_Free(dest_buf); +} + +void CPDF_Image::ResetCache(CPDF_Page* pPage, const CFX_DIBitmap* pBitmap) { + pPage->GetRenderCache()->ResetBitmap(m_pStream, pBitmap); +} + +CFX_DIBSource* CPDF_Image::LoadDIBSource(CFX_DIBSource** ppMask, + FX_DWORD* pMatteColor, + FX_BOOL bStdCS, + FX_DWORD GroupFamily, + FX_BOOL bLoadMask) const { + std::unique_ptr<CPDF_DIBSource> source(new CPDF_DIBSource); + if (source->Load(m_pDocument, m_pStream, + reinterpret_cast<CPDF_DIBSource**>(ppMask), pMatteColor, + nullptr, nullptr, bStdCS, GroupFamily, bLoadMask)) { + return source.release(); + } + return nullptr; +} + +CFX_DIBSource* CPDF_Image::DetachBitmap() { + CFX_DIBSource* pBitmap = m_pDIBSource; + m_pDIBSource = nullptr; + return pBitmap; +} + +CFX_DIBSource* CPDF_Image::DetachMask() { + CFX_DIBSource* pBitmap = m_pMask; + m_pMask = nullptr; + return pBitmap; +} + +FX_BOOL CPDF_Image::StartLoadDIBSource(CPDF_Dictionary* pFormResource, + CPDF_Dictionary* pPageResource, + FX_BOOL bStdCS, + FX_DWORD GroupFamily, + FX_BOOL bLoadMask) { + std::unique_ptr<CPDF_DIBSource> source(new CPDF_DIBSource); + int ret = + source->StartLoadDIBSource(m_pDocument, m_pStream, TRUE, pFormResource, + pPageResource, bStdCS, GroupFamily, bLoadMask); + if (ret == 2) { + m_pDIBSource = source.release(); + return TRUE; + } + if (!ret) { + m_pDIBSource = nullptr; + return FALSE; + } + m_pMask = source->DetachMask(); + m_MatteColor = source->GetMatteColor(); + m_pDIBSource = source.release(); + return FALSE; +} + +FX_BOOL CPDF_Image::Continue(IFX_Pause* pPause) { + CPDF_DIBSource* pSource = static_cast<CPDF_DIBSource*>(m_pDIBSource); + int ret = pSource->ContinueLoadDIBSource(pPause); + if (ret == 2) { + return TRUE; + } + if (!ret) { + delete m_pDIBSource; + m_pDIBSource = nullptr; + return FALSE; + } + m_pMask = pSource->DetachMask(); + m_MatteColor = pSource->GetMatteColor(); + return FALSE; +} diff --git a/core/fpdfapi/fpdf_page/cpdf_imageobject.cpp b/core/fpdfapi/fpdf_page/cpdf_imageobject.cpp index f339935ecb..6203d56273 100644 --- a/core/fpdfapi/fpdf_page/cpdf_imageobject.cpp +++ b/core/fpdfapi/fpdf_page/cpdf_imageobject.cpp @@ -6,6 +6,7 @@ #include "core/fpdfapi/fpdf_page/include/cpdf_imageobject.h" +#include "core/fpdfapi/fpdf_page/include/cpdf_image.h" #include "core/fpdfapi/fpdf_page/pageint.h" #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" diff --git a/core/fpdfapi/fpdf_page/cpdf_meshstream.cpp b/core/fpdfapi/fpdf_page/cpdf_meshstream.cpp new file mode 100644 index 0000000000..1d983d2b25 --- /dev/null +++ b/core/fpdfapi/fpdf_page/cpdf_meshstream.cpp @@ -0,0 +1,119 @@ +// 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 + +#include "core/fpdfapi/fpdf_page/cpdf_meshstream.h" + +#include "core/fpdfapi/fpdf_page/include/cpdf_colorspace.h" +#include "core/fpdfapi/fpdf_page/pageint.h" +#include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" + +FX_BOOL CPDF_MeshStream::Load(CPDF_Stream* pShadingStream, + CPDF_Function** pFuncs, + int nFuncs, + CPDF_ColorSpace* pCS) { + m_Stream.LoadAllData(pShadingStream); + m_BitStream.Init(m_Stream.GetData(), m_Stream.GetSize()); + m_pFuncs = pFuncs; + m_nFuncs = nFuncs; + m_pCS = pCS; + CPDF_Dictionary* pDict = pShadingStream->GetDict(); + m_nCoordBits = pDict->GetIntegerBy("BitsPerCoordinate"); + m_nCompBits = pDict->GetIntegerBy("BitsPerComponent"); + m_nFlagBits = pDict->GetIntegerBy("BitsPerFlag"); + if (!m_nCoordBits || !m_nCompBits) + return FALSE; + + FX_DWORD nComps = pCS->CountComponents(); + if (nComps > 8) + return FALSE; + + m_nComps = nFuncs ? 1 : nComps; + if (((int)m_nComps < 0) || m_nComps > 8) + return FALSE; + + m_CoordMax = m_nCoordBits == 32 ? -1 : (1 << m_nCoordBits) - 1; + m_CompMax = (1 << m_nCompBits) - 1; + CPDF_Array* pDecode = pDict->GetArrayBy("Decode"); + if (!pDecode || pDecode->GetCount() != 4 + m_nComps * 2) + return FALSE; + + m_xmin = pDecode->GetNumberAt(0); + m_xmax = pDecode->GetNumberAt(1); + m_ymin = pDecode->GetNumberAt(2); + m_ymax = pDecode->GetNumberAt(3); + for (FX_DWORD i = 0; i < m_nComps; i++) { + m_ColorMin[i] = pDecode->GetNumberAt(i * 2 + 4); + m_ColorMax[i] = pDecode->GetNumberAt(i * 2 + 5); + } + return TRUE; +} + +FX_DWORD CPDF_MeshStream::GetFlag() { + return m_BitStream.GetBits(m_nFlagBits) & 0x03; +} + +void CPDF_MeshStream::GetCoords(FX_FLOAT& x, FX_FLOAT& y) { + if (m_nCoordBits == 32) { + x = m_xmin + (FX_FLOAT)(m_BitStream.GetBits(m_nCoordBits) * + (m_xmax - m_xmin) / (double)m_CoordMax); + y = m_ymin + (FX_FLOAT)(m_BitStream.GetBits(m_nCoordBits) * + (m_ymax - m_ymin) / (double)m_CoordMax); + } else { + x = m_xmin + + m_BitStream.GetBits(m_nCoordBits) * (m_xmax - m_xmin) / m_CoordMax; + y = m_ymin + + m_BitStream.GetBits(m_nCoordBits) * (m_ymax - m_ymin) / m_CoordMax; + } +} + +void CPDF_MeshStream::GetColor(FX_FLOAT& r, FX_FLOAT& g, FX_FLOAT& b) { + FX_DWORD i; + FX_FLOAT color_value[8]; + for (i = 0; i < m_nComps; i++) { + color_value[i] = m_ColorMin[i] + + m_BitStream.GetBits(m_nCompBits) * + (m_ColorMax[i] - m_ColorMin[i]) / m_CompMax; + } + if (m_nFuncs) { + static const int kMaxResults = 8; + FX_FLOAT result[kMaxResults]; + int nResults; + FXSYS_memset(result, 0, sizeof(result)); + for (FX_DWORD i = 0; i < m_nFuncs; i++) { + if (m_pFuncs[i] && m_pFuncs[i]->CountOutputs() <= kMaxResults) { + m_pFuncs[i]->Call(color_value, 1, result, nResults); + } + } + m_pCS->GetRGB(result, r, g, b); + } else { + m_pCS->GetRGB(color_value, r, g, b); + } +} + +FX_DWORD CPDF_MeshStream::GetVertex(CPDF_MeshVertex& vertex, + CFX_Matrix* pObject2Bitmap) { + FX_DWORD flag = GetFlag(); + GetCoords(vertex.x, vertex.y); + pObject2Bitmap->Transform(vertex.x, vertex.y); + GetColor(vertex.r, vertex.g, vertex.b); + m_BitStream.ByteAlign(); + return flag; +} + +FX_BOOL CPDF_MeshStream::GetVertexRow(CPDF_MeshVertex* vertex, + int count, + CFX_Matrix* pObject2Bitmap) { + for (int i = 0; i < count; i++) { + if (m_BitStream.IsEOF()) + return FALSE; + + GetCoords(vertex[i].x, vertex[i].y); + pObject2Bitmap->Transform(vertex[i].x, vertex[i].y); + GetColor(vertex[i].r, vertex[i].g, vertex[i].b); + m_BitStream.ByteAlign(); + } + return TRUE; +} diff --git a/core/fpdfapi/fpdf_page/cpdf_meshstream.h b/core/fpdfapi/fpdf_page/cpdf_meshstream.h new file mode 100644 index 0000000000..4934c0b133 --- /dev/null +++ b/core/fpdfapi/fpdf_page/cpdf_meshstream.h @@ -0,0 +1,63 @@ +// 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_FPDFAPI_FPDF_PAGE_CPDF_MESHSTREAM_H_ +#define CORE_FPDFAPI_FPDF_PAGE_CPDF_MESHSTREAM_H_ + +#include "core/fpdfapi/fpdf_parser/include/cpdf_stream_acc.h" +#include "core/fxcrt/include/fx_basic.h" +#include "core/fxcrt/include/fx_system.h" + +struct CPDF_MeshVertex { + FX_FLOAT x; + FX_FLOAT y; + FX_FLOAT r; + FX_FLOAT g; + FX_FLOAT b; +}; + +class CFX_Matrix; +class CPDF_ColorSpace; +class CPDF_Function; +class CPDF_Stream; + +class CPDF_MeshStream { + public: + FX_BOOL Load(CPDF_Stream* pShadingStream, + CPDF_Function** pFuncs, + int nFuncs, + CPDF_ColorSpace* pCS); + + FX_DWORD GetFlag(); + + void GetCoords(FX_FLOAT& x, FX_FLOAT& y); + void GetColor(FX_FLOAT& r, FX_FLOAT& g, FX_FLOAT& b); + + FX_DWORD GetVertex(CPDF_MeshVertex& vertex, CFX_Matrix* pObject2Bitmap); + FX_BOOL GetVertexRow(CPDF_MeshVertex* vertex, + int count, + CFX_Matrix* pObject2Bitmap); + + CPDF_Function** m_pFuncs; + CPDF_ColorSpace* m_pCS; + FX_DWORD m_nFuncs; + FX_DWORD m_nCoordBits; + FX_DWORD m_nCompBits; + FX_DWORD m_nFlagBits; + FX_DWORD m_nComps; + FX_DWORD m_CoordMax; + FX_DWORD m_CompMax; + FX_FLOAT m_xmin; + FX_FLOAT m_xmax; + FX_FLOAT m_ymin; + FX_FLOAT m_ymax; + FX_FLOAT m_ColorMin[8]; + FX_FLOAT m_ColorMax[8]; + CPDF_StreamAcc m_Stream; + CFX_BitStream m_BitStream; +}; + +#endif // CORE_FPDFAPI_FPDF_PAGE_CPDF_MESHSTREAM_H_ diff --git a/core/fpdfapi/fpdf_page/cpdf_page.cpp b/core/fpdfapi/fpdf_page/cpdf_page.cpp index 2c01cd5573..6fd9b0a9b3 100644 --- a/core/fpdfapi/fpdf_page/cpdf_page.cpp +++ b/core/fpdfapi/fpdf_page/cpdf_page.cpp @@ -9,6 +9,7 @@ #include "core/fpdfapi/fpdf_page/include/cpdf_pageobject.h" #include "core/fpdfapi/fpdf_page/pageint.h" #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" +#include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" #include "core/fpdfapi/fpdf_parser/include/cpdf_object.h" #include "core/fpdfapi/include/cpdf_modulemgr.h" #include "core/fpdfapi/ipdf_rendermodule.h" diff --git a/core/fpdfapi/fpdf_page/cpdf_pageobjectholder.cpp b/core/fpdfapi/fpdf_page/cpdf_pageobjectholder.cpp index fa6347e5bc..cd6eca5adc 100644 --- a/core/fpdfapi/fpdf_page/cpdf_pageobjectholder.cpp +++ b/core/fpdfapi/fpdf_page/cpdf_pageobjectholder.cpp @@ -8,6 +8,7 @@ #include "core/fpdfapi/fpdf_page/include/cpdf_pageobject.h" #include "core/fpdfapi/fpdf_page/pageint.h" +#include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" CPDF_PageObjectHolder::CPDF_PageObjectHolder() : m_pFormDict(nullptr), diff --git a/core/fpdfapi/fpdf_page/cpdf_pattern.cpp b/core/fpdfapi/fpdf_page/cpdf_pattern.cpp new file mode 100644 index 0000000000..fc9d10e7ab --- /dev/null +++ b/core/fpdfapi/fpdf_page/cpdf_pattern.cpp @@ -0,0 +1,21 @@ +// 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 + +#include "core/fpdfapi/fpdf_page/cpdf_pattern.h" + +CPDF_Pattern::CPDF_Pattern(PatternType type, + CPDF_Document* pDoc, + CPDF_Object* pObj, + const CFX_Matrix* pParentMatrix) + : m_PatternType(type), + m_pDocument(pDoc), + m_pPatternObj(pObj), + m_bForceClear(FALSE) { + if (pParentMatrix) + m_ParentMatrix = *pParentMatrix; +} + +CPDF_Pattern::~CPDF_Pattern() {} diff --git a/core/fpdfapi/fpdf_page/cpdf_pattern.h b/core/fpdfapi/fpdf_page/cpdf_pattern.h new file mode 100644 index 0000000000..7f9a33bac9 --- /dev/null +++ b/core/fpdfapi/fpdf_page/cpdf_pattern.h @@ -0,0 +1,39 @@ +// 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_FPDFAPI_FPDF_PAGE_CPDF_PATTERN_H_ +#define CORE_FPDFAPI_FPDF_PAGE_CPDF_PATTERN_H_ + +#include "core/fxcrt/include/fx_coordinates.h" +#include "core/fxcrt/include/fx_system.h" + +class CPDF_Document; +class CPDF_Object; + +class CPDF_Pattern { + public: + enum PatternType { TILING = 1, SHADING }; + + virtual ~CPDF_Pattern(); + + void SetForceClear(FX_BOOL bForceClear) { m_bForceClear = bForceClear; } + + const PatternType m_PatternType; + CPDF_Document* const m_pDocument; + CPDF_Object* const m_pPatternObj; + CFX_Matrix m_Pattern2Form; + CFX_Matrix m_ParentMatrix; + + protected: + CPDF_Pattern(PatternType type, + CPDF_Document* pDoc, + CPDF_Object* pObj, + const CFX_Matrix* pParentMatrix); + + FX_BOOL m_bForceClear; +}; + +#endif // CORE_FPDFAPI_FPDF_PAGE_CPDF_PATTERN_H_ diff --git a/core/fpdfapi/fpdf_page/cpdf_shadingobject.cpp b/core/fpdfapi/fpdf_page/cpdf_shadingobject.cpp index 16287fe692..56dd6d4af4 100644 --- a/core/fpdfapi/fpdf_page/cpdf_shadingobject.cpp +++ b/core/fpdfapi/fpdf_page/cpdf_shadingobject.cpp @@ -6,6 +6,7 @@ #include "core/fpdfapi/fpdf_page/include/cpdf_shadingobject.h" +#include "core/fpdfapi/fpdf_page/cpdf_shadingpattern.h" #include "core/fpdfapi/fpdf_page/pageint.h" #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" diff --git a/core/fpdfapi/fpdf_page/cpdf_shadingpattern.cpp b/core/fpdfapi/fpdf_page/cpdf_shadingpattern.cpp new file mode 100644 index 0000000000..6ccf1403f9 --- /dev/null +++ b/core/fpdfapi/fpdf_page/cpdf_shadingpattern.cpp @@ -0,0 +1,101 @@ +// 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 + +#include "core/fpdfapi/fpdf_page/cpdf_shadingpattern.h" + +#include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" +#include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" +#include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" +#include "core/fpdfapi/fpdf_parser/include/cpdf_object.h" + +namespace { + +ShadingType ToShadingType(int type) { + return (type > static_cast<int>(kInvalidShading) && + type < static_cast<int>(kMaxShading)) + ? static_cast<ShadingType>(type) + : kInvalidShading; +} + +} // namespace + +CPDF_ShadingPattern::CPDF_ShadingPattern(CPDF_Document* pDoc, + CPDF_Object* pPatternObj, + FX_BOOL bShading, + const CFX_Matrix* parentMatrix) + : CPDF_Pattern(SHADING, + pDoc, + bShading ? nullptr : pPatternObj, + parentMatrix), + m_ShadingType(kInvalidShading), + m_bShadingObj(bShading), + m_pShadingObj(pPatternObj), + m_pCS(nullptr), + m_pCountedCS(nullptr), + m_nFuncs(0) { + if (!bShading) { + CPDF_Dictionary* pDict = m_pPatternObj->GetDict(); + m_Pattern2Form = pDict->GetMatrixBy("Matrix"); + m_pShadingObj = pDict->GetElementValue("Shading"); + if (parentMatrix) + m_Pattern2Form.Concat(*parentMatrix); + } + for (int i = 0; i < FX_ArraySize(m_pFunctions); ++i) + m_pFunctions[i] = nullptr; +} + +CPDF_ShadingPattern::~CPDF_ShadingPattern() { + for (int i = 0; i < m_nFuncs; ++i) + delete m_pFunctions[i]; + + CPDF_ColorSpace* pCS = m_pCountedCS ? m_pCountedCS->get() : nullptr; + if (pCS && m_pDocument) + m_pDocument->GetPageData()->ReleaseColorSpace(pCS->GetArray()); +} + +FX_BOOL CPDF_ShadingPattern::Load() { + if (m_ShadingType != kInvalidShading) + return TRUE; + + CPDF_Dictionary* pShadingDict = + m_pShadingObj ? m_pShadingObj->GetDict() : nullptr; + if (!pShadingDict) + return FALSE; + + if (m_nFuncs) { + for (int i = 0; i < m_nFuncs; i++) + delete m_pFunctions[i]; + m_nFuncs = 0; + } + CPDF_Object* pFunc = pShadingDict->GetElementValue("Function"); + if (pFunc) { + if (CPDF_Array* pArray = pFunc->AsArray()) { + m_nFuncs = std::min<int>(pArray->GetCount(), 4); + + for (int i = 0; i < m_nFuncs; i++) + m_pFunctions[i] = CPDF_Function::Load(pArray->GetElementValue(i)); + } else { + m_pFunctions[0] = CPDF_Function::Load(pFunc); + m_nFuncs = 1; + } + } + CPDF_Object* pCSObj = pShadingDict->GetElementValue("ColorSpace"); + if (!pCSObj) + return FALSE; + + CPDF_DocPageData* pDocPageData = m_pDocument->GetPageData(); + m_pCS = pDocPageData->GetColorSpace(pCSObj, nullptr); + if (m_pCS) + m_pCountedCS = pDocPageData->FindColorSpacePtr(m_pCS->GetArray()); + + m_ShadingType = ToShadingType(pShadingDict->GetIntegerBy("ShadingType")); + + // We expect to have a stream if our shading type is a mesh. + if (IsMeshShading() && !ToStream(m_pShadingObj)) + return FALSE; + + return TRUE; +} diff --git a/core/fpdfapi/fpdf_page/cpdf_shadingpattern.h b/core/fpdfapi/fpdf_page/cpdf_shadingpattern.h new file mode 100644 index 0000000000..55a249a1bc --- /dev/null +++ b/core/fpdfapi/fpdf_page/cpdf_shadingpattern.h @@ -0,0 +1,62 @@ +// 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_FPDFAPI_FPDF_PAGE_CPDF_SHADINGPATTERN_H_ +#define CORE_FPDFAPI_FPDF_PAGE_CPDF_SHADINGPATTERN_H_ + +#include "core/fpdfapi/fpdf_page/cpdf_countedobject.h" +#include "core/fpdfapi/fpdf_page/cpdf_pattern.h" +#include "core/fpdfapi/fpdf_page/pageint.h" +#include "core/fxcrt/include/fx_system.h" + +typedef enum { + kInvalidShading = 0, + kFunctionBasedShading = 1, + kAxialShading = 2, + kRadialShading = 3, + kFreeFormGouraudTriangleMeshShading = 4, + kLatticeFormGouraudTriangleMeshShading = 5, + kCoonsPatchMeshShading = 6, + kTensorProductPatchMeshShading = 7, + kMaxShading = 8 +} ShadingType; + +class CFX_Matrix; +class CPDF_ColorSpace; +class CPDF_Document; +class CPDF_Object; + +class CPDF_ShadingPattern : public CPDF_Pattern { + public: + CPDF_ShadingPattern(CPDF_Document* pDoc, + CPDF_Object* pPatternObj, + FX_BOOL bShading, + const CFX_Matrix* parentMatrix); + + ~CPDF_ShadingPattern() override; + + bool IsMeshShading() const { + return m_ShadingType == kFreeFormGouraudTriangleMeshShading || + m_ShadingType == kLatticeFormGouraudTriangleMeshShading || + m_ShadingType == kCoonsPatchMeshShading || + m_ShadingType == kTensorProductPatchMeshShading; + } + FX_BOOL Load(); + + ShadingType m_ShadingType; + FX_BOOL m_bShadingObj; + CPDF_Object* m_pShadingObj; + + // Still keep |m_pCS| as some CPDF_ColorSpace (name object) are not managed + // as counted objects. Refer to CPDF_DocPageData::GetColorSpace. + CPDF_ColorSpace* m_pCS; + + CPDF_CountedColorSpace* m_pCountedCS; + CPDF_Function* m_pFunctions[4]; + int m_nFuncs; +}; + +#endif // CORE_FPDFAPI_FPDF_PAGE_CPDF_SHADINGPATTERN_H_ diff --git a/core/fpdfapi/fpdf_page/cpdf_textobject.cpp b/core/fpdfapi/fpdf_page/cpdf_textobject.cpp index ef37b30631..cf588dcf3d 100644 --- a/core/fpdfapi/fpdf_page/cpdf_textobject.cpp +++ b/core/fpdfapi/fpdf_page/cpdf_textobject.cpp @@ -6,6 +6,9 @@ #include "core/fpdfapi/fpdf_page/include/cpdf_textobject.h" +#include "core/fpdfapi/fpdf_font/cpdf_cidfont.h" +#include "core/fpdfapi/fpdf_font/include/cpdf_font.h" + CPDF_TextObject::CPDF_TextObject() : m_PosX(0), m_PosY(0), diff --git a/core/fpdfapi/fpdf_page/cpdf_textstate.cpp b/core/fpdfapi/fpdf_page/cpdf_textstate.cpp index f56d58ed0a..5269cae44b 100644 --- a/core/fpdfapi/fpdf_page/cpdf_textstate.cpp +++ b/core/fpdfapi/fpdf_page/cpdf_textstate.cpp @@ -4,6 +4,7 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +#include "core/fpdfapi/fpdf_font/include/cpdf_font.h" #include "core/fpdfapi/fpdf_page/cpdf_textstate.h" #include "core/fpdfapi/fpdf_page/pageint.h" #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" diff --git a/core/fpdfapi/fpdf_page/cpdf_textstatedata.cpp b/core/fpdfapi/fpdf_page/cpdf_textstatedata.cpp index af65fb530a..5cd6061542 100644 --- a/core/fpdfapi/fpdf_page/cpdf_textstatedata.cpp +++ b/core/fpdfapi/fpdf_page/cpdf_textstatedata.cpp @@ -6,9 +6,9 @@ #include "core/fpdfapi/fpdf_page/include/cpdf_textstatedata.h" +#include "core/fpdfapi/fpdf_font/include/cpdf_font.h" #include "core/fpdfapi/fpdf_page/pageint.h" #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" -#include "core/include/fpdfapi/fpdf_resource.h" CPDF_TextStateData::CPDF_TextStateData() : m_pFont(nullptr), diff --git a/core/fpdfapi/fpdf_page/cpdf_tilingpattern.cpp b/core/fpdfapi/fpdf_page/cpdf_tilingpattern.cpp new file mode 100644 index 0000000000..f49ad2b8fb --- /dev/null +++ b/core/fpdfapi/fpdf_page/cpdf_tilingpattern.cpp @@ -0,0 +1,51 @@ +// 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 + +#include "core/fpdfapi/fpdf_page/cpdf_tilingpattern.h" + +#include "core/fpdfapi/fpdf_page/include/cpdf_form.h" +#include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" +#include "core/fpdfapi/fpdf_parser/include/cpdf_object.h" +#include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h" + +CPDF_TilingPattern::CPDF_TilingPattern(CPDF_Document* pDoc, + CPDF_Object* pPatternObj, + const CFX_Matrix* parentMatrix) + : CPDF_Pattern(TILING, pDoc, pPatternObj, parentMatrix) { + CPDF_Dictionary* pDict = m_pPatternObj->GetDict(); + m_Pattern2Form = pDict->GetMatrixBy("Matrix"); + m_bColored = pDict->GetIntegerBy("PaintType") == 1; + if (parentMatrix) + m_Pattern2Form.Concat(*parentMatrix); + + m_pForm = nullptr; +} + +CPDF_TilingPattern::~CPDF_TilingPattern() { + delete m_pForm; +} + +FX_BOOL CPDF_TilingPattern::Load() { + if (m_pForm) + return TRUE; + + CPDF_Dictionary* pDict = m_pPatternObj->GetDict(); + if (!pDict) + return FALSE; + + m_bColored = pDict->GetIntegerBy("PaintType") == 1; + m_XStep = (FX_FLOAT)FXSYS_fabs(pDict->GetNumberBy("XStep")); + m_YStep = (FX_FLOAT)FXSYS_fabs(pDict->GetNumberBy("YStep")); + + CPDF_Stream* pStream = m_pPatternObj->AsStream(); + if (!pStream) + return FALSE; + + m_pForm = new CPDF_Form(m_pDocument, NULL, pStream); + m_pForm->ParseContent(NULL, &m_ParentMatrix, NULL, NULL); + m_BBox = pDict->GetRectBy("BBox"); + return TRUE; +} diff --git a/core/fpdfapi/fpdf_page/cpdf_tilingpattern.h b/core/fpdfapi/fpdf_page/cpdf_tilingpattern.h new file mode 100644 index 0000000000..44446eac91 --- /dev/null +++ b/core/fpdfapi/fpdf_page/cpdf_tilingpattern.h @@ -0,0 +1,34 @@ +// 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_FPDFAPI_FPDF_PAGE_CPDF_TILINGPATTERN_H_ +#define CORE_FPDFAPI_FPDF_PAGE_CPDF_TILINGPATTERN_H_ + +#include "core/fpdfapi/fpdf_page/cpdf_pattern.h" +#include "core/fxcrt/include/fx_coordinates.h" +#include "core/fxcrt/include/fx_system.h" + +class CPDF_Document; +class CPDF_Form; +class CPDF_Object; + +class CPDF_TilingPattern : public CPDF_Pattern { + public: + CPDF_TilingPattern(CPDF_Document* pDoc, + CPDF_Object* pPatternObj, + const CFX_Matrix* parentMatrix); + ~CPDF_TilingPattern() override; + + FX_BOOL Load(); + + FX_BOOL m_bColored; + CFX_FloatRect m_BBox; + FX_FLOAT m_XStep; + FX_FLOAT m_YStep; + CPDF_Form* m_pForm; +}; + +#endif // CORE_FPDFAPI_FPDF_PAGE_CPDF_TILINGPATTERN_H_ diff --git a/core/fpdfapi/fpdf_page/fpdf_page_colors.cpp b/core/fpdfapi/fpdf_page/fpdf_page_colors.cpp index d4b69f2d3f..cc40976f29 100644 --- a/core/fpdfapi/fpdf_page/fpdf_page_colors.cpp +++ b/core/fpdfapi/fpdf_page/fpdf_page_colors.cpp @@ -13,12 +13,24 @@ #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" #include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" +#include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h" +#include "core/fpdfapi/fpdf_parser/include/cpdf_stream_acc.h" #include "core/fpdfapi/fpdf_parser/include/cpdf_string.h" #include "core/fpdfapi/include/cpdf_modulemgr.h" #include "core/include/fxcodec/fx_codec.h" namespace { +FX_DWORD ComponentsForFamily(int family) { + if (family == PDFCS_DEVICERGB) + return 3; + if (family == PDFCS_DEVICEGRAY) + return 1; + return 4; +} + +} // namespace + void sRGB_to_AdobeCMYK(FX_FLOAT R, FX_FLOAT G, FX_FLOAT B, @@ -38,14 +50,6 @@ void sRGB_to_AdobeCMYK(FX_FLOAT R, } } -FX_DWORD ComponentsForFamily(int family) { - if (family == PDFCS_DEVICERGB) - return 3; - if (family == PDFCS_DEVICEGRAY) - return 1; - return 4; -} - void ReverseRGB(uint8_t* pDestBuf, const uint8_t* pSrcBuf, int pixels) { if (pDestBuf == pSrcBuf) { for (int i = 0; i < pixels; i++) { @@ -64,8 +68,6 @@ void ReverseRGB(uint8_t* pDestBuf, const uint8_t* pSrcBuf, int pixels) { } } -} // namespace - CPDF_DeviceCS::CPDF_DeviceCS(CPDF_Document* pDoc, int family) : CPDF_ColorSpace(pDoc, family, ComponentsForFamily(family)) {} @@ -213,420 +215,7 @@ void CPDF_DeviceCS::TranslateImageLine(uint8_t* pDestBuf, } } } -const uint8_t g_sRGBSamples1[] = { - 0, 3, 6, 10, 13, 15, 18, 20, 22, 23, 25, 27, 28, 30, 31, - 32, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, - 48, 49, 49, 50, 51, 52, 53, 53, 54, 55, 56, 56, 57, 58, 58, - 59, 60, 61, 61, 62, 62, 63, 64, 64, 65, 66, 66, 67, 67, 68, - 68, 69, 70, 70, 71, 71, 72, 72, 73, 73, 74, 74, 75, 76, 76, - 77, 77, 78, 78, 79, 79, 79, 80, 80, 81, 81, 82, 82, 83, 83, - 84, 84, 85, 85, 85, 86, 86, 87, 87, 88, 88, 88, 89, 89, 90, - 90, 91, 91, 91, 92, 92, 93, 93, 93, 94, 94, 95, 95, 95, 96, - 96, 97, 97, 97, 98, 98, 98, 99, 99, 99, 100, 100, 101, 101, 101, - 102, 102, 102, 103, 103, 103, 104, 104, 104, 105, 105, 106, 106, 106, 107, - 107, 107, 108, 108, 108, 109, 109, 109, 110, 110, 110, 110, 111, 111, 111, - 112, 112, 112, 113, 113, 113, 114, 114, 114, 115, 115, 115, 115, 116, 116, - 116, 117, 117, 117, 118, 118, 118, 118, 119, 119, 119, 120, -}; -const uint8_t g_sRGBSamples2[] = { - 120, 121, 122, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, - 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 148, 149, - 150, 151, 152, 153, 154, 155, 155, 156, 157, 158, 159, 159, 160, 161, 162, - 163, 163, 164, 165, 166, 167, 167, 168, 169, 170, 170, 171, 172, 173, 173, - 174, 175, 175, 176, 177, 178, 178, 179, 180, 180, 181, 182, 182, 183, 184, - 185, 185, 186, 187, 187, 188, 189, 189, 190, 190, 191, 192, 192, 193, 194, - 194, 195, 196, 196, 197, 197, 198, 199, 199, 200, 200, 201, 202, 202, 203, - 203, 204, 205, 205, 206, 206, 207, 208, 208, 209, 209, 210, 210, 211, 212, - 212, 213, 213, 214, 214, 215, 215, 216, 216, 217, 218, 218, 219, 219, 220, - 220, 221, 221, 222, 222, 223, 223, 224, 224, 225, 226, 226, 227, 227, 228, - 228, 229, 229, 230, 230, 231, 231, 232, 232, 233, 233, 234, 234, 235, 235, - 236, 236, 237, 237, 238, 238, 238, 239, 239, 240, 240, 241, 241, 242, 242, - 243, 243, 244, 244, 245, 245, 246, 246, 246, 247, 247, 248, 248, 249, 249, - 250, 250, 251, 251, 251, 252, 252, 253, 253, 254, 254, 255, 255, -}; - -static FX_FLOAT RGB_Conversion(FX_FLOAT colorComponent) { - if (colorComponent > 1) { - colorComponent = 1; - } - if (colorComponent < 0) { - colorComponent = 0; - } - int scale = (int)(colorComponent * 1023); - if (scale < 0) { - scale = 0; - } - if (scale < 192) { - colorComponent = (g_sRGBSamples1[scale] / 255.0f); - } else { - colorComponent = (g_sRGBSamples2[scale / 4 - 48] / 255.0f); - } - return colorComponent; -} - -static void XYZ_to_sRGB(FX_FLOAT X, - FX_FLOAT Y, - FX_FLOAT Z, - FX_FLOAT& R, - FX_FLOAT& G, - FX_FLOAT& B) { - FX_FLOAT R1 = 3.2410f * X - 1.5374f * Y - 0.4986f * Z; - FX_FLOAT G1 = -0.9692f * X + 1.8760f * Y + 0.0416f * Z; - FX_FLOAT B1 = 0.0556f * X - 0.2040f * Y + 1.0570f * Z; - - R = RGB_Conversion(R1); - G = RGB_Conversion(G1); - B = RGB_Conversion(B1); -} - -static void XYZ_to_sRGB_WhitePoint(FX_FLOAT X, - FX_FLOAT Y, - FX_FLOAT Z, - FX_FLOAT& R, - FX_FLOAT& G, - FX_FLOAT& B, - FX_FLOAT Xw, - FX_FLOAT Yw, - FX_FLOAT Zw) { - // The following RGB_xyz is based on - // sRGB value {Rx,Ry}={0.64, 0.33}, {Gx,Gy}={0.30, 0.60}, {Bx,By}={0.15, 0.06} - - FX_FLOAT Rx = 0.64f, Ry = 0.33f; - FX_FLOAT Gx = 0.30f, Gy = 0.60f; - FX_FLOAT Bx = 0.15f, By = 0.06f; - CFX_Matrix_3by3 RGB_xyz(Rx, Gx, Bx, Ry, Gy, By, 1 - Rx - Ry, 1 - Gx - Gy, - 1 - Bx - By); - CFX_Vector_3by1 whitePoint(Xw, Yw, Zw); - CFX_Vector_3by1 XYZ(X, Y, Z); - - CFX_Vector_3by1 RGB_Sum_XYZ = RGB_xyz.Inverse().TransformVector(whitePoint); - CFX_Matrix_3by3 RGB_SUM_XYZ_DIAG(RGB_Sum_XYZ.a, 0, 0, 0, RGB_Sum_XYZ.b, 0, 0, - 0, RGB_Sum_XYZ.c); - CFX_Matrix_3by3 M = RGB_xyz.Multiply(RGB_SUM_XYZ_DIAG); - CFX_Vector_3by1 RGB = M.Inverse().TransformVector(XYZ); - - R = RGB_Conversion(RGB.a); - G = RGB_Conversion(RGB.b); - B = RGB_Conversion(RGB.c); -} -class CPDF_CalGray : public CPDF_ColorSpace { - public: - explicit CPDF_CalGray(CPDF_Document* pDoc) - : CPDF_ColorSpace(pDoc, PDFCS_CALGRAY, 1) {} - FX_BOOL v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) override; - FX_BOOL GetRGB(FX_FLOAT* pBuf, - FX_FLOAT& R, - FX_FLOAT& G, - FX_FLOAT& B) const override; - FX_BOOL SetRGB(FX_FLOAT* pBuf, - FX_FLOAT R, - FX_FLOAT G, - FX_FLOAT B) const override; - void TranslateImageLine(uint8_t* pDestBuf, - const uint8_t* pSrcBuf, - int pixels, - int image_width, - int image_height, - FX_BOOL bTransMask = FALSE) const override; - private: - FX_FLOAT m_WhitePoint[3]; - FX_FLOAT m_BlackPoint[3]; - FX_FLOAT m_Gamma; -}; - -FX_BOOL CPDF_CalGray::v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) { - CPDF_Dictionary* pDict = pArray->GetDictAt(1); - if (!pDict) - return FALSE; - - CPDF_Array* pParam = pDict->GetArrayBy("WhitePoint"); - int i; - for (i = 0; i < 3; i++) { - m_WhitePoint[i] = pParam ? pParam->GetNumberAt(i) : 0; - } - pParam = pDict->GetArrayBy("BlackPoint"); - for (i = 0; i < 3; i++) { - m_BlackPoint[i] = pParam ? pParam->GetNumberAt(i) : 0; - } - m_Gamma = pDict->GetNumberBy("Gamma"); - if (m_Gamma == 0) { - m_Gamma = 1.0f; - } - return TRUE; -} -FX_BOOL CPDF_CalGray::GetRGB(FX_FLOAT* pBuf, - FX_FLOAT& R, - FX_FLOAT& G, - FX_FLOAT& B) const { - R = G = B = *pBuf; - return TRUE; -} -FX_BOOL CPDF_CalGray::SetRGB(FX_FLOAT* pBuf, - FX_FLOAT R, - FX_FLOAT G, - FX_FLOAT B) const { - if (R == G && R == B) { - *pBuf = R; - return TRUE; - } - return FALSE; -} -void CPDF_CalGray::TranslateImageLine(uint8_t* pDestBuf, - const uint8_t* pSrcBuf, - int pixels, - int image_width, - int image_height, - FX_BOOL bTransMask) const { - for (int i = 0; i < pixels; i++) { - *pDestBuf++ = pSrcBuf[i]; - *pDestBuf++ = pSrcBuf[i]; - *pDestBuf++ = pSrcBuf[i]; - } -} -class CPDF_CalRGB : public CPDF_ColorSpace { - public: - explicit CPDF_CalRGB(CPDF_Document* pDoc) - : CPDF_ColorSpace(pDoc, PDFCS_CALRGB, 3) {} - FX_BOOL v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) override; - FX_BOOL GetRGB(FX_FLOAT* pBuf, - FX_FLOAT& R, - FX_FLOAT& G, - FX_FLOAT& B) const override; - FX_BOOL SetRGB(FX_FLOAT* pBuf, - FX_FLOAT R, - FX_FLOAT G, - FX_FLOAT B) const override; - void TranslateImageLine(uint8_t* pDestBuf, - const uint8_t* pSrcBuf, - int pixels, - int image_width, - int image_height, - FX_BOOL bTransMask = FALSE) const override; - - FX_FLOAT m_WhitePoint[3]; - FX_FLOAT m_BlackPoint[3]; - FX_FLOAT m_Gamma[3]; - FX_FLOAT m_Matrix[9]; - FX_BOOL m_bGamma; - FX_BOOL m_bMatrix; -}; -FX_BOOL CPDF_CalRGB::v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) { - CPDF_Dictionary* pDict = pArray->GetDictAt(1); - if (!pDict) - return FALSE; - - CPDF_Array* pParam = pDict->GetArrayBy("WhitePoint"); - int i; - for (i = 0; i < 3; i++) { - m_WhitePoint[i] = pParam ? pParam->GetNumberAt(i) : 0; - } - pParam = pDict->GetArrayBy("BlackPoint"); - for (i = 0; i < 3; i++) { - m_BlackPoint[i] = pParam ? pParam->GetNumberAt(i) : 0; - } - pParam = pDict->GetArrayBy("Gamma"); - if (pParam) { - m_bGamma = TRUE; - for (i = 0; i < 3; i++) { - m_Gamma[i] = pParam->GetNumberAt(i); - } - } else { - m_bGamma = FALSE; - } - pParam = pDict->GetArrayBy("Matrix"); - if (pParam) { - m_bMatrix = TRUE; - for (i = 0; i < 9; i++) { - m_Matrix[i] = pParam->GetNumberAt(i); - } - } else { - m_bMatrix = FALSE; - } - return TRUE; -} -FX_BOOL CPDF_CalRGB::GetRGB(FX_FLOAT* pBuf, - FX_FLOAT& R, - FX_FLOAT& G, - FX_FLOAT& B) const { - FX_FLOAT A_ = pBuf[0]; - FX_FLOAT B_ = pBuf[1]; - FX_FLOAT C_ = pBuf[2]; - if (m_bGamma) { - A_ = (FX_FLOAT)FXSYS_pow(A_, m_Gamma[0]); - B_ = (FX_FLOAT)FXSYS_pow(B_, m_Gamma[1]); - C_ = (FX_FLOAT)FXSYS_pow(C_, m_Gamma[2]); - } - FX_FLOAT X, Y, Z; - if (m_bMatrix) { - X = m_Matrix[0] * A_ + m_Matrix[3] * B_ + m_Matrix[6] * C_; - Y = m_Matrix[1] * A_ + m_Matrix[4] * B_ + m_Matrix[7] * C_; - Z = m_Matrix[2] * A_ + m_Matrix[5] * B_ + m_Matrix[8] * C_; - } else { - X = A_; - Y = B_; - Z = C_; - } - XYZ_to_sRGB_WhitePoint(X, Y, Z, R, G, B, m_WhitePoint[0], m_WhitePoint[1], - m_WhitePoint[2]); - return TRUE; -} -FX_BOOL CPDF_CalRGB::SetRGB(FX_FLOAT* pBuf, - FX_FLOAT R, - FX_FLOAT G, - FX_FLOAT B) const { - pBuf[0] = R; - pBuf[1] = G; - pBuf[2] = B; - return TRUE; -} -void CPDF_CalRGB::TranslateImageLine(uint8_t* pDestBuf, - const uint8_t* pSrcBuf, - int pixels, - int image_width, - int image_height, - FX_BOOL bTransMask) const { - if (bTransMask) { - FX_FLOAT Cal[3]; - FX_FLOAT R, G, B; - for (int i = 0; i < pixels; i++) { - Cal[0] = ((FX_FLOAT)pSrcBuf[2]) / 255; - Cal[1] = ((FX_FLOAT)pSrcBuf[1]) / 255; - Cal[2] = ((FX_FLOAT)pSrcBuf[0]) / 255; - GetRGB(Cal, R, G, B); - pDestBuf[0] = FXSYS_round(B * 255); - pDestBuf[1] = FXSYS_round(G * 255); - pDestBuf[2] = FXSYS_round(R * 255); - pSrcBuf += 3; - pDestBuf += 3; - } - } - ReverseRGB(pDestBuf, pSrcBuf, pixels); -} -class CPDF_LabCS : public CPDF_ColorSpace { - public: - explicit CPDF_LabCS(CPDF_Document* pDoc) - : CPDF_ColorSpace(pDoc, PDFCS_LAB, 3) {} - void GetDefaultValue(int iComponent, - FX_FLOAT& value, - FX_FLOAT& min, - FX_FLOAT& max) const override; - FX_BOOL v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) override; - FX_BOOL GetRGB(FX_FLOAT* pBuf, - FX_FLOAT& R, - FX_FLOAT& G, - FX_FLOAT& B) const override; - FX_BOOL SetRGB(FX_FLOAT* pBuf, - FX_FLOAT R, - FX_FLOAT G, - FX_FLOAT B) const override; - void TranslateImageLine(uint8_t* pDestBuf, - const uint8_t* pSrcBuf, - int pixels, - int image_width, - int image_height, - FX_BOOL bTransMask = FALSE) const override; - - FX_FLOAT m_WhitePoint[3]; - FX_FLOAT m_BlackPoint[3]; - FX_FLOAT m_Ranges[4]; -}; -FX_BOOL CPDF_LabCS::v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) { - CPDF_Dictionary* pDict = pArray->GetDictAt(1); - if (!pDict) { - return FALSE; - } - CPDF_Array* pParam = pDict->GetArrayBy("WhitePoint"); - int i; - for (i = 0; i < 3; i++) { - m_WhitePoint[i] = pParam ? pParam->GetNumberAt(i) : 0; - } - pParam = pDict->GetArrayBy("BlackPoint"); - for (i = 0; i < 3; i++) { - m_BlackPoint[i] = pParam ? pParam->GetNumberAt(i) : 0; - } - pParam = pDict->GetArrayBy("Range"); - const FX_FLOAT def_ranges[4] = {-100 * 1.0f, 100 * 1.0f, -100 * 1.0f, - 100 * 1.0f}; - for (i = 0; i < 4; i++) { - m_Ranges[i] = pParam ? pParam->GetNumberAt(i) : def_ranges[i]; - } - return TRUE; -} -void CPDF_LabCS::GetDefaultValue(int iComponent, - FX_FLOAT& value, - FX_FLOAT& min, - FX_FLOAT& max) const { - assert(iComponent < 3); - value = 0; - if (iComponent == 0) { - min = 0; - max = 100 * 1.0f; - } else { - min = m_Ranges[iComponent * 2 - 2]; - max = m_Ranges[iComponent * 2 - 1]; - if (value < min) { - value = min; - } else if (value > max) { - value = max; - } - } -} -FX_BOOL CPDF_LabCS::GetRGB(FX_FLOAT* pBuf, - FX_FLOAT& R, - FX_FLOAT& G, - FX_FLOAT& B) const { - FX_FLOAT Lstar = pBuf[0]; - FX_FLOAT astar = pBuf[1]; - FX_FLOAT bstar = pBuf[2]; - FX_FLOAT M = (Lstar + 16.0f) / 116.0f; - FX_FLOAT L = M + astar / 500.0f; - FX_FLOAT N = M - bstar / 200.0f; - FX_FLOAT X, Y, Z; - if (L < 0.2069f) { - X = 0.957f * 0.12842f * (L - 0.1379f); - } else { - X = 0.957f * L * L * L; - } - if (M < 0.2069f) { - Y = 0.12842f * (M - 0.1379f); - } else { - Y = M * M * M; - } - if (N < 0.2069f) { - Z = 1.0889f * 0.12842f * (N - 0.1379f); - } else { - Z = 1.0889f * N * N * N; - } - XYZ_to_sRGB(X, Y, Z, R, G, B); - return TRUE; -} -FX_BOOL CPDF_LabCS::SetRGB(FX_FLOAT* pBuf, - FX_FLOAT R, - FX_FLOAT G, - FX_FLOAT B) const { - return FALSE; -} -void CPDF_LabCS::TranslateImageLine(uint8_t* pDestBuf, - const uint8_t* pSrcBuf, - int pixels, - int image_width, - int image_height, - FX_BOOL bTransMask) const { - for (int i = 0; i < pixels; i++) { - FX_FLOAT lab[3]; - FX_FLOAT R, G, B; - lab[0] = (pSrcBuf[0] * 100 / 255.0f); - lab[1] = (FX_FLOAT)(pSrcBuf[1] - 128); - lab[2] = (FX_FLOAT)(pSrcBuf[2] - 128); - GetRGB(lab, R, G, B); - pDestBuf[0] = (int32_t)(B * 255); - pDestBuf[1] = (int32_t)(G * 255); - pDestBuf[2] = (int32_t)(R * 255); - pDestBuf += 3; - pSrcBuf += 3; - } -} CPDF_IccProfile::CPDF_IccProfile(const uint8_t* pData, FX_DWORD dwSize) : m_bsRGB(FALSE), m_pTransform(NULL), m_nSrcComponents(0) { if (dwSize == 3144 && @@ -643,907 +232,4 @@ CPDF_IccProfile::~CPDF_IccProfile() { CPDF_ModuleMgr::Get()->GetIccModule()->DestroyTransform(m_pTransform); } } -class CPDF_ICCBasedCS : public CPDF_ColorSpace { - public: - explicit CPDF_ICCBasedCS(CPDF_Document* pDoc) - : CPDF_ColorSpace(pDoc, PDFCS_ICCBASED, 0), - m_pAlterCS(nullptr), - m_pProfile(nullptr), - m_pCache(nullptr), - m_pRanges(nullptr), - m_bOwn(FALSE) {} - ~CPDF_ICCBasedCS() override; - - FX_BOOL v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) override; - FX_BOOL GetRGB(FX_FLOAT* pBuf, - FX_FLOAT& R, - FX_FLOAT& G, - FX_FLOAT& B) const override; - FX_BOOL SetRGB(FX_FLOAT* pBuf, - FX_FLOAT R, - FX_FLOAT G, - FX_FLOAT B) const override; - FX_BOOL v_GetCMYK(FX_FLOAT* pBuf, - FX_FLOAT& c, - FX_FLOAT& m, - FX_FLOAT& y, - FX_FLOAT& k) const override; - void EnableStdConversion(FX_BOOL bEnabled) override; - void TranslateImageLine(uint8_t* pDestBuf, - const uint8_t* pSrcBuf, - int pixels, - int image_width, - int image_height, - FX_BOOL bTransMask = FALSE) const override; - - CPDF_ColorSpace* m_pAlterCS; - CPDF_IccProfile* m_pProfile; - uint8_t* m_pCache; - FX_FLOAT* m_pRanges; - FX_BOOL m_bOwn; -}; - -CPDF_ICCBasedCS::~CPDF_ICCBasedCS() { - FX_Free(m_pCache); - FX_Free(m_pRanges); - if (m_pAlterCS && m_bOwn) { - m_pAlterCS->ReleaseCS(); - } - if (m_pProfile && m_pDocument) { - m_pDocument->GetPageData()->ReleaseIccProfile(m_pProfile); - } -} - -FX_BOOL CPDF_ICCBasedCS::v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) { - CPDF_Stream* pStream = pArray->GetStreamAt(1); - if (!pStream) { - return FALSE; - } - m_pProfile = pDoc->LoadIccProfile(pStream); - if (!m_pProfile) { - return FALSE; - } - m_nComponents = - m_pProfile - ->GetComponents(); // Try using the nComponents from ICC profile - CPDF_Dictionary* pDict = pStream->GetDict(); - if (!m_pProfile->m_pTransform) { // No valid ICC profile or using sRGB - CPDF_Object* pAlterCSObj = - pDict ? pDict->GetElementValue("Alternate") : NULL; - if (pAlterCSObj) { - CPDF_ColorSpace* pAlterCS = CPDF_ColorSpace::Load(pDoc, pAlterCSObj); - if (pAlterCS) { - if (m_nComponents == 0) { // NO valid ICC profile - if (pAlterCS->CountComponents() > 0) { // Use Alternative colorspace - m_nComponents = pAlterCS->CountComponents(); - m_pAlterCS = pAlterCS; - m_bOwn = TRUE; - } else { // No valid alternative colorspace - pAlterCS->ReleaseCS(); - int32_t nDictComponents = pDict ? pDict->GetIntegerBy("N") : 0; - if (nDictComponents != 1 && nDictComponents != 3 && - nDictComponents != 4) { - return FALSE; - } - m_nComponents = nDictComponents; - } - - } else { // Using sRGB - if (pAlterCS->CountComponents() != m_nComponents) { - pAlterCS->ReleaseCS(); - } else { - m_pAlterCS = pAlterCS; - m_bOwn = TRUE; - } - } - } - } - if (!m_pAlterCS) { - if (m_nComponents == 1) { - m_pAlterCS = GetStockCS(PDFCS_DEVICEGRAY); - } else if (m_nComponents == 3) { - m_pAlterCS = GetStockCS(PDFCS_DEVICERGB); - } else if (m_nComponents == 4) { - m_pAlterCS = GetStockCS(PDFCS_DEVICECMYK); - } - } - } - CPDF_Array* pRanges = pDict->GetArrayBy("Range"); - m_pRanges = FX_Alloc2D(FX_FLOAT, m_nComponents, 2); - for (int i = 0; i < m_nComponents * 2; i++) { - if (pRanges) { - m_pRanges[i] = pRanges->GetNumberAt(i); - } else if (i % 2) { - m_pRanges[i] = 1.0f; - } else { - m_pRanges[i] = 0; - } - } - return TRUE; -} -FX_BOOL CPDF_ICCBasedCS::GetRGB(FX_FLOAT* pBuf, - FX_FLOAT& R, - FX_FLOAT& G, - FX_FLOAT& B) const { - if (m_pProfile && m_pProfile->m_bsRGB) { - R = pBuf[0]; - G = pBuf[1]; - B = pBuf[2]; - return TRUE; - } - ICodec_IccModule* pIccModule = CPDF_ModuleMgr::Get()->GetIccModule(); - if (!m_pProfile->m_pTransform || !pIccModule) { - if (m_pAlterCS) { - return m_pAlterCS->GetRGB(pBuf, R, G, B); - } - R = G = B = 0.0f; - return TRUE; - } - FX_FLOAT rgb[3]; - pIccModule->SetComponents(m_nComponents); - pIccModule->Translate(m_pProfile->m_pTransform, pBuf, rgb); - R = rgb[0]; - G = rgb[1]; - B = rgb[2]; - return TRUE; -} -FX_BOOL CPDF_ICCBasedCS::v_GetCMYK(FX_FLOAT* pBuf, - FX_FLOAT& c, - FX_FLOAT& m, - FX_FLOAT& y, - FX_FLOAT& k) const { - if (m_nComponents != 4) { - return FALSE; - } - c = pBuf[0]; - m = pBuf[1]; - y = pBuf[2]; - k = pBuf[3]; - return TRUE; -} -FX_BOOL CPDF_ICCBasedCS::SetRGB(FX_FLOAT* pBuf, - FX_FLOAT R, - FX_FLOAT G, - FX_FLOAT B) const { - return FALSE; -} -void CPDF_ICCBasedCS::EnableStdConversion(FX_BOOL bEnabled) { - CPDF_ColorSpace::EnableStdConversion(bEnabled); - if (m_pAlterCS) { - m_pAlterCS->EnableStdConversion(bEnabled); - } -} -void CPDF_ICCBasedCS::TranslateImageLine(uint8_t* pDestBuf, - const uint8_t* pSrcBuf, - int pixels, - int image_width, - int image_height, - FX_BOOL bTransMask) const { - if (m_pProfile->m_bsRGB) { - ReverseRGB(pDestBuf, pSrcBuf, pixels); - } else if (m_pProfile->m_pTransform) { - int nMaxColors = 1; - for (int i = 0; i < m_nComponents; i++) { - nMaxColors *= 52; - } - if (m_nComponents > 3 || image_width * image_height < nMaxColors * 3 / 2) { - CPDF_ModuleMgr::Get()->GetIccModule()->TranslateScanline( - m_pProfile->m_pTransform, pDestBuf, pSrcBuf, pixels); - } else { - if (!m_pCache) { - ((CPDF_ICCBasedCS*)this)->m_pCache = FX_Alloc2D(uint8_t, nMaxColors, 3); - uint8_t* temp_src = FX_Alloc2D(uint8_t, nMaxColors, m_nComponents); - uint8_t* pSrc = temp_src; - for (int i = 0; i < nMaxColors; i++) { - FX_DWORD color = i; - FX_DWORD order = nMaxColors / 52; - for (int c = 0; c < m_nComponents; c++) { - *pSrc++ = (uint8_t)(color / order * 5); - color %= order; - order /= 52; - } - } - CPDF_ModuleMgr::Get()->GetIccModule()->TranslateScanline( - m_pProfile->m_pTransform, m_pCache, temp_src, nMaxColors); - FX_Free(temp_src); - } - for (int i = 0; i < pixels; i++) { - int index = 0; - for (int c = 0; c < m_nComponents; c++) { - index = index * 52 + (*pSrcBuf) / 5; - pSrcBuf++; - } - index *= 3; - *pDestBuf++ = m_pCache[index]; - *pDestBuf++ = m_pCache[index + 1]; - *pDestBuf++ = m_pCache[index + 2]; - } - } - } else if (m_pAlterCS) { - m_pAlterCS->TranslateImageLine(pDestBuf, pSrcBuf, pixels, image_width, - image_height); - } -} -class CPDF_IndexedCS : public CPDF_ColorSpace { - public: - explicit CPDF_IndexedCS(CPDF_Document* pDoc) - : CPDF_ColorSpace(pDoc, PDFCS_INDEXED, 1), - m_pBaseCS(nullptr), - m_pCountedBaseCS(nullptr), - m_pCompMinMax(nullptr) {} - ~CPDF_IndexedCS() override; - - FX_BOOL v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) override; - FX_BOOL GetRGB(FX_FLOAT* pBuf, - FX_FLOAT& R, - FX_FLOAT& G, - FX_FLOAT& B) const override; - CPDF_ColorSpace* GetBaseCS() const override; - void EnableStdConversion(FX_BOOL bEnabled) override; - - CPDF_ColorSpace* m_pBaseCS; - CPDF_CountedColorSpace* m_pCountedBaseCS; - int m_nBaseComponents; - int m_MaxIndex; - CFX_ByteString m_Table; - FX_FLOAT* m_pCompMinMax; -}; -CPDF_IndexedCS::~CPDF_IndexedCS() { - FX_Free(m_pCompMinMax); - CPDF_ColorSpace* pCS = m_pCountedBaseCS ? m_pCountedBaseCS->get() : NULL; - if (pCS && m_pDocument) { - m_pDocument->GetPageData()->ReleaseColorSpace(pCS->GetArray()); - } -} -FX_BOOL CPDF_IndexedCS::v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) { - if (pArray->GetCount() < 4) { - return FALSE; - } - CPDF_Object* pBaseObj = pArray->GetElementValue(1); - if (pBaseObj == m_pArray) { - return FALSE; - } - CPDF_DocPageData* pDocPageData = pDoc->GetPageData(); - m_pBaseCS = pDocPageData->GetColorSpace(pBaseObj, NULL); - if (!m_pBaseCS) { - return FALSE; - } - m_pCountedBaseCS = pDocPageData->FindColorSpacePtr(m_pBaseCS->GetArray()); - m_nBaseComponents = m_pBaseCS->CountComponents(); - m_pCompMinMax = FX_Alloc2D(FX_FLOAT, m_nBaseComponents, 2); - FX_FLOAT defvalue; - for (int i = 0; i < m_nBaseComponents; i++) { - m_pBaseCS->GetDefaultValue(i, defvalue, m_pCompMinMax[i * 2], - m_pCompMinMax[i * 2 + 1]); - m_pCompMinMax[i * 2 + 1] -= m_pCompMinMax[i * 2]; - } - m_MaxIndex = pArray->GetIntegerAt(2); - - CPDF_Object* pTableObj = pArray->GetElementValue(3); - if (!pTableObj) - return FALSE; - - if (CPDF_String* pString = pTableObj->AsString()) { - m_Table = pString->GetString(); - } else if (CPDF_Stream* pStream = pTableObj->AsStream()) { - CPDF_StreamAcc acc; - acc.LoadAllData(pStream, FALSE); - m_Table = CFX_ByteStringC(acc.GetData(), acc.GetSize()); - } - return TRUE; -} - -FX_BOOL CPDF_IndexedCS::GetRGB(FX_FLOAT* pBuf, - FX_FLOAT& R, - FX_FLOAT& G, - FX_FLOAT& B) const { - int index = (int32_t)(*pBuf); - if (index < 0 || index > m_MaxIndex) { - return FALSE; - } - if (m_nBaseComponents) { - if (index == INT_MAX || (index + 1) > INT_MAX / m_nBaseComponents || - (index + 1) * m_nBaseComponents > (int)m_Table.GetLength()) { - R = G = B = 0; - return FALSE; - } - } - CFX_FixedBufGrow<FX_FLOAT, 16> Comps(m_nBaseComponents); - FX_FLOAT* comps = Comps; - const uint8_t* pTable = m_Table; - for (int i = 0; i < m_nBaseComponents; i++) { - comps[i] = - m_pCompMinMax[i * 2] + - m_pCompMinMax[i * 2 + 1] * pTable[index * m_nBaseComponents + i] / 255; - } - return m_pBaseCS->GetRGB(comps, R, G, B); -} -CPDF_ColorSpace* CPDF_IndexedCS::GetBaseCS() const { - return m_pBaseCS; -} -void CPDF_IndexedCS::EnableStdConversion(FX_BOOL bEnabled) { - CPDF_ColorSpace::EnableStdConversion(bEnabled); - if (m_pBaseCS) { - m_pBaseCS->EnableStdConversion(bEnabled); - } -} - -#define MAX_PATTERN_COLORCOMPS 16 -struct PatternValue { - CPDF_Pattern* m_pPattern; - CPDF_CountedPattern* m_pCountedPattern; - int m_nComps; - FX_FLOAT m_Comps[MAX_PATTERN_COLORCOMPS]; -}; -CPDF_PatternCS::~CPDF_PatternCS() { - CPDF_ColorSpace* pCS = m_pCountedBaseCS ? m_pCountedBaseCS->get() : NULL; - if (pCS && m_pDocument) { - m_pDocument->GetPageData()->ReleaseColorSpace(pCS->GetArray()); - } -} -FX_BOOL CPDF_PatternCS::v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) { - CPDF_Object* pBaseCS = pArray->GetElementValue(1); - if (pBaseCS == m_pArray) { - return FALSE; - } - CPDF_DocPageData* pDocPageData = pDoc->GetPageData(); - m_pBaseCS = pDocPageData->GetColorSpace(pBaseCS, NULL); - if (m_pBaseCS) { - if (m_pBaseCS->GetFamily() == PDFCS_PATTERN) { - return FALSE; - } - m_pCountedBaseCS = pDocPageData->FindColorSpacePtr(m_pBaseCS->GetArray()); - m_nComponents = m_pBaseCS->CountComponents() + 1; - if (m_pBaseCS->CountComponents() > MAX_PATTERN_COLORCOMPS) { - return FALSE; - } - } else { - m_nComponents = 1; - } - return TRUE; -} -FX_BOOL CPDF_PatternCS::GetRGB(FX_FLOAT* pBuf, - FX_FLOAT& R, - FX_FLOAT& G, - FX_FLOAT& B) const { - if (m_pBaseCS) { - ASSERT(m_pBaseCS->GetFamily() != PDFCS_PATTERN); - PatternValue* pvalue = (PatternValue*)pBuf; - if (m_pBaseCS->GetRGB(pvalue->m_Comps, R, G, B)) { - return TRUE; - } - } - R = G = B = 0.75f; - return FALSE; -} -CPDF_ColorSpace* CPDF_PatternCS::GetBaseCS() const { - return m_pBaseCS; -} -class CPDF_SeparationCS : public CPDF_ColorSpace { - public: - explicit CPDF_SeparationCS(CPDF_Document* pDoc) - : CPDF_ColorSpace(pDoc, PDFCS_SEPARATION, 1), - m_pAltCS(nullptr), - m_pFunc(nullptr) {} - ~CPDF_SeparationCS() override; - - // CPDF_ColorSpace: - void GetDefaultValue(int iComponent, - FX_FLOAT& value, - FX_FLOAT& min, - FX_FLOAT& max) const override; - FX_BOOL v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) override; - FX_BOOL GetRGB(FX_FLOAT* pBuf, - FX_FLOAT& R, - FX_FLOAT& G, - FX_FLOAT& B) const override; - void EnableStdConversion(FX_BOOL bEnabled) override; - - CPDF_ColorSpace* m_pAltCS; - CPDF_Function* m_pFunc; - enum { None, All, Colorant } m_Type; -}; -CPDF_SeparationCS::~CPDF_SeparationCS() { - if (m_pAltCS) { - m_pAltCS->ReleaseCS(); - } - delete m_pFunc; -} -void CPDF_SeparationCS::GetDefaultValue(int iComponent, - FX_FLOAT& value, - FX_FLOAT& min, - FX_FLOAT& max) const { - value = 1.0f; - min = 0; - max = 1.0f; -} -FX_BOOL CPDF_SeparationCS::v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) { - CFX_ByteString name = pArray->GetStringAt(1); - if (name == "None") { - m_Type = None; - } else { - m_Type = Colorant; - CPDF_Object* pAltCS = pArray->GetElementValue(2); - if (pAltCS == m_pArray) { - return FALSE; - } - m_pAltCS = Load(pDoc, pAltCS); - if (!m_pAltCS) { - return FALSE; - } - CPDF_Object* pFuncObj = pArray->GetElementValue(3); - if (pFuncObj && !pFuncObj->IsName()) - m_pFunc = CPDF_Function::Load(pFuncObj); - - if (m_pFunc && m_pFunc->CountOutputs() < m_pAltCS->CountComponents()) { - delete m_pFunc; - m_pFunc = NULL; - } - } - return TRUE; -} -FX_BOOL CPDF_SeparationCS::GetRGB(FX_FLOAT* pBuf, - FX_FLOAT& R, - FX_FLOAT& G, - FX_FLOAT& B) const { - if (m_Type == None) { - return FALSE; - } - if (!m_pFunc) { - if (!m_pAltCS) { - return FALSE; - } - int nComps = m_pAltCS->CountComponents(); - CFX_FixedBufGrow<FX_FLOAT, 16> results(nComps); - for (int i = 0; i < nComps; i++) { - results[i] = *pBuf; - } - return m_pAltCS->GetRGB(results, R, G, B); - } - CFX_FixedBufGrow<FX_FLOAT, 16> results(m_pFunc->CountOutputs()); - int nresults = 0; - m_pFunc->Call(pBuf, 1, results, nresults); - if (nresults == 0) { - return FALSE; - } - if (m_pAltCS) { - return m_pAltCS->GetRGB(results, R, G, B); - } - R = G = B = 0; - return FALSE; -} -void CPDF_SeparationCS::EnableStdConversion(FX_BOOL bEnabled) { - CPDF_ColorSpace::EnableStdConversion(bEnabled); - if (m_pAltCS) { - m_pAltCS->EnableStdConversion(bEnabled); - } -} -class CPDF_DeviceNCS : public CPDF_ColorSpace { - public: - explicit CPDF_DeviceNCS(CPDF_Document* pDoc) - : CPDF_ColorSpace(pDoc, PDFCS_DEVICEN, 0), - m_pAltCS(nullptr), - m_pFunc(nullptr) {} - ~CPDF_DeviceNCS() override; - - // CPDF_ColorSpace: - void GetDefaultValue(int iComponent, - FX_FLOAT& value, - FX_FLOAT& min, - FX_FLOAT& max) const override; - FX_BOOL v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) override; - FX_BOOL GetRGB(FX_FLOAT* pBuf, - FX_FLOAT& R, - FX_FLOAT& G, - FX_FLOAT& B) const override; - void EnableStdConversion(FX_BOOL bEnabled) override; - - CPDF_ColorSpace* m_pAltCS; - CPDF_Function* m_pFunc; -}; -CPDF_DeviceNCS::~CPDF_DeviceNCS() { - delete m_pFunc; - if (m_pAltCS) { - m_pAltCS->ReleaseCS(); - } -} -void CPDF_DeviceNCS::GetDefaultValue(int iComponent, - FX_FLOAT& value, - FX_FLOAT& min, - FX_FLOAT& max) const { - value = 1.0f; - min = 0; - max = 1.0f; -} -FX_BOOL CPDF_DeviceNCS::v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) { - CPDF_Array* pObj = ToArray(pArray->GetElementValue(1)); - if (!pObj) - return FALSE; - - m_nComponents = pObj->GetCount(); - CPDF_Object* pAltCS = pArray->GetElementValue(2); - if (!pAltCS || pAltCS == m_pArray) { - return FALSE; - } - m_pAltCS = Load(pDoc, pAltCS); - m_pFunc = CPDF_Function::Load(pArray->GetElementValue(3)); - if (!m_pAltCS || !m_pFunc) { - return FALSE; - } - if (m_pFunc->CountOutputs() < m_pAltCS->CountComponents()) { - return FALSE; - } - return TRUE; -} -FX_BOOL CPDF_DeviceNCS::GetRGB(FX_FLOAT* pBuf, - FX_FLOAT& R, - FX_FLOAT& G, - FX_FLOAT& B) const { - if (!m_pFunc) { - return FALSE; - } - CFX_FixedBufGrow<FX_FLOAT, 16> results(m_pFunc->CountOutputs()); - int nresults = 0; - m_pFunc->Call(pBuf, m_nComponents, results, nresults); - if (nresults == 0) { - return FALSE; - } - return m_pAltCS->GetRGB(results, R, G, B); -} -void CPDF_DeviceNCS::EnableStdConversion(FX_BOOL bEnabled) { - CPDF_ColorSpace::EnableStdConversion(bEnabled); - if (m_pAltCS) { - m_pAltCS->EnableStdConversion(bEnabled); - } -} - -CPDF_ColorSpace* CPDF_ColorSpace::GetStockCS(int family) { - return CPDF_ModuleMgr::Get()->GetPageModule()->GetStockCS(family); -} - -CPDF_ColorSpace* _CSFromName(const CFX_ByteString& name) { - if (name == "DeviceRGB" || name == "RGB") { - return CPDF_ColorSpace::GetStockCS(PDFCS_DEVICERGB); - } - if (name == "DeviceGray" || name == "G") { - return CPDF_ColorSpace::GetStockCS(PDFCS_DEVICEGRAY); - } - if (name == "DeviceCMYK" || name == "CMYK") { - return CPDF_ColorSpace::GetStockCS(PDFCS_DEVICECMYK); - } - if (name == "Pattern") { - return CPDF_ColorSpace::GetStockCS(PDFCS_PATTERN); - } - return NULL; -} -CPDF_ColorSpace* CPDF_ColorSpace::Load(CPDF_Document* pDoc, CPDF_Object* pObj) { - if (!pObj) - return nullptr; - if (pObj->IsName()) - return _CSFromName(pObj->GetString()); - - if (CPDF_Stream* pStream = pObj->AsStream()) { - CPDF_Dictionary* pDict = pStream->GetDict(); - if (!pDict) - return nullptr; - - for (const auto& it : *pDict) { - CPDF_ColorSpace* pRet = nullptr; - CPDF_Object* pValue = it.second; - if (ToName(pValue)) - pRet = _CSFromName(pValue->GetString()); - if (pRet) - return pRet; - } - return nullptr; - } - - CPDF_Array* pArray = pObj->AsArray(); - if (!pArray || pArray->GetCount() == 0) - return nullptr; - - CPDF_Object* pFamilyObj = pArray->GetElementValue(0); - if (!pFamilyObj) - return nullptr; - - CFX_ByteString familyname = pFamilyObj->GetString(); - if (pArray->GetCount() == 1) - return _CSFromName(familyname); - - CPDF_ColorSpace* pCS = NULL; - FX_DWORD id = familyname.GetID(); - if (id == FXBSTR_ID('C', 'a', 'l', 'G')) { - pCS = new CPDF_CalGray(pDoc); - } else if (id == FXBSTR_ID('C', 'a', 'l', 'R')) { - pCS = new CPDF_CalRGB(pDoc); - } else if (id == FXBSTR_ID('L', 'a', 'b', 0)) { - pCS = new CPDF_LabCS(pDoc); - } else if (id == FXBSTR_ID('I', 'C', 'C', 'B')) { - pCS = new CPDF_ICCBasedCS(pDoc); - } else if (id == FXBSTR_ID('I', 'n', 'd', 'e') || - id == FXBSTR_ID('I', 0, 0, 0)) { - pCS = new CPDF_IndexedCS(pDoc); - } else if (id == FXBSTR_ID('S', 'e', 'p', 'a')) { - pCS = new CPDF_SeparationCS(pDoc); - } else if (id == FXBSTR_ID('D', 'e', 'v', 'i')) { - pCS = new CPDF_DeviceNCS(pDoc); - } else if (id == FXBSTR_ID('P', 'a', 't', 't')) { - pCS = new CPDF_PatternCS(pDoc); - } else { - return NULL; - } - pCS->m_pArray = pArray; - if (!pCS->v_Load(pDoc, pArray)) { - pCS->ReleaseCS(); - return NULL; - } - return pCS; -} -void CPDF_ColorSpace::ReleaseCS() { - if (this == GetStockCS(PDFCS_DEVICERGB)) { - return; - } - if (this == GetStockCS(PDFCS_DEVICEGRAY)) { - return; - } - if (this == GetStockCS(PDFCS_DEVICECMYK)) { - return; - } - if (this == GetStockCS(PDFCS_PATTERN)) { - return; - } - delete this; -} -int CPDF_ColorSpace::GetBufSize() const { - if (m_Family == PDFCS_PATTERN) { - return sizeof(PatternValue); - } - return m_nComponents * sizeof(FX_FLOAT); -} -FX_FLOAT* CPDF_ColorSpace::CreateBuf() { - int size = GetBufSize(); - uint8_t* pBuf = FX_Alloc(uint8_t, size); - return (FX_FLOAT*)pBuf; -} -FX_BOOL CPDF_ColorSpace::sRGB() const { - if (m_Family == PDFCS_DEVICERGB) { - return TRUE; - } - if (m_Family != PDFCS_ICCBASED) { - return FALSE; - } - CPDF_ICCBasedCS* pCS = (CPDF_ICCBasedCS*)this; - return pCS->m_pProfile->m_bsRGB; -} -FX_BOOL CPDF_ColorSpace::GetCMYK(FX_FLOAT* pBuf, - FX_FLOAT& c, - FX_FLOAT& m, - FX_FLOAT& y, - FX_FLOAT& k) const { - if (v_GetCMYK(pBuf, c, m, y, k)) { - return TRUE; - } - FX_FLOAT R, G, B; - if (!GetRGB(pBuf, R, G, B)) { - return FALSE; - } - sRGB_to_AdobeCMYK(R, G, B, c, m, y, k); - return TRUE; -} -FX_BOOL CPDF_ColorSpace::SetCMYK(FX_FLOAT* pBuf, - FX_FLOAT c, - FX_FLOAT m, - FX_FLOAT y, - FX_FLOAT k) const { - if (v_SetCMYK(pBuf, c, m, y, k)) { - return TRUE; - } - FX_FLOAT R, G, B; - AdobeCMYK_to_sRGB(c, m, y, k, R, G, B); - return SetRGB(pBuf, R, G, B); -} -void CPDF_ColorSpace::GetDefaultColor(FX_FLOAT* buf) const { - if (!buf || m_Family == PDFCS_PATTERN) { - return; - } - FX_FLOAT min, max; - for (int i = 0; i < m_nComponents; i++) { - GetDefaultValue(i, buf[i], min, max); - } -} -int CPDF_ColorSpace::GetMaxIndex() const { - if (m_Family != PDFCS_INDEXED) { - return 0; - } - CPDF_IndexedCS* pCS = (CPDF_IndexedCS*)this; - return pCS->m_MaxIndex; -} -void CPDF_ColorSpace::TranslateImageLine(uint8_t* dest_buf, - const uint8_t* src_buf, - int pixels, - int image_width, - int image_height, - FX_BOOL bTransMask) const { - CFX_FixedBufGrow<FX_FLOAT, 16> srcbuf(m_nComponents); - FX_FLOAT* src = srcbuf; - FX_FLOAT R, G, B; - for (int i = 0; i < pixels; i++) { - for (int j = 0; j < m_nComponents; j++) - if (m_Family == PDFCS_INDEXED) { - src[j] = (FX_FLOAT)(*src_buf++); - } else { - src[j] = (FX_FLOAT)(*src_buf++) / 255; - } - GetRGB(src, R, G, B); - *dest_buf++ = (int32_t)(B * 255); - *dest_buf++ = (int32_t)(G * 255); - *dest_buf++ = (int32_t)(R * 255); - } -} -void CPDF_ColorSpace::EnableStdConversion(FX_BOOL bEnabled) { - if (bEnabled) { - m_dwStdConversion++; - } else if (m_dwStdConversion) { - m_dwStdConversion--; - } -} -CPDF_Color::CPDF_Color(int family) { - m_pCS = CPDF_ColorSpace::GetStockCS(family); - int nComps = 3; - if (family == PDFCS_DEVICEGRAY) { - nComps = 1; - } else if (family == PDFCS_DEVICECMYK) { - nComps = 4; - } - m_pBuffer = FX_Alloc(FX_FLOAT, nComps); - for (int i = 0; i < nComps; i++) { - m_pBuffer[i] = 0; - } -} -CPDF_Color::~CPDF_Color() { - ReleaseBuffer(); - ReleaseColorSpace(); -} -void CPDF_Color::ReleaseBuffer() { - if (!m_pBuffer) { - return; - } - if (m_pCS->GetFamily() == PDFCS_PATTERN) { - PatternValue* pvalue = (PatternValue*)m_pBuffer; - CPDF_Pattern* pPattern = - pvalue->m_pCountedPattern ? pvalue->m_pCountedPattern->get() : NULL; - if (pPattern && pPattern->m_pDocument) { - CPDF_DocPageData* pPageData = pPattern->m_pDocument->GetPageData(); - if (pPageData) { - pPageData->ReleasePattern(pPattern->m_pPatternObj); - } - } - } - FX_Free(m_pBuffer); - m_pBuffer = NULL; -} -void CPDF_Color::ReleaseColorSpace() { - if (m_pCS && m_pCS->m_pDocument && m_pCS->GetArray()) { - m_pCS->m_pDocument->GetPageData()->ReleaseColorSpace(m_pCS->GetArray()); - m_pCS = NULL; - } -} -void CPDF_Color::SetColorSpace(CPDF_ColorSpace* pCS) { - if (m_pCS == pCS) { - if (!m_pBuffer) { - m_pBuffer = pCS->CreateBuf(); - } - ReleaseColorSpace(); - m_pCS = pCS; - return; - } - ReleaseBuffer(); - ReleaseColorSpace(); - m_pCS = pCS; - if (m_pCS) { - m_pBuffer = pCS->CreateBuf(); - pCS->GetDefaultColor(m_pBuffer); - } -} -void CPDF_Color::SetValue(FX_FLOAT* comps) { - if (!m_pBuffer) { - return; - } - if (m_pCS->GetFamily() != PDFCS_PATTERN) { - FXSYS_memcpy(m_pBuffer, comps, m_pCS->CountComponents() * sizeof(FX_FLOAT)); - } -} -void CPDF_Color::SetValue(CPDF_Pattern* pPattern, FX_FLOAT* comps, int ncomps) { - if (ncomps > MAX_PATTERN_COLORCOMPS) { - return; - } - if (!m_pCS || m_pCS->GetFamily() != PDFCS_PATTERN) { - FX_Free(m_pBuffer); - m_pCS = CPDF_ColorSpace::GetStockCS(PDFCS_PATTERN); - m_pBuffer = m_pCS->CreateBuf(); - } - CPDF_DocPageData* pDocPageData = NULL; - PatternValue* pvalue = (PatternValue*)m_pBuffer; - if (pvalue->m_pPattern && pvalue->m_pPattern->m_pDocument) { - pDocPageData = pvalue->m_pPattern->m_pDocument->GetPageData(); - if (pDocPageData) { - pDocPageData->ReleasePattern(pvalue->m_pPattern->m_pPatternObj); - } - } - pvalue->m_nComps = ncomps; - pvalue->m_pPattern = pPattern; - if (ncomps) { - FXSYS_memcpy(pvalue->m_Comps, comps, ncomps * sizeof(FX_FLOAT)); - } - pvalue->m_pCountedPattern = NULL; - if (pPattern && pPattern->m_pDocument) { - if (!pDocPageData) { - pDocPageData = pPattern->m_pDocument->GetPageData(); - } - pvalue->m_pCountedPattern = - pDocPageData->FindPatternPtr(pPattern->m_pPatternObj); - } -} -void CPDF_Color::Copy(const CPDF_Color* pSrc) { - ReleaseBuffer(); - ReleaseColorSpace(); - m_pCS = pSrc->m_pCS; - if (m_pCS && m_pCS->m_pDocument) { - CPDF_Array* pArray = m_pCS->GetArray(); - if (pArray) { - m_pCS = m_pCS->m_pDocument->GetPageData()->GetCopiedColorSpace(pArray); - } - } - if (!m_pCS) { - return; - } - m_pBuffer = m_pCS->CreateBuf(); - FXSYS_memcpy(m_pBuffer, pSrc->m_pBuffer, m_pCS->GetBufSize()); - if (m_pCS->GetFamily() == PDFCS_PATTERN) { - PatternValue* pvalue = (PatternValue*)m_pBuffer; - if (pvalue->m_pPattern && pvalue->m_pPattern->m_pDocument) { - pvalue->m_pPattern = - pvalue->m_pPattern->m_pDocument->GetPageData()->GetPattern( - pvalue->m_pPattern->m_pPatternObj, FALSE, - &pvalue->m_pPattern->m_ParentMatrix); - } - } -} -FX_BOOL CPDF_Color::GetRGB(int& R, int& G, int& B) const { - if (!m_pCS || !m_pBuffer) { - return FALSE; - } - FX_FLOAT r = 0.0f, g = 0.0f, b = 0.0f; - if (!m_pCS->GetRGB(m_pBuffer, r, g, b)) { - return FALSE; - } - R = (int32_t)(r * 255 + 0.5f); - G = (int32_t)(g * 255 + 0.5f); - B = (int32_t)(b * 255 + 0.5f); - return TRUE; -} -CPDF_Pattern* CPDF_Color::GetPattern() const { - if (!m_pBuffer || m_pCS->GetFamily() != PDFCS_PATTERN) { - return NULL; - } - PatternValue* pvalue = (PatternValue*)m_pBuffer; - return pvalue->m_pPattern; -} -CPDF_ColorSpace* CPDF_Color::GetPatternCS() const { - if (!m_pBuffer || m_pCS->GetFamily() != PDFCS_PATTERN) { - return NULL; - } - return m_pCS->GetBaseCS(); -} -FX_FLOAT* CPDF_Color::GetPatternColor() const { - if (!m_pBuffer || m_pCS->GetFamily() != PDFCS_PATTERN) { - return NULL; - } - PatternValue* pvalue = (PatternValue*)m_pBuffer; - return pvalue->m_nComps ? pvalue->m_Comps : NULL; -} -FX_BOOL CPDF_Color::IsEqual(const CPDF_Color& other) const { - return m_pCS && m_pCS == other.m_pCS && - FXSYS_memcmp(m_pBuffer, other.m_pBuffer, m_pCS->GetBufSize()) == 0; -} diff --git a/core/fpdfapi/fpdf_page/fpdf_page_doc.cpp b/core/fpdfapi/fpdf_page/fpdf_page_doc.cpp index 0d0c373c56..2e68702127 100644 --- a/core/fpdfapi/fpdf_page/fpdf_page_doc.cpp +++ b/core/fpdfapi/fpdf_page/fpdf_page_doc.cpp @@ -7,10 +7,16 @@ #include "core/fpdfapi/fpdf_page/pageint.h" #include "core/fdrm/crypto/include/fx_crypt.h" +#include "core/fpdfapi/fpdf_font/cpdf_type1font.h" #include "core/fpdfapi/fpdf_font/font_int.h" +#include "core/fpdfapi/fpdf_page/cpdf_pattern.h" +#include "core/fpdfapi/fpdf_page/cpdf_shadingpattern.h" +#include "core/fpdfapi/fpdf_page/cpdf_tilingpattern.h" +#include "core/fpdfapi/fpdf_page/include/cpdf_image.h" #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" #include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" +#include "core/fpdfapi/fpdf_parser/include/cpdf_stream_acc.h" #include "core/fpdfapi/include/cpdf_modulemgr.h" #include "core/fpdfapi/ipdf_pagemodule.h" @@ -91,7 +97,6 @@ CPDF_StreamAcc* CPDF_Document::LoadFontFile(CPDF_Stream* pStream) { return GetValidatePageData()->GetFontFileStreamAcc(pStream); } -CPDF_ColorSpace* _CSFromName(const CFX_ByteString& name); CPDF_ColorSpace* CPDF_Document::LoadColorSpace(CPDF_Object* pCSObj, CPDF_Dictionary* pResources) { return GetValidatePageData()->GetColorSpace(pCSObj, pResources); @@ -322,7 +327,7 @@ CPDF_ColorSpace* CPDF_DocPageData::GetColorSpace( if (pCSObj->IsName()) { CFX_ByteString name = pCSObj->GetConstString(); - CPDF_ColorSpace* pCS = _CSFromName(name); + CPDF_ColorSpace* pCS = CPDF_ColorSpace::ColorspaceFromName(name); if (!pCS && pResources) { CPDF_Dictionary* pList = pResources->GetDictBy("ColorSpace"); if (pList) { diff --git a/core/fpdfapi/fpdf_page/fpdf_page_func.cpp b/core/fpdfapi/fpdf_page/fpdf_page_func.cpp index cea7d6b8e7..96aad6d87f 100644 --- a/core/fpdfapi/fpdf_page/fpdf_page_func.cpp +++ b/core/fpdfapi/fpdf_page/fpdf_page_func.cpp @@ -15,6 +15,8 @@ #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" #include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" #include "core/fpdfapi/fpdf_parser/include/cpdf_simple_parser.h" +#include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h" +#include "core/fpdfapi/fpdf_parser/include/cpdf_stream_acc.h" #include "core/fxcrt/include/fx_safe_types.h" #include "third_party/base/numerics/safe_conversions_impl.h" diff --git a/core/fpdfapi/fpdf_page/fpdf_page_image.cpp b/core/fpdfapi/fpdf_page/fpdf_page_image.cpp deleted file mode 100644 index f66823657f..0000000000 --- a/core/fpdfapi/fpdf_page/fpdf_page_image.cpp +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright 2014 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 - -#include "core/fpdfapi/fpdf_page/pageint.h" - -#include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" -#include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" - -void CPDF_Image::Release() { - if (m_bInline || (m_pStream && m_pStream->GetObjNum() == 0)) { - delete this; - } -} -CPDF_Image* CPDF_Image::Clone() { - if (m_pStream->GetObjNum()) - return m_pDocument->GetPageData()->GetImage(m_pStream); - - CPDF_Image* pImage = new CPDF_Image(m_pDocument); - pImage->LoadImageF(ToStream(m_pStream->Clone()), m_bInline); - if (m_bInline) - pImage->SetInlineDict(ToDictionary(m_pInlineDict->Clone(TRUE))); - - return pImage; -} -CPDF_Image::CPDF_Image(CPDF_Document* pDoc) { - m_pDocument = pDoc; - m_pStream = NULL; - m_pOC = NULL; - m_bInline = FALSE; - m_pInlineDict = NULL; - m_pDIBSource = NULL; - m_pMask = NULL; - m_MatteColor = 0; -} -CPDF_Image::~CPDF_Image() { - if (m_bInline) { - if (m_pStream) { - m_pStream->Release(); - } - if (m_pInlineDict) { - m_pInlineDict->Release(); - } - } -} -FX_BOOL CPDF_Image::LoadImageF(CPDF_Stream* pStream, FX_BOOL bInline) { - m_pStream = pStream; - if (m_bInline && m_pInlineDict) { - m_pInlineDict->Release(); - m_pInlineDict = NULL; - } - m_bInline = bInline; - CPDF_Dictionary* pDict = pStream->GetDict(); - if (m_bInline) { - m_pInlineDict = ToDictionary(pDict->Clone()); - } - m_pOC = pDict->GetDictBy("OC"); - m_bIsMask = - !pDict->KeyExist("ColorSpace") || pDict->GetIntegerBy("ImageMask"); - m_bInterpolate = pDict->GetIntegerBy("Interpolate"); - m_Height = pDict->GetIntegerBy("Height"); - m_Width = pDict->GetIntegerBy("Width"); - return TRUE; -} diff --git a/core/fpdfapi/fpdf_page/fpdf_page_parser.cpp b/core/fpdfapi/fpdf_page/fpdf_page_parser.cpp index 700103ed41..63c84bbd22 100644 --- a/core/fpdfapi/fpdf_page/fpdf_page_parser.cpp +++ b/core/fpdfapi/fpdf_page/fpdf_page_parser.cpp @@ -9,9 +9,13 @@ #include <vector> #include "core/fpdfapi/fpdf_edit/include/cpdf_creator.h" +#include "core/fpdfapi/fpdf_font/cpdf_type3font.h" +#include "core/fpdfapi/fpdf_font/include/cpdf_font.h" #include "core/fpdfapi/fpdf_page/cpdf_allstates.h" +#include "core/fpdfapi/fpdf_page/cpdf_shadingpattern.h" #include "core/fpdfapi/fpdf_page/include/cpdf_form.h" #include "core/fpdfapi/fpdf_page/include/cpdf_formobject.h" +#include "core/fpdfapi/fpdf_page/include/cpdf_image.h" #include "core/fpdfapi/fpdf_page/include/cpdf_imageobject.h" #include "core/fpdfapi/fpdf_page/include/cpdf_pageobject.h" #include "core/fpdfapi/fpdf_page/include/cpdf_pathobject.h" @@ -23,6 +27,8 @@ #include "core/fpdfapi/fpdf_parser/include/cpdf_name.h" #include "core/fpdfapi/fpdf_parser/include/cpdf_number.h" #include "core/fpdfapi/fpdf_parser/include/cpdf_reference.h" +#include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h" +#include "core/fpdfapi/fpdf_parser/include/cpdf_stream_acc.h" #include "core/fpdfapi/fpdf_parser/include/fpdf_parser_decode.h" namespace { diff --git a/core/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp b/core/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp index c0d7c08ee1..04211041fe 100644 --- a/core/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp +++ b/core/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp @@ -8,6 +8,7 @@ #include <limits.h> +#include "core/fpdfapi/fpdf_font/cpdf_type3char.h" #include "core/fpdfapi/fpdf_page/cpdf_allstates.h" #include "core/fpdfapi/fpdf_page/include/cpdf_form.h" #include "core/fpdfapi/fpdf_page/include/cpdf_page.h" @@ -21,6 +22,8 @@ #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" #include "core/fpdfapi/fpdf_parser/include/cpdf_name.h" #include "core/fpdfapi/fpdf_parser/include/cpdf_number.h" +#include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h" +#include "core/fpdfapi/fpdf_parser/include/cpdf_stream_acc.h" #include "core/fpdfapi/fpdf_parser/include/cpdf_string.h" #include "core/fpdfapi/fpdf_parser/include/fpdf_parser_decode.h" #include "core/fpdfapi/include/cpdf_modulemgr.h" diff --git a/core/fpdfapi/fpdf_page/fpdf_page_pattern.cpp b/core/fpdfapi/fpdf_page/fpdf_page_pattern.cpp index fbb2c28373..822d06d84c 100644 --- a/core/fpdfapi/fpdf_page/fpdf_page_pattern.cpp +++ b/core/fpdfapi/fpdf_page/fpdf_page_pattern.cpp @@ -8,10 +8,13 @@ #include <algorithm> +#include "core/fpdfapi/fpdf_page/cpdf_meshstream.h" +#include "core/fpdfapi/fpdf_page/cpdf_shadingpattern.h" #include "core/fpdfapi/fpdf_page/include/cpdf_form.h" #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" #include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" +#include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h" namespace { @@ -22,245 +25,9 @@ const int kCoonsCoordinatePairs = 12; const int kSingleColorPerPatch = 1; const int kQuadColorsPerPatch = 4; -ShadingType ToShadingType(int type) { - return (type > static_cast<int>(kInvalidShading) && - type < static_cast<int>(kMaxShading)) - ? static_cast<ShadingType>(type) - : kInvalidShading; -} - } // namespace -CPDF_Pattern::CPDF_Pattern(PatternType type, - CPDF_Document* pDoc, - CPDF_Object* pObj, - const CFX_Matrix* pParentMatrix) - : m_PatternType(type), - m_pDocument(pDoc), - m_pPatternObj(pObj), - m_bForceClear(FALSE) { - if (pParentMatrix) - m_ParentMatrix = *pParentMatrix; -} -CPDF_Pattern::~CPDF_Pattern() {} -CPDF_TilingPattern::CPDF_TilingPattern(CPDF_Document* pDoc, - CPDF_Object* pPatternObj, - const CFX_Matrix* parentMatrix) - : CPDF_Pattern(TILING, pDoc, pPatternObj, parentMatrix) { - CPDF_Dictionary* pDict = m_pPatternObj->GetDict(); - m_Pattern2Form = pDict->GetMatrixBy("Matrix"); - m_bColored = pDict->GetIntegerBy("PaintType") == 1; - if (parentMatrix) { - m_Pattern2Form.Concat(*parentMatrix); - } - m_pForm = NULL; -} -CPDF_TilingPattern::~CPDF_TilingPattern() { - delete m_pForm; - m_pForm = NULL; -} -FX_BOOL CPDF_TilingPattern::Load() { - if (m_pForm) - return TRUE; - - CPDF_Dictionary* pDict = m_pPatternObj->GetDict(); - if (!pDict) - return FALSE; - - m_bColored = pDict->GetIntegerBy("PaintType") == 1; - m_XStep = (FX_FLOAT)FXSYS_fabs(pDict->GetNumberBy("XStep")); - m_YStep = (FX_FLOAT)FXSYS_fabs(pDict->GetNumberBy("YStep")); - - CPDF_Stream* pStream = m_pPatternObj->AsStream(); - if (!pStream) - return FALSE; - - m_pForm = new CPDF_Form(m_pDocument, NULL, pStream); - m_pForm->ParseContent(NULL, &m_ParentMatrix, NULL, NULL); - m_BBox = pDict->GetRectBy("BBox"); - return TRUE; -} -CPDF_ShadingPattern::CPDF_ShadingPattern(CPDF_Document* pDoc, - CPDF_Object* pPatternObj, - FX_BOOL bShading, - const CFX_Matrix* parentMatrix) - : CPDF_Pattern(SHADING, - pDoc, - bShading ? nullptr : pPatternObj, - parentMatrix), - m_ShadingType(kInvalidShading), - m_bShadingObj(bShading), - m_pShadingObj(pPatternObj), - m_pCS(nullptr), - m_pCountedCS(nullptr), - m_nFuncs(0) { - if (!bShading) { - CPDF_Dictionary* pDict = m_pPatternObj->GetDict(); - m_Pattern2Form = pDict->GetMatrixBy("Matrix"); - m_pShadingObj = pDict->GetElementValue("Shading"); - if (parentMatrix) - m_Pattern2Form.Concat(*parentMatrix); - } - for (int i = 0; i < FX_ArraySize(m_pFunctions); ++i) - m_pFunctions[i] = nullptr; -} - -CPDF_ShadingPattern::~CPDF_ShadingPattern() { - for (int i = 0; i < m_nFuncs; ++i) - delete m_pFunctions[i]; - - CPDF_ColorSpace* pCS = m_pCountedCS ? m_pCountedCS->get() : NULL; - if (pCS && m_pDocument) - m_pDocument->GetPageData()->ReleaseColorSpace(pCS->GetArray()); -} - -FX_BOOL CPDF_ShadingPattern::Load() { - if (m_ShadingType != kInvalidShading) - return TRUE; - - CPDF_Dictionary* pShadingDict = - m_pShadingObj ? m_pShadingObj->GetDict() : NULL; - if (!pShadingDict) { - return FALSE; - } - if (m_nFuncs) { - for (int i = 0; i < m_nFuncs; i++) - delete m_pFunctions[i]; - m_nFuncs = 0; - } - CPDF_Object* pFunc = pShadingDict->GetElementValue("Function"); - if (pFunc) { - if (CPDF_Array* pArray = pFunc->AsArray()) { - m_nFuncs = std::min<int>(pArray->GetCount(), 4); - for (int i = 0; i < m_nFuncs; i++) { - m_pFunctions[i] = CPDF_Function::Load(pArray->GetElementValue(i)); - } - } else { - m_pFunctions[0] = CPDF_Function::Load(pFunc); - m_nFuncs = 1; - } - } - CPDF_Object* pCSObj = pShadingDict->GetElementValue("ColorSpace"); - if (!pCSObj) { - return FALSE; - } - CPDF_DocPageData* pDocPageData = m_pDocument->GetPageData(); - m_pCS = pDocPageData->GetColorSpace(pCSObj, NULL); - if (m_pCS) { - m_pCountedCS = pDocPageData->FindColorSpacePtr(m_pCS->GetArray()); - } - - m_ShadingType = ToShadingType(pShadingDict->GetIntegerBy("ShadingType")); - - // We expect to have a stream if our shading type is a mesh. - if (IsMeshShading() && !ToStream(m_pShadingObj)) - return FALSE; - - return TRUE; -} -FX_BOOL CPDF_MeshStream::Load(CPDF_Stream* pShadingStream, - CPDF_Function** pFuncs, - int nFuncs, - CPDF_ColorSpace* pCS) { - m_Stream.LoadAllData(pShadingStream); - m_BitStream.Init(m_Stream.GetData(), m_Stream.GetSize()); - m_pFuncs = pFuncs; - m_nFuncs = nFuncs; - m_pCS = pCS; - CPDF_Dictionary* pDict = pShadingStream->GetDict(); - m_nCoordBits = pDict->GetIntegerBy("BitsPerCoordinate"); - m_nCompBits = pDict->GetIntegerBy("BitsPerComponent"); - m_nFlagBits = pDict->GetIntegerBy("BitsPerFlag"); - if (!m_nCoordBits || !m_nCompBits) { - return FALSE; - } - FX_DWORD nComps = pCS->CountComponents(); - if (nComps > 8) { - return FALSE; - } - m_nComps = nFuncs ? 1 : nComps; - if (((int)m_nComps < 0) || m_nComps > 8) { - return FALSE; - } - m_CoordMax = m_nCoordBits == 32 ? -1 : (1 << m_nCoordBits) - 1; - m_CompMax = (1 << m_nCompBits) - 1; - CPDF_Array* pDecode = pDict->GetArrayBy("Decode"); - if (!pDecode || pDecode->GetCount() != 4 + m_nComps * 2) { - return FALSE; - } - m_xmin = pDecode->GetNumberAt(0); - m_xmax = pDecode->GetNumberAt(1); - m_ymin = pDecode->GetNumberAt(2); - m_ymax = pDecode->GetNumberAt(3); - for (FX_DWORD i = 0; i < m_nComps; i++) { - m_ColorMin[i] = pDecode->GetNumberAt(i * 2 + 4); - m_ColorMax[i] = pDecode->GetNumberAt(i * 2 + 5); - } - return TRUE; -} -FX_DWORD CPDF_MeshStream::GetFlag() { - return m_BitStream.GetBits(m_nFlagBits) & 0x03; -} -void CPDF_MeshStream::GetCoords(FX_FLOAT& x, FX_FLOAT& y) { - if (m_nCoordBits == 32) { - x = m_xmin + (FX_FLOAT)(m_BitStream.GetBits(m_nCoordBits) * - (m_xmax - m_xmin) / (double)m_CoordMax); - y = m_ymin + (FX_FLOAT)(m_BitStream.GetBits(m_nCoordBits) * - (m_ymax - m_ymin) / (double)m_CoordMax); - } else { - x = m_xmin + - m_BitStream.GetBits(m_nCoordBits) * (m_xmax - m_xmin) / m_CoordMax; - y = m_ymin + - m_BitStream.GetBits(m_nCoordBits) * (m_ymax - m_ymin) / m_CoordMax; - } -} -void CPDF_MeshStream::GetColor(FX_FLOAT& r, FX_FLOAT& g, FX_FLOAT& b) { - FX_DWORD i; - FX_FLOAT color_value[8]; - for (i = 0; i < m_nComps; i++) { - color_value[i] = m_ColorMin[i] + - m_BitStream.GetBits(m_nCompBits) * - (m_ColorMax[i] - m_ColorMin[i]) / m_CompMax; - } - if (m_nFuncs) { - static const int kMaxResults = 8; - FX_FLOAT result[kMaxResults]; - int nResults; - FXSYS_memset(result, 0, sizeof(result)); - for (FX_DWORD i = 0; i < m_nFuncs; i++) { - if (m_pFuncs[i] && m_pFuncs[i]->CountOutputs() <= kMaxResults) { - m_pFuncs[i]->Call(color_value, 1, result, nResults); - } - } - m_pCS->GetRGB(result, r, g, b); - } else { - m_pCS->GetRGB(color_value, r, g, b); - } -} -FX_DWORD CPDF_MeshStream::GetVertex(CPDF_MeshVertex& vertex, - CFX_Matrix* pObject2Bitmap) { - FX_DWORD flag = GetFlag(); - GetCoords(vertex.x, vertex.y); - pObject2Bitmap->Transform(vertex.x, vertex.y); - GetColor(vertex.r, vertex.g, vertex.b); - m_BitStream.ByteAlign(); - return flag; -} -FX_BOOL CPDF_MeshStream::GetVertexRow(CPDF_MeshVertex* vertex, - int count, - CFX_Matrix* pObject2Bitmap) { - for (int i = 0; i < count; i++) { - if (m_BitStream.IsEOF()) { - return FALSE; - } - GetCoords(vertex[i].x, vertex[i].y); - pObject2Bitmap->Transform(vertex[i].x, vertex[i].y); - GetColor(vertex[i].r, vertex[i].g, vertex[i].b); - m_BitStream.ByteAlign(); - } - return TRUE; -} CFX_FloatRect GetShadingBBox(CPDF_Stream* pStream, ShadingType type, diff --git a/core/fpdfapi/fpdf_page/include/cpdf_color.h b/core/fpdfapi/fpdf_page/include/cpdf_color.h new file mode 100644 index 0000000000..9e20684d83 --- /dev/null +++ b/core/fpdfapi/fpdf_page/include/cpdf_color.h @@ -0,0 +1,47 @@ +// 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_FPDFAPI_FPDF_PAGE_INCLUDE_CPDF_COLOR_H_ +#define CORE_FPDFAPI_FPDF_PAGE_INCLUDE_CPDF_COLOR_H_ + +#include "core/fpdfapi/fpdf_page/include/cpdf_colorspace.h" +#include "core/fxcrt/include/fx_system.h" + +class CPDF_Pattern; + +class CPDF_Color { + public: + CPDF_Color() : m_pCS(nullptr), m_pBuffer(nullptr) {} + explicit CPDF_Color(int family); + ~CPDF_Color(); + + FX_BOOL IsNull() const { return !m_pBuffer; } + FX_BOOL IsEqual(const CPDF_Color& other) const; + FX_BOOL IsPattern() const { + return m_pCS && m_pCS->GetFamily() == PDFCS_PATTERN; + } + + void Copy(const CPDF_Color* pSrc); + + void SetColorSpace(CPDF_ColorSpace* pCS); + void SetValue(FX_FLOAT* comp); + void SetValue(CPDF_Pattern* pPattern, FX_FLOAT* comp, int ncomps); + + FX_BOOL GetRGB(int& R, int& G, int& B) const; + CPDF_Pattern* GetPattern() const; + CPDF_ColorSpace* GetPatternCS() const; + FX_FLOAT* GetPatternColor() const; + + CPDF_ColorSpace* m_pCS; + + protected: + void ReleaseBuffer(); + void ReleaseColorSpace(); + + FX_FLOAT* m_pBuffer; +}; + +#endif // CORE_FPDFAPI_FPDF_PAGE_INCLUDE_CPDF_COLOR_H_ diff --git a/core/fpdfapi/fpdf_page/include/cpdf_colorspace.h b/core/fpdfapi/fpdf_page/include/cpdf_colorspace.h new file mode 100644 index 0000000000..afdd71dc4d --- /dev/null +++ b/core/fpdfapi/fpdf_page/include/cpdf_colorspace.h @@ -0,0 +1,122 @@ +// 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_FPDFAPI_FPDF_PAGE_INCLUDE_CPDF_COLORSPACE_H_ +#define CORE_FPDFAPI_FPDF_PAGE_INCLUDE_CPDF_COLORSPACE_H_ + +#include "core/fxcrt/include/fx_string.h" +#include "core/fxcrt/include/fx_system.h" + +#define PDFCS_DEVICEGRAY 1 +#define PDFCS_DEVICERGB 2 +#define PDFCS_DEVICECMYK 3 +#define PDFCS_CALGRAY 4 +#define PDFCS_CALRGB 5 +#define PDFCS_LAB 6 +#define PDFCS_ICCBASED 7 +#define PDFCS_SEPARATION 8 +#define PDFCS_DEVICEN 9 +#define PDFCS_INDEXED 10 +#define PDFCS_PATTERN 11 + +class CPDF_Array; +class CPDF_Document; +class CPDF_Object; + +class CPDF_ColorSpace { + public: + static CPDF_ColorSpace* GetStockCS(int Family); + static CPDF_ColorSpace* Load(CPDF_Document* pDoc, CPDF_Object* pCSObj); + static CPDF_ColorSpace* ColorspaceFromName(const CFX_ByteString& name); + + void ReleaseCS(); + + int GetBufSize() const; + FX_FLOAT* CreateBuf(); + void GetDefaultColor(FX_FLOAT* buf) const; + FX_DWORD CountComponents() const { return m_nComponents; } + int GetFamily() const { return m_Family; } + virtual void GetDefaultValue(int iComponent, + FX_FLOAT& value, + FX_FLOAT& min, + FX_FLOAT& max) const { + value = 0; + min = 0; + max = 1.0f; + } + + FX_BOOL sRGB() const; + virtual FX_BOOL GetRGB(FX_FLOAT* pBuf, + FX_FLOAT& R, + FX_FLOAT& G, + FX_FLOAT& B) const = 0; + virtual FX_BOOL SetRGB(FX_FLOAT* pBuf, + FX_FLOAT R, + FX_FLOAT G, + FX_FLOAT B) const { + return FALSE; + } + + FX_BOOL GetCMYK(FX_FLOAT* pBuf, + FX_FLOAT& c, + FX_FLOAT& m, + FX_FLOAT& y, + FX_FLOAT& k) const; + FX_BOOL SetCMYK(FX_FLOAT* pBuf, + FX_FLOAT c, + FX_FLOAT m, + FX_FLOAT y, + FX_FLOAT k) const; + + virtual void TranslateImageLine(uint8_t* dest_buf, + const uint8_t* src_buf, + int pixels, + int image_width, + int image_height, + FX_BOOL bTransMask = FALSE) const; + + CPDF_Array*& GetArray() { return m_pArray; } + int GetMaxIndex() const; + virtual CPDF_ColorSpace* GetBaseCS() const { return NULL; } + + virtual void EnableStdConversion(FX_BOOL bEnabled); + + CPDF_Document* const m_pDocument; + + protected: + CPDF_ColorSpace(CPDF_Document* pDoc, int family, FX_DWORD nComponents) + : m_pDocument(pDoc), + m_Family(family), + m_nComponents(nComponents), + m_pArray(nullptr), + m_dwStdConversion(0) {} + virtual ~CPDF_ColorSpace() {} + + virtual FX_BOOL v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) { + return TRUE; + } + virtual FX_BOOL v_GetCMYK(FX_FLOAT* pBuf, + FX_FLOAT& c, + FX_FLOAT& m, + FX_FLOAT& y, + FX_FLOAT& k) const { + return FALSE; + } + virtual FX_BOOL v_SetCMYK(FX_FLOAT* pBuf, + FX_FLOAT c, + FX_FLOAT m, + FX_FLOAT y, + FX_FLOAT k) const { + return FALSE; + } + + int m_Family; + FX_DWORD m_nComponents; + CPDF_Array* m_pArray; + FX_DWORD m_dwStdConversion; +}; + +#endif // CORE_FPDFAPI_FPDF_PAGE_INCLUDE_CPDF_COLORSPACE_H_ diff --git a/core/fpdfapi/fpdf_page/include/cpdf_formobject.h b/core/fpdfapi/fpdf_page/include/cpdf_formobject.h index 234cd73328..b12d68c312 100644 --- a/core/fpdfapi/fpdf_page/include/cpdf_formobject.h +++ b/core/fpdfapi/fpdf_page/include/cpdf_formobject.h @@ -10,7 +10,7 @@ #include "core/fpdfapi/fpdf_page/include/cpdf_pageobject.h" #include "core/fxcrt/include/fx_coordinates.h" -class Form; +class CPDF_Form; class CPDF_FormObject : public CPDF_PageObject { public: diff --git a/core/fpdfapi/fpdf_page/include/cpdf_image.h b/core/fpdfapi/fpdf_page/include/cpdf_image.h new file mode 100644 index 0000000000..048b86d171 --- /dev/null +++ b/core/fpdfapi/fpdf_page/include/cpdf_image.h @@ -0,0 +1,98 @@ +// 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_FPDFAPI_FPDF_PAGE_INCLUDE_CPDF_IMAGE_H_ +#define CORE_FPDFAPI_FPDF_PAGE_INCLUDE_CPDF_IMAGE_H_ + +#include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h" +#include "core/fxcrt/include/fx_system.h" + +#define PDF_IMAGE_NO_COMPRESS 0x0000 +#define PDF_IMAGE_LOSSY_COMPRESS 0x0001 +#define PDF_IMAGE_LOSSLESS_COMPRESS 0x0002 +#define PDF_IMAGE_MASK_LOSSY_COMPRESS 0x0004 +#define PDF_IMAGE_MASK_LOSSLESS_COMPRESS 0x0008 + +class CFX_DIBitmap; +class CFX_DIBSource; +class CPDF_Dictionay; +class CPDF_Document; +class CPDF_Page; +class IFX_FileRead; +class IFX_FileWrite; +class IFX_Pause; + +class CPDF_Image { + public: + explicit CPDF_Image(CPDF_Document* pDoc); + ~CPDF_Image(); + + FX_BOOL LoadImageF(CPDF_Stream* pImageStream, FX_BOOL bInline); + + void Release(); + + CPDF_Image* Clone(); + + CPDF_Dictionary* GetInlineDict() const { return m_pInlineDict; } + CPDF_Stream* GetStream() const { return m_pStream; } + CPDF_Dictionary* GetDict() const { + return m_pStream ? m_pStream->GetDict() : NULL; + } + CPDF_Dictionary* GetOC() const { return m_pOC; } + CPDF_Document* GetDocument() const { return m_pDocument; } + + int32_t GetPixelHeight() const { return m_Height; } + int32_t GetPixelWidth() const { return m_Width; } + + FX_BOOL IsInline() { return m_bInline; } + FX_BOOL IsMask() const { return m_bIsMask; } + FX_BOOL IsInterpol() const { return m_bInterpolate; } + + CFX_DIBSource* LoadDIBSource(CFX_DIBSource** ppMask = NULL, + FX_DWORD* pMatteColor = NULL, + FX_BOOL bStdCS = FALSE, + FX_DWORD GroupFamily = 0, + FX_BOOL bLoadMask = FALSE) const; + + void SetInlineDict(CPDF_Dictionary* pDict) { m_pInlineDict = pDict; } + void SetImage(const CFX_DIBitmap* pDIBitmap, + int32_t iCompress, + IFX_FileWrite* pFileWrite = NULL, + IFX_FileRead* pFileRead = NULL, + const CFX_DIBitmap* pMask = NULL); + void SetJpegImage(uint8_t* pImageData, FX_DWORD size); + void SetJpegImage(IFX_FileRead* pFile); + + void ResetCache(CPDF_Page* pPage, const CFX_DIBitmap* pDIBitmap); + + FX_BOOL StartLoadDIBSource(CPDF_Dictionary* pFormResource, + CPDF_Dictionary* pPageResource, + FX_BOOL bStdCS = FALSE, + FX_DWORD GroupFamily = 0, + FX_BOOL bLoadMask = FALSE); + FX_BOOL Continue(IFX_Pause* pPause); + CFX_DIBSource* DetachBitmap(); + CFX_DIBSource* DetachMask(); + + CFX_DIBSource* m_pDIBSource; + CFX_DIBSource* m_pMask; + FX_DWORD m_MatteColor; + + private: + CPDF_Dictionary* InitJPEG(uint8_t* pData, FX_DWORD size); + + CPDF_Stream* m_pStream; + FX_BOOL m_bInline; + CPDF_Dictionary* m_pInlineDict; + int32_t m_Height; + int32_t m_Width; + FX_BOOL m_bIsMask; + FX_BOOL m_bInterpolate; + CPDF_Document* m_pDocument; + CPDF_Dictionary* m_pOC; +}; + +#endif // CORE_FPDFAPI_FPDF_PAGE_INCLUDE_CPDF_IMAGE_H_ diff --git a/core/fpdfapi/fpdf_page/pageint.h b/core/fpdfapi/fpdf_page/pageint.h index e6a150072e..d254477dc3 100644 --- a/core/fpdfapi/fpdf_page/pageint.h +++ b/core/fpdfapi/fpdf_page/pageint.h @@ -13,9 +13,9 @@ #include <vector> #include "core/fpdfapi/fpdf_page/cpdf_contentmark.h" +#include "core/fpdfapi/fpdf_page/cpdf_countedobject.h" #include "core/fpdfapi/fpdf_page/cpdf_parseoptions.h" #include "core/fpdfapi/fpdf_page/include/cpdf_pageobjectholder.h" -#include "core/include/fpdfapi/fpdf_resource.h" #include "core/include/fxge/fx_ge.h" class CPDF_AllStates; @@ -26,6 +26,7 @@ class CPDF_Form; class CPDF_IccProfile; class CPDF_Image; class CPDF_ImageObject; +class CPDF_Page; class CPDF_ParseOptions; class CPDF_Pattern; class CPDF_StreamAcc; @@ -438,10 +439,7 @@ class CPDF_DeviceCS : public CPDF_ColorSpace { class CPDF_PatternCS : public CPDF_ColorSpace { public: - explicit CPDF_PatternCS(CPDF_Document* pDoc) - : CPDF_ColorSpace(pDoc, PDFCS_PATTERN, 1), - m_pBaseCS(nullptr), - m_pCountedBaseCS(nullptr) {} + explicit CPDF_PatternCS(CPDF_Document* pDoc); ~CPDF_PatternCS() override; FX_BOOL v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) override; FX_BOOL GetRGB(FX_FLOAT* pBuf, @@ -455,6 +453,14 @@ class CPDF_PatternCS : public CPDF_ColorSpace { CPDF_CountedColorSpace* m_pCountedBaseCS; }; +#define MAX_PATTERN_COLORCOMPS 16 +struct PatternValue { + CPDF_Pattern* m_pPattern; + CPDF_CountedPattern* m_pCountedPattern; + int m_nComps; + FX_FLOAT m_Comps[MAX_PATTERN_COLORCOMPS]; +}; + void PDF_ReplaceAbbr(CPDF_Object* pObj); bool IsPathOperator(const uint8_t* buf, size_t len); |