1/************************************************************************** 2 * 3 * Copyright 2010 VMware, Inc. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 * USE OR OTHER DEALINGS IN THE SOFTWARE. 21 * 22 * The above copyright notice and this permission notice (including the 23 * next paragraph) shall be included in all copies or substantial portions 24 * of the Software. 25 * 26 **************************************************************************/ 27 28/** 29 * @file 30 * SRGB translation. 31 * 32 * @author Brian Paul <brianp@vmware.com> 33 * @author Michal Krol <michal@vmware.com> 34 * @author Jose Fonseca <jfonseca@vmware.com> 35 */ 36 37#ifndef U_FORMAT_SRGB_H_ 38#define U_FORMAT_SRGB_H_ 39 40#include <stdint.h> 41#include <math.h> 42#include "c99_compat.h" 43 44extern const float 45util_format_srgb_8unorm_to_linear_float_table[256]; 46 47extern const uint8_t 48util_format_srgb_to_linear_8unorm_table[256]; 49 50extern const uint8_t 51util_format_linear_to_srgb_8unorm_table[256]; 52 53extern const unsigned 54util_format_linear_to_srgb_helper_table[104]; 55 56 57static inline float 58util_format_linear_to_srgb_float(float cl) 59{ 60 if (cl <= 0.0f) 61 return 0.0f; 62 else if (cl < 0.0031308f) 63 return 12.92f * cl; 64 else if (cl < 1.0f) 65 return 1.055f * powf(cl, 0.41666f) - 0.055f; 66 else 67 return 1.0f; 68} 69 70 71/** 72 * Convert a unclamped linear float to srgb value in the [0,255]. 73 */ 74static inline uint8_t 75util_format_linear_float_to_srgb_8unorm(float x) 76{ 77 /* 78 * This is taken from https://gist.github.com/rygorous/2203834 79 * Use LUT and do linear interpolation. 80 */ 81 union { 82 uint32_t ui; 83 float f; 84 } almostone, minval, f; 85 unsigned tab, bias, scale, t; 86 87 almostone.ui = 0x3f7fffff; 88 minval.ui = (127-13) << 23; 89 90 /* 91 * Clamp to [2^(-13), 1-eps]; these two values map to 0 and 1, respectively. 92 * The tests are carefully written so that NaNs map to 0, same as in the 93 * reference implementation. 94 */ 95 if (!(x > minval.f)) 96 x = minval.f; 97 if (x > almostone.f) 98 x = almostone.f; 99 100 /* Do the table lookup and unpack bias, scale */ 101 f.f = x; 102 tab = util_format_linear_to_srgb_helper_table[(f.ui - minval.ui) >> 20]; 103 bias = (tab >> 16) << 9; 104 scale = tab & 0xffff; 105 106 /* Grab next-highest mantissa bits and perform linear interpolation */ 107 t = (f.ui >> 12) & 0xff; 108 return (uint8_t) ((bias + scale*t) >> 16); 109} 110 111 112/** 113 * Convert an 8-bit sRGB value from non-linear space to a 114 * linear RGB value in [0, 1]. 115 * Implemented with a 256-entry lookup table. 116 */ 117static inline float 118util_format_srgb_8unorm_to_linear_float(uint8_t x) 119{ 120 return util_format_srgb_8unorm_to_linear_float_table[x]; 121} 122 123 124/* 125 * XXX These 2 functions probably don't make a lot of sense (but lots 126 * of potential callers which most likely all don't make sense neither) 127 */ 128 129/** 130 * Convert a 8bit normalized value from linear to srgb. 131 */ 132static inline uint8_t 133util_format_linear_to_srgb_8unorm(uint8_t x) 134{ 135 return util_format_linear_to_srgb_8unorm_table[x]; 136} 137 138 139/** 140 * Convert a 8bit normalized value from srgb to linear. 141 */ 142static inline uint8_t 143util_format_srgb_to_linear_8unorm(uint8_t x) 144{ 145 return util_format_srgb_to_linear_8unorm_table[x]; 146} 147 148 149#endif /* U_FORMAT_SRGB_H_ */ 150