summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLei Zhang <thestig@chromium.org>2018-06-29 20:41:09 +0000
committerChromium commit bot <commit-bot@chromium.org>2018-06-29 20:41:09 +0000
commit013d065a0ad9cd4dd913997c0cc503234efe436e (patch)
treee25c9952d9f1ecac9a0e20a9b4335725558bc60b
parent1839a4bc01a6c6fe6a22474d2c6d4f09a9772371 (diff)
downloadpdfium-013d065a0ad9cd4dd913997c0cc503234efe436e.tar.xz
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 <thestig@chromium.org> Reviewed-by: Henrique Nakashima <hnakashima@chromium.org>
-rw-r--r--core/fxcodec/codec/fx_codec_icc.cpp50
1 files 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<cmsHPROFILE>.
+struct CmsProfileDeleter {
+ inline void operator()(cmsHPROFILE p) { cmsCloseProfile(p); }
+};
+
+using ScopedCmsProfile = std::unique_ptr<void, CmsProfileDeleter>;
+
bool Check3Components(cmsColorSpaceSignature cs) {
switch (cs) {
case cmsSigGrayData:
@@ -47,26 +54,21 @@ std::unique_ptr<CLcmsCmm> 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<CLcmsCmm> 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<CLcmsCmm> CCodec_IccModule::CreateTransform_sRGB(
default:
break;
}
- if (!hTransform) {
- cmsCloseProfile(srcProfile);
- cmsCloseProfile(dstProfile);
+ if (!hTransform)
return nullptr;
- }
- auto pCmm =
- pdfium::MakeUnique<CLcmsCmm>(hTransform, *nSrcComponents, bLab, bNormal);
- cmsCloseProfile(srcProfile);
- cmsCloseProfile(dstProfile);
- return pCmm;
+
+ return pdfium::MakeUnique<CLcmsCmm>(hTransform, *nSrcComponents, bLab,
+ bNormal);
}
void CCodec_IccModule::Translate(CLcmsCmm* pTransform,