1
0

srgb.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /******************************************************************************
  2. Copyright (C) 2021 by Hugh 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)
  23. : powf((u + 0.055f) / 1.055f, 2.4f);
  24. }
  25. static inline float gs_srgb_linear_to_nonlinear(float u)
  26. {
  27. return (u <= 0.0031308f) ? (12.92f * u)
  28. : ((1.055f * powf(u, 1.0f / 2.4f)) - 0.055f);
  29. }
  30. static inline float gs_u8_to_float(uint8_t u)
  31. {
  32. return (float)u / 255.0f;
  33. }
  34. static inline void gs_u8x4_to_float4(float *f, const uint8_t *u)
  35. {
  36. f[0] = gs_u8_to_float(u[0]);
  37. f[1] = gs_u8_to_float(u[1]);
  38. f[2] = gs_u8_to_float(u[2]);
  39. f[3] = gs_u8_to_float(u[3]);
  40. }
  41. static inline uint8_t gs_float_to_u8(float f)
  42. {
  43. return (uint8_t)(f * 255.0f + 0.5f);
  44. }
  45. static inline void gs_premultiply_float4(float *f)
  46. {
  47. f[0] *= f[3];
  48. f[1] *= f[3];
  49. f[2] *= f[3];
  50. }
  51. static inline void gs_float3_to_u8x3(uint8_t *u, const float *f)
  52. {
  53. u[0] = gs_float_to_u8(f[0]);
  54. u[1] = gs_float_to_u8(f[1]);
  55. u[2] = gs_float_to_u8(f[2]);
  56. }
  57. static inline void gs_float4_to_u8x4(uint8_t *u, const float *f)
  58. {
  59. u[0] = gs_float_to_u8(f[0]);
  60. u[1] = gs_float_to_u8(f[1]);
  61. u[2] = gs_float_to_u8(f[2]);
  62. u[3] = gs_float_to_u8(f[3]);
  63. }
  64. static inline void gs_float3_srgb_nonlinear_to_linear(float *f)
  65. {
  66. f[0] = gs_srgb_nonlinear_to_linear(f[0]);
  67. f[1] = gs_srgb_nonlinear_to_linear(f[1]);
  68. f[2] = gs_srgb_nonlinear_to_linear(f[2]);
  69. }
  70. static inline void gs_float3_srgb_linear_to_nonlinear(float *f)
  71. {
  72. f[0] = gs_srgb_linear_to_nonlinear(f[0]);
  73. f[1] = gs_srgb_linear_to_nonlinear(f[1]);
  74. f[2] = gs_srgb_linear_to_nonlinear(f[2]);
  75. }
  76. static inline void gs_premultiply_xyza(uint8_t *data)
  77. {
  78. uint8_t u[4];
  79. float f[4];
  80. memcpy(&u, data, sizeof(u));
  81. gs_u8x4_to_float4(f, u);
  82. gs_premultiply_float4(f);
  83. gs_float3_to_u8x3(u, f);
  84. memcpy(data, &u, sizeof(u));
  85. }
  86. static inline void gs_premultiply_xyza_srgb(uint8_t *data)
  87. {
  88. uint8_t u[4];
  89. float f[4];
  90. memcpy(&u, data, sizeof(u));
  91. gs_u8x4_to_float4(f, u);
  92. gs_float3_srgb_nonlinear_to_linear(f);
  93. gs_premultiply_float4(f);
  94. gs_float3_srgb_linear_to_nonlinear(f);
  95. gs_float3_to_u8x3(u, f);
  96. memcpy(data, &u, sizeof(u));
  97. }
  98. static inline void gs_premultiply_xyza_restrict(uint8_t *__restrict dst,
  99. const uint8_t *__restrict src)
  100. {
  101. uint8_t u[4];
  102. float f[4];
  103. memcpy(&u, src, sizeof(u));
  104. gs_u8x4_to_float4(f, u);
  105. gs_premultiply_float4(f);
  106. gs_float3_to_u8x3(u, f);
  107. memcpy(dst, &u, sizeof(u));
  108. }
  109. static inline void
  110. gs_premultiply_xyza_srgb_restrict(uint8_t *__restrict dst,
  111. const uint8_t *__restrict src)
  112. {
  113. uint8_t u[4];
  114. float f[4];
  115. memcpy(&u, src, sizeof(u));
  116. gs_u8x4_to_float4(f, u);
  117. gs_float3_srgb_nonlinear_to_linear(f);
  118. gs_premultiply_float4(f);
  119. gs_float3_srgb_linear_to_nonlinear(f);
  120. gs_float3_to_u8x3(u, f);
  121. memcpy(dst, &u, sizeof(u));
  122. }
  123. static inline void gs_premultiply_xyza_loop(uint8_t *data, size_t texel_count)
  124. {
  125. for (size_t i = 0; i < texel_count; ++i) {
  126. gs_premultiply_xyza(data);
  127. data += 4;
  128. }
  129. }
  130. static inline void gs_premultiply_xyza_srgb_loop(uint8_t *data,
  131. size_t texel_count)
  132. {
  133. for (size_t i = 0; i < texel_count; ++i) {
  134. gs_premultiply_xyza_srgb(data);
  135. data += 4;
  136. }
  137. }
  138. static inline void
  139. gs_premultiply_xyza_loop_restrict(uint8_t *__restrict dst,
  140. const uint8_t *__restrict src,
  141. size_t texel_count)
  142. {
  143. for (size_t i = 0; i < texel_count; ++i) {
  144. gs_premultiply_xyza_restrict(dst, src);
  145. dst += 4;
  146. src += 4;
  147. }
  148. }
  149. static inline void
  150. gs_premultiply_xyza_srgb_loop_restrict(uint8_t *__restrict dst,
  151. const uint8_t *__restrict src,
  152. size_t texel_count)
  153. {
  154. for (size_t i = 0; i < texel_count; ++i) {
  155. gs_premultiply_xyza_srgb_restrict(dst, src);
  156. dst += 4;
  157. src += 4;
  158. }
  159. }
  160. #ifdef __cplusplus
  161. }
  162. #endif