MagickCore  6.8.5
gem-private.h
Go to the documentation of this file.
1 /*
2  Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization
3  dedicated to making software imaging solutions freely available.
4 
5  You may not use this file except in compliance with the License.
6  obtain a copy of the License at
7 
8  http://www.imagemagick.org/script/license.php
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 
16  MagickCore private graphic gems methods.
17 */
18 #ifndef _MAGICKCORE_GEM_PRIVATE_H
19 #define _MAGICKCORE_GEM_PRIVATE_H
20 
21 #if defined(__cplusplus) || defined(c_plusplus)
22 extern "C" {
23 #endif
24 
25 #include "magick/pixel-private.h"
26 
27 #define D65X 0.950456
28 #define D65Y 1.0
29 #define D65Z 1.088754
30 #define CIEEpsilon (216.0/24389.0)
31 #define CIEK (24389.0/27.0)
32 
33 static inline void ConvertLabToXYZ(const double L,const double a,const double b,
34  double *X,double *Y,double *Z)
35 {
36  double
37  x,
38  y,
39  z;
40 
41  assert(X != (double *) NULL);
42  assert(Y != (double *) NULL);
43  assert(Z != (double *) NULL);
44  y=(L+16.0)/116.0;
45  x=y+a/500.0;
46  z=y-b/200.0;
47  if ((x*x*x) > CIEEpsilon)
48  x=(x*x*x);
49  else
50  x=(116.0*x-16.0)/CIEK;
51  if ((y*y*y) > CIEEpsilon)
52  y=(y*y*y);
53  else
54  y=L/CIEK;
55  if ((z*z*z) > CIEEpsilon)
56  z=(z*z*z);
57  else
58  z=(116.0*z-16.0)/CIEK;
59  *X=D65X*x;
60  *Y=D65Y*y;
61  *Z=D65Z*z;
62 }
63 
64 static inline void ConvertXYZToLuv(const double X,const double Y,const double Z,
65  double *L,double *u,double *v)
66 {
67  double
68  alpha;
69 
70  assert(L != (double *) NULL);
71  assert(u != (double *) NULL);
72  assert(v != (double *) NULL);
73  if ((Y/D65Y) > CIEEpsilon)
74  *L=(double) (116.0*pow(Y/D65Y,1.0/3.0)-16.0);
75  else
76  *L=CIEK*(Y/D65Y);
77  alpha=PerceptibleReciprocal(X+15.0*Y+3.0*Z);
78  *u=13.0*(*L)*((4.0*alpha*X)-(4.0*D65X/(D65X+15.0*D65Y+3.0*D65Z)));
79  *v=13.0*(*L)*((9.0*alpha*Y)-(9.0*D65Y/(D65X+15.0*D65Y+3.0*D65Z)));
80  *L/=100.0;
81  *u=(*u+134.0)/354.0;
82  *v=(*v+140.0)/262.0;
83 }
84 
85 static inline void ConvertRGBToXYZ(const Quantum red,const Quantum green,
86  const Quantum blue,double *X,double *Y,double *Z)
87 {
88  double
89  b,
90  g,
91  r;
92 
93  assert(X != (double *) NULL);
94  assert(Y != (double *) NULL);
95  assert(Z != (double *) NULL);
99  *X=0.41239558896741421610*r+0.35758343076371481710*g+0.18049264738170157350*b;
100  *Y=0.21258623078559555160*r+0.71517030370341084990*g+0.07220049864333622685*b;
101  *Z=0.01929721549174694484*r+0.11918386458084853180*g+0.95049712513157976600*b;
102 }
103 
104 static inline void ConvertXYZToLab(const double X,const double Y,const double Z,
105  double *L,double *a,double *b)
106 {
107  double
108  x,
109  y,
110  z;
111 
112  assert(L != (double *) NULL);
113  assert(a != (double *) NULL);
114  assert(b != (double *) NULL);
115  if ((X/D65X) > CIEEpsilon)
116  x=pow(X/D65X,1.0/3.0);
117  else
118  x=(CIEK*X/D65X+16.0)/116.0;
119  if ((Y/D65Y) > CIEEpsilon)
120  y=pow(Y/D65Y,1.0/3.0);
121  else
122  y=(CIEK*Y/D65Y+16.0)/116.0;
123  if ((Z/D65Z) > CIEEpsilon)
124  z=pow(Z/D65Z,1.0/3.0);
125  else
126  z=(CIEK*Z/D65Z+16.0)/116.0;
127  *L=((116.0*y)-16.0)/100.0;
128  *a=(500.0*(x-y))/255.0+0.5;
129  *b=(200.0*(y-z))/255.0+0.5;
130 }
131 
132 static inline void ConvertLuvToXYZ(const double L,const double u,const double v,
133  double *X,double *Y,double *Z)
134 {
135  assert(X != (double *) NULL);
136  assert(Y != (double *) NULL);
137  assert(Z != (double *) NULL);
138  if (L > (CIEK*CIEEpsilon))
139  *Y=(double) pow((L+16.0)/116.0,3.0);
140  else
141  *Y=L/CIEK;
142  *X=((*Y*((39.0*L/(v+13.0*L*(9.0*D65Y/(D65X+15.0*D65Y+3.0*D65Z))))-5.0))+
143  5.0*(*Y))/((((52.0f*L/(u+13.0*L*(4.0*D65X/(D65X+15.0*D65Y+3.0*D65Z))))-1.0)/
144  3.0)-(-1.0/3.0));
145  *Z=(*X*(((52.0f*L/(u+13.0*L*(4.0*D65X/(D65X+15.0*D65Y+3.0*D65Z))))-1.0)/3.0))-
146  5.0*(*Y);
147 }
148 
149 static inline void ConvertXYZToRGB(const double X,const double Y,const double Z,
150  Quantum *red,Quantum *green,Quantum *blue)
151 {
152  double
153  b,
154  g,
155  r;
156 
157  assert(red != (Quantum *) NULL);
158  assert(green != (Quantum *) NULL);
159  assert(blue != (Quantum *) NULL);
160  r=3.2406*X-1.5372*Y-0.4986*Z;
161  g=(-0.9689)*X+1.8758*Y+0.0415*Z;
162  b=0.0557*X-0.2040*Y+1.0570*Z;
166 }
167 
168 
169 #if defined(__cplusplus) || defined(c_plusplus)
170 }
171 #endif
172 
173 #endif