1848b8605Smrg/**************************************************************************
2848b8605Smrg *
3848b8605Smrg * Copyright 2010 VMware, Inc.
4848b8605Smrg * All Rights Reserved.
5848b8605Smrg *
6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7848b8605Smrg * copy of this software and associated documentation files (the
8848b8605Smrg * "Software"), to deal in the Software without restriction, including
9848b8605Smrg * without limitation the rights to use, copy, modify, merge, publish,
10848b8605Smrg * distribute, sub license, and/or sell copies of the Software, and to
11848b8605Smrg * permit persons to whom the Software is furnished to do so, subject to
12848b8605Smrg * the following conditions:
13848b8605Smrg *
14848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15848b8605Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17848b8605Smrg * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18848b8605Smrg * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19848b8605Smrg * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20848b8605Smrg * USE OR OTHER DEALINGS IN THE SOFTWARE.
21848b8605Smrg *
22848b8605Smrg * The above copyright notice and this permission notice (including the
23848b8605Smrg * next paragraph) shall be included in all copies or substantial portions
24848b8605Smrg * of the Software.
25848b8605Smrg *
26848b8605Smrg **************************************************************************/
27848b8605Smrg
28848b8605Smrg/**
29848b8605Smrg * @file
30848b8605Smrg * SRGB translation.
31848b8605Smrg *
32848b8605Smrg * @author Brian Paul <brianp@vmware.com>
33848b8605Smrg * @author Michal Krol <michal@vmware.com>
34848b8605Smrg * @author Jose Fonseca <jfonseca@vmware.com>
35848b8605Smrg */
36848b8605Smrg
37848b8605Smrg#ifndef U_FORMAT_SRGB_H_
38848b8605Smrg#define U_FORMAT_SRGB_H_
39848b8605Smrg
40848b8605Smrg#include <stdint.h>
41848b8605Smrg#include <math.h>
42848b8605Smrg#include "c99_compat.h"
43848b8605Smrg
44848b8605Smrgextern const float
45848b8605Smrgutil_format_srgb_8unorm_to_linear_float_table[256];
46848b8605Smrg
47848b8605Smrgextern const uint8_t
48848b8605Smrgutil_format_srgb_to_linear_8unorm_table[256];
49848b8605Smrg
50848b8605Smrgextern const uint8_t
51848b8605Smrgutil_format_linear_to_srgb_8unorm_table[256];
52848b8605Smrg
53848b8605Smrgextern const unsigned
54848b8605Smrgutil_format_linear_to_srgb_helper_table[104];
55848b8605Smrg
56848b8605Smrg
57b8e80941Smrgstatic inline float
58b8e80941Smrgutil_format_srgb_to_linear_float(float cs)
59b8e80941Smrg{
60b8e80941Smrg   if (cs <= 0.0f)
61b8e80941Smrg      return 0.0f;
62b8e80941Smrg   else if (cs <= 0.04045f)
63b8e80941Smrg      return cs / 12.92f;
64b8e80941Smrg   else if (cs < 1.0f)
65b8e80941Smrg      return powf((cs + 0.055) / 1.055f, 2.4f);
66b8e80941Smrg   else
67b8e80941Smrg      return 1.0f;
68b8e80941Smrg}
69b8e80941Smrg
70b8e80941Smrg
71848b8605Smrgstatic inline float
72848b8605Smrgutil_format_linear_to_srgb_float(float cl)
73848b8605Smrg{
74b8e80941Smrg   if (cl <= 0.0f)
75848b8605Smrg      return 0.0f;
76848b8605Smrg   else if (cl < 0.0031308f)
77848b8605Smrg      return 12.92f * cl;
78848b8605Smrg   else if (cl < 1.0f)
79848b8605Smrg      return 1.055f * powf(cl, 0.41666f) - 0.055f;
80848b8605Smrg   else
81848b8605Smrg      return 1.0f;
82848b8605Smrg}
83848b8605Smrg
84848b8605Smrg
85848b8605Smrg/**
86848b8605Smrg * Convert a unclamped linear float to srgb value in the [0,255].
87848b8605Smrg */
88848b8605Smrgstatic inline uint8_t
89848b8605Smrgutil_format_linear_float_to_srgb_8unorm(float x)
90848b8605Smrg{
91848b8605Smrg   /*
92848b8605Smrg    * This is taken from https://gist.github.com/rygorous/2203834
93848b8605Smrg    * Use LUT and do linear interpolation.
94848b8605Smrg    */
95848b8605Smrg   union {
96848b8605Smrg      uint32_t ui;
97848b8605Smrg      float f;
98848b8605Smrg   } almostone, minval, f;
99848b8605Smrg   unsigned tab, bias, scale, t;
100848b8605Smrg
101848b8605Smrg   almostone.ui = 0x3f7fffff;
102848b8605Smrg   minval.ui = (127-13) << 23;
103848b8605Smrg
104848b8605Smrg   /*
105848b8605Smrg    * Clamp to [2^(-13), 1-eps]; these two values map to 0 and 1, respectively.
106848b8605Smrg    * The tests are carefully written so that NaNs map to 0, same as in the
107848b8605Smrg    * reference implementation.
108848b8605Smrg    */
109848b8605Smrg   if (!(x > minval.f))
110848b8605Smrg      x = minval.f;
111848b8605Smrg   if (x > almostone.f)
112848b8605Smrg      x = almostone.f;
113848b8605Smrg
114848b8605Smrg   /* Do the table lookup and unpack bias, scale */
115848b8605Smrg   f.f = x;
116848b8605Smrg   tab = util_format_linear_to_srgb_helper_table[(f.ui - minval.ui) >> 20];
117848b8605Smrg   bias = (tab >> 16) << 9;
118848b8605Smrg   scale = tab & 0xffff;
119848b8605Smrg
120848b8605Smrg   /* Grab next-highest mantissa bits and perform linear interpolation */
121848b8605Smrg   t = (f.ui >> 12) & 0xff;
122848b8605Smrg   return (uint8_t) ((bias + scale*t) >> 16);
123848b8605Smrg}
124848b8605Smrg
125848b8605Smrg
126848b8605Smrg/**
127848b8605Smrg * Convert an 8-bit sRGB value from non-linear space to a
128848b8605Smrg * linear RGB value in [0, 1].
129848b8605Smrg * Implemented with a 256-entry lookup table.
130848b8605Smrg */
131848b8605Smrgstatic inline float
132848b8605Smrgutil_format_srgb_8unorm_to_linear_float(uint8_t x)
133848b8605Smrg{
134848b8605Smrg   return util_format_srgb_8unorm_to_linear_float_table[x];
135848b8605Smrg}
136848b8605Smrg
137848b8605Smrg
138848b8605Smrg/*
139848b8605Smrg * XXX These 2 functions probably don't make a lot of sense (but lots
140848b8605Smrg * of potential callers which most likely all don't make sense neither)
141848b8605Smrg */
142848b8605Smrg
143848b8605Smrg/**
144848b8605Smrg * Convert a 8bit normalized value from linear to srgb.
145848b8605Smrg */
146848b8605Smrgstatic inline uint8_t
147848b8605Smrgutil_format_linear_to_srgb_8unorm(uint8_t x)
148848b8605Smrg{
149848b8605Smrg   return util_format_linear_to_srgb_8unorm_table[x];
150848b8605Smrg}
151848b8605Smrg
152848b8605Smrg
153848b8605Smrg/**
154848b8605Smrg * Convert a 8bit normalized value from srgb to linear.
155848b8605Smrg */
156848b8605Smrgstatic inline uint8_t
157848b8605Smrgutil_format_srgb_to_linear_8unorm(uint8_t x)
158848b8605Smrg{
159848b8605Smrg   return util_format_srgb_to_linear_8unorm_table[x];
160848b8605Smrg}
161848b8605Smrg
162848b8605Smrg
163848b8605Smrg#endif /* U_FORMAT_SRGB_H_ */
164