ab822f1cd55a38d9aa0c34fa090ce1628e97492d
[blender.git] / source / blender / imbuf / IMB_colormanagement.h
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
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  * The Original Code is Copyright (C) 2012 by Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Xavier Thomas,
24  *                 Lukas Toenne,
25  *                 Sergey Sharybin
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  *
29  */
30
31 #ifndef __IMB_COLORMANAGEMENT_H__
32 #define __IMB_COLORMANAGEMENT_H__
33
34 /** \file blender/imbuf/IMB_colormanagement.h
35  *  \ingroup imbuf
36  */
37
38 #include "BLI_sys_types.h"
39 #include "BLI_compiler_compat.h"
40
41 #define BCM_CONFIG_FILE "config.ocio"
42
43
44 struct bContext;
45 struct ColorManagedColorspaceSettings;
46 struct ColorManagedDisplaySettings;
47 struct ColorManagedViewSettings;
48 struct ColormanageProcessor;
49 struct EnumPropertyItem;
50 struct ImBuf;
51 struct Main;
52 struct ImageFormatData;
53
54 struct ColorSpace;
55 struct ColorManagedDisplay;
56
57 /* ** Generic functions ** */
58
59 void IMB_colormanagement_check_file_config(struct Main *bmain);
60
61 void IMB_colormanagement_validate_settings(struct ColorManagedDisplaySettings *display_settings,
62                                            struct ColorManagedViewSettings *view_settings);
63
64 const char *IMB_colormanagement_role_colorspace_name_get(int role);
65 void IMB_colormanagement_check_is_data(struct ImBuf *ibuf, const char *name);
66 void IMB_colormanagement_assign_float_colorspace(struct ImBuf *ibuf, const char *name);
67 void IMB_colormanagement_assign_rect_colorspace(struct ImBuf *ibuf, const char *name);
68
69 const char *IMB_colormanagement_get_float_colorspace(struct ImBuf *ibuf);
70 const char *IMB_colormanagement_get_rect_colorspace(struct ImBuf *ibuf);
71
72 BLI_INLINE float IMB_colormanagement_get_luminance(const float rgb[3]);
73 BLI_INLINE unsigned char IMB_colormanagement_get_luminance_byte(const unsigned char[3]);
74
75 /* ** Color space transformation functions ** */
76 void IMB_colormanagement_transform(float *buffer, int width, int height, int channels,
77                                    const char *from_colorspace, const char *to_colorspace, bool predivide);
78 void IMB_colormanagement_transform_threaded(float *buffer, int width, int height, int channels,
79                                             const char *from_colorspace, const char *to_colorspace, bool predivide);
80 void IMB_colormanagement_transform_v4(float pixel[4], const char *from_colorspace, const char *to_colorspace);
81
82 void IMB_colormanagement_colorspace_to_scene_linear_v3(float pixel[3], struct ColorSpace *colorspace);
83 void IMB_colormanagement_colorspace_to_scene_linear_v4(float pixel[4], bool predivide, struct ColorSpace *colorspace);
84
85 void IMB_colormanagement_scene_linear_to_colorspace_v3(float pixel[3], struct ColorSpace *colorspace);
86
87 void IMB_colormanagement_colorspace_to_scene_linear(float *buffer, int width, int height, int channels, struct ColorSpace *colorspace, bool predivide);
88
89 void IMB_colormanagement_scene_linear_to_display_v3(float pixel[3], struct ColorManagedDisplay *display);
90 void IMB_colormanagement_display_to_scene_linear_v3(float pixel[3], struct ColorManagedDisplay *display);
91
92 void IMB_colormanagement_pixel_to_display_space_v4(float result[4], const float pixel[4],  const struct ColorManagedViewSettings *view_settings,
93                                                    const struct ColorManagedDisplaySettings *display_settings);
94
95 void IMB_colormanagement_pixel_to_display_space_v3(float result[3], const float pixel[3],  const struct ColorManagedViewSettings *view_settings,
96                                                    const struct ColorManagedDisplaySettings *display_settings);
97
98 void IMB_colormanagement_imbuf_make_display_space(struct ImBuf *ibuf, const struct ColorManagedViewSettings *view_settings,
99                                                   const struct ColorManagedDisplaySettings *display_settings);
100
101 struct ImBuf *IMB_colormanagement_imbuf_for_write(struct ImBuf *ibuf, bool save_as_render, bool allocate_result,
102                                                   const struct ColorManagedViewSettings *view_settings,
103                                                   const struct ColorManagedDisplaySettings *display_settings,
104                                                   struct ImageFormatData *image_format_data);
105
106 void IMB_colormanagement_buffer_make_display_space(float *buffer, unsigned char *display_buffer,
107                                                    int width, int height, int channels, float dither,
108                                                    const struct ColorManagedViewSettings *view_settings,
109                                                    const struct ColorManagedDisplaySettings *display_settings);
110
111 /* ** Public display buffers interfaces ** */
112
113 void IMB_colormanagement_display_settings_from_ctx(const struct bContext *C,
114                                                    struct ColorManagedViewSettings **view_settings_r,
115                                                    struct ColorManagedDisplaySettings **display_settings_r);
116
117 const char *IMB_colormanagement_get_display_colorspace_name(const struct ColorManagedViewSettings *view_settings,
118                                                             const struct ColorManagedDisplaySettings *display_settings);
119
120 unsigned char *IMB_display_buffer_acquire(struct ImBuf *ibuf, const struct ColorManagedViewSettings *view_settings,
121                                           const struct ColorManagedDisplaySettings *display_settings, void **cache_handle);
122 unsigned char *IMB_display_buffer_acquire_ctx(const struct bContext *C, struct ImBuf *ibuf, void **cache_handle);
123
124 void IMB_display_buffer_transform_apply(unsigned char *display_buffer, float *linear_buffer, int width, int height,
125                                         int channels, const struct ColorManagedViewSettings *view_settings,
126                                         const struct ColorManagedDisplaySettings *display_settings, bool predivide);
127
128 void IMB_display_buffer_release(void *cache_handle);
129
130 /* ** Display funcrions ** */
131 int IMB_colormanagement_display_get_named_index(const char *name);
132 const char *IMB_colormanagement_display_get_indexed_name(int index);
133 const char *IMB_colormanagement_display_get_default_name(void);
134 struct ColorManagedDisplay *IMB_colormanagement_display_get_named(const char *name);
135 const char *IMB_colormanagement_display_get_none_name(void);
136
137 /* ** View funcrions ** */
138 int IMB_colormanagement_view_get_named_index(const char *name);
139 const char *IMB_colormanagement_view_get_indexed_name(int index);
140
141 /* ** Look funcrions ** */
142 int IMB_colormanagement_look_get_named_index(const char *name);
143 const char *IMB_colormanagement_look_get_indexed_name(int index);
144
145 /* ** Color space functions ** */
146 int IMB_colormanagement_colorspace_get_named_index(const char *name);
147 const char *IMB_colormanagement_colorspace_get_indexed_name(int index);
148 const char *IMB_colormanagement_view_get_default_name(const char *display_name);
149
150 void IMB_colormanagment_colorspace_from_ibuf_ftype(struct ColorManagedColorspaceSettings *colorspace_settings, struct ImBuf *ibuf);
151
152 /* ** RNA helper functions ** */
153 void IMB_colormanagement_display_items_add(struct EnumPropertyItem **items, int *totitem);
154 void IMB_colormanagement_view_items_add(struct EnumPropertyItem **items, int *totitem, const char *display_name);
155 void IMB_colormanagement_look_items_add(struct EnumPropertyItem **items, int *totitem);
156 void IMB_colormanagement_colorspace_items_add(struct EnumPropertyItem **items, int *totitem);
157
158 /* ** Tile-based buffer management ** */
159 void IMB_partial_display_buffer_update(struct ImBuf *ibuf, const float *linear_buffer, const unsigned char *buffer_byte,
160                                        int stride, int offset_x, int offset_y,
161                                        const struct ColorManagedViewSettings *view_settings,
162                                        const struct ColorManagedDisplaySettings *display_settings,
163                                        int xmin, int ymin, int xmax, int ymax,
164                                        bool copy_display_to_byte_buffer);
165
166 void IMB_partial_display_buffer_update_delayed(struct ImBuf *ibuf, int xmin, int ymin, int xmax, int ymax);
167
168 /* ** Pixel processor functions ** */
169 struct ColormanageProcessor *IMB_colormanagement_display_processor_new(const struct ColorManagedViewSettings *view_settings,
170                                                                        const struct ColorManagedDisplaySettings *display_settings);
171 struct ColormanageProcessor *IMB_colormanagement_colorspace_processor_new(const char *from_colorspace, const char *to_colorspace);
172 void IMB_colormanagement_processor_apply_v4(struct ColormanageProcessor *cm_processor, float pixel[4]);
173 void IMB_colormanagement_processor_apply_v4_predivide(struct ColormanageProcessor *cm_processor, float pixel[4]);
174 void IMB_colormanagement_processor_apply_v3(struct ColormanageProcessor *cm_processor, float pixel[3]);
175 void IMB_colormanagement_processor_apply_pixel(struct ColormanageProcessor *cm_processor, float *pixel, int channels);
176 void IMB_colormanagement_processor_apply(struct ColormanageProcessor *cm_processor, float *buffer, int width, int height,
177                                          int channels, bool predivide);
178 void IMB_colormanagement_processor_free(struct ColormanageProcessor *cm_processor);
179
180 /* ** OpenGL drawing routines using GLSL for color space transform ** */
181
182 /* Test if GLSL drawing is supported for combination of graphics card and this configuration */
183 bool IMB_colormanagement_support_glsl_draw(const struct ColorManagedViewSettings *view_settings);
184 /* Configures GLSL shader for conversion from scene linear to display space */
185 bool IMB_colormanagement_setup_glsl_draw(const struct ColorManagedViewSettings *view_settings,
186                                          const struct ColorManagedDisplaySettings *display_settings,
187                                          float dither, bool predivide);
188 /* Same as above, but display space conversion happens from a specified space */
189 bool IMB_colormanagement_setup_glsl_draw_from_space(const struct ColorManagedViewSettings *view_settings,
190                                                     const struct ColorManagedDisplaySettings *display_settings,
191                                                     struct ColorSpace *colorspace,
192                                                     float dither, bool predivide);
193 /* Same as setup_glsl_draw, but color management settings are guessing from a given context */
194 bool IMB_colormanagement_setup_glsl_draw_ctx(const struct bContext *C, float dither, bool predivide);
195 /* Same as setup_glsl_draw_from_space, but color management settings are guessing from a given context */
196 bool IMB_colormanagement_setup_glsl_draw_from_space_ctx(const struct bContext *C, struct ColorSpace *colorspace,
197                                                         float dither, bool predivide);
198 /* Finish GLSL-based display space conversion */
199 void IMB_colormanagement_finish_glsl_draw(void);
200
201 /* Roles */
202 enum {
203         COLOR_ROLE_SCENE_LINEAR = 0,
204         COLOR_ROLE_COLOR_PICKING,
205         COLOR_ROLE_TEXTURE_PAINTING,
206         COLOR_ROLE_DEFAULT_SEQUENCER,
207         COLOR_ROLE_DEFAULT_BYTE,
208         COLOR_ROLE_DEFAULT_FLOAT,
209 };
210
211 #include "intern/colormanagement_inline.c"
212
213 #endif  /* __IMB_COLORMANAGEMENT_H__ */