Chromium Code Reviews| Index: core/fxcodec/codec/fx_codec_tiff.cpp |
| diff --git a/core/fxcodec/codec/fx_codec_tiff.cpp b/core/fxcodec/codec/fx_codec_tiff.cpp |
| index c8c94dbcdbb69bb0bf0bfa686a057b876a17a1db..a80b15b08d7fbd04388ef10f4193b25bbbb7c248 100644 |
| --- a/core/fxcodec/codec/fx_codec_tiff.cpp |
| +++ b/core/fxcodec/codec/fx_codec_tiff.cpp |
| @@ -22,6 +22,7 @@ void IccLib_TranslateImage(void* pTransform, |
| const unsigned char* pSrc, |
| int pixels); |
| void IccLib_DestroyTransform(void* pTransform); |
| + |
| class CCodec_TiffContext { |
| public: |
| CCodec_TiffContext(); |
| @@ -69,25 +70,37 @@ class CCodec_TiffContext { |
| uint16_t bps, |
| uint16_t spp); |
| }; |
| -CCodec_TiffContext::CCodec_TiffContext() { |
| - offset = 0; |
| - frame_num = 0; |
| - frame_cur = 0; |
| - io.in = nullptr; |
| - tif_ctx = nullptr; |
| - icc_ctx = nullptr; |
| - isDecoder = TRUE; |
| + |
| +void* _TIFFmalloc(tmsize_t size) { |
|
Tom Sepez
2016/06/09 17:53:16
nit: _CAPS is reserved for the pre-processor. Fix
Lei Zhang
2016/06/09 22:00:38
We'll have to argue with libtiff on these. See ext
|
| + return FXMEM_DefaultAlloc(size, 0); |
| } |
| -CCodec_TiffContext::~CCodec_TiffContext() { |
| - if (icc_ctx) { |
| - IccLib_DestroyTransform(icc_ctx); |
| - icc_ctx = nullptr; |
| - } |
| - if (tif_ctx) { |
| - TIFFClose(tif_ctx); |
| - } |
| + |
| +void _TIFFfree(void* ptr) { |
| + FXMEM_DefaultFree(ptr, 0); |
| } |
| -static tsize_t _tiff_read(thandle_t context, tdata_t buf, tsize_t length) { |
| + |
| +void* _TIFFrealloc(void* ptr, tmsize_t size) { |
| + return FXMEM_DefaultRealloc(ptr, size, 0); |
| +} |
| + |
| +void _TIFFmemset(void* ptr, int val, tmsize_t size) { |
| + FXSYS_memset(ptr, val, (size_t)size); |
| +} |
| + |
| +void _TIFFmemcpy(void* des, const void* src, tmsize_t size) { |
| + FXSYS_memcpy(des, src, (size_t)size); |
| +} |
| + |
| +int _TIFFmemcmp(const void* ptr1, const void* ptr2, tmsize_t size) { |
| + return FXSYS_memcmp(ptr1, ptr2, (size_t)size); |
| +} |
| + |
| +TIFFErrorHandler _TIFFwarningHandler = nullptr; |
| +TIFFErrorHandler _TIFFerrorHandler = nullptr; |
| + |
| +namespace { |
| + |
| +tsize_t tiff_read(thandle_t context, tdata_t buf, tsize_t length) { |
| CCodec_TiffContext* pTiffContext = (CCodec_TiffContext*)context; |
| FX_BOOL ret = FALSE; |
| if (pTiffContext->isDecoder) { |
| @@ -101,7 +114,8 @@ static tsize_t _tiff_read(thandle_t context, tdata_t buf, tsize_t length) { |
| pTiffContext->offset += (uint32_t)length; |
| return length; |
| } |
| -static tsize_t _tiff_write(thandle_t context, tdata_t buf, tsize_t length) { |
| + |
| +tsize_t tiff_write(thandle_t context, tdata_t buf, tsize_t length) { |
| CCodec_TiffContext* pTiffContext = (CCodec_TiffContext*)context; |
| ASSERT(!pTiffContext->isDecoder); |
| if (!pTiffContext->io.out->WriteBlock(buf, pTiffContext->offset, length)) { |
| @@ -110,7 +124,8 @@ static tsize_t _tiff_write(thandle_t context, tdata_t buf, tsize_t length) { |
| pTiffContext->offset += (uint32_t)length; |
| return length; |
| } |
| -static toff_t _tiff_seek(thandle_t context, toff_t offset, int whence) { |
| + |
| +toff_t tiff_seek(thandle_t context, toff_t offset, int whence) { |
| CCodec_TiffContext* pTiffContext = (CCodec_TiffContext*)context; |
| switch (whence) { |
| case 0: |
| @@ -142,108 +157,32 @@ static toff_t _tiff_seek(thandle_t context, toff_t offset, int whence) { |
| : TRUE); |
| return pTiffContext->offset; |
| } |
| -static int _tiff_close(thandle_t context) { |
| + |
| +int tiff_close(thandle_t context) { |
| return 0; |
| } |
| -static toff_t _tiff_get_size(thandle_t context) { |
| + |
| +toff_t tiff_get_size(thandle_t context) { |
| CCodec_TiffContext* pTiffContext = (CCodec_TiffContext*)context; |
| return pTiffContext->isDecoder ? (toff_t)pTiffContext->io.in->GetSize() |
| : (toff_t)pTiffContext->io.out->GetSize(); |
| } |
| -static int _tiff_map(thandle_t context, tdata_t*, toff_t*) { |
| + |
| +int tiff_map(thandle_t context, tdata_t*, toff_t*) { |
| return 0; |
| } |
| -static void _tiff_unmap(thandle_t context, tdata_t, toff_t) {} |
| -TIFF* _tiff_open(void* context, const char* mode) { |
| - TIFF* tif = TIFFClientOpen("Tiff Image", mode, (thandle_t)context, _tiff_read, |
| - _tiff_write, _tiff_seek, _tiff_close, |
| - _tiff_get_size, _tiff_map, _tiff_unmap); |
| + |
| +void tiff_unmap(thandle_t context, tdata_t, toff_t) {} |
| + |
| +TIFF* tiff_open(void* context, const char* mode) { |
| + TIFF* tif = TIFFClientOpen("Tiff Image", mode, (thandle_t)context, tiff_read, |
| + tiff_write, tiff_seek, tiff_close, tiff_get_size, |
| + tiff_map, tiff_unmap); |
| if (tif) { |
| tif->tif_fd = (int)(intptr_t)context; |
| } |
| return tif; |
| } |
| -void* _TIFFmalloc(tmsize_t size) { |
| - return FXMEM_DefaultAlloc(size, 0); |
| -} |
| -void _TIFFfree(void* ptr) { |
| - FXMEM_DefaultFree(ptr, 0); |
| -} |
| -void* _TIFFrealloc(void* ptr, tmsize_t size) { |
| - return FXMEM_DefaultRealloc(ptr, size, 0); |
| -} |
| -void _TIFFmemset(void* ptr, int val, tmsize_t size) { |
| - FXSYS_memset(ptr, val, (size_t)size); |
| -} |
| -void _TIFFmemcpy(void* des, const void* src, tmsize_t size) { |
| - FXSYS_memcpy(des, src, (size_t)size); |
| -} |
| -int _TIFFmemcmp(const void* ptr1, const void* ptr2, tmsize_t size) { |
| - return FXSYS_memcmp(ptr1, ptr2, (size_t)size); |
| -} |
| - |
| -TIFFErrorHandler _TIFFwarningHandler = nullptr; |
| -TIFFErrorHandler _TIFFerrorHandler = nullptr; |
| - |
| -int TIFFCmyk2Rgb(thandle_t context, |
| - uint8 c, |
| - uint8 m, |
| - uint8 y, |
| - uint8 k, |
| - uint8* r, |
| - uint8* g, |
| - uint8* b) { |
| - if (!context) |
| - return 0; |
| - |
| - CCodec_TiffContext* p = (CCodec_TiffContext*)context; |
| - if (p->icc_ctx) { |
| - unsigned char cmyk[4], bgr[3]; |
| - cmyk[0] = c, cmyk[1] = m, cmyk[2] = y, cmyk[3] = k; |
| - IccLib_TranslateImage(p->icc_ctx, bgr, cmyk, 1); |
| - *r = bgr[2], *g = bgr[1], *b = bgr[0]; |
| - } else { |
| - AdobeCMYK_to_sRGB1(c, m, y, k, *r, *g, *b); |
| - } |
| - return 1; |
| -} |
| -FX_BOOL CCodec_TiffContext::InitDecoder(IFX_FileRead* file_ptr) { |
| - io.in = file_ptr; |
| - return !!_tiff_open(this, "r"); |
| -} |
| -void CCodec_TiffContext::GetFrames(int32_t& frames) { |
| - frames = frame_num = TIFFNumberOfDirectories(tif_ctx); |
| -} |
| -#define TIFF_EXIF_GETINFO(key, T, tag) \ |
| - { \ |
| - T val = (T)0; \ |
| - TIFFGetField(tif_ctx, tag, &val); \ |
| - if (val) { \ |
| - (key) = FX_Alloc(uint8_t, sizeof(T)); \ |
| - if ((key)) { \ |
| - T* ptr = (T*)(key); \ |
| - *ptr = val; \ |
| - pExif->m_TagVal.SetAt(tag, (key)); \ |
| - } \ |
| - } \ |
| - } \ |
| - (key) = nullptr; |
| -#define TIFF_EXIF_GETSTRINGINFO(key, tag) \ |
| - { \ |
| - uint32_t size = 0; \ |
| - uint8_t* buf = nullptr; \ |
| - TIFFGetField(tif_ctx, tag, &size, &buf); \ |
| - if (size && buf) { \ |
| - (key) = FX_Alloc(uint8_t, size); \ |
| - if ((key)) { \ |
| - FXSYS_memcpy((key), buf, size); \ |
| - pExif->m_TagVal.SetAt(tag, (key)); \ |
| - } \ |
| - } \ |
| - } \ |
| - (key) = nullptr; |
| - |
| -namespace { |
| template <class T> |
| FX_BOOL Tiff_Exif_GetInfo(TIFF* tif_ctx, ttag_t tag, CFX_DIBAttribute* pAttr) { |
| @@ -256,6 +195,7 @@ FX_BOOL Tiff_Exif_GetInfo(TIFF* tif_ctx, ttag_t tag, CFX_DIBAttribute* pAttr) { |
| pAttr->m_Exif[tag] = (void*)ptr; |
| return TRUE; |
| } |
| + |
| void Tiff_Exif_GetStringInfo(TIFF* tif_ctx, |
| ttag_t tag, |
| CFX_DIBAttribute* pAttr) { |
| @@ -270,8 +210,45 @@ void Tiff_Exif_GetStringInfo(TIFF* tif_ctx, |
| pAttr->m_Exif[tag] = ptr; |
| } |
| +void TiffBGRA2RGBA(uint8_t* pBuf, int32_t pixel, int32_t spp) { |
| + for (int32_t n = 0; n < pixel; n++) { |
| + uint8_t tmp = pBuf[0]; |
| + pBuf[0] = pBuf[2]; |
| + pBuf[2] = tmp; |
| + pBuf += spp; |
| + } |
| +} |
| + |
| } // namespace |
| +CCodec_TiffContext::CCodec_TiffContext() |
| + : offset(0), |
| + tif_ctx(nullptr), |
| + icc_ctx(nullptr), |
| + frame_num(0), |
| + frame_cur(0), |
| + isDecoder(TRUE) { |
| + io.in = nullptr; |
| +} |
| + |
| +CCodec_TiffContext::~CCodec_TiffContext() { |
| + if (icc_ctx) { |
| + IccLib_DestroyTransform(icc_ctx); |
| + icc_ctx = nullptr; |
| + } |
| + if (tif_ctx) { |
| + TIFFClose(tif_ctx); |
| + } |
| +} |
| + |
| +FX_BOOL CCodec_TiffContext::InitDecoder(IFX_FileRead* file_ptr) { |
| + io.in = file_ptr; |
| + return !!tiff_open(this, "r"); |
| +} |
| +void CCodec_TiffContext::GetFrames(int32_t& frames) { |
| + frames = frame_num = TIFFNumberOfDirectories(tif_ctx); |
| +} |
| + |
| FX_BOOL CCodec_TiffContext::LoadFrameInfo(int32_t frame, |
| uint32_t& width, |
| uint32_t& height, |
| @@ -323,14 +300,7 @@ FX_BOOL CCodec_TiffContext::LoadFrameInfo(int32_t frame, |
| } |
| return TRUE; |
| } |
| -void _TiffBGRA2RGBA(uint8_t* pBuf, int32_t pixel, int32_t spp) { |
| - for (int32_t n = 0; n < pixel; n++) { |
| - uint8_t tmp = pBuf[0]; |
| - pBuf[0] = pBuf[2]; |
| - pBuf[2] = tmp; |
| - pBuf += spp; |
| - } |
| -} |
| + |
| FX_BOOL CCodec_TiffContext::isSupport(CFX_DIBitmap* pDIBitmap) { |
| if (TIFFIsTiled(tif_ctx)) { |
| return FALSE; |
| @@ -363,6 +333,7 @@ FX_BOOL CCodec_TiffContext::isSupport(CFX_DIBitmap* pDIBitmap) { |
| } |
| return TRUE; |
| } |
| + |
| void CCodec_TiffContext::SetPalette(CFX_DIBitmap* pDIBitmap, uint16_t bps) { |
| uint16_t *red_orig, *green_orig, *blue_orig; |
| TIFFGetField(tif_ctx, TIFFTAG_COLORMAP, &red_orig, &green_orig, &blue_orig); |
| @@ -383,6 +354,7 @@ void CCodec_TiffContext::SetPalette(CFX_DIBitmap* pDIBitmap, uint16_t bps) { |
| pDIBitmap->SetPaletteEntry(index, color); |
| } |
| } |
| + |
| FX_BOOL CCodec_TiffContext::Decode1bppRGB(CFX_DIBitmap* pDIBitmap, |
| int32_t height, |
| int32_t width, |
| @@ -410,6 +382,7 @@ FX_BOOL CCodec_TiffContext::Decode1bppRGB(CFX_DIBitmap* pDIBitmap, |
| _TIFFfree(buf); |
| return TRUE; |
| } |
| + |
| FX_BOOL CCodec_TiffContext::Decode8bppRGB(CFX_DIBitmap* pDIBitmap, |
| int32_t height, |
| int32_t width, |
| @@ -445,6 +418,7 @@ FX_BOOL CCodec_TiffContext::Decode8bppRGB(CFX_DIBitmap* pDIBitmap, |
| _TIFFfree(buf); |
| return TRUE; |
| } |
| + |
| FX_BOOL CCodec_TiffContext::Decode24bppRGB(CFX_DIBitmap* pDIBitmap, |
| int32_t height, |
| int32_t width, |
| @@ -472,6 +446,7 @@ FX_BOOL CCodec_TiffContext::Decode24bppRGB(CFX_DIBitmap* pDIBitmap, |
| _TIFFfree(buf); |
| return TRUE; |
| } |
| + |
| FX_BOOL CCodec_TiffContext::Decode(CFX_DIBitmap* pDIBitmap) { |
| uint32_t img_wid = pDIBitmap->GetWidth(); |
| uint32_t img_hei = pDIBitmap->GetHeight(); |
| @@ -490,7 +465,7 @@ FX_BOOL CCodec_TiffContext::Decode(CFX_DIBitmap* pDIBitmap) { |
| 1)) { |
| for (uint32_t row = 0; row < img_hei; row++) { |
| uint8_t* row_buf = (uint8_t*)pDIBitmap->GetScanline(row); |
| - _TiffBGRA2RGBA(row_buf, img_wid, 4); |
| + TiffBGRA2RGBA(row_buf, img_wid, 4); |
| } |
| return TRUE; |
| } |