numbafun.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. # uHDR: HDR image editing software
  2. # Copyright (C) 2021 remi cozot
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # hdrCore project 2020
  15. # author: remi.cozot@univ-littoral.fr
  16. # -----------------------------------------------------------------------------
  17. # --- Package hdrCore ---------------------------------------------------------
  18. # -----------------------------------------------------------------------------
  19. """
  20. package hdrCore consists of the core classes for HDR imaging.
  21. """
  22. # -----------------------------------------------------------------------------
  23. # --- Import ------------------------------------------------------------------
  24. # -----------------------------------------------------------------------------
  25. import numba
  26. import numpy as np
  27. # -----------------------------------------------------------------------------
  28. # --- Functions: numba version ------------------------------------------------
  29. # -----------------------------------------------------------------------------
  30. @numba.jit(cache=True, parallel=True)
  31. def numba_cctf_sRGB_encoding(L):
  32. """cctf SRGB encoding (numba acceleration)
  33. Args:
  34. L (float or numpy.ndarray, Required)
  35. Returns:
  36. (float)
  37. """
  38. v = np.where(L <= 0.0031308, L * 12.92, 1.055 * np.power(L, 1 / 2.4) - 0.055)
  39. return v
  40. # -----------------------------------------------------------------------------
  41. @numba.jit(cache=True, parallel=True)
  42. def numba_cctf_sRGB_decoding(V):
  43. """cctf SRGB decoding (numba acceleration)
  44. Args:
  45. V (float or numpy.ndarray, Required)
  46. Returns:
  47. (float)
  48. """
  49. L = np.where(V <= numba_cctf_sRGB_encoding(0.0031308), V / 12.92, np.power((V + 0.055) / 1.055, 2.4))
  50. return L
  51. # -----------------------------------------------------------------------------
  52. # --- Functions: cuda version ------------------------------------------------
  53. # -----------------------------------------------------------------------------
  54. @numba.vectorize('float32(float32)', target='cuda' )
  55. def cuda_cctf_sRGB_decoding(V):
  56. """cctf sRGB decoding (cuda acceleration)
  57. Args:
  58. V (float or numpy.ndarray, Required)
  59. Returns:
  60. (float)
  61. """
  62. if V <= 0.040449935999999999:
  63. L = V / 12.92
  64. else:
  65. L = ((V + 0.055) / 1.055)**( 2.4)
  66. return L
  67. # -----------------------------------------------------------------------------
  68. @numba.vectorize('float32(float32)', target='cuda' )
  69. def cuda_cctf_sRGB_encoding(L):
  70. """cctf SRGB encoding (cuda acceleration)
  71. Args:
  72. L (float or numpy.ndarray, Required)
  73. Returns:
  74. (float)
  75. """
  76. if L <= 0.0031308:
  77. v = L * 12.92
  78. else:
  79. v = 1.055 * (L**(1 / 2.4)) - 0.055
  80. return v
  81. # -----------------------------------------------------------------------------
  82. # -----------------------------------------------------------------------------
  83. def numba_sRGB_to_XYZ(sRGB, cctf_decoding=None):
  84. pass
  85. # -----------------------------------------------------------------------------
  86. # -----------------------------------------------------------------------------
  87. #CAT_CAT02 = np.array([
  88. # [0.7328, 0.4296, -0.1624],
  89. # [-0.7036, 1.6975, 0.0061],
  90. # [0.0030, 0.0136, 0.9834],
  91. #])
  92. ## -----------------------------------------------------------------------------
  93. #def RGB_to_XYZ(RGB, illuminant_RGB, illuminant_XYZ, RGB_to_XYZ_matrix, chromatic_adaptation_transform='CAT02', cctf_decoding=None,**kwargs):
  94. # cctf_decoding = handle_arguments_deprecation({
  95. # 'ArgumentRenamed': [['decoding_cctf', 'cctf_decoding']],
  96. # }, **kwargs).get('cctf_decoding', cctf_decoding)
  97. # RGB = to_domain_1(RGB)
  98. # if cctf_decoding is not None:
  99. # with domain_range_scale('ignore'):
  100. # RGB = cctf_decoding(RGB)
  101. # XYZ = dot_vector(RGB_to_XYZ_matrix, RGB)
  102. # if chromatic_adaptation_transform is not None:
  103. # M_CAT = chromatic_adaptation_matrix_VonKries(
  104. # xyY_to_XYZ(xy_to_xyY(illuminant_RGB)),
  105. # xyY_to_XYZ(xy_to_xyY(illuminant_XYZ)),
  106. # transform=chromatic_adaptation_transform)
  107. # XYZ = dot_vector(M_CAT, XYZ)
  108. # return from_range_1(XYZ)
  109. ## -----------------------------------------------------------------------------
  110. #def RGB_to_RGB_matrix(input_colourspace,output_colourspace, chromatic_adaptation_transform='CAT02'):
  111. # M = input_colourspace.RGB_to_XYZ_matrix
  112. # if chromatic_adaptation_transform is not None:
  113. # M_CAT = chromatic_adaptation_matrix_VonKries(
  114. # xy_to_XYZ(input_colourspace.whitepoint),
  115. # xy_to_XYZ(output_colourspace.whitepoint),
  116. # chromatic_adaptation_transform)
  117. # M = dot_matrix(M_CAT, input_colourspace.RGB_to_XYZ_matrix)
  118. # M = dot_matrix(output_colourspace.XYZ_to_RGB_matrix, M)
  119. # return M
  120. ## -----------------------------------------------------------------------------
  121. #def intermediate_luminance_function_CIE1976(f_Y_Y_n, Y_n=100):
  122. # f_Y_Y_n = as_float_array(f_Y_Y_n)
  123. # Y_n = as_float_array(Y_n)
  124. # Y = as_float(
  125. # np.where(
  126. # f_Y_Y_n > 24 / 116,
  127. # Y_n * f_Y_Y_n ** 3,
  128. # Y_n * (f_Y_Y_n - 16 / 116) * (108 / 841),
  129. # ))
  130. # return Y
  131. ## -----------------------------------------------------------------------------
  132. #def Lab_to_XYZ(Lab, illuminant=CCS_ILLUMINANTS['CIE 1931 2 Degree Standard Observer']['D65']):
  133. # L, a, b = tsplit(to_domain_100(Lab))
  134. # X_n, Y_n, Z_n = tsplit(xyY_to_XYZ(xy_to_xyY(illuminant)))
  135. # f_Y_Y_n = (L + 16) / 116
  136. # f_X_X_n = a / 500 + f_Y_Y_n
  137. # f_Z_Z_n = f_Y_Y_n - b / 200
  138. # X = intermediate_luminance_function_CIE1976(f_X_X_n, X_n)
  139. # Y = intermediate_luminance_function_CIE1976(f_Y_Y_n, Y_n)
  140. # Z = intermediate_luminance_function_CIE1976(f_Z_Z_n, Z_n)
  141. # XYZ = tstack([X, Y, Z])
  142. # return from_range_1(XYZ)
  143. ## -----------------------------------------------------------------------------
  144. #def XYZ_to_Lab(XYZ, illuminant=CCS_ILLUMINANTS['CIE 1931 2 Degree Standard Observer']['D65']):
  145. # X, Y, Z = tsplit(to_domain_1(XYZ))
  146. # X_n, Y_n, Z_n = tsplit(xyY_to_XYZ(xy_to_xyY(illuminant)))
  147. # f_X_X_n = intermediate_lightness_function_CIE1976(X, X_n)
  148. # f_Y_Y_n = intermediate_lightness_function_CIE1976(Y, Y_n)
  149. # f_Z_Z_n = intermediate_lightness_function_CIE1976(Z, Z_n)
  150. # L = 116 * f_Y_Y_n - 16
  151. # a = 500 * (f_X_X_n - f_Y_Y_n)
  152. # b = 200 * (f_Y_Y_n - f_Z_Z_n)
  153. # Lab = tstack([L, a, b])
  154. # return from_range_100(Lab)
  155. ## -----------------------------------------------------------------------------