Cycles: svn merge -r41225:41232 ^/trunk/blender
[blender.git] / intern / cycles / util / util_color.h
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #ifndef __UTIL_COLOR_H__
20 #define __UTIL_COLOR_H__
21
22 #include "util_math.h"
23 #include "util_types.h"
24
25 CCL_NAMESPACE_BEGIN
26
27 __device float color_srgb_to_scene_linear(float c)
28 {
29         if(c < 0.04045f)
30                 return (c < 0.0f)? 0.0f: c * (1.0f/12.92f);
31         else
32                 return pow((c + 0.055f)*(1.0f/1.055f), 2.4f);
33 }
34
35 __device float color_scene_linear_to_srgb(float c)
36 {
37         if(c < 0.0031308f)
38                 return (c < 0.0f)? 0.0f: c * 12.92f;
39     else
40                 return 1.055f * pow(c, 1.0f/2.4f) - 0.055f;
41 }
42
43 #ifndef __KERNEL_OPENCL__
44
45 __device float3 color_srgb_to_scene_linear(float3 c)
46 {
47         return make_float3(
48                 color_srgb_to_scene_linear(c.x),
49                 color_srgb_to_scene_linear(c.y),
50                 color_srgb_to_scene_linear(c.z));
51 }
52
53 __device float3 color_scene_linear_to_srgb(float3 c)
54 {
55         return make_float3(
56                 color_scene_linear_to_srgb(c.x),
57                 color_scene_linear_to_srgb(c.y),
58                 color_scene_linear_to_srgb(c.z));
59 }
60
61 #endif
62
63 __device float linear_rgb_to_gray(float3 c)
64 {
65         return c.x*0.2126f + c.y*0.7152f + c.z*0.0722f;
66 }
67
68 CCL_NAMESPACE_END
69
70 #endif /* __UTIL_COLOR_H__ */
71