srgb.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #pragma once
  15. #include <math.h>
  16. #include <string.h>
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. static inline float gs_srgb_nonlinear_to_linear(float u)
  21. {
  22. return (u <= 0.04045f) ? (u / 12.92f) : powf((u + 0.055f) / 1.055f, 2.4f);
  23. }
  24. static inline float gs_srgb_linear_to_nonlinear(float u)
  25. {
  26. return (u <= 0.0031308f) ? (12.92f * u) : ((1.055f * powf(u, 1.0f / 2.4f)) - 0.055f);
  27. }
  28. static inline float gs_u8_to_float(uint8_t u)
  29. {
  30. return (float)u / 255.0f;
  31. }
  32. static inline void gs_u8x4_to_float4(float *f, const uint8_t *u)
  33. {
  34. f[0] = gs_u8_to_float(u[0]);
  35. f[1] = gs_u8_to_float(u[1]);
  36. f[2] = gs_u8_to_float(u[2]);
  37. f[3] = gs_u8_to_float(u[3]);
  38. }
  39. static inline uint8_t gs_float_to_u8(float f)
  40. {
  41. return (uint8_t)(f * 255.0f + 0.5f);
  42. }
  43. static inline void gs_premultiply_float4(float *f)
  44. {
  45. f[0] *= f[3];
  46. f[1] *= f[3];
  47. f[2] *= f[3];
  48. }
  49. static inline void gs_float3_to_u8x3(uint8_t *u, const float *f)
  50. {
  51. u[0] = gs_float_to_u8(f[0]);
  52. u[1] = gs_float_to_u8(f[1]);
  53. u[2] = gs_float_to_u8(f[2]);
  54. }
  55. static inline void gs_float4_to_u8x4(uint8_t *u, const float *f)
  56. {
  57. u[0] = gs_float_to_u8(f[0]);
  58. u[1] = gs_float_to_u8(f[1]);
  59. u[2] = gs_float_to_u8(f[2]);
  60. u[3] = gs_float_to_u8(f[3]);
  61. }
  62. static inline void gs_float3_srgb_nonlinear_to_linear(float *f)
  63. {
  64. f[0] = gs_srgb_nonlinear_to_linear(f[0]);
  65. f[1] = gs_srgb_nonlinear_to_linear(f[1]);
  66. f[2] = gs_srgb_nonlinear_to_linear(f[2]);
  67. }
  68. static inline void gs_float3_srgb_linear_to_nonlinear(float *f)
  69. {
  70. f[0] = gs_srgb_linear_to_nonlinear(f[0]);
  71. f[1] = gs_srgb_linear_to_nonlinear(f[1]);
  72. f[2] = gs_srgb_linear_to_nonlinear(f[2]);
  73. }
  74. static inline void gs_premultiply_xyza(uint8_t *data)
  75. {
  76. uint8_t u[4];
  77. float f[4];
  78. memcpy(&u, data, sizeof(u));
  79. gs_u8x4_to_float4(f, u);
  80. gs_premultiply_float4(f);
  81. gs_float3_to_u8x3(u, f);
  82. memcpy(data, &u, sizeof(u));
  83. }
  84. static inline void gs_premultiply_xyza_srgb(uint8_t *data)
  85. {
  86. uint8_t u[4];
  87. float f[4];
  88. memcpy(&u, data, sizeof(u));
  89. gs_u8x4_to_float4(f, u);
  90. gs_float3_srgb_nonlinear_to_linear(f);
  91. gs_premultiply_float4(f);
  92. gs_float3_srgb_linear_to_nonlinear(f);
  93. gs_float3_to_u8x3(u, f);
  94. memcpy(data, &u, sizeof(u));
  95. }
  96. static inline void gs_premultiply_xyza_restrict(uint8_t *__restrict dst, const uint8_t *__restrict src)
  97. {
  98. uint8_t u[4];
  99. float f[4];
  100. memcpy(&u, src, sizeof(u));
  101. gs_u8x4_to_float4(f, u);
  102. gs_premultiply_float4(f);
  103. gs_float3_to_u8x3(u, f);
  104. memcpy(dst, &u, sizeof(u));
  105. }
  106. static inline void gs_premultiply_xyza_srgb_restrict(uint8_t *__restrict dst, const uint8_t *__restrict src)
  107. {
  108. uint8_t u[4];
  109. float f[4];
  110. memcpy(&u, src, sizeof(u));
  111. gs_u8x4_to_float4(f, u);
  112. gs_float3_srgb_nonlinear_to_linear(f);
  113. gs_premultiply_float4(f);
  114. gs_float3_srgb_linear_to_nonlinear(f);
  115. gs_float3_to_u8x3(u, f);
  116. memcpy(dst, &u, sizeof(u));
  117. }
  118. static inline void gs_premultiply_xyza_loop(uint8_t *data, size_t texel_count)
  119. {
  120. for (size_t i = 0; i < texel_count; ++i) {
  121. gs_premultiply_xyza(data);
  122. data += 4;
  123. }
  124. }
  125. static inline void gs_premultiply_xyza_srgb_loop(uint8_t *data, size_t texel_count)
  126. {
  127. for (size_t i = 0; i < texel_count; ++i) {
  128. gs_premultiply_xyza_srgb(data);
  129. data += 4;
  130. }
  131. }
  132. static inline void gs_premultiply_xyza_loop_restrict(uint8_t *__restrict dst, const uint8_t *__restrict src,
  133. size_t texel_count)
  134. {
  135. for (size_t i = 0; i < texel_count; ++i) {
  136. gs_premultiply_xyza_restrict(dst, src);
  137. dst += 4;
  138. src += 4;
  139. }
  140. }
  141. static inline void gs_premultiply_xyza_srgb_loop_restrict(uint8_t *__restrict dst, const uint8_t *__restrict src,
  142. size_t texel_count)
  143. {
  144. for (size_t i = 0; i < texel_count; ++i) {
  145. gs_premultiply_xyza_srgb_restrict(dst, src);
  146. dst += 4;
  147. src += 4;
  148. }
  149. }
  150. #ifdef __cplusplus
  151. }
  152. #endif