1/*
2Copyright (c) 2012, Broadcom Europe Ltd
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the copyright holder nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#ifndef KHRN_INT_COLOR_H
29#define KHRN_INT_COLOR_H
30
31#include "interface/khronos/common/khrn_int_util.h"
32
33extern const uint8_t COLOR_S_TO_LIN[256];
34extern const uint16_t COLOR_S_TO_LIN16[256];
35extern const uint8_t COLOR_LIN_TO_S[256];
36extern const uint8_t COLOR_LIN16_TO_S[320];
37extern const uint32_t COLOR_RECIP[256];
38
39static INLINE uint32_t color_clamp_times_256(float x)
40{
41 return (uint32_t)(_minf(_maxf(x, 0.0f), 255.0f / 256.0f) * 256.0f);
42}
43
44//Note: this will not do the same as hardware if you put NaN in.
45static INLINE uint32_t color_floats_to_rgba(float r, float g, float b, float a)
46{
47 return
48 (color_clamp_times_256(r) << 0) |
49 (color_clamp_times_256(g) << 8) |
50 (color_clamp_times_256(b) << 16) |
51 (color_clamp_times_256(a) << 24);
52}
53
54static INLINE uint32_t color_floats_to_rgba_clean(const float *color)
55{
56 return color_floats_to_rgba(clean_float(color[0]), clean_float(color[1]), clean_float(color[2]), clean_float(color[3]));
57}
58
59static INLINE void khrn_color_rgba_to_floats(float *floats, uint32_t rgba)
60{
61 floats[0] = (float)((rgba >> 0) & 0xff) * (1.0f / 255.0f);
62 floats[1] = (float)((rgba >> 8) & 0xff) * (1.0f / 255.0f);
63 floats[2] = (float)((rgba >> 16) & 0xff) * (1.0f / 255.0f);
64 floats[3] = (float)((rgba >> 24) & 0xff) * (1.0f / 255.0f);
65}
66
67extern uint32_t khrn_color_rgba_pre(uint32_t rgba);
68extern uint32_t khrn_color_rgba_unpre(uint32_t rgba);
69extern uint32_t khrn_color_rgba_rz(uint32_t rgba);
70extern uint32_t khrn_color_rgba_clamp_to_a(uint32_t rgba);
71extern uint32_t khrn_color_rgba_s_to_lin(uint32_t rgba);
72extern uint32_t khrn_color_rgba_lin_to_s(uint32_t rgba);
73extern uint32_t khrn_color_rgba_to_la_lin(uint32_t rgba);
74extern uint32_t khrn_color_rgba_to_la_s(uint32_t rgba);
75
76static INLINE uint32_t khrn_color_rgba_flip(uint32_t rgba)
77{
78 return
79 (((rgba >> 0) & 0xff) << 24) |
80 (((rgba >> 8) & 0xff) << 16) |
81 (((rgba >> 16) & 0xff) << 8) |
82 (((rgba >> 24) & 0xff) << 0);
83}
84
85extern uint32_t khrn_color_rgba_add_dither(uint32_t rgba, int r, int g, int b, int a);
86
87extern uint32_t khrn_color_rgba_transform(uint32_t rgba, const float *color_transform);
88
89#endif
90