| 2013 | #ifndef STBI_NO_HDR |
| 2014 | #define stbi__float2int(x) ((int)(x)) |
| 2015 | static stbi_uc* stbi__hdr_to_ldr(float* data, int x, int y, int comp) { |
| 2016 | int i, k, n; |
| 2017 | stbi_uc* output; |
| 2018 | if (!data) |
| 2019 | return NULL; |
| 2020 | output = (stbi_uc*)stbi__malloc_mad3(x, y, comp, 0); |
| 2021 | if (output == NULL) { |
| 2022 | STBI_FREE(data); |
| 2023 | return stbi__errpuc("outofmem", "Out of memory"); |
| 2024 | } |
| 2025 | // compute number of non-alpha components |
| 2026 | if (comp & 1) |
| 2027 | n = comp; |
| 2028 | else |
| 2029 | n = comp - 1; |
| 2030 | for (i = 0; i < x * y; ++i) { |
| 2031 | for (k = 0; k < n; ++k) { |
| 2032 | float z = |
| 2033 | (float)pow( |
| 2034 | data[i * comp + k] * stbi__h2l_scale_i, stbi__h2l_gamma_i) * |
| 2035 | 255 + |
| 2036 | 0.5f; |
| 2037 | if (z < 0) |
| 2038 | z = 0; |
| 2039 | if (z > 255) |
| 2040 | z = 255; |
| 2041 | output[i * comp + k] = (stbi_uc)stbi__float2int(z); |
| 2042 | } |
| 2043 | if (k < comp) { |
| 2044 | float z = data[i * comp + k] * 255 + 0.5f; |
| 2045 | if (z < 0) |
| 2046 | z = 0; |
| 2047 | if (z > 255) |
| 2048 | z = 255; |
| 2049 | output[i * comp + k] = (stbi_uc)stbi__float2int(z); |
| 2050 | } |
| 2051 | } |
| 2052 | STBI_FREE(data); |
| 2053 | return output; |
| 2054 | } |
| 2055 | #endif |
| 2056 | |
| 2057 | ////////////////////////////////////////////////////////////////////////////// |
no test coverage detected