1. // Adrian Cruceru
    2. // Redshift Rendering Technologies 2020
    3. // This file is licensed under Apache 2.0 license
    4. // luma
    5. shader Luma
    6. [[ string help = "computes the relative luminance = luma of an input in various formats",
    7. string label = "Luma/Relative Luminance" ]]
    8. (
    9. // inputs
    10. color inColor = 0
    11. [[string label = "In Color"]],
    12. // various modes to compute the relative Luminance
    13. int mode = 0
    14. [[ string label = "Mode",
    15. string widget = "mapper",
    16. string options = "Rec. 709 (HD):0|Rec. 2020 (4k):1|Ccir 601 (SD):2|Average:3|Red:4|Green:5|Blue:6"]],
    17. float gamma = 2.2
    18. [[ string label = "Gamma",
    19. float min = 0, float max = 3 ]],
    20. int preGamma = 1
    21. [[ string widget = "checkBox",
    22. string label = "Pre Gamma" ]],
    23. int postGamma = 1
    24. [[ string widget = "checkBox" ,
    25. string label = "Post Gamma" ]],
    26. // outputs
    27. output color outColor = 1
    28. [[ string label = "Out Color" ]]
    29. )
    30. {
    31. // define various luma standards
    32. vector rec709 = {0.2126, 0.7152, 0.0722};
    33. vector rec2020 = {0.2627, 0.678, 0.0593};
    34. vector ccir601 = {0.299, 0.587, 0.114};
    35. float R = preGamma*pow(inColor[0],1/gamma)+(1-preGamma)*inColor[0];
    36. float G = preGamma*pow(inColor[1],1/gamma)+(1-preGamma)*inColor[1];
    37. float B = preGamma*pow(inColor[2],1/gamma)+(1-preGamma)*inColor[2];
    38. if (mode == 0)
    39. {
    40. // internally the luminance function is using REC709
    41. // outColor = luminance(inColor);
    42. color C = rec709[0]*R +rec709[1]*G+rec709[2]*B;
    43. color powC = pow(C,gamma);
    44. outColor = postGamma*powC+(1-postGamma)*C;
    45. }
    46. else if (mode == 1)
    47. {
    48. color C = rec2020[0]*R+rec2020[1]*G+rec2020[2]*B;
    49. color powC = pow(C,gamma);
    50. outColor = postGamma*powC+(1-postGamma)*C;
    51. }
    52. else if (mode == 2)
    53. {
    54. color C = ccir601[0]*R+ccir601[1]*G+ccir601[2]*B;
    55. color powC = pow(C,gamma);
    56. outColor = postGamma*powC+(1-postGamma)*C;
    57. }
    58. else if (mode == 3)
    59. {
    60. color C = (R+G+B)/3;
    61. color powC = pow(C,gamma);
    62. outColor = postGamma*powC+(1-postGamma)*C;
    63. }
    64. else if (mode == 4)
    65. {
    66. outColor = postGamma*pow(R,gamma)+(1-postGamma)*R;
    67. }
    68. else if (mode == 5)
    69. {
    70. outColor = postGamma*pow(G,gamma)+(1-postGamma)*G;
    71. }
    72. else if (mode == 6)
    73. {
    74. outColor = postGamma*pow(B,gamma)+(1-postGamma)*B;
    75. }
    76. }