Tizen 2.0 Release
[external/lcms.git] / src / cmssamp.c
1 //---------------------------------------------------------------------------------
2 //
3 //  Little Color Management System
4 //  Copyright (c) 1998-2010 Marti Maria Saguer
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining 
7 // a copy of this software and associated documentation files (the "Software"), 
8 // to deal in the Software without restriction, including without limitation 
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense, 
10 // and/or sell copies of the Software, and to permit persons to whom the Software 
11 // is furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in 
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
18 // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 //
24 //---------------------------------------------------------------------------------
25 //
26
27 #include "lcms2_internal.h"
28
29
30
31 // This file contains routines for resampling and LUT optimization, black point detection
32 // and black preservation. 
33
34 // Black point detection -------------------------------------------------------------------------
35
36
37 // PCS -> PCS round trip transform, always uses relative intent on the device -> pcs 
38 static
39 cmsHTRANSFORM CreateRoundtripXForm(cmsHPROFILE hProfile, cmsUInt32Number nIntent)
40 {
41     cmsHPROFILE hLab = cmsCreateLab4Profile(NULL);
42     cmsHTRANSFORM xform;
43     cmsBool BPC[4] = { FALSE, FALSE, FALSE, FALSE };
44     cmsFloat64Number States[4] = { 1.0, 1.0, 1.0, 1.0 };
45     cmsHPROFILE hProfiles[4];
46     cmsUInt32Number Intents[4];
47     cmsContext ContextID = cmsGetProfileContextID(hProfile);
48
49     hProfiles[0] = hLab; hProfiles[1] = hProfile; hProfiles[2] = hProfile; hProfiles[3] = hLab;
50     Intents[0]   = INTENT_RELATIVE_COLORIMETRIC; Intents[1] = nIntent; Intents[2] = INTENT_RELATIVE_COLORIMETRIC; Intents[3] = INTENT_RELATIVE_COLORIMETRIC;
51
52     xform =  cmsCreateExtendedTransform(ContextID, 4, hProfiles, BPC, Intents, 
53         States, NULL, 0, TYPE_Lab_DBL, TYPE_Lab_DBL, cmsFLAGS_NOCACHE|cmsFLAGS_NOOPTIMIZE);
54
55     cmsCloseProfile(hLab);
56     return xform;
57 }
58
59 // Use darker colorants to obtain black point. This works in the relative colorimetric intent and
60 // assumes more ink results in darker colors. No ink limit is assumed.
61 static
62 cmsBool  BlackPointAsDarkerColorant(cmsHPROFILE    hInput,                               
63                                     cmsUInt32Number Intent,
64                                     cmsCIEXYZ* BlackPoint,
65                                     cmsUInt32Number dwFlags)
66 {
67     cmsUInt16Number *Black;
68     cmsHTRANSFORM xform;
69     cmsColorSpaceSignature Space;
70     cmsUInt32Number nChannels;
71     cmsUInt32Number dwFormat; 
72     cmsHPROFILE hLab;
73     cmsCIELab  Lab;
74     cmsCIEXYZ  BlackXYZ;        
75     cmsContext ContextID = cmsGetProfileContextID(hInput);
76     
77     // If the profile does not support input direction, assume Black point 0    
78     if (!cmsIsIntentSupported(hInput, Intent, LCMS_USED_AS_INPUT)) {
79
80         BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
81         return FALSE;
82     }
83     
84     // Create a formatter which has n channels and floating point
85     dwFormat = cmsFormatterForColorspaceOfProfile(hInput, 2, FALSE);
86
87    // Try to get black by using black colorant    
88     Space = cmsGetColorSpace(hInput);
89
90     // This function returns darker colorant in 16 bits for several spaces
91     if (!_cmsEndPointsBySpace(Space, NULL, &Black, &nChannels)) {
92         
93         BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
94         return FALSE;
95     }
96
97     if (nChannels != T_CHANNELS(dwFormat)) {
98        BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
99        return FALSE;
100     }
101
102     // Lab will be used as the output space, but lab2 will avoid recursion
103     hLab = cmsCreateLab2ProfileTHR(ContextID, NULL);
104     if (hLab == NULL) {
105        BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
106        return FALSE;    
107     }
108
109     // Create the transform
110     xform = cmsCreateTransformTHR(ContextID, hInput, dwFormat,
111                                 hLab, TYPE_Lab_DBL, Intent, cmsFLAGS_NOOPTIMIZE|cmsFLAGS_NOCACHE);
112     cmsCloseProfile(hLab);
113     
114     if (xform == NULL) {
115         // Something went wrong. Get rid of open resources and return zero as black
116         
117         BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
118         return FALSE;
119     }
120     
121     // Convert black to Lab
122     cmsDoTransform(xform, Black, &Lab, 1);
123
124     // Force it to be neutral, clip to max. L* of 50
125     Lab.a = Lab.b = 0;
126     if (Lab.L > 50) Lab.L = 50;
127
128     // Free the resources    
129     cmsDeleteTransform(xform);
130     
131     // Convert from Lab (which is now clipped) to XYZ.
132     cmsLab2XYZ(NULL, &BlackXYZ, &Lab);
133     
134     if (BlackPoint != NULL)
135         *BlackPoint = BlackXYZ;
136       
137     return TRUE;
138
139     cmsUNUSED_PARAMETER(dwFlags);
140 }
141
142 // Get a black point of output CMYK profile, discounting any ink-limiting embedded 
143 // in the profile. For doing that, we use perceptual intent in input direction:
144 // Lab (0, 0, 0) -> [Perceptual] Profile -> CMYK -> [Rel. colorimetric] Profile -> Lab
145 static
146 cmsBool BlackPointUsingPerceptualBlack(cmsCIEXYZ* BlackPoint, cmsHPROFILE hProfile)
147                                    
148 {    
149     cmsHTRANSFORM hRoundTrip;    
150     cmsCIELab LabIn, LabOut;
151     cmsCIEXYZ  BlackXYZ;        
152  
153      // Is the intent supported by the profile?
154     if (!cmsIsIntentSupported(hProfile, INTENT_PERCEPTUAL, LCMS_USED_AS_INPUT)) {
155
156         BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
157         return TRUE;
158     }
159         
160     hRoundTrip = CreateRoundtripXForm(hProfile, INTENT_PERCEPTUAL);
161     if (hRoundTrip == NULL) {
162         BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
163         return FALSE;
164     }
165
166     LabIn.L = LabIn.a = LabIn.b = 0;
167     cmsDoTransform(hRoundTrip, &LabIn, &LabOut, 1);
168
169     // Clip Lab to reasonable limits
170     if (LabOut.L > 50) LabOut.L = 50;
171     LabOut.a = LabOut.b = 0;
172
173     cmsDeleteTransform(hRoundTrip);
174   
175     // Convert it to XYZ
176     cmsLab2XYZ(NULL, &BlackXYZ, &LabOut);   
177     
178     if (BlackPoint != NULL)
179         *BlackPoint = BlackXYZ;
180
181     return TRUE;
182 }
183
184 // This function shouldn't exist at all -- there is such quantity of broken
185 // profiles on black point tag, that we must somehow fix chromaticity to 
186 // avoid huge tint when doing Black point compensation. This function does
187 // just that. There is a special flag for using black point tag, but turned 
188 // off by default because it is bogus on most profiles. The detection algorithm 
189 // involves to turn BP to neutral and to use only L component.  
190
191 cmsBool CMSEXPORT cmsDetectBlackPoint(cmsCIEXYZ* BlackPoint, cmsHPROFILE hProfile, cmsUInt32Number Intent, cmsUInt32Number dwFlags)
192 {    
193
194     // Zero for black point
195     if (cmsGetDeviceClass(hProfile) == cmsSigLinkClass) {
196
197         BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
198         return FALSE;       
199     }
200
201     // v4 + perceptual & saturation intents does have its own black point, and it is 
202     // well specified enough to use it. Black point tag is deprecated in V4.
203
204     if ((cmsGetEncodedICCversion(hProfile) >= 0x4000000) &&     
205         (Intent == INTENT_PERCEPTUAL || Intent == INTENT_SATURATION)) {
206
207             // Matrix shaper share MRC & perceptual intents
208             if (cmsIsMatrixShaper(hProfile)) 
209                 return BlackPointAsDarkerColorant(hProfile, INTENT_RELATIVE_COLORIMETRIC, BlackPoint, 0);
210
211             // Get Perceptual black out of v4 profiles. That is fixed for perceptual & saturation intents
212             BlackPoint -> X = cmsPERCEPTUAL_BLACK_X; 
213             BlackPoint -> Y = cmsPERCEPTUAL_BLACK_Y;
214             BlackPoint -> Z = cmsPERCEPTUAL_BLACK_Z;
215
216             return TRUE;
217     }
218
219
220 #ifdef CMS_USE_PROFILE_BLACK_POINT_TAG
221
222     // v2, v4 rel/abs colorimetric
223     if (cmsIsTag(hProfile, cmsSigMediaBlackPointTag) && 
224         Intent == INTENT_RELATIVE_COLORIMETRIC) {
225
226             cmsCIEXYZ *BlackPtr, BlackXYZ, UntrustedBlackPoint, TrustedBlackPoint, MediaWhite;
227             cmsCIELab Lab;
228
229             // If black point is specified, then use it, 
230
231             BlackPtr = cmsReadTag(hProfile, cmsSigMediaBlackPointTag);
232             if (BlackPtr != NULL) {
233
234                 BlackXYZ = *BlackPtr;
235                 _cmsReadMediaWhitePoint(&MediaWhite, hProfile);
236
237                 // Black point is absolute XYZ, so adapt to D50 to get PCS value
238                 cmsAdaptToIlluminant(&UntrustedBlackPoint, &MediaWhite, cmsD50_XYZ(), &BlackXYZ);
239
240                 // Force a=b=0 to get rid of any chroma
241                 cmsXYZ2Lab(NULL, &Lab, &UntrustedBlackPoint);
242                 Lab.a = Lab.b = 0;
243                 if (Lab.L > 50) Lab.L = 50; // Clip to L* <= 50
244                 cmsLab2XYZ(NULL, &TrustedBlackPoint, &Lab);
245
246                 if (BlackPoint != NULL)
247                     *BlackPoint = TrustedBlackPoint;
248                 
249                 return TRUE;
250             }
251     }
252 #endif
253
254     // That is about v2 profiles. 
255
256     // If output profile, discount ink-limiting and that's all
257     if (Intent == INTENT_RELATIVE_COLORIMETRIC && 
258         (cmsGetDeviceClass(hProfile) == cmsSigOutputClass) &&
259         (cmsGetColorSpace(hProfile)  == cmsSigCmykData))
260         return BlackPointUsingPerceptualBlack(BlackPoint, hProfile);
261
262     // Nope, compute BP using current intent.
263     return BlackPointAsDarkerColorant(hProfile, Intent, BlackPoint, dwFlags);
264 }
265
266