| OLD | NEW |
| (Empty) |
| 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 #ifndef CORE_SRC_FXCODEC_CODEC_CODEC_INT_H_ | |
| 8 #define CORE_SRC_FXCODEC_CODEC_CODEC_INT_H_ | |
| 9 | |
| 10 #include <limits.h> | |
| 11 | |
| 12 #include <list> | |
| 13 #include <map> | |
| 14 #include <memory> | |
| 15 #include <vector> | |
| 16 | |
| 17 #include "core/include/fxcodec/fx_codec.h" | |
| 18 #include "core/src/fxcodec/jbig2/JBig2_Context.h" | |
| 19 #include "third_party/libopenjpeg20/openjpeg.h" // For OPJ_SIZE_T. | |
| 20 | |
| 21 class CFX_IccProfileCache; | |
| 22 class CFX_IccTransformCache; | |
| 23 class CPDF_ColorSpace; | |
| 24 | |
| 25 class CCodec_BasicModule : public ICodec_BasicModule { | |
| 26 public: | |
| 27 // ICodec_BasicModule: | |
| 28 FX_BOOL RunLengthEncode(const uint8_t* src_buf, | |
| 29 FX_DWORD src_size, | |
| 30 uint8_t*& dest_buf, | |
| 31 FX_DWORD& dest_size) override; | |
| 32 FX_BOOL A85Encode(const uint8_t* src_buf, | |
| 33 FX_DWORD src_size, | |
| 34 uint8_t*& dest_buf, | |
| 35 FX_DWORD& dest_size) override; | |
| 36 ICodec_ScanlineDecoder* CreateRunLengthDecoder(const uint8_t* src_buf, | |
| 37 FX_DWORD src_size, | |
| 38 int width, | |
| 39 int height, | |
| 40 int nComps, | |
| 41 int bpc) override; | |
| 42 }; | |
| 43 | |
| 44 class CCodec_ScanlineDecoder : public ICodec_ScanlineDecoder { | |
| 45 public: | |
| 46 CCodec_ScanlineDecoder(); | |
| 47 ~CCodec_ScanlineDecoder() override; | |
| 48 | |
| 49 // ICodec_ScanlineDecoder | |
| 50 FX_DWORD GetSrcOffset() override { return -1; } | |
| 51 void DownScale(int dest_width, int dest_height) override; | |
| 52 const uint8_t* GetScanline(int line) override; | |
| 53 FX_BOOL SkipToScanline(int line, IFX_Pause* pPause) override; | |
| 54 int GetWidth() override { return m_OutputWidth; } | |
| 55 int GetHeight() override { return m_OutputHeight; } | |
| 56 int CountComps() override { return m_nComps; } | |
| 57 int GetBPC() override { return m_bpc; } | |
| 58 FX_BOOL IsColorTransformed() override { return m_bColorTransformed; } | |
| 59 void ClearImageData() override { m_pDataCache.reset(); } | |
| 60 | |
| 61 protected: | |
| 62 class ImageDataCache { | |
| 63 public: | |
| 64 ImageDataCache(int width, int height, FX_DWORD pitch); | |
| 65 ~ImageDataCache(); | |
| 66 | |
| 67 bool AllocateCache(); | |
| 68 void AppendLine(const uint8_t* line); | |
| 69 | |
| 70 int NumLines() const { return m_nCachedLines; } | |
| 71 const uint8_t* GetLine(int line) const; | |
| 72 bool IsSameDimensions(int width, int height) const { | |
| 73 return width == m_Width && height == m_Height; | |
| 74 } | |
| 75 | |
| 76 private: | |
| 77 bool IsValid() const { return m_Data.get() != nullptr; } | |
| 78 | |
| 79 const int m_Width; | |
| 80 const int m_Height; | |
| 81 const FX_DWORD m_Pitch; | |
| 82 int m_nCachedLines; | |
| 83 std::unique_ptr<uint8_t, FxFreeDeleter> m_Data; | |
| 84 }; | |
| 85 | |
| 86 virtual FX_BOOL v_Rewind() = 0; | |
| 87 virtual uint8_t* v_GetNextLine() = 0; | |
| 88 virtual void v_DownScale(int dest_width, int dest_height) = 0; | |
| 89 | |
| 90 uint8_t* ReadNextLine(); | |
| 91 | |
| 92 int m_OrigWidth; | |
| 93 int m_OrigHeight; | |
| 94 int m_DownScale; | |
| 95 int m_OutputWidth; | |
| 96 int m_OutputHeight; | |
| 97 int m_nComps; | |
| 98 int m_bpc; | |
| 99 FX_DWORD m_Pitch; | |
| 100 FX_BOOL m_bColorTransformed; | |
| 101 int m_NextLine; | |
| 102 uint8_t* m_pLastScanline; | |
| 103 std::unique_ptr<ImageDataCache> m_pDataCache; | |
| 104 }; | |
| 105 | |
| 106 class CCodec_FaxModule : public ICodec_FaxModule { | |
| 107 public: | |
| 108 // ICodec_FaxModule: | |
| 109 ICodec_ScanlineDecoder* CreateDecoder(const uint8_t* src_buf, | |
| 110 FX_DWORD src_size, | |
| 111 int width, | |
| 112 int height, | |
| 113 int K, | |
| 114 FX_BOOL EndOfLine, | |
| 115 FX_BOOL EncodedByteAlign, | |
| 116 FX_BOOL BlackIs1, | |
| 117 int Columns, | |
| 118 int Rows) override; | |
| 119 FX_BOOL Encode(const uint8_t* src_buf, | |
| 120 int width, | |
| 121 int height, | |
| 122 int pitch, | |
| 123 uint8_t*& dest_buf, | |
| 124 FX_DWORD& dest_size) override; | |
| 125 }; | |
| 126 | |
| 127 class CCodec_FlateModule : public ICodec_FlateModule { | |
| 128 public: | |
| 129 virtual ICodec_ScanlineDecoder* CreateDecoder(const uint8_t* src_buf, | |
| 130 FX_DWORD src_size, | |
| 131 int width, | |
| 132 int height, | |
| 133 int nComps, | |
| 134 int bpc, | |
| 135 int predictor, | |
| 136 int Colors, | |
| 137 int BitsPerComponent, | |
| 138 int Columns); | |
| 139 virtual FX_DWORD FlateOrLZWDecode(FX_BOOL bLZW, | |
| 140 const uint8_t* src_buf, | |
| 141 FX_DWORD src_size, | |
| 142 FX_BOOL bEarlyChange, | |
| 143 int predictor, | |
| 144 int Colors, | |
| 145 int BitsPerComponent, | |
| 146 int Columns, | |
| 147 FX_DWORD estimated_size, | |
| 148 uint8_t*& dest_buf, | |
| 149 FX_DWORD& dest_size); | |
| 150 virtual FX_BOOL Encode(const uint8_t* src_buf, | |
| 151 FX_DWORD src_size, | |
| 152 int predictor, | |
| 153 int Colors, | |
| 154 int BitsPerComponent, | |
| 155 int Columns, | |
| 156 uint8_t*& dest_buf, | |
| 157 FX_DWORD& dest_size); | |
| 158 virtual FX_BOOL Encode(const uint8_t* src_buf, | |
| 159 FX_DWORD src_size, | |
| 160 uint8_t*& dest_buf, | |
| 161 FX_DWORD& dest_size); | |
| 162 }; | |
| 163 | |
| 164 class CCodec_JpegModule : public ICodec_JpegModule { | |
| 165 public: | |
| 166 CCodec_JpegModule() {} | |
| 167 ICodec_ScanlineDecoder* CreateDecoder(const uint8_t* src_buf, | |
| 168 FX_DWORD src_size, | |
| 169 int width, | |
| 170 int height, | |
| 171 int nComps, | |
| 172 FX_BOOL ColorTransform) override; | |
| 173 FX_BOOL LoadInfo(const uint8_t* src_buf, | |
| 174 FX_DWORD src_size, | |
| 175 int& width, | |
| 176 int& height, | |
| 177 int& num_components, | |
| 178 int& bits_per_components, | |
| 179 FX_BOOL& color_transform, | |
| 180 uint8_t** icc_buf_ptr, | |
| 181 FX_DWORD* icc_length) override; | |
| 182 FX_BOOL Encode(const CFX_DIBSource* pSource, | |
| 183 uint8_t*& dest_buf, | |
| 184 FX_STRSIZE& dest_size, | |
| 185 int quality, | |
| 186 const uint8_t* icc_buf, | |
| 187 FX_DWORD icc_length) override; | |
| 188 void* Start() override; | |
| 189 void Finish(void* pContext) override; | |
| 190 void Input(void* pContext, | |
| 191 const uint8_t* src_buf, | |
| 192 FX_DWORD src_size) override; | |
| 193 #ifndef PDF_ENABLE_XFA | |
| 194 int ReadHeader(void* pContext, int* width, int* height, int* nComps) override; | |
| 195 #else // PDF_ENABLE_XFA | |
| 196 int ReadHeader(void* pContext, | |
| 197 int* width, | |
| 198 int* height, | |
| 199 int* nComps, | |
| 200 CFX_DIBAttribute* pAttribute) override; | |
| 201 #endif // PDF_ENABLE_XFA | |
| 202 int StartScanline(void* pContext, int down_scale) override; | |
| 203 FX_BOOL ReadScanline(void* pContext, uint8_t* dest_buf) override; | |
| 204 FX_DWORD GetAvailInput(void* pContext, uint8_t** avail_buf_ptr) override; | |
| 205 }; | |
| 206 | |
| 207 #ifdef PDF_ENABLE_XFA | |
| 208 #define PNG_ERROR_SIZE 256 | |
| 209 class CCodec_PngModule : public ICodec_PngModule { | |
| 210 public: | |
| 211 CCodec_PngModule() { FXSYS_memset(m_szLastError, '\0', PNG_ERROR_SIZE); } | |
| 212 | |
| 213 virtual void* Start(void* pModule); | |
| 214 virtual void Finish(void* pContext); | |
| 215 virtual FX_BOOL Input(void* pContext, | |
| 216 const uint8_t* src_buf, | |
| 217 FX_DWORD src_size, | |
| 218 CFX_DIBAttribute* pAttribute); | |
| 219 | |
| 220 protected: | |
| 221 FX_CHAR m_szLastError[PNG_ERROR_SIZE]; | |
| 222 }; | |
| 223 class CCodec_GifModule : public ICodec_GifModule { | |
| 224 public: | |
| 225 CCodec_GifModule() { FXSYS_memset(m_szLastError, '\0', 256); } | |
| 226 virtual void* Start(void* pModule); | |
| 227 virtual void Finish(void* pContext); | |
| 228 virtual FX_DWORD GetAvailInput(void* pContext, uint8_t** avail_buf_ptr); | |
| 229 virtual void Input(void* pContext, const uint8_t* src_buf, FX_DWORD src_size); | |
| 230 | |
| 231 virtual int32_t ReadHeader(void* pContext, | |
| 232 int* width, | |
| 233 int* height, | |
| 234 int* pal_num, | |
| 235 void** pal_pp, | |
| 236 int* bg_index, | |
| 237 CFX_DIBAttribute* pAttribute); | |
| 238 | |
| 239 virtual int32_t LoadFrameInfo(void* pContext, int* frame_num); | |
| 240 | |
| 241 virtual int32_t LoadFrame(void* pContext, | |
| 242 int frame_num, | |
| 243 CFX_DIBAttribute* pAttribute); | |
| 244 | |
| 245 protected: | |
| 246 FX_CHAR m_szLastError[256]; | |
| 247 }; | |
| 248 class CCodec_BmpModule : public ICodec_BmpModule { | |
| 249 public: | |
| 250 CCodec_BmpModule() { FXSYS_memset(m_szLastError, 0, sizeof(m_szLastError)); } | |
| 251 void* Start(void* pModule) override; | |
| 252 void Finish(void* pContext) override; | |
| 253 FX_DWORD GetAvailInput(void* pContext, uint8_t** avail_buf_ptr) override; | |
| 254 void Input(void* pContext, | |
| 255 const uint8_t* src_buf, | |
| 256 FX_DWORD src_size) override; | |
| 257 int32_t ReadHeader(void* pContext, | |
| 258 int32_t* width, | |
| 259 int32_t* height, | |
| 260 FX_BOOL* tb_flag, | |
| 261 int32_t* components, | |
| 262 int32_t* pal_num, | |
| 263 FX_DWORD** pal_pp, | |
| 264 CFX_DIBAttribute* pAttribute) override; | |
| 265 int32_t LoadImage(void* pContext) override; | |
| 266 | |
| 267 protected: | |
| 268 FX_CHAR m_szLastError[256]; | |
| 269 }; | |
| 270 #endif // PDF_ENABLE_XFA | |
| 271 | |
| 272 class CCodec_IccModule : public ICodec_IccModule { | |
| 273 public: | |
| 274 ~CCodec_IccModule() override; | |
| 275 | |
| 276 // ICodec_IccModule: | |
| 277 IccCS GetProfileCS(const uint8_t* pProfileData, | |
| 278 unsigned int dwProfileSize) override; | |
| 279 IccCS GetProfileCS(IFX_FileRead* pFile) override; | |
| 280 void* CreateTransform(ICodec_IccModule::IccParam* pInputParam, | |
| 281 ICodec_IccModule::IccParam* pOutputParam, | |
| 282 ICodec_IccModule::IccParam* pProofParam = NULL, | |
| 283 FX_DWORD dwIntent = Icc_INTENT_PERCEPTUAL, | |
| 284 FX_DWORD dwFlag = Icc_FLAGS_DEFAULT, | |
| 285 FX_DWORD dwPrfIntent = Icc_INTENT_ABSOLUTE_COLORIMETRIC, | |
| 286 FX_DWORD dwPrfFlag = Icc_FLAGS_SOFTPROOFING) override; | |
| 287 void* CreateTransform_sRGB( | |
| 288 const uint8_t* pProfileData, | |
| 289 FX_DWORD dwProfileSize, | |
| 290 FX_DWORD& nComponents, | |
| 291 int32_t intent = 0, | |
| 292 FX_DWORD dwSrcFormat = Icc_FORMAT_DEFAULT) override; | |
| 293 void* CreateTransform_CMYK( | |
| 294 const uint8_t* pSrcProfileData, | |
| 295 FX_DWORD dwSrcProfileSize, | |
| 296 FX_DWORD& nSrcComponents, | |
| 297 const uint8_t* pDstProfileData, | |
| 298 FX_DWORD dwDstProfileSize, | |
| 299 int32_t intent = 0, | |
| 300 FX_DWORD dwSrcFormat = Icc_FORMAT_DEFAULT, | |
| 301 FX_DWORD dwDstFormat = Icc_FORMAT_DEFAULT) override; | |
| 302 void DestroyTransform(void* pTransform) override; | |
| 303 void Translate(void* pTransform, | |
| 304 FX_FLOAT* pSrcValues, | |
| 305 FX_FLOAT* pDestValues) override; | |
| 306 void TranslateScanline(void* pTransform, | |
| 307 uint8_t* pDest, | |
| 308 const uint8_t* pSrc, | |
| 309 int pixels) override; | |
| 310 void SetComponents(FX_DWORD nComponents) override { | |
| 311 m_nComponents = nComponents; | |
| 312 } | |
| 313 | |
| 314 protected: | |
| 315 enum Icc_CLASS { | |
| 316 Icc_CLASS_INPUT = 0, | |
| 317 Icc_CLASS_OUTPUT, | |
| 318 Icc_CLASS_PROOF, | |
| 319 Icc_CLASS_MAX | |
| 320 }; | |
| 321 void* CreateProfile(ICodec_IccModule::IccParam* pIccParam, | |
| 322 Icc_CLASS ic, | |
| 323 CFX_BinaryBuf* pTransformKey); | |
| 324 | |
| 325 FX_DWORD m_nComponents; | |
| 326 std::map<CFX_ByteString, CFX_IccTransformCache*> m_MapTranform; | |
| 327 std::map<CFX_ByteString, CFX_IccProfileCache*> m_MapProfile; | |
| 328 }; | |
| 329 | |
| 330 class CCodec_JpxModule : public ICodec_JpxModule { | |
| 331 public: | |
| 332 CCodec_JpxModule(); | |
| 333 ~CCodec_JpxModule() override; | |
| 334 | |
| 335 // ICodec_JpxModule: | |
| 336 CJPX_Decoder* CreateDecoder(const uint8_t* src_buf, | |
| 337 FX_DWORD src_size, | |
| 338 CPDF_ColorSpace* cs) override; | |
| 339 void GetImageInfo(CJPX_Decoder* pDecoder, | |
| 340 FX_DWORD* width, | |
| 341 FX_DWORD* height, | |
| 342 FX_DWORD* components) override; | |
| 343 bool Decode(CJPX_Decoder* pDecoder, | |
| 344 uint8_t* dest_data, | |
| 345 int pitch, | |
| 346 const std::vector<uint8_t>& offsets) override; | |
| 347 void DestroyDecoder(CJPX_Decoder* pDecoder) override; | |
| 348 }; | |
| 349 | |
| 350 #ifdef PDF_ENABLE_XFA | |
| 351 class CCodec_TiffModule : public ICodec_TiffModule { | |
| 352 public: | |
| 353 // ICodec_TiffModule | |
| 354 void* CreateDecoder(IFX_FileRead* file_ptr) override; | |
| 355 void GetFrames(void* ctx, int32_t& frames) override; | |
| 356 FX_BOOL LoadFrameInfo(void* ctx, | |
| 357 int32_t frame, | |
| 358 FX_DWORD& width, | |
| 359 FX_DWORD& height, | |
| 360 FX_DWORD& comps, | |
| 361 FX_DWORD& bpc, | |
| 362 CFX_DIBAttribute* pAttribute) override; | |
| 363 FX_BOOL Decode(void* ctx, class CFX_DIBitmap* pDIBitmap) override; | |
| 364 void DestroyDecoder(void* ctx) override; | |
| 365 | |
| 366 protected: | |
| 367 ~CCodec_TiffModule() override {} | |
| 368 }; | |
| 369 #endif // PDF_ENABLE_XFA | |
| 370 | |
| 371 class CCodec_Jbig2Context { | |
| 372 public: | |
| 373 CCodec_Jbig2Context(); | |
| 374 ~CCodec_Jbig2Context() {} | |
| 375 | |
| 376 FX_DWORD m_width; | |
| 377 FX_DWORD m_height; | |
| 378 CPDF_StreamAcc* m_pGlobalStream; | |
| 379 CPDF_StreamAcc* m_pSrcStream; | |
| 380 uint8_t* m_dest_buf; | |
| 381 FX_DWORD m_dest_pitch; | |
| 382 IFX_Pause* m_pPause; | |
| 383 CJBig2_Context* m_pContext; | |
| 384 CJBig2_Image* m_dest_image; | |
| 385 }; | |
| 386 class CCodec_Jbig2Module : public ICodec_Jbig2Module { | |
| 387 public: | |
| 388 CCodec_Jbig2Module() {} | |
| 389 ~CCodec_Jbig2Module() override; | |
| 390 | |
| 391 // ICodec_Jbig2Module | |
| 392 void* CreateJbig2Context() override; | |
| 393 FXCODEC_STATUS StartDecode(void* pJbig2Context, | |
| 394 CFX_PrivateData* pPrivateData, | |
| 395 FX_DWORD width, | |
| 396 FX_DWORD height, | |
| 397 CPDF_StreamAcc* src_stream, | |
| 398 CPDF_StreamAcc* global_stream, | |
| 399 uint8_t* dest_buf, | |
| 400 FX_DWORD dest_pitch, | |
| 401 IFX_Pause* pPause) override; | |
| 402 FXCODEC_STATUS ContinueDecode(void* pJbig2Context, | |
| 403 IFX_Pause* pPause) override; | |
| 404 void DestroyJbig2Context(void* pJbig2Context) override; | |
| 405 }; | |
| 406 | |
| 407 struct DecodeData { | |
| 408 public: | |
| 409 DecodeData(unsigned char* src_data, OPJ_SIZE_T src_size) | |
| 410 : src_data(src_data), src_size(src_size), offset(0) {} | |
| 411 unsigned char* src_data; | |
| 412 OPJ_SIZE_T src_size; | |
| 413 OPJ_SIZE_T offset; | |
| 414 }; | |
| 415 | |
| 416 void sycc420_to_rgb(opj_image_t* img); | |
| 417 | |
| 418 /* Wrappers for C-style callbacks. */ | |
| 419 OPJ_SIZE_T opj_read_from_memory(void* p_buffer, | |
| 420 OPJ_SIZE_T nb_bytes, | |
| 421 void* p_user_data); | |
| 422 OPJ_SIZE_T opj_write_from_memory(void* p_buffer, | |
| 423 OPJ_SIZE_T nb_bytes, | |
| 424 void* p_user_data); | |
| 425 OPJ_OFF_T opj_skip_from_memory(OPJ_OFF_T nb_bytes, void* p_user_data); | |
| 426 OPJ_BOOL opj_seek_from_memory(OPJ_OFF_T nb_bytes, void* p_user_data); | |
| 427 | |
| 428 #endif // CORE_SRC_FXCODEC_CODEC_CODEC_INT_H_ | |
| OLD | NEW |