OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "core/fxcodec/codec/codec_int.h" | 9 #include "core/fxcodec/codec/codec_int.h" |
10 #include "core/fxcodec/fx_codec.h" | 10 #include "core/fxcodec/fx_codec.h" |
| 11 #include "core/fxcrt/cfx_retain_ptr.h" |
11 #include "core/fxcrt/fx_safe_types.h" | 12 #include "core/fxcrt/fx_safe_types.h" |
12 #include "core/fxge/fx_dib.h" | 13 #include "core/fxge/fx_dib.h" |
| 14 #include "third_party/base/ptr_util.h" |
13 | 15 |
14 extern "C" { | 16 extern "C" { |
15 #include "third_party/libtiff/tiffiop.h" | 17 #include "third_party/libtiff/tiffiop.h" |
16 } | 18 } |
17 | 19 |
18 class CCodec_TiffContext { | 20 class CCodec_TiffContext { |
19 public: | 21 public: |
20 CCodec_TiffContext(); | 22 CCodec_TiffContext(); |
21 ~CCodec_TiffContext(); | 23 ~CCodec_TiffContext(); |
22 | 24 |
23 bool InitDecoder(IFX_SeekableReadStream* file_ptr); | 25 bool InitDecoder(const CFX_RetainPtr<IFX_SeekableReadStream>& file_ptr); |
24 bool LoadFrameInfo(int32_t frame, | 26 bool LoadFrameInfo(int32_t frame, |
25 int32_t* width, | 27 int32_t* width, |
26 int32_t* height, | 28 int32_t* height, |
27 int32_t* comps, | 29 int32_t* comps, |
28 int32_t* bpc, | 30 int32_t* bpc, |
29 CFX_DIBAttribute* pAttribute); | 31 CFX_DIBAttribute* pAttribute); |
30 bool Decode(CFX_DIBitmap* pDIBitmap); | 32 bool Decode(CFX_DIBitmap* pDIBitmap); |
31 | 33 |
32 IFX_SeekableReadStream* io_in() const { return m_io_in; } | 34 CFX_RetainPtr<IFX_SeekableReadStream> io_in() const { return m_io_in; } |
33 uint32_t offset() const { return m_offset; } | 35 uint32_t offset() const { return m_offset; } |
34 void set_offset(uint32_t offset) { m_offset = offset; } | 36 void set_offset(uint32_t offset) { m_offset = offset; } |
35 | 37 |
36 private: | 38 private: |
37 bool IsSupport(const CFX_DIBitmap* pDIBitmap) const; | 39 bool IsSupport(const CFX_DIBitmap* pDIBitmap) const; |
38 void SetPalette(CFX_DIBitmap* pDIBitmap, uint16_t bps); | 40 void SetPalette(CFX_DIBitmap* pDIBitmap, uint16_t bps); |
39 bool Decode1bppRGB(CFX_DIBitmap* pDIBitmap, | 41 bool Decode1bppRGB(CFX_DIBitmap* pDIBitmap, |
40 int32_t height, | 42 int32_t height, |
41 int32_t width, | 43 int32_t width, |
42 uint16_t bps, | 44 uint16_t bps, |
43 uint16_t spp); | 45 uint16_t spp); |
44 bool Decode8bppRGB(CFX_DIBitmap* pDIBitmap, | 46 bool Decode8bppRGB(CFX_DIBitmap* pDIBitmap, |
45 int32_t height, | 47 int32_t height, |
46 int32_t width, | 48 int32_t width, |
47 uint16_t bps, | 49 uint16_t bps, |
48 uint16_t spp); | 50 uint16_t spp); |
49 bool Decode24bppRGB(CFX_DIBitmap* pDIBitmap, | 51 bool Decode24bppRGB(CFX_DIBitmap* pDIBitmap, |
50 int32_t height, | 52 int32_t height, |
51 int32_t width, | 53 int32_t width, |
52 uint16_t bps, | 54 uint16_t bps, |
53 uint16_t spp); | 55 uint16_t spp); |
54 | 56 |
55 IFX_SeekableReadStream* m_io_in; | 57 CFX_RetainPtr<IFX_SeekableReadStream> m_io_in; |
56 uint32_t m_offset; | 58 uint32_t m_offset; |
57 TIFF* m_tif_ctx; | 59 TIFF* m_tif_ctx; |
58 }; | 60 }; |
59 | 61 |
60 void* _TIFFmalloc(tmsize_t size) { | 62 void* _TIFFmalloc(tmsize_t size) { |
61 return FXMEM_DefaultAlloc(size, 0); | 63 return FXMEM_DefaultAlloc(size, 0); |
62 } | 64 } |
63 | 65 |
64 void _TIFFfree(void* ptr) { | 66 void _TIFFfree(void* ptr) { |
65 FXMEM_DefaultFree(ptr, 0); | 67 FXMEM_DefaultFree(ptr, 0); |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 } // namespace | 210 } // namespace |
209 | 211 |
210 CCodec_TiffContext::CCodec_TiffContext() | 212 CCodec_TiffContext::CCodec_TiffContext() |
211 : m_io_in(nullptr), m_offset(0), m_tif_ctx(nullptr) {} | 213 : m_io_in(nullptr), m_offset(0), m_tif_ctx(nullptr) {} |
212 | 214 |
213 CCodec_TiffContext::~CCodec_TiffContext() { | 215 CCodec_TiffContext::~CCodec_TiffContext() { |
214 if (m_tif_ctx) | 216 if (m_tif_ctx) |
215 TIFFClose(m_tif_ctx); | 217 TIFFClose(m_tif_ctx); |
216 } | 218 } |
217 | 219 |
218 bool CCodec_TiffContext::InitDecoder(IFX_SeekableReadStream* file_ptr) { | 220 bool CCodec_TiffContext::InitDecoder( |
| 221 const CFX_RetainPtr<IFX_SeekableReadStream>& file_ptr) { |
219 m_io_in = file_ptr; | 222 m_io_in = file_ptr; |
220 m_tif_ctx = tiff_open(this, "r"); | 223 m_tif_ctx = tiff_open(this, "r"); |
221 return !!m_tif_ctx; | 224 return !!m_tif_ctx; |
222 } | 225 } |
223 | 226 |
224 bool CCodec_TiffContext::LoadFrameInfo(int32_t frame, | 227 bool CCodec_TiffContext::LoadFrameInfo(int32_t frame, |
225 int32_t* width, | 228 int32_t* width, |
226 int32_t* height, | 229 int32_t* height, |
227 int32_t* comps, | 230 int32_t* comps, |
228 int32_t* bpc, | 231 int32_t* bpc, |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
455 if (bpp == 1) | 458 if (bpp == 1) |
456 return Decode1bppRGB(pDIBitmap, height, width, bps, spp); | 459 return Decode1bppRGB(pDIBitmap, height, width, bps, spp); |
457 if (bpp <= 8) | 460 if (bpp <= 8) |
458 return Decode8bppRGB(pDIBitmap, height, width, bps, spp); | 461 return Decode8bppRGB(pDIBitmap, height, width, bps, spp); |
459 if (bpp <= 24) | 462 if (bpp <= 24) |
460 return Decode24bppRGB(pDIBitmap, height, width, bps, spp); | 463 return Decode24bppRGB(pDIBitmap, height, width, bps, spp); |
461 return false; | 464 return false; |
462 } | 465 } |
463 | 466 |
464 CCodec_TiffContext* CCodec_TiffModule::CreateDecoder( | 467 CCodec_TiffContext* CCodec_TiffModule::CreateDecoder( |
465 IFX_SeekableReadStream* file_ptr) { | 468 const CFX_RetainPtr<IFX_SeekableReadStream>& file_ptr) { |
466 CCodec_TiffContext* pDecoder = new CCodec_TiffContext; | 469 auto pDecoder = pdfium::MakeUnique<CCodec_TiffContext>(); |
467 if (!pDecoder->InitDecoder(file_ptr)) { | 470 if (!pDecoder->InitDecoder(file_ptr)) |
468 delete pDecoder; | |
469 return nullptr; | 471 return nullptr; |
470 } | 472 |
471 return pDecoder; | 473 return pDecoder.release(); |
472 } | 474 } |
473 | 475 |
474 bool CCodec_TiffModule::LoadFrameInfo(CCodec_TiffContext* ctx, | 476 bool CCodec_TiffModule::LoadFrameInfo(CCodec_TiffContext* ctx, |
475 int32_t frame, | 477 int32_t frame, |
476 int32_t* width, | 478 int32_t* width, |
477 int32_t* height, | 479 int32_t* height, |
478 int32_t* comps, | 480 int32_t* comps, |
479 int32_t* bpc, | 481 int32_t* bpc, |
480 CFX_DIBAttribute* pAttribute) { | 482 CFX_DIBAttribute* pAttribute) { |
481 return ctx->LoadFrameInfo(frame, width, height, comps, bpc, pAttribute); | 483 return ctx->LoadFrameInfo(frame, width, height, comps, bpc, pAttribute); |
482 } | 484 } |
483 | 485 |
484 bool CCodec_TiffModule::Decode(CCodec_TiffContext* ctx, | 486 bool CCodec_TiffModule::Decode(CCodec_TiffContext* ctx, |
485 class CFX_DIBitmap* pDIBitmap) { | 487 class CFX_DIBitmap* pDIBitmap) { |
486 return ctx->Decode(pDIBitmap); | 488 return ctx->Decode(pDIBitmap); |
487 } | 489 } |
488 | 490 |
489 void CCodec_TiffModule::DestroyDecoder(CCodec_TiffContext* ctx) { | 491 void CCodec_TiffModule::DestroyDecoder(CCodec_TiffContext* ctx) { |
490 delete ctx; | 492 delete ctx; |
491 } | 493 } |
OLD | NEW |