/**************************************************************************1*2* Copyright 2010 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL16* THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,17* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR18* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE19* USE OR OTHER DEALINGS IN THE SOFTWARE.20*21* The above copyright notice and this permission notice (including the22* next paragraph) shall be included in all copies or substantial portions23* of the Software.24*25**************************************************************************/2627/**28* @file29* SRGB translation.30*31* @author Brian Paul <[email protected]>32* @author Michal Krol <[email protected]>33* @author Jose Fonseca <[email protected]>34*/3536#ifndef U_FORMAT_SRGB_H_37#define U_FORMAT_SRGB_H_3839#include <stdint.h>40#include <math.h>41#include "c99_compat.h"4243extern const float44util_format_srgb_8unorm_to_linear_float_table[256];4546extern const uint8_t47util_format_srgb_to_linear_8unorm_table[256];4849extern const uint8_t50util_format_linear_to_srgb_8unorm_table[256];5152extern const unsigned53util_format_linear_to_srgb_helper_table[104];545556static inline float57util_format_srgb_to_linear_float(float cs)58{59if (cs <= 0.0f)60return 0.0f;61else if (cs <= 0.04045f)62return cs / 12.92f;63else if (cs < 1.0f)64return powf((cs + 0.055) / 1.055f, 2.4f);65else66return 1.0f;67}686970static inline float71util_format_linear_to_srgb_float(float cl)72{73if (cl <= 0.0f)74return 0.0f;75else if (cl < 0.0031308f)76return 12.92f * cl;77else if (cl < 1.0f)78return 1.055f * powf(cl, 0.41666f) - 0.055f;79else80return 1.0f;81}828384/**85* Convert a unclamped linear float to srgb value in the [0,255].86*/87static inline uint8_t88util_format_linear_float_to_srgb_8unorm(float x)89{90/*91* This is taken from https://gist.github.com/rygorous/220383492* Use LUT and do linear interpolation.93*/94union {95uint32_t ui;96float f;97} almostone, minval, f;98unsigned tab, bias, scale, t;99100almostone.ui = 0x3f7fffff;101minval.ui = (127-13) << 23;102103/*104* Clamp to [2^(-13), 1-eps]; these two values map to 0 and 1, respectively.105* The tests are carefully written so that NaNs map to 0, same as in the106* reference implementation.107*/108if (!(x > minval.f))109x = minval.f;110if (x > almostone.f)111x = almostone.f;112113/* Do the table lookup and unpack bias, scale */114f.f = x;115tab = util_format_linear_to_srgb_helper_table[(f.ui - minval.ui) >> 20];116bias = (tab >> 16) << 9;117scale = tab & 0xffff;118119/* Grab next-highest mantissa bits and perform linear interpolation */120t = (f.ui >> 12) & 0xff;121return (uint8_t) ((bias + scale*t) >> 16);122}123124125/**126* Convert an 8-bit sRGB value from non-linear space to a127* linear RGB value in [0, 1].128* Implemented with a 256-entry lookup table.129*/130static inline float131util_format_srgb_8unorm_to_linear_float(uint8_t x)132{133return util_format_srgb_8unorm_to_linear_float_table[x];134}135136137/*138* XXX These 2 functions probably don't make a lot of sense (but lots139* of potential callers which most likely all don't make sense neither)140*/141142/**143* Convert a 8bit normalized value from linear to srgb.144*/145static inline uint8_t146util_format_linear_to_srgb_8unorm(uint8_t x)147{148return util_format_linear_to_srgb_8unorm_table[x];149}150151152/**153* Convert a 8bit normalized value from srgb to linear.154*/155static inline uint8_t156util_format_srgb_to_linear_8unorm(uint8_t x)157{158return util_format_srgb_to_linear_8unorm_table[x];159}160161162#endif /* U_FORMAT_SRGB_H_ */163164165