cms.h

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00009 /*  This is free software; you can redistribute it and/or
00010 *  modify it under the terms of the GNU General Public
00011 *  License as published by the Free Software Foundation; either
00012 *  version 2 of the License, or (at your option) any later version.
00013 *
00014 *  This software is distributed in the hope that it will be useful,
00015 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 *  Lesser General Public License for more details.
00018 *
00019 *  You should have received a copy of the GNU General Public
00020 *  License along with this software. If not, see
00021 *  <http://www.gnu.org/licenses/>.
00022 *
00023 */
00024 
00025 #ifndef _CMS_H
00026 #define _CMS_H
00027 
00028 #include "lcms2.h"
00029 
00030 namespace HuginBase
00031 {
00032 namespace Color
00033 {
00036 template<class ImageType>
00037 void ApplyICCProfile(ImageType& image, const vigra::ImageImportInfo::ICCProfile& iccProfile, const cmsUInt32Number imageFormat)
00038 {
00039     // is color or grayscale image given?
00040     typedef typename vigra::NumericTraits<typename ImageType::value_type>::isScalar is_scalar;
00041     const bool isGrayscale(is_scalar().asBool);
00042     // create input icc profile
00043     cmsHPROFILE inputICC = NULL;
00044     if (!iccProfile.empty())
00045     {
00046         inputICC = cmsOpenProfileFromMem(iccProfile.data(), iccProfile.size());
00047     };
00048     if (inputICC != NULL)
00049     {
00050         // check that input profile matches image type
00051         if (isGrayscale)
00052         {
00053             if (cmsGetColorSpace(inputICC) != cmsSigGrayData)
00054             {
00055                 cmsCloseProfile(inputICC);
00056                 inputICC = NULL;
00057                 return;
00058             };
00059         }
00060         else
00061         {
00062             if (cmsGetColorSpace(inputICC) != cmsSigRgbData)
00063             {
00064                 cmsCloseProfile(inputICC);
00065                 inputICC = NULL;
00066                 return;
00067             };
00068         };
00069     };
00070     // if there is no icc profile in file do nothing
00071     if (inputICC == NULL)
00072     {
00073         return;
00074     };
00075     // build output profile
00076     cmsHPROFILE outputICC;
00077     if (isGrayscale)
00078     {
00079         // default grayscale curve with gamma of 2.2
00080         cmsToneCurve* Curve = cmsBuildGamma(NULL, 2.2);
00081         if (Curve == NULL)
00082         {
00083             return;
00084         };
00085         outputICC = cmsCreateGrayProfile(cmsD50_xyY(), Curve);
00086         cmsFreeToneCurve(Curve);
00087     }
00088     else
00089     {
00090         // sRGB for color images
00091         outputICC = cmsCreate_sRGBProfile();
00092     }
00093     // now build transform and do actual transformation
00094     cmsHTRANSFORM transform = cmsCreateTransform(inputICC, imageFormat, outputICC, imageFormat,
00095         INTENT_PERCEPTUAL, cmsFLAGS_BLACKPOINTCOMPENSATION);
00096     cmsDoTransform(transform, image.begin(), image.begin(), image.width()*image.height());
00097     // clean up
00098     cmsDeleteTransform(transform);
00099     cmsCloseProfile(inputICC);
00100     cmsCloseProfile(outputICC);
00101 };
00102 
00103 };  // namespace Color
00104 
00105 }; // namespace HuginBase
00106 
00107 #endif // _CMS_H

Generated on 20 Apr 2018 for Hugintrunk by  doxygen 1.4.7