Advertisement
Guest User

sRGB to Jzazbz and back shader

a guest
Nov 7th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. #version 150
  2.  
  3. uniform sampler2D source[];
  4. uniform sampler2D pixmap[];
  5. in Vertex { vec2 texCoord; };
  6. out vec4 fragColor;
  7.  
  8. const float Pi = 3.1415926535897932384626433832795;
  9.  
  10. // --- Reference White Values --- //{
  11.  
  12. const vec3 D50 = vec3(0.9642, 1.0000, 0.8251);
  13. const vec3 D55 = vec3(0.9568, 1.0000, 0.9214);
  14. const vec3 D65 = vec3(0.9504, 1.0000, 1.0888);
  15. //D9000 apparently isn't a real standard so here's the CCT daylight calculation result
  16. const vec3 D9000 = vec3(0.9520, 1.0000, 1.3661);
  17. //D9300 apparently isn't a real standard so here's the CCT daylight calculation result
  18. const vec3 D9300 = vec3(0.95271,1.00000,1.39177);
  19. //Various CRT monitors, Duv describes distance from the blackbody curve. The smaller it is, the closer to "white" it is. +/- 0.006 is recommended by ANSI and EnergyStar.
  20. //NEC Multisync C400, claims 9300K but it isn't
  21. const vec3 D9000NEC = vec3(0.88889,1.00000,1.28571);//8890K, 0.0139 Duv
  22. //KDS VS19
  23. const vec3 D9000KDS = vec3(0.90354,1.00000,1.31190);//8939K, 0.0114 Duv
  24. //}
  25.  
  26. // --- sRGB --- //{
  27. vec3 XYZ_to_sRGB(vec3 x) {
  28. x = x * mat3x3( 3.2404542, -1.5371385, -0.4985314, -0.9692660, 1.8760108, 0.0415560, 0.0556434, -0.2040259, 1.0572252 );
  29. x = mix(1.055*pow(x, vec3(1./2.4)) - 0.055, 12.92*x, step(x,vec3(0.0031308)));
  30. return x;
  31. }
  32.  
  33. vec3 sRGB_to_XYZ(vec3 x) {
  34. x = mix(pow((x + 0.055)/1.055,vec3(2.4)), x / 12.92, step(x,vec3(0.04045)));
  35. x = x * mat3x3( 0.4124564, 0.3575761, 0.1804375, 0.2126729, 0.7151522, 0.0721750, 0.0193339, 0.1191920, 0.9503041 );
  36. return x;
  37. }
  38. //}
  39.  
  40. // --- Jzazbz --- //{
  41. vec3 XYZ_to_Jzazbz(vec3 XYZ) {
  42. float b = 1.15;
  43. float g = 0.66;
  44. vec3 XYZprime = XYZ;
  45. XYZprime.x = XYZ.x * b - (b - 1) * XYZ.z;
  46. XYZprime.y = XYZ.y * g - (g - 1) * XYZ.x;
  47. XYZprime.z = XYZ.z;
  48. vec3 LMS = XYZprime * mat3x3(0.41478972, 0.579999, 0.0146480, -0.2015100, 1.120649, 0.0531008, -0.0166008, 0.264800, 0.6684799);
  49. float c1 = 3424 / pow(2.0,12.0);
  50. float c2 = 2413 / pow(2.0,7.0);
  51. float c3 = 2392 / pow(2.0,7.0);
  52. float n = 2610 / pow(2.0,14.0);
  53. float p = 1.7 * 2523 / pow(2.0,5.0);
  54. vec3 LMSprime = pow((c1 + c2 * pow(LMS/10000,vec3(n)))/(1 + c3 * pow(LMS/10000,vec3(n))),vec3(p));
  55. vec3 Izazbz = LMSprime * mat3x3(0.5, 0.5, 0.0, 3.524000, -4.066708, 0.542708, 0.199076, 1.096799, -1.295875);
  56. float d = -0.56;
  57. float d0 = 1.6295499532821566 * pow(10.0,-11.0);
  58. vec3 Jzazbz = Izazbz;
  59. Jzazbz.x = ((1 + d) * Izazbz.x)/(1 + d * Izazbz.x) - d0;
  60. return Jzazbz;
  61. }
  62.  
  63. vec3 Jzazbz_to_XYZ(vec3 Jzazbz) {
  64. float d0 = 1.6295499532821566 * pow(10.0,-11.0);
  65. float d = -0.56;
  66. float Iz = (Jzazbz.x + d0) / (1 + d - d * (Jzazbz.x + d0));
  67. vec3 Izazbz = vec3(Iz,Jzazbz.y,Jzazbz.z);
  68. vec3 LMSprime = Izazbz * mat3x3(1.0, 0.138605043271539, 0.0580473161561189, 1.0, -0.138605043271539, -0.0580473161561189, 1.0, -0.0960192420263189, -0.811891896056039);
  69. float c1 = 3424 / pow(2.0,12.0);
  70. float c2 = 2413 / pow(2.0,7.0);
  71. float c3 = 2392 / pow(2.0,7.0);
  72. float n = 2610 / pow(2.0,14.0);
  73. float p = 1.7 * 2523 / pow(2.0,5.0);
  74. vec3 LMS = 10000 * pow((c1 - pow(LMSprime,vec3(1.0/p)))/(c3 * pow(LMSprime,vec3(1.0/p)) - c2),vec3(1.0/n));
  75. vec3 XYZprime = LMS * mat3x3(1.92422643578761, -1.00479231259537, 0.037651404030618, 0.350316762094999, 0.726481193931655, -0.065384422948085, -0.0909828109828476, -0.312728290523074, 1.52276656130526);
  76. float b = 1.15;
  77. float g = 0.66;
  78. vec3 XYZ = XYZprime;
  79. XYZ.x = (XYZprime.x + (b - 1.0) * XYZprime.z) / b;
  80. XYZ.y = (XYZprime.y + (g - 1.0) * XYZ.x) / g;
  81. XYZ.z = XYZprime.z;
  82. return XYZ;
  83. }
  84. //}
  85.  
  86. void main() {
  87. vec3 Picture = texture(source[0], texCoord).xyz;
  88. vec3 Backup = Picture;
  89.  
  90. Picture = sRGB_to_XYZ(Picture);
  91. Picture = XYZ_to_Jzazbz(Picture);
  92.  
  93. //Picture = Jzazbz_to_XYZ(Picture);
  94. //Picture = XYZ_to_sRGB(Picture);
  95.  
  96. Picture = clamp(Picture,0.0,1.0);
  97.  
  98. fragColor = vec4(Picture,1.0);
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement