1 // Copyright 2014 PDFium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 6 7 #include <limits.h> 8 class CCodec_BasicModule : public ICodec_BasicModule 9 { 10 public: 11 virtual FX_BOOL RunLengthEncode(const FX_BYTE* src_buf, FX_DWORD src_size, FX_LPBYTE& dest_buf, 12 FX_DWORD& dest_size); 13 virtual FX_BOOL A85Encode(const FX_BYTE* src_buf, FX_DWORD src_size, FX_LPBYTE& dest_buf, 14 FX_DWORD& dest_size); 15 virtual ICodec_ScanlineDecoder* CreateRunLengthDecoder(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height, 16 int nComps, int bpc); 17 }; 18 struct CCodec_ImageDataCache { 19 int m_Width, m_Height; 20 int m_nCachedLines; 21 FX_BYTE m_Data; 22 }; 23 class CCodec_ScanlineDecoder : public ICodec_ScanlineDecoder 24 { 25 public: 26 27 CCodec_ScanlineDecoder(); 28 29 virtual ~CCodec_ScanlineDecoder(); 30 GetSrcOffset()31 virtual FX_DWORD GetSrcOffset() 32 { 33 return -1; 34 } 35 36 virtual void DownScale(int dest_width, int dest_height); 37 38 FX_LPBYTE GetScanline(int line); 39 40 FX_BOOL SkipToScanline(int line, IFX_Pause* pPause); 41 GetWidth()42 int GetWidth() 43 { 44 return m_OutputWidth; 45 } 46 GetHeight()47 int GetHeight() 48 { 49 return m_OutputHeight; 50 } 51 CountComps()52 int CountComps() 53 { 54 return m_nComps; 55 } 56 GetBPC()57 int GetBPC() 58 { 59 return m_bpc; 60 } 61 IsColorTransformed()62 FX_BOOL IsColorTransformed() 63 { 64 return m_bColorTransformed; 65 } 66 ClearImageData()67 void ClearImageData() 68 { 69 if (m_pDataCache) { 70 FX_Free(m_pDataCache); 71 } 72 m_pDataCache = NULL; 73 } 74 protected: 75 76 int m_OrigWidth; 77 78 int m_OrigHeight; 79 80 int m_DownScale; 81 82 int m_OutputWidth; 83 84 int m_OutputHeight; 85 86 int m_nComps; 87 88 int m_bpc; 89 90 int m_Pitch; 91 92 FX_BOOL m_bColorTransformed; 93 94 FX_LPBYTE ReadNextLine(); 95 96 virtual FX_BOOL v_Rewind() = 0; 97 98 virtual FX_LPBYTE v_GetNextLine() = 0; 99 100 virtual void v_DownScale(int dest_width, int dest_height) = 0; 101 102 int m_NextLine; 103 104 FX_LPBYTE m_pLastScanline; 105 106 CCodec_ImageDataCache* m_pDataCache; 107 }; 108 class CCodec_FaxModule : public ICodec_FaxModule 109 { 110 public: 111 virtual ICodec_ScanlineDecoder* CreateDecoder(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height, 112 int K, FX_BOOL EndOfLine, FX_BOOL EncodedByteAlign, FX_BOOL BlackIs1, int Columns, int Rows); 113 FX_BOOL Encode(FX_LPCBYTE src_buf, int width, int height, int pitch, FX_LPBYTE& dest_buf, FX_DWORD& dest_size); 114 }; 115 class CCodec_FlateModule : public ICodec_FlateModule 116 { 117 public: 118 virtual ICodec_ScanlineDecoder* CreateDecoder(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height, 119 int nComps, int bpc, int predictor, int Colors, int BitsPerComponent, int Columns); 120 virtual FX_DWORD FlateOrLZWDecode(FX_BOOL bLZW, const FX_BYTE* src_buf, FX_DWORD src_size, FX_BOOL bEarlyChange, 121 int predictor, int Colors, int BitsPerComponent, int Columns, 122 FX_DWORD estimated_size, FX_LPBYTE& dest_buf, FX_DWORD& dest_size); 123 virtual FX_BOOL Encode(const FX_BYTE* src_buf, FX_DWORD src_size, 124 int predictor, int Colors, int BitsPerComponent, int Columns, 125 FX_LPBYTE& dest_buf, FX_DWORD& dest_size); 126 virtual FX_BOOL Encode(FX_LPCBYTE src_buf, FX_DWORD src_size, FX_LPBYTE& dest_buf, FX_DWORD& dest_size); 127 }; 128 class CCodec_JpegModule : public ICodec_JpegModule 129 { 130 public: CCodec_JpegModule()131 CCodec_JpegModule() : m_pExtProvider(NULL) {} SetPovider(IFX_JpegProvider * pJP)132 void SetPovider(IFX_JpegProvider* pJP) 133 { 134 m_pExtProvider = pJP; 135 } 136 ICodec_ScanlineDecoder* CreateDecoder(FX_LPCBYTE src_buf, FX_DWORD src_size, 137 int width, int height, int nComps, FX_BOOL ColorTransform); 138 FX_BOOL LoadInfo(FX_LPCBYTE src_buf, FX_DWORD src_size, int& width, int& height, 139 int& num_components, int& bits_per_components, FX_BOOL& color_transform, 140 FX_LPBYTE* icc_buf_ptr, FX_DWORD* icc_length); 141 FX_BOOL Encode(const CFX_DIBSource* pSource, FX_LPBYTE& dest_buf, FX_STRSIZE& dest_size, int quality, FX_LPCBYTE icc_buf, FX_DWORD icc_length); 142 virtual void* Start(); 143 virtual void Finish(void* pContext); 144 virtual void Input(void* pContext, FX_LPCBYTE src_buf, FX_DWORD src_size); 145 virtual int ReadHeader(void* pContext, int* width, int* height, int* nComps); 146 virtual FX_BOOL StartScanline(void* pContext, int down_scale); 147 virtual FX_BOOL ReadScanline(void* pContext, FX_LPBYTE dest_buf); 148 virtual FX_DWORD GetAvailInput(void* pContext, FX_LPBYTE* avail_buf_ptr); 149 protected: 150 IFX_JpegProvider* m_pExtProvider; 151 }; 152 class CCodec_IccModule : public ICodec_IccModule 153 { 154 public: 155 virtual IccCS GetProfileCS(FX_LPCBYTE pProfileData, unsigned int dwProfileSize); 156 virtual IccCS GetProfileCS(IFX_FileRead* pFile); 157 virtual FX_LPVOID CreateTransform(ICodec_IccModule::IccParam* pInputParam, 158 ICodec_IccModule::IccParam* pOutputParam, 159 ICodec_IccModule::IccParam* pProofParam = NULL, 160 FX_DWORD dwIntent = Icc_INTENT_PERCEPTUAL, 161 FX_DWORD dwFlag = Icc_FLAGS_DEFAULT, 162 FX_DWORD dwPrfIntent = Icc_INTENT_ABSOLUTE_COLORIMETRIC, 163 FX_DWORD dwPrfFlag = Icc_FLAGS_SOFTPROOFING 164 ); 165 virtual FX_LPVOID CreateTransform_sRGB(FX_LPCBYTE pProfileData, unsigned int dwProfileSize, int nComponents, int intent = 0, 166 FX_DWORD dwSrcFormat = Icc_FORMAT_DEFAULT); 167 virtual FX_LPVOID CreateTransform_CMYK(FX_LPCBYTE pSrcProfileData, unsigned int dwSrcProfileSize, int nSrcComponents, 168 FX_LPCBYTE pDstProfileData, unsigned int dwDstProfileSize, int intent = 0, 169 FX_DWORD dwSrcFormat = Icc_FORMAT_DEFAULT, 170 FX_DWORD dwDstFormat = Icc_FORMAT_DEFAULT 171 ); 172 virtual void DestroyTransform(FX_LPVOID pTransform); 173 virtual void Translate(FX_LPVOID pTransform, FX_FLOAT* pSrcValues, FX_FLOAT* pDestValues); 174 virtual void TranslateScanline(FX_LPVOID pTransform, FX_LPBYTE pDest, FX_LPCBYTE pSrc, int pixels); 175 virtual ~CCodec_IccModule(); 176 protected: 177 CFX_MapByteStringToPtr m_MapTranform; 178 CFX_MapByteStringToPtr m_MapProfile; 179 typedef enum { 180 Icc_CLASS_INPUT = 0, 181 Icc_CLASS_OUTPUT, 182 Icc_CLASS_PROOF, 183 Icc_CLASS_MAX 184 } Icc_CLASS; 185 FX_LPVOID CreateProfile(ICodec_IccModule::IccParam* pIccParam, Icc_CLASS ic, CFX_BinaryBuf* pTransformKey); 186 }; 187 class CCodec_JpxModule : public ICodec_JpxModule 188 { 189 public: 190 CCodec_JpxModule(); 191 void* CreateDecoder(FX_LPCBYTE src_buf, FX_DWORD src_size, FX_BOOL useColorSpace = FALSE); 192 void GetImageInfo(FX_LPVOID ctx, FX_DWORD& width, FX_DWORD& height, 193 FX_DWORD& codestream_nComps, FX_DWORD& output_nComps); 194 FX_BOOL Decode(void* ctx, FX_LPBYTE dest_data, int pitch, FX_BOOL bTranslateColor, FX_LPBYTE offsets); 195 void DestroyDecoder(void* ctx); 196 }; 197 #include "../jbig2/JBig2_Context.h" 198 class CPDF_Jbig2Interface : public CFX_Object, public CJBig2_Module 199 { 200 public: JBig2_Malloc(FX_DWORD dwSize)201 virtual void *JBig2_Malloc(FX_DWORD dwSize) 202 { 203 return FX_Alloc(FX_BYTE, dwSize); 204 } JBig2_Malloc2(FX_DWORD num,FX_DWORD dwSize)205 virtual void *JBig2_Malloc2(FX_DWORD num, FX_DWORD dwSize) 206 { 207 if (dwSize && num >= UINT_MAX / dwSize) { 208 return NULL; 209 } 210 return FX_Alloc(FX_BYTE, num * dwSize); 211 } JBig2_Malloc3(FX_DWORD num,FX_DWORD dwSize,FX_DWORD dwSize2)212 virtual void *JBig2_Malloc3(FX_DWORD num, FX_DWORD dwSize, FX_DWORD dwSize2) 213 { 214 if (dwSize2 && dwSize >= UINT_MAX / dwSize2) { 215 return NULL; 216 } 217 FX_DWORD size = dwSize2 * dwSize; 218 if (size && num >= UINT_MAX / size) { 219 return NULL; 220 } 221 return FX_Alloc(FX_BYTE, num * size); 222 } JBig2_Realloc(FX_LPVOID pMem,FX_DWORD dwSize)223 virtual void *JBig2_Realloc(FX_LPVOID pMem, FX_DWORD dwSize) 224 { 225 return FX_Realloc(FX_BYTE, pMem, dwSize); 226 } JBig2_Free(FX_LPVOID pMem)227 virtual void JBig2_Free(FX_LPVOID pMem) 228 { 229 FX_Free(pMem); 230 } 231 }; 232 class CCodec_Jbig2Context : public CFX_Object 233 { 234 public: 235 CCodec_Jbig2Context(); ~CCodec_Jbig2Context()236 ~CCodec_Jbig2Context() {}; 237 IFX_FileRead* m_file_ptr; 238 FX_DWORD m_width; 239 FX_DWORD m_height; 240 FX_LPBYTE m_src_buf; 241 FX_DWORD m_src_size; 242 FX_LPCBYTE m_global_data; 243 FX_DWORD m_global_size; 244 FX_LPBYTE m_dest_buf; 245 FX_DWORD m_dest_pitch; 246 FX_BOOL m_bFileReader; 247 IFX_Pause* m_pPause; 248 CJBig2_Context* m_pContext; 249 CJBig2_Image* m_dest_image; 250 }; 251 class CCodec_Jbig2Module : public ICodec_Jbig2Module 252 { 253 public: CCodec_Jbig2Module()254 CCodec_Jbig2Module() {}; 255 ~CCodec_Jbig2Module(); 256 FX_BOOL Decode(FX_DWORD width, FX_DWORD height, FX_LPCBYTE src_buf, FX_DWORD src_size, 257 FX_LPCBYTE global_data, FX_DWORD global_size, FX_LPBYTE dest_buf, FX_DWORD dest_pitch); 258 FX_BOOL Decode(IFX_FileRead* file_ptr, 259 FX_DWORD& width, FX_DWORD& height, FX_DWORD& pitch, FX_LPBYTE& dest_buf); 260 void* CreateJbig2Context(); 261 FXCODEC_STATUS StartDecode(void* pJbig2Context, FX_DWORD width, FX_DWORD height, FX_LPCBYTE src_buf, FX_DWORD src_size, 262 FX_LPCBYTE global_data, FX_DWORD global_size, FX_LPBYTE dest_buf, FX_DWORD dest_pitch, IFX_Pause* pPause); 263 264 FXCODEC_STATUS StartDecode(void* pJbig2Context, IFX_FileRead* file_ptr, 265 FX_DWORD& width, FX_DWORD& height, FX_DWORD& pitch, FX_LPBYTE& dest_buf, IFX_Pause* pPause); 266 FXCODEC_STATUS ContinueDecode(void* pJbig2Context, IFX_Pause* pPause); 267 void DestroyJbig2Context(void* pJbig2Context); 268 CPDF_Jbig2Interface m_Module; 269 private: 270 }; 271