cristy | d1dd6e4 | 2011-09-04 01:46:08 +0000 | [diff] [blame] | 1 | /* |
Cristy | f775a5c | 2019-11-26 14:27:47 -0500 | [diff] [blame] | 2 | Copyright 1999-2020 ImageMagick Studio LLC, a non-profit organization |
cristy | d1dd6e4 | 2011-09-04 01:46:08 +0000 | [diff] [blame] | 3 | dedicated to making software imaging solutions freely available. |
| 4 | |
Cristy | 57b308b | 2019-01-19 17:53:40 -0500 | [diff] [blame] | 5 | You may not use this file except in compliance with the License. You may |
cristy | d1dd6e4 | 2011-09-04 01:46:08 +0000 | [diff] [blame] | 6 | obtain a copy of the License at |
| 7 | |
Cristy | 9ddfcca | 2018-09-09 19:46:34 -0400 | [diff] [blame] | 8 | https://imagemagick.org/script/license.php |
cristy | d1dd6e4 | 2011-09-04 01:46:08 +0000 | [diff] [blame] | 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 | */ |
Cristy | 83bceaa | 2016-06-03 20:39:35 -0400 | [diff] [blame] | 18 | #ifndef MAGICKCORE_GEM_PRIVATE_H |
| 19 | #define MAGICKCORE_GEM_PRIVATE_H |
cristy | d1dd6e4 | 2011-09-04 01:46:08 +0000 | [diff] [blame] | 20 | |
cristy | 0b9c0d8 | 2015-05-24 22:27:57 +0000 | [diff] [blame] | 21 | #include "MagickCore/pixel-accessor.h" |
Cristy | d2f2234 | 2020-01-05 11:46:47 -0500 | [diff] [blame] | 22 | #include "MagickCore/visual-effects.h" |
cristy | 6398ec7 | 2013-11-28 02:00:27 +0000 | [diff] [blame] | 23 | |
cristy | d1dd6e4 | 2011-09-04 01:46:08 +0000 | [diff] [blame] | 24 | #if defined(__cplusplus) || defined(c_plusplus) |
| 25 | extern "C" { |
| 26 | #endif |
| 27 | |
cristy | 15ec6a6 | 2013-04-20 01:11:51 +0000 | [diff] [blame] | 28 | #define D65X 0.950456 |
| 29 | #define D65Y 1.0 |
| 30 | #define D65Z 1.088754 |
| 31 | #define CIEEpsilon (216.0/24389.0) |
| 32 | #define CIEK (24389.0/27.0) |
cristy | d803bd0 | 2013-04-03 19:10:51 +0000 | [diff] [blame] | 33 | |
cristy | 8ea8122 | 2011-09-04 10:33:32 +0000 | [diff] [blame] | 34 | extern MagickPrivate double |
| 35 | GenerateDifferentialNoise(RandomInfo *,const Quantum,const NoiseType, |
cristy | 9ed1f81 | 2011-10-08 02:00:08 +0000 | [diff] [blame] | 36 | const double); |
cristy | 8ea8122 | 2011-09-04 10:33:32 +0000 | [diff] [blame] | 37 | |
| 38 | extern MagickPrivate size_t |
| 39 | GetOptimalKernelWidth(const double,const double), |
| 40 | GetOptimalKernelWidth1D(const double,const double), |
| 41 | GetOptimalKernelWidth2D(const double,const double); |
| 42 | |
cristy | d1dd6e4 | 2011-09-04 01:46:08 +0000 | [diff] [blame] | 43 | extern MagickPrivate void |
cristy | 722fc0c | 2012-08-04 23:15:43 +0000 | [diff] [blame] | 44 | ConvertHCLToRGB(const double,const double,const double,double *,double *, |
| 45 | double *), |
cristy | 9e2436a | 2013-05-02 20:35:59 +0000 | [diff] [blame] | 46 | ConvertHCLpToRGB(const double,const double,const double,double *,double *, |
| 47 | double *), |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 48 | ConvertHSBToRGB(const double,const double,const double,double *,double *, |
cristy | d1dd6e4 | 2011-09-04 01:46:08 +0000 | [diff] [blame] | 49 | double *), |
cristy | af64eb2 | 2013-05-02 14:07:10 +0000 | [diff] [blame] | 50 | ConvertHSIToRGB(const double,const double,const double,double *,double *, |
| 51 | double *), |
cristy | 246c313 | 2013-05-02 16:35:53 +0000 | [diff] [blame] | 52 | ConvertHSVToRGB(const double,const double,const double,double *,double *, |
| 53 | double *), |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 54 | ConvertHWBToRGB(const double,const double,const double,double *,double *, |
cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 55 | double *), |
cristy | df42b17 | 2013-04-05 13:35:37 +0000 | [diff] [blame] | 56 | ConvertLCHabToRGB(const double,const double,const double,double *,double *, |
| 57 | double *), |
| 58 | ConvertLCHuvToRGB(const double,const double,const double,double *,double *, |
cristy | b285095 | 2013-04-04 19:00:52 +0000 | [diff] [blame] | 59 | double *), |
cristy | 722fc0c | 2012-08-04 23:15:43 +0000 | [diff] [blame] | 60 | ConvertRGBToHCL(const double,const double,const double,double *,double *, |
| 61 | double *), |
cristy | 9e2436a | 2013-05-02 20:35:59 +0000 | [diff] [blame] | 62 | ConvertRGBToHCLp(const double,const double,const double,double *,double *, |
| 63 | double *), |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 64 | ConvertRGBToHSB(const double,const double,const double,double *,double *, |
cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 65 | double *), |
cristy | af64eb2 | 2013-05-02 14:07:10 +0000 | [diff] [blame] | 66 | ConvertRGBToHSI(const double,const double,const double,double *,double *, |
| 67 | double *), |
cristy | 246c313 | 2013-05-02 16:35:53 +0000 | [diff] [blame] | 68 | ConvertRGBToHSV(const double,const double,const double,double *,double *, |
| 69 | double *), |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 70 | ConvertRGBToHWB(const double,const double,const double,double *,double *, |
cristy | b285095 | 2013-04-04 19:00:52 +0000 | [diff] [blame] | 71 | double *), |
cristy | df42b17 | 2013-04-05 13:35:37 +0000 | [diff] [blame] | 72 | ConvertRGBToLCHab(const double,const double,const double,double *,double *, |
| 73 | double *), |
| 74 | ConvertRGBToLCHuv(const double,const double,const double,double *,double *, |
cristy | d1dd6e4 | 2011-09-04 01:46:08 +0000 | [diff] [blame] | 75 | double *); |
| 76 | |
cristy | d803bd0 | 2013-04-03 19:10:51 +0000 | [diff] [blame] | 77 | static inline void ConvertLabToXYZ(const double L,const double a,const double b, |
| 78 | double *X,double *Y,double *Z) |
| 79 | { |
| 80 | double |
| 81 | x, |
| 82 | y, |
| 83 | z; |
| 84 | |
| 85 | assert(X != (double *) NULL); |
| 86 | assert(Y != (double *) NULL); |
| 87 | assert(Z != (double *) NULL); |
cristy | 35605e9 | 2013-05-06 11:33:57 +0000 | [diff] [blame] | 88 | y=(L+16.0)/116.0; |
| 89 | x=y+a/500.0; |
| 90 | z=y-b/200.0; |
cristy | d803bd0 | 2013-04-03 19:10:51 +0000 | [diff] [blame] | 91 | if ((x*x*x) > CIEEpsilon) |
| 92 | x=(x*x*x); |
| 93 | else |
cristy | 15ec6a6 | 2013-04-20 01:11:51 +0000 | [diff] [blame] | 94 | x=(116.0*x-16.0)/CIEK; |
cristy | d803bd0 | 2013-04-03 19:10:51 +0000 | [diff] [blame] | 95 | if ((y*y*y) > CIEEpsilon) |
| 96 | y=(y*y*y); |
| 97 | else |
cristy | 35605e9 | 2013-05-06 11:33:57 +0000 | [diff] [blame] | 98 | y=L/CIEK; |
cristy | d803bd0 | 2013-04-03 19:10:51 +0000 | [diff] [blame] | 99 | if ((z*z*z) > CIEEpsilon) |
| 100 | z=(z*z*z); |
| 101 | else |
cristy | 15ec6a6 | 2013-04-20 01:11:51 +0000 | [diff] [blame] | 102 | z=(116.0*z-16.0)/CIEK; |
| 103 | *X=D65X*x; |
| 104 | *Y=D65Y*y; |
| 105 | *Z=D65Z*z; |
cristy | d803bd0 | 2013-04-03 19:10:51 +0000 | [diff] [blame] | 106 | } |
| 107 | |
cristy | df42b17 | 2013-04-05 13:35:37 +0000 | [diff] [blame] | 108 | static inline void ConvertLuvToXYZ(const double L,const double u,const double v, |
| 109 | double *X,double *Y,double *Z) |
| 110 | { |
Cristy | 3d71aa8 | 2019-10-11 19:47:56 -0400 | [diff] [blame] | 111 | double |
| 112 | gamma; |
| 113 | |
cristy | df42b17 | 2013-04-05 13:35:37 +0000 | [diff] [blame] | 114 | assert(X != (double *) NULL); |
| 115 | assert(Y != (double *) NULL); |
| 116 | assert(Z != (double *) NULL); |
cristy | a871691 | 2013-05-06 12:04:10 +0000 | [diff] [blame] | 117 | if (L > (CIEK*CIEEpsilon)) |
| 118 | *Y=(double) pow((L+16.0)/116.0,3.0); |
cristy | df42b17 | 2013-04-05 13:35:37 +0000 | [diff] [blame] | 119 | else |
cristy | a871691 | 2013-05-06 12:04:10 +0000 | [diff] [blame] | 120 | *Y=L/CIEK; |
Cristy | 3d71aa8 | 2019-10-11 19:47:56 -0400 | [diff] [blame] | 121 | gamma=PerceptibleReciprocal((((52.0*L/(u+13.0*L*(4.0*D65X/(D65X+15.0*D65Y+ |
| 122 | 3.0*D65Z))))-1.0)/3.0)-(-1.0/3.0)); |
| 123 | *X=gamma*((*Y*((39.0*L/(v+13.0*L*(9.0*D65Y/(D65X+15.0*D65Y+3.0*D65Z))))-5.0))+ |
| 124 | 5.0*(*Y)); |
cristy | 887d513 | 2013-05-06 20:05:35 +0000 | [diff] [blame] | 125 | *Z=(*X*(((52.0*L/(u+13.0*L*(4.0*D65X/(D65X+15.0*D65Y+3.0*D65Z))))-1.0)/3.0))- |
cristy | a871691 | 2013-05-06 12:04:10 +0000 | [diff] [blame] | 126 | 5.0*(*Y); |
cristy | df42b17 | 2013-04-05 13:35:37 +0000 | [diff] [blame] | 127 | } |
| 128 | |
cristy | d803bd0 | 2013-04-03 19:10:51 +0000 | [diff] [blame] | 129 | static inline void ConvertRGBToXYZ(const double red,const double green, |
| 130 | const double blue,double *X,double *Y,double *Z) |
| 131 | { |
| 132 | double |
| 133 | b, |
| 134 | g, |
| 135 | r; |
| 136 | |
cristy | 8eccf57 | 2013-05-03 17:30:54 +0000 | [diff] [blame] | 137 | /* |
| 138 | Convert RGB to XYZ colorspace. |
| 139 | */ |
cristy | d803bd0 | 2013-04-03 19:10:51 +0000 | [diff] [blame] | 140 | assert(X != (double *) NULL); |
| 141 | assert(Y != (double *) NULL); |
| 142 | assert(Z != (double *) NULL); |
cristy | 58f7537 | 2013-05-05 17:34:43 +0000 | [diff] [blame] | 143 | r=QuantumScale*DecodePixelGamma(red); |
| 144 | g=QuantumScale*DecodePixelGamma(green); |
| 145 | b=QuantumScale*DecodePixelGamma(blue); |
cristy | d65a94d | 2015-01-28 22:20:48 +0000 | [diff] [blame] | 146 | *X=0.4124564*r+0.3575761*g+0.1804375*b; |
| 147 | *Y=0.2126729*r+0.7151522*g+0.0721750*b; |
| 148 | *Z=0.0193339*r+0.1191920*g+0.9503041*b; |
cristy | d803bd0 | 2013-04-03 19:10:51 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static inline void ConvertXYZToLab(const double X,const double Y,const double Z, |
| 152 | double *L,double *a,double *b) |
| 153 | { |
| 154 | double |
| 155 | x, |
| 156 | y, |
| 157 | z; |
cristy | c32cb02 | 2013-05-06 13:32:31 +0000 | [diff] [blame] | 158 | |
cristy | d803bd0 | 2013-04-03 19:10:51 +0000 | [diff] [blame] | 159 | assert(L != (double *) NULL); |
| 160 | assert(a != (double *) NULL); |
| 161 | assert(b != (double *) NULL); |
cristy | 15ec6a6 | 2013-04-20 01:11:51 +0000 | [diff] [blame] | 162 | if ((X/D65X) > CIEEpsilon) |
| 163 | x=pow(X/D65X,1.0/3.0); |
cristy | d803bd0 | 2013-04-03 19:10:51 +0000 | [diff] [blame] | 164 | else |
cristy | 15ec6a6 | 2013-04-20 01:11:51 +0000 | [diff] [blame] | 165 | x=(CIEK*X/D65X+16.0)/116.0; |
| 166 | if ((Y/D65Y) > CIEEpsilon) |
| 167 | y=pow(Y/D65Y,1.0/3.0); |
cristy | d803bd0 | 2013-04-03 19:10:51 +0000 | [diff] [blame] | 168 | else |
cristy | 15ec6a6 | 2013-04-20 01:11:51 +0000 | [diff] [blame] | 169 | y=(CIEK*Y/D65Y+16.0)/116.0; |
| 170 | if ((Z/D65Z) > CIEEpsilon) |
| 171 | z=pow(Z/D65Z,1.0/3.0); |
cristy | d803bd0 | 2013-04-03 19:10:51 +0000 | [diff] [blame] | 172 | else |
cristy | 15ec6a6 | 2013-04-20 01:11:51 +0000 | [diff] [blame] | 173 | z=(CIEK*Z/D65Z+16.0)/116.0; |
| 174 | *L=((116.0*y)-16.0)/100.0; |
| 175 | *a=(500.0*(x-y))/255.0+0.5; |
| 176 | *b=(200.0*(y-z))/255.0+0.5; |
cristy | d803bd0 | 2013-04-03 19:10:51 +0000 | [diff] [blame] | 177 | } |
| 178 | |
cristy | df42b17 | 2013-04-05 13:35:37 +0000 | [diff] [blame] | 179 | static inline void ConvertXYZToLuv(const double X,const double Y,const double Z, |
| 180 | double *L,double *u,double *v) |
| 181 | { |
| 182 | double |
| 183 | alpha; |
| 184 | |
| 185 | assert(L != (double *) NULL); |
| 186 | assert(u != (double *) NULL); |
| 187 | assert(v != (double *) NULL); |
cristy | 15ec6a6 | 2013-04-20 01:11:51 +0000 | [diff] [blame] | 188 | if ((Y/D65Y) > CIEEpsilon) |
| 189 | *L=(double) (116.0*pow(Y/D65Y,1.0/3.0)-16.0); |
cristy | df42b17 | 2013-04-05 13:35:37 +0000 | [diff] [blame] | 190 | else |
cristy | 15ec6a6 | 2013-04-20 01:11:51 +0000 | [diff] [blame] | 191 | *L=CIEK*(Y/D65Y); |
| 192 | alpha=PerceptibleReciprocal(X+15.0*Y+3.0*Z); |
| 193 | *u=13.0*(*L)*((4.0*alpha*X)-(4.0*D65X/(D65X+15.0*D65Y+3.0*D65Z))); |
| 194 | *v=13.0*(*L)*((9.0*alpha*Y)-(9.0*D65Y/(D65X+15.0*D65Y+3.0*D65Z))); |
| 195 | *L/=100.0; |
| 196 | *u=(*u+134.0)/354.0; |
| 197 | *v=(*v+140.0)/262.0; |
cristy | df42b17 | 2013-04-05 13:35:37 +0000 | [diff] [blame] | 198 | } |
| 199 | |
cristy | 48348e7 | 2013-05-07 01:21:28 +0000 | [diff] [blame] | 200 | static inline void ConvertXYZToRGB(const double X,const double Y,const double Z, |
cristy | d803bd0 | 2013-04-03 19:10:51 +0000 | [diff] [blame] | 201 | double *red,double *green,double *blue) |
| 202 | { |
| 203 | double |
| 204 | b, |
| 205 | g, |
| 206 | r; |
| 207 | |
cristy | d803bd0 | 2013-04-03 19:10:51 +0000 | [diff] [blame] | 208 | assert(red != (double *) NULL); |
| 209 | assert(green != (double *) NULL); |
| 210 | assert(blue != (double *) NULL); |
cristy | d65a94d | 2015-01-28 22:20:48 +0000 | [diff] [blame] | 211 | r=3.2404542*X-1.5371385*Y-0.4985314*Z; |
| 212 | g=(-0.9692660)*X+1.8760108*Y+0.0415560*Z; |
| 213 | b=0.0556434*X-0.2040259*Y+1.0572252*Z; |
cristy | 58f7537 | 2013-05-05 17:34:43 +0000 | [diff] [blame] | 214 | *red=EncodePixelGamma(QuantumRange*r); |
| 215 | *green=EncodePixelGamma(QuantumRange*g); |
| 216 | *blue=EncodePixelGamma(QuantumRange*b); |
cristy | d803bd0 | 2013-04-03 19:10:51 +0000 | [diff] [blame] | 217 | } |
| 218 | |
cristy | d1dd6e4 | 2011-09-04 01:46:08 +0000 | [diff] [blame] | 219 | #if defined(__cplusplus) || defined(c_plusplus) |
| 220 | } |
| 221 | #endif |
| 222 | |
| 223 | #endif |