From 013d065a0ad9cd4dd913997c0cc503234efe436e Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Fri, 29 Jun 2018 20:41:09 +0000 Subject: Add ScopedCmsProfile. Using this unique_ptr avoids the need for explicit cmsCloseProfile() calls in CCodec_IccModule::CreateTransform_sRGB(). Change-Id: I5b2d5d9800ea344aceb0ed5c6b745a2925bab9c9 Reviewed-on: https://pdfium-review.googlesource.com/36632 Commit-Queue: Lei Zhang Reviewed-by: Henrique Nakashima --- core/fxcodec/codec/fx_codec_icc.cpp | 50 ++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/core/fxcodec/codec/fx_codec_icc.cpp b/core/fxcodec/codec/fx_codec_icc.cpp index a7c7f98bba..b2f2b4064d 100644 --- a/core/fxcodec/codec/fx_codec_icc.cpp +++ b/core/fxcodec/codec/fx_codec_icc.cpp @@ -13,6 +13,13 @@ namespace { +// For use with std::unique_ptr. +struct CmsProfileDeleter { + inline void operator()(cmsHPROFILE p) { cmsCloseProfile(p); } +}; + +using ScopedCmsProfile = std::unique_ptr; + bool Check3Components(cmsColorSpaceSignature cs) { switch (cs) { case cmsSigGrayData: @@ -47,26 +54,21 @@ std::unique_ptr CCodec_IccModule::CreateTransform_sRGB( uint32_t dwSrcProfileSize, uint32_t* nSrcComponents) { *nSrcComponents = 0; - cmsHPROFILE srcProfile = - cmsOpenProfileFromMem(pSrcProfileData, dwSrcProfileSize); + ScopedCmsProfile srcProfile( + cmsOpenProfileFromMem(pSrcProfileData, dwSrcProfileSize)); if (!srcProfile) return nullptr; - cmsHPROFILE dstProfile; - dstProfile = cmsCreate_sRGBProfile(); - if (!dstProfile) { - cmsCloseProfile(srcProfile); + ScopedCmsProfile dstProfile(cmsCreate_sRGBProfile()); + if (!dstProfile) return nullptr; - } - cmsColorSpaceSignature srcCS = cmsGetColorSpace(srcProfile); + + cmsColorSpaceSignature srcCS = cmsGetColorSpace(srcProfile.get()); *nSrcComponents = cmsChannelsOf(srcCS); // According to PDF spec, number of components must be 1, 3, or 4. - if (*nSrcComponents != 1 && *nSrcComponents != 3 && *nSrcComponents != 4) { - cmsCloseProfile(srcProfile); - cmsCloseProfile(dstProfile); + if (*nSrcComponents != 1 && *nSrcComponents != 3 && *nSrcComponents != 4) return nullptr; - } int srcFormat; bool bLab = false; @@ -83,19 +85,16 @@ std::unique_ptr CCodec_IccModule::CreateTransform_sRGB( bNormal = srcCS == cmsSigGrayData || srcCS == cmsSigRgbData || srcCS == cmsSigCmykData; } - cmsColorSpaceSignature dstCS = cmsGetColorSpace(dstProfile); - if (!Check3Components(dstCS)) { - cmsCloseProfile(srcProfile); - cmsCloseProfile(dstProfile); + cmsColorSpaceSignature dstCS = cmsGetColorSpace(dstProfile.get()); + if (!Check3Components(dstCS)) return nullptr; - } cmsHTRANSFORM hTransform = nullptr; const int intent = 0; switch (dstCS) { case cmsSigRgbData: - hTransform = cmsCreateTransform(srcProfile, srcFormat, dstProfile, - TYPE_BGR_8, intent, 0); + hTransform = cmsCreateTransform(srcProfile.get(), srcFormat, + dstProfile.get(), TYPE_BGR_8, intent, 0); break; case cmsSigGrayData: case cmsSigCmykData: @@ -105,16 +104,11 @@ std::unique_ptr CCodec_IccModule::CreateTransform_sRGB( default: break; } - if (!hTransform) { - cmsCloseProfile(srcProfile); - cmsCloseProfile(dstProfile); + if (!hTransform) return nullptr; - } - auto pCmm = - pdfium::MakeUnique(hTransform, *nSrcComponents, bLab, bNormal); - cmsCloseProfile(srcProfile); - cmsCloseProfile(dstProfile); - return pCmm; + + return pdfium::MakeUnique(hTransform, *nSrcComponents, bLab, + bNormal); } void CCodec_IccModule::Translate(CLcmsCmm* pTransform, -- cgit v1.2.3