00001
00002
00003
00004
00005
00006
00007 #ifndef __IM_COLOR_H
00008 #define __IM_COLOR_H
00009
00010 #include "im_math.h"
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 inline float imColorZero(int data_type)
00077 {
00078 float zero[] = {128.0f, 32768.0f, 8388608.0f, 0.5f};
00079 return zero[data_type];
00080 }
00081
00082
00083
00084 inline int imColorMax(int data_type)
00085 {
00086 int max[] = {255, 65535, 16777215, 1};
00087 return max[data_type];
00088 }
00089
00090
00091
00092
00093
00094
00095
00096 template <class T>
00097 inline T imColorQuantize(const float& value, const T& max)
00098 {
00099 if (max == 1) return (T)value;
00100 if (value >= 1) return max;
00101 if (value <= 0) return 0;
00102
00103 return (T)(value*(max + 1));
00104 }
00105
00106
00107
00108
00109
00110
00111
00112 template <class T>
00113 inline float imColorReconstruct(const T& value, const T& max)
00114 {
00115 if (max == 1) return (float)value;
00116 if (value <= 0) return 0;
00117 if (value >= max) return 1;
00118 return (((float)value + 0.5f)/((float)max + 1.0f));
00119 }
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131 template <class T>
00132 inline void imColorYCbCr2RGB(const T Y, const T Cb, const T Cr,
00133 T& R, T& G, T& B,
00134 const T& zero, const T& max)
00135 {
00136 float r = float(Y + 1.402f * (Cr - zero));
00137 float g = float(Y - 0.344f * (Cb - zero) - 0.714f * (Cr - zero));
00138 float b = float(Y + 1.772f * (Cb - zero));
00139
00140
00141
00142 R = (T)IM_CROPMAX(r, max);
00143 G = (T)IM_CROPMAX(g, max);
00144 B = (T)IM_CROPMAX(b, max);
00145 }
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157 template <class T>
00158 inline void imColorRGB2YCbCr(const T R, const T G, const T B,
00159 T& Y, T& Cb, T& Cr,
00160 const T& zero)
00161 {
00162 Y = (T)( 0.299f *R + 0.587f *G + 0.114f *B);
00163 Cb = (T)(-0.169f *R - 0.331f *G + 0.500f *B + (float)zero);
00164 Cr = (T)( 0.500f *R - 0.419f *G - 0.081f *B + (float)zero);
00165
00166
00167 }
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 template <class T>
00180 inline void imColorCMYK2RGB(const T C, const T M, const T Y, const T K,
00181 T& R, T& G, T& B, const T& max)
00182 {
00183 T W = max - K;
00184 R = (T)((W * (max - C)) / max);
00185 G = (T)((W * (max - M)) / max);
00186 B = (T)((W * (max - Y)) / max);
00187
00188
00189 }
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201 template <class T>
00202 inline void imColorXYZ2RGB(const T X, const T Y, const T Z,
00203 T& R, T& G, T& B, const T& max)
00204 {
00205 float r = 3.2406f *X - 1.5372f *Y - 0.4986f *Z;
00206 float g = -0.9689f *X + 1.8758f *Y + 0.0415f *Z;
00207 float b = 0.0557f *X - 0.2040f *Y + 1.0570f *Z;
00208
00209
00210
00211 R = (T)IM_CROPMAX(r, max);
00212 G = (T)IM_CROPMAX(g, max);
00213 B = (T)IM_CROPMAX(b, max);
00214 }
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226 template <class T>
00227 inline void imColorRGB2XYZ(const T R, const T G, const T B,
00228 T& X, T& Y, T& Z)
00229 {
00230 X = (T)(0.4124f *R + 0.3576f *G + 0.1805f *B);
00231 Y = (T)(0.2126f *R + 0.7152f *G + 0.0722f *B);
00232 Z = (T)(0.0193f *R + 0.1192f *G + 0.9505f *B);
00233
00234
00235 }
00236
00237 #define IM_FWLAB(_w) (_w > 0.008856f? \
00238 powf(_w, 1.0f/3.0f): \
00239 7.787f * _w + 0.16f/1.16f)
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259 inline void imColorXYZ2Lab(const float X, const float Y, const float Z,
00260 float& L, float& a, float& b)
00261 {
00262 float fX = X / 0.9505f;
00263 float fY = Y / 1.0f;
00264 float fZ = Z / 1.0890f;
00265
00266 fX = IM_FWLAB(fX);
00267 fY = IM_FWLAB(fY);
00268 fZ = IM_FWLAB(fZ);
00269
00270 L = 1.16f * fY - 0.16f;
00271 a = 2.5f * (fX - fY);
00272 b = (fY - fZ);
00273 }
00274
00275 #define IM_GWLAB(_w) (_w > 0.20689f? \
00276 powf(_w, 3.0f): \
00277 0.1284f * (_w - 0.16f/1.16f))
00278
00279
00280
00281
00282
00283 inline void imColorLab2XYZ(const float L, const float a, const float b,
00284 float& X, float& Y, float& Z)
00285
00286 {
00287 float fY = (L + 0.16f) / 1.16f;
00288 float gY = IM_GWLAB(fY);
00289
00290 float fgY = IM_FWLAB(gY);
00291 float gX = fgY + a / 2.5f;
00292 float gZ = fgY - b;
00293 gX = IM_GWLAB(gX);
00294 gZ = IM_GWLAB(gZ);
00295
00296 X = gX * 0.9505f;
00297 Y = gY * 1.0f;
00298 Z = gZ * 1.0890f;
00299 }
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324 inline void imColorXYZ2Luv(const float X, const float Y, const float Z,
00325 float& L, float& u, float& v)
00326 {
00327 float XYZ = (float)(X + 15 * Y + 3 * Z);
00328 float fY = Y / 1.0f;
00329
00330 if (XYZ != 0)
00331 {
00332 L = 1.16f * IM_FWLAB(fY) - 0.16f;
00333 u = 6.5f * L * ((4 * X)/XYZ - 0.1978f);
00334 v = 6.5f * L * ((9 * Y)/XYZ - 0.4683f);
00335 }
00336 else
00337 {
00338 L = u = v = 0;
00339 }
00340 }
00341
00342
00343
00344
00345
00346 inline void imColorLuv2XYZ(const float L, const float u, const float v,
00347 float& X, float& Y, float& Z)
00348
00349 {
00350 float fY = (L + 0.16f) / 1.16f;
00351 Y = IM_GWLAB(fY) * 1.0f;
00352
00353 float ul = 0.1978f, vl = 0.4683f;
00354 if (L != 0)
00355 {
00356 ul = u / (6.5f * L) + 0.1978f;
00357 vl = v / (6.5f * L) + 0.4683f;
00358 }
00359
00360 X = ((9 * ul) / (4 * vl)) * Y;
00361 Z = ((12 - 3 * ul - 20 * vl) / (4 * vl)) * Y;
00362 }
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375 inline float imColorTransfer2Linear(const float& nonlinear_value)
00376 {
00377 if (nonlinear_value < 0.03928f)
00378 return nonlinear_value / 12.92f;
00379 else
00380 return powf((nonlinear_value + 0.055f) / 1.055f, 2.4f);
00381 }
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394 inline float imColorTransfer2Nonlinear(const float& value)
00395 {
00396 if (value < 0.0031308f)
00397 return 12.92f * value;
00398 else
00399 return 1.055f * powf(value, 1.0f/2.4f) - 0.055f;
00400 }
00401
00402
00403
00404 inline void imColorRGB2RGBNonlinear(const float RL, const float GL, const float BL,
00405 float& R, float& G, float& B)
00406 {
00407 R = imColorTransfer2Nonlinear(RL);
00408 G = imColorTransfer2Nonlinear(GL);
00409 B = imColorTransfer2Nonlinear(BL);
00410 }
00411
00412
00413
00414
00415
00416
00417 template <class T>
00418 inline T imColorRGB2Luma(const T R, const T G, const T B)
00419 {
00420 return (T)((299 * R + 587 * G + 114 * B) / 1000);
00421 }
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436 inline float imColorLuminance2Lightness(const float& Y)
00437 {
00438 return 1.16f * IM_FWLAB(Y) - 0.16f;
00439 }
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454 inline float imColorLightness2Luminance(const float& L)
00455 {
00456 float fY = (L + 0.16f) / 1.16f;
00457 return IM_GWLAB(fY);
00458 }
00459
00460 #undef IM_FWLAB
00461 #undef IM_GWLAB
00462 #undef IM_CROPL
00463 #undef IM_CROPC
00464
00465 #endif