Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cms.h
Go to the documentation of this file.
1 // -*- c-basic-offset: 4 -*-
9 /* This is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This software is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public
20 * License along with this software. If not, see
21 * <http://www.gnu.org/licenses/>.
22 *
23 */
24 
25 #ifndef _CMS_H
26 #define _CMS_H
27 
28 #include "lcms2.h"
29 
30 namespace HuginBase
31 {
32 namespace Color
33 {
36 template<class ImageType>
37 void ApplyICCProfile(ImageType& image, const vigra::ImageImportInfo::ICCProfile& iccProfile, const cmsUInt32Number imageFormat)
38 {
39  // is color or grayscale image given?
40  typedef typename vigra::NumericTraits<typename ImageType::value_type>::isScalar is_scalar;
41  const bool isGrayscale(is_scalar().asBool);
42  // create input icc profile
43  cmsHPROFILE inputICC = NULL;
44  if (!iccProfile.empty())
45  {
46  inputICC = cmsOpenProfileFromMem(iccProfile.data(), iccProfile.size());
47  };
48  if (inputICC != NULL)
49  {
50  // check that input profile matches image type
51  if (isGrayscale)
52  {
53  if (cmsGetColorSpace(inputICC) != cmsSigGrayData)
54  {
55  cmsCloseProfile(inputICC);
56  inputICC = NULL;
57  return;
58  };
59  }
60  else
61  {
62  if (cmsGetColorSpace(inputICC) != cmsSigRgbData)
63  {
64  cmsCloseProfile(inputICC);
65  inputICC = NULL;
66  return;
67  };
68  };
69  };
70  // if there is no icc profile in file do nothing
71  if (inputICC == NULL)
72  {
73  return;
74  };
75  // build output profile
76  cmsHPROFILE outputICC;
77  if (isGrayscale)
78  {
79  // default grayscale curve with gamma of 2.2
80  cmsToneCurve* Curve = cmsBuildGamma(NULL, 2.2);
81  if (Curve == NULL)
82  {
83  return;
84  };
85  outputICC = cmsCreateGrayProfile(cmsD50_xyY(), Curve);
86  cmsFreeToneCurve(Curve);
87  }
88  else
89  {
90  // sRGB for color images
91  outputICC = cmsCreate_sRGBProfile();
92  }
93  // now build transform and do actual transformation
94  cmsHTRANSFORM transform = cmsCreateTransform(inputICC, imageFormat, outputICC, imageFormat,
95  INTENT_PERCEPTUAL, cmsFLAGS_BLACKPOINTCOMPENSATION);
96  cmsDoTransform(transform, image.begin(), image.begin(), image.width()*image.height());
97  // clean up
98  cmsDeleteTransform(transform);
99  cmsCloseProfile(inputICC);
100  cmsCloseProfile(outputICC);
101 };
102 
103 }; // namespace Color
104 
105 }; // namespace HuginBase
106 
107 #endif // _CMS_H
vigra::FRGBImage ImageType
void ApplyICCProfile(ImageType &image, const vigra::ImageImportInfo::ICCProfile &iccProfile, const cmsUInt32Number imageFormat)
converts given image with iccProfile to sRGB/gray space, need to give pixel type in lcms2 format work...
Definition: cms.h:37