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_INCLUDE_FXCODEC_FX_CODEC_H_ 8 #define CORE_INCLUDE_FXCODEC_FX_CODEC_H_ 9 10 #include <map> 11 #include <memory> 12 #include <vector> 13 14 #include "core/include/fxcodec/fx_codec_def.h" 15 #include "core/include/fxcrt/fx_basic.h" 16 #include "core/include/fxcrt/fx_coordinates.h" // For FX_RECT. 17 #include "fx_codec_def.h" 18 19 class CFX_DIBSource; 20 class CJPX_Decoder; 21 class CPDF_ColorSpace; 22 class CPDF_PrivateData; 23 class CPDF_StreamAcc; 24 class ICodec_BasicModule; 25 class ICodec_FaxModule; 26 class ICodec_FlateModule; 27 class ICodec_IccModule; 28 class ICodec_Jbig2Encoder; 29 class ICodec_Jbig2Module; 30 class ICodec_JpegModule; 31 class ICodec_JpxModule; 32 class ICodec_ScanlineDecoder; 33 34 #ifdef PDF_ENABLE_XFA 35 class ICodec_BmpModule; 36 class ICodec_GifModule; 37 class ICodec_PngModule; 38 class ICodec_ProgressiveDecoder; 39 class ICodec_TiffModule; 40 #endif // PDF_ENABLE_XFA 41 42 #ifdef PDF_ENABLE_XFA 43 class CFX_DIBAttribute { 44 public: 45 CFX_DIBAttribute(); 46 ~CFX_DIBAttribute(); 47 48 int32_t m_nXDPI; 49 int32_t m_nYDPI; 50 FX_FLOAT m_fAspectRatio; 51 FX_WORD m_wDPIUnit; 52 CFX_ByteString m_strAuthor; 53 uint8_t m_strTime[20]; 54 int32_t m_nGifLeft; 55 int32_t m_nGifTop; 56 FX_DWORD* m_pGifLocalPalette; 57 FX_DWORD m_nGifLocalPalNum; 58 int32_t m_nBmpCompressType; 59 std::map<FX_DWORD, void*> m_Exif; 60 }; 61 #endif // PDF_ENABLE_XFA 62 63 class CCodec_ModuleMgr { 64 public: 65 CCodec_ModuleMgr(); 66 GetBasicModule()67 ICodec_BasicModule* GetBasicModule() const { return m_pBasicModule.get(); } GetFaxModule()68 ICodec_FaxModule* GetFaxModule() const { return m_pFaxModule.get(); } GetJpegModule()69 ICodec_JpegModule* GetJpegModule() const { return m_pJpegModule.get(); } GetJpxModule()70 ICodec_JpxModule* GetJpxModule() const { return m_pJpxModule.get(); } GetJbig2Module()71 ICodec_Jbig2Module* GetJbig2Module() const { return m_pJbig2Module.get(); } GetIccModule()72 ICodec_IccModule* GetIccModule() const { return m_pIccModule.get(); } GetFlateModule()73 ICodec_FlateModule* GetFlateModule() const { return m_pFlateModule.get(); } 74 75 #ifdef PDF_ENABLE_XFA 76 ICodec_ProgressiveDecoder* CreateProgressiveDecoder(); GetPngModule()77 ICodec_PngModule* GetPngModule() const { return m_pPngModule.get(); } GetGifModule()78 ICodec_GifModule* GetGifModule() const { return m_pGifModule.get(); } GetBmpModule()79 ICodec_BmpModule* GetBmpModule() const { return m_pBmpModule.get(); } GetTiffModule()80 ICodec_TiffModule* GetTiffModule() const { return m_pTiffModule.get(); } 81 #endif // PDF_ENABLE_XFA 82 83 protected: 84 std::unique_ptr<ICodec_BasicModule> m_pBasicModule; 85 std::unique_ptr<ICodec_FaxModule> m_pFaxModule; 86 std::unique_ptr<ICodec_JpegModule> m_pJpegModule; 87 std::unique_ptr<ICodec_JpxModule> m_pJpxModule; 88 std::unique_ptr<ICodec_Jbig2Module> m_pJbig2Module; 89 std::unique_ptr<ICodec_IccModule> m_pIccModule; 90 #ifdef PDF_ENABLE_XFA 91 std::unique_ptr<ICodec_PngModule> m_pPngModule; 92 std::unique_ptr<ICodec_GifModule> m_pGifModule; 93 std::unique_ptr<ICodec_BmpModule> m_pBmpModule; 94 std::unique_ptr<ICodec_TiffModule> m_pTiffModule; 95 #endif // PDF_ENABLE_XFA 96 std::unique_ptr<ICodec_FlateModule> m_pFlateModule; 97 }; 98 class ICodec_BasicModule { 99 public: ~ICodec_BasicModule()100 virtual ~ICodec_BasicModule() {} 101 virtual FX_BOOL RunLengthEncode(const uint8_t* src_buf, 102 FX_DWORD src_size, 103 uint8_t*& dest_buf, 104 FX_DWORD& dest_size) = 0; 105 virtual FX_BOOL A85Encode(const uint8_t* src_buf, 106 FX_DWORD src_size, 107 uint8_t*& dest_buf, 108 FX_DWORD& dest_size) = 0; 109 virtual ICodec_ScanlineDecoder* CreateRunLengthDecoder(const uint8_t* src_buf, 110 FX_DWORD src_size, 111 int width, 112 int height, 113 int nComps, 114 int bpc) = 0; 115 }; 116 117 class ICodec_ScanlineDecoder { 118 public: ~ICodec_ScanlineDecoder()119 virtual ~ICodec_ScanlineDecoder() {} 120 121 virtual FX_DWORD GetSrcOffset() = 0; 122 123 virtual void DownScale(int dest_width, int dest_height) = 0; 124 125 virtual const uint8_t* GetScanline(int line) = 0; 126 127 virtual FX_BOOL SkipToScanline(int line, IFX_Pause* pPause) = 0; 128 129 virtual int GetWidth() = 0; 130 131 virtual int GetHeight() = 0; 132 133 virtual int CountComps() = 0; 134 135 virtual int GetBPC() = 0; 136 137 virtual FX_BOOL IsColorTransformed() = 0; 138 139 virtual void ClearImageData() = 0; 140 }; 141 142 class ICodec_FlateModule { 143 public: ~ICodec_FlateModule()144 virtual ~ICodec_FlateModule() {} 145 virtual ICodec_ScanlineDecoder* CreateDecoder(const uint8_t* src_buf, 146 FX_DWORD src_size, 147 int width, 148 int height, 149 int nComps, 150 int bpc, 151 int predictor, 152 int Colors, 153 int BitsPerComponent, 154 int Columns) = 0; 155 virtual FX_DWORD FlateOrLZWDecode(FX_BOOL bLZW, 156 const uint8_t* src_buf, 157 FX_DWORD src_size, 158 FX_BOOL bEarlyChange, 159 int predictor, 160 int Colors, 161 int BitsPerComponent, 162 int Columns, 163 FX_DWORD estimated_size, 164 uint8_t*& dest_buf, 165 FX_DWORD& dest_size) = 0; 166 virtual FX_BOOL Encode(const uint8_t* src_buf, 167 FX_DWORD src_size, 168 int predictor, 169 int Colors, 170 int BitsPerComponent, 171 int Columns, 172 uint8_t*& dest_buf, 173 FX_DWORD& dest_size) = 0; 174 virtual FX_BOOL Encode(const uint8_t* src_buf, 175 FX_DWORD src_size, 176 uint8_t*& dest_buf, 177 FX_DWORD& dest_size) = 0; 178 }; 179 class ICodec_FaxModule { 180 public: ~ICodec_FaxModule()181 virtual ~ICodec_FaxModule() {} 182 183 virtual ICodec_ScanlineDecoder* CreateDecoder(const uint8_t* src_buf, 184 FX_DWORD src_size, 185 int width, 186 int height, 187 int K, 188 FX_BOOL EndOfLine, 189 FX_BOOL EncodedByteAlign, 190 FX_BOOL BlackIs1, 191 int Columns, 192 int Rows) = 0; 193 194 virtual FX_BOOL Encode(const uint8_t* src_buf, 195 int width, 196 int height, 197 int pitch, 198 uint8_t*& dest_buf, 199 FX_DWORD& dest_size) = 0; 200 }; 201 class ICodec_JpegModule { 202 public: ~ICodec_JpegModule()203 virtual ~ICodec_JpegModule() {} 204 205 virtual ICodec_ScanlineDecoder* CreateDecoder(const uint8_t* src_buf, 206 FX_DWORD src_size, 207 int width, 208 int height, 209 int nComps, 210 FX_BOOL ColorTransform) = 0; 211 212 virtual FX_BOOL LoadInfo(const uint8_t* src_buf, 213 FX_DWORD src_size, 214 int& width, 215 int& height, 216 int& num_components, 217 int& bits_per_components, 218 FX_BOOL& color_transform, 219 uint8_t** icc_buf_ptr = NULL, 220 FX_DWORD* icc_length = NULL) = 0; 221 222 virtual FX_BOOL Encode(const class CFX_DIBSource* pSource, 223 uint8_t*& dest_buf, 224 FX_STRSIZE& dest_size, 225 int quality = 75, 226 const uint8_t* icc_buf = NULL, 227 FX_DWORD icc_length = 0) = 0; 228 229 virtual void* Start() = 0; 230 231 virtual void Finish(void* pContext) = 0; 232 233 virtual void Input(void* pContext, 234 const uint8_t* src_buf, 235 FX_DWORD src_size) = 0; 236 237 #ifdef PDF_ENABLE_XFA 238 virtual int ReadHeader(void* pContext, 239 int* width, 240 int* height, 241 int* nComps, 242 CFX_DIBAttribute* pAttribute) = 0; 243 #else // PDF_ENABLE_XFA 244 virtual int ReadHeader(void* pContext, 245 int* width, 246 int* height, 247 int* nComps) = 0; 248 #endif // PDF_ENABLE_XFA 249 250 virtual int StartScanline(void* pContext, int down_scale) = 0; 251 252 virtual FX_BOOL ReadScanline(void* pContext, uint8_t* dest_buf) = 0; 253 254 virtual FX_DWORD GetAvailInput(void* pContext, 255 uint8_t** avail_buf_ptr = NULL) = 0; 256 }; 257 258 class ICodec_JpxModule { 259 public: ~ICodec_JpxModule()260 virtual ~ICodec_JpxModule() {} 261 262 virtual CJPX_Decoder* CreateDecoder(const uint8_t* src_buf, 263 FX_DWORD src_size, 264 CPDF_ColorSpace* cs) = 0; 265 266 virtual void GetImageInfo(CJPX_Decoder* pDecoder, 267 FX_DWORD* width, 268 FX_DWORD* height, 269 FX_DWORD* components) = 0; 270 271 virtual bool Decode(CJPX_Decoder* pDecoder, 272 uint8_t* dest_data, 273 int pitch, 274 const std::vector<uint8_t>& offsets) = 0; 275 276 virtual void DestroyDecoder(CJPX_Decoder* pDecoder) = 0; 277 }; 278 #ifdef PDF_ENABLE_XFA 279 class ICodec_PngModule { 280 public: ~ICodec_PngModule()281 virtual ~ICodec_PngModule() {} 282 283 virtual void* Start(void* pModule) = 0; 284 285 virtual void Finish(void* pContext) = 0; 286 287 virtual FX_BOOL Input(void* pContext, 288 const uint8_t* src_buf, 289 FX_DWORD src_size, 290 CFX_DIBAttribute* pAttribute) = 0; 291 292 FX_BOOL (*ReadHeaderCallback)(void* pModule, 293 int width, 294 int height, 295 int bpc, 296 int pass, 297 int* color_type, 298 double* gamma); 299 300 FX_BOOL (*AskScanlineBufCallback)(void* pModule, int line, uint8_t*& src_buf); 301 302 void (*FillScanlineBufCompletedCallback)(void* pModule, int pass, int line); 303 }; 304 class ICodec_GifModule { 305 public: ~ICodec_GifModule()306 virtual ~ICodec_GifModule() {} 307 308 virtual void* Start(void* pModule) = 0; 309 310 virtual void Finish(void* pContext) = 0; 311 312 virtual FX_DWORD GetAvailInput(void* pContext, 313 uint8_t** avail_buf_ptr = NULL) = 0; 314 315 virtual void Input(void* pContext, 316 const uint8_t* src_buf, 317 FX_DWORD src_size) = 0; 318 319 virtual int32_t ReadHeader(void* pContext, 320 int* width, 321 int* height, 322 int* pal_num, 323 void** pal_pp, 324 int* bg_index, 325 CFX_DIBAttribute* pAttribute) = 0; 326 327 virtual int32_t LoadFrameInfo(void* pContext, int* frame_num) = 0; 328 329 void (*RecordCurrentPositionCallback)(void* pModule, FX_DWORD& cur_pos); 330 331 uint8_t* (*AskLocalPaletteBufCallback)(void* pModule, 332 int32_t frame_num, 333 int32_t pal_size); 334 335 virtual int32_t LoadFrame(void* pContext, 336 int frame_num, 337 CFX_DIBAttribute* pAttribute) = 0; 338 339 FX_BOOL (*InputRecordPositionBufCallback)(void* pModule, 340 FX_DWORD rcd_pos, 341 const FX_RECT& img_rc, 342 int32_t pal_num, 343 void* pal_ptr, 344 int32_t delay_time, 345 FX_BOOL user_input, 346 int32_t trans_index, 347 int32_t disposal_method, 348 FX_BOOL interlace); 349 350 void (*ReadScanlineCallback)(void* pModule, 351 int32_t row_num, 352 uint8_t* row_buf); 353 }; 354 class ICodec_BmpModule { 355 public: ~ICodec_BmpModule()356 virtual ~ICodec_BmpModule() {} 357 358 virtual void* Start(void* pModule) = 0; 359 360 virtual void Finish(void* pContext) = 0; 361 362 virtual FX_DWORD GetAvailInput(void* pContext, 363 uint8_t** avail_buf_ptr = NULL) = 0; 364 365 virtual void Input(void* pContext, 366 const uint8_t* src_buf, 367 FX_DWORD src_size) = 0; 368 369 virtual int32_t ReadHeader(void* pContext, 370 int32_t* width, 371 int32_t* height, 372 FX_BOOL* tb_flag, 373 int32_t* components, 374 int* pal_num, 375 FX_DWORD** pal_pp, 376 CFX_DIBAttribute* pAttribute) = 0; 377 378 virtual int32_t LoadImage(void* pContext) = 0; 379 380 FX_BOOL (*InputImagePositionBufCallback)(void* pModule, FX_DWORD rcd_pos); 381 382 void (*ReadScanlineCallback)(void* pModule, 383 int32_t row_num, 384 uint8_t* row_buf); 385 }; 386 class ICodec_TiffModule { 387 public: ~ICodec_TiffModule()388 virtual ~ICodec_TiffModule() {} 389 390 virtual void* CreateDecoder(IFX_FileRead* file_ptr) = 0; 391 392 virtual void GetFrames(void* ctx, int32_t& frames) = 0; 393 394 virtual FX_BOOL LoadFrameInfo(void* ctx, 395 int32_t frame, 396 FX_DWORD& width, 397 FX_DWORD& height, 398 FX_DWORD& comps, 399 FX_DWORD& bpc, 400 CFX_DIBAttribute* pAttribute) = 0; 401 402 virtual FX_BOOL Decode(void* ctx, class CFX_DIBitmap* pDIBitmap) = 0; 403 404 virtual void DestroyDecoder(void* ctx) = 0; 405 }; 406 #endif 407 408 class ICodec_Jbig2Module { 409 public: ~ICodec_Jbig2Module()410 virtual ~ICodec_Jbig2Module() {} 411 412 virtual void* CreateJbig2Context() = 0; 413 414 virtual FXCODEC_STATUS StartDecode(void* pJbig2Context, 415 CFX_PrivateData* pPrivateData, 416 FX_DWORD width, 417 FX_DWORD height, 418 CPDF_StreamAcc* src_stream, 419 CPDF_StreamAcc* global_stream, 420 uint8_t* dest_buf, 421 FX_DWORD dest_pitch, 422 IFX_Pause* pPause) = 0; 423 424 virtual FXCODEC_STATUS ContinueDecode(void* pJbig2Content, 425 IFX_Pause* pPause) = 0; 426 virtual void DestroyJbig2Context(void* pJbig2Content) = 0; 427 }; 428 #ifdef PDF_ENABLE_XFA 429 class ICodec_ProgressiveDecoder { 430 public: ~ICodec_ProgressiveDecoder()431 virtual ~ICodec_ProgressiveDecoder() {} 432 433 virtual FXCODEC_STATUS LoadImageInfo(IFX_FileRead* pFile, 434 FXCODEC_IMAGE_TYPE imageType, 435 CFX_DIBAttribute* pAttribute) = 0; 436 437 virtual FXCODEC_IMAGE_TYPE GetType() const = 0; 438 virtual int32_t GetWidth() const = 0; 439 virtual int32_t GetHeight() const = 0; 440 virtual int32_t GetNumComponents() const = 0; 441 virtual int32_t GetBPC() const = 0; 442 443 virtual void SetClipBox(FX_RECT* clip) = 0; 444 445 virtual FXCODEC_STATUS GetFrames(int32_t& frames, 446 IFX_Pause* pPause = NULL) = 0; 447 448 virtual FXCODEC_STATUS StartDecode(class CFX_DIBitmap* pDIBitmap, 449 int32_t start_x, 450 int32_t start_y, 451 int32_t size_x, 452 int32_t size_y, 453 int32_t frames = 0, 454 FX_BOOL bInterpol = TRUE) = 0; 455 456 virtual FXCODEC_STATUS ContinueDecode(IFX_Pause* pPause = NULL) = 0; 457 }; 458 #endif // PDF_ENABLE_XFA 459 class ICodec_Jbig2Encoder { 460 public: ~ICodec_Jbig2Encoder()461 virtual ~ICodec_Jbig2Encoder() {} 462 }; 463 class ICodec_IccModule { 464 public: 465 typedef enum { 466 IccCS_Unknown = 0, 467 IccCS_XYZ, 468 IccCS_Lab, 469 IccCS_Luv, 470 IccCS_YCbCr, 471 IccCS_Yxy, 472 IccCS_Hsv, 473 IccCS_Hls, 474 IccCS_Gray, 475 IccCS_Rgb, 476 IccCS_Cmyk, 477 IccCS_Cmy 478 } IccCS; 479 typedef struct _IccParam { 480 FX_DWORD Version; 481 IccCS ColorSpace; 482 FX_DWORD dwProfileType; 483 FX_DWORD dwFormat; 484 uint8_t* pProfileData; 485 FX_DWORD dwProfileSize; 486 double Gamma; 487 } IccParam; 488 ~ICodec_IccModule()489 virtual ~ICodec_IccModule() {} 490 491 virtual IccCS GetProfileCS(const uint8_t* pProfileData, 492 unsigned int dwProfileSize) = 0; 493 494 virtual IccCS GetProfileCS(IFX_FileRead* pFile) = 0; 495 496 virtual void* CreateTransform( 497 ICodec_IccModule::IccParam* pInputParam, 498 ICodec_IccModule::IccParam* pOutputParam, 499 ICodec_IccModule::IccParam* pProofParam = NULL, 500 FX_DWORD dwIntent = Icc_INTENT_PERCEPTUAL, 501 FX_DWORD dwFlag = Icc_FLAGS_DEFAULT, 502 FX_DWORD dwPrfIntent = Icc_INTENT_ABSOLUTE_COLORIMETRIC, 503 FX_DWORD dwPrfFlag = Icc_FLAGS_SOFTPROOFING) = 0; 504 505 virtual void* CreateTransform_sRGB( 506 const uint8_t* pProfileData, 507 FX_DWORD dwProfileSize, 508 int32_t& nComponents, 509 int32_t intent = 0, 510 FX_DWORD dwSrcFormat = Icc_FORMAT_DEFAULT) = 0; 511 512 virtual void* CreateTransform_CMYK( 513 const uint8_t* pSrcProfileData, 514 FX_DWORD dwSrcProfileSize, 515 int32_t& nSrcComponents, 516 const uint8_t* pDstProfileData, 517 FX_DWORD dwDstProfileSize, 518 int32_t intent = 0, 519 FX_DWORD dwSrcFormat = Icc_FORMAT_DEFAULT, 520 FX_DWORD dwDstFormat = Icc_FORMAT_DEFAULT) = 0; 521 522 virtual void DestroyTransform(void* pTransform) = 0; 523 524 virtual void Translate(void* pTransform, 525 FX_FLOAT* pSrcValues, 526 FX_FLOAT* pDestValues) = 0; 527 528 virtual void TranslateScanline(void* pTransform, 529 uint8_t* pDest, 530 const uint8_t* pSrc, 531 int pixels) = 0; 532 virtual void SetComponents(FX_DWORD nComponents) = 0; 533 }; 534 535 void AdobeCMYK_to_sRGB(FX_FLOAT c, 536 FX_FLOAT m, 537 FX_FLOAT y, 538 FX_FLOAT k, 539 FX_FLOAT& R, 540 FX_FLOAT& G, 541 FX_FLOAT& B); 542 void AdobeCMYK_to_sRGB1(uint8_t c, 543 uint8_t m, 544 uint8_t y, 545 uint8_t k, 546 uint8_t& R, 547 uint8_t& G, 548 uint8_t& B); 549 FX_BOOL MD5ComputeID(const void* buf, FX_DWORD dwSize, uint8_t ID[16]); 550 void FaxG4Decode(const uint8_t* src_buf, 551 FX_DWORD src_size, 552 int* pbitpos, 553 uint8_t* dest_buf, 554 int width, 555 int height, 556 int pitch); 557 558 #endif // CORE_INCLUDE_FXCODEC_FX_CODEC_H_ 559