Color management: change view transform for color pickers and display modes.
[blender.git] / source / blender / imbuf / IMB_colormanagement.h
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2012 by Blender Foundation.
17  * All rights reserved.
18  */
19
20 #ifndef __IMB_COLORMANAGEMENT_H__
21 #define __IMB_COLORMANAGEMENT_H__
22
23 /** \file blender/imbuf/IMB_colormanagement.h
24  *  \ingroup imbuf
25  */
26
27 #include "BLI_sys_types.h"
28 #include "BLI_compiler_compat.h"
29
30 #define BCM_CONFIG_FILE "config.ocio"
31
32
33 struct ColorManagedColorspaceSettings;
34 struct ColorManagedDisplaySettings;
35 struct ColorManagedViewSettings;
36 struct ColormanageProcessor;
37 struct EnumPropertyItem;
38 struct ImBuf;
39 struct ImageFormatData;
40 struct Main;
41 struct bContext;
42
43 struct ColorManagedDisplay;
44 struct ColorSpace;
45
46 /* ** Generic functions ** */
47
48 void IMB_colormanagement_check_file_config(struct Main *bmain);
49
50 void IMB_colormanagement_validate_settings(const struct ColorManagedDisplaySettings *display_settings,
51                                            struct ColorManagedViewSettings *view_settings);
52
53 const char *IMB_colormanagement_role_colorspace_name_get(int role);
54 void IMB_colormanagement_check_is_data(struct ImBuf *ibuf, const char *name);
55 void IMB_colormanagement_assign_float_colorspace(struct ImBuf *ibuf, const char *name);
56 void IMB_colormanagement_assign_rect_colorspace(struct ImBuf *ibuf, const char *name);
57
58 const char *IMB_colormanagement_get_float_colorspace(struct ImBuf *ibuf);
59 const char *IMB_colormanagement_get_rect_colorspace(struct ImBuf *ibuf);
60
61 BLI_INLINE float IMB_colormanagement_get_luminance(const float rgb[3]);
62 BLI_INLINE unsigned char IMB_colormanagement_get_luminance_byte(const unsigned char[3]);
63 BLI_INLINE void IMB_colormangement_xyz_to_rgb(float rgb[3], const float xyz[3]);
64 BLI_INLINE void IMB_colormangement_rgb_to_xyz(float xyz[3], const float rgb[3]);
65
66 /* ** Color space transformation functions ** */
67 void IMB_colormanagement_transform(float *buffer, int width, int height, int channels,
68                                    const char *from_colorspace, const char *to_colorspace, bool predivide);
69 void IMB_colormanagement_transform_threaded(float *buffer, int width, int height, int channels,
70                                             const char *from_colorspace, const char *to_colorspace, bool predivide);
71 void IMB_colormanagement_transform_byte(unsigned char *buffer, int width, int height, int channels,
72                                         const char *from_colorspace, const char *to_colorspace);
73 void IMB_colormanagement_transform_byte_threaded(unsigned char *buffer, int width, int height, int channels,
74                                                  const char *from_colorspace, const char *to_colorspace);
75 void IMB_colormanagement_transform_from_byte(float *float_buffer, unsigned char *byte_buffer,
76                                              int width, int height, int channels,
77                                              const char *from_colorspace, const char *to_colorspace);
78 void IMB_colormanagement_transform_from_byte_threaded(float *float_buffer, unsigned char *byte_buffer,
79                                                       int width, int height, int channels,
80                                                       const char *from_colorspace, const char *to_colorspace);
81 void IMB_colormanagement_transform_v4(float pixel[4], const char *from_colorspace, const char *to_colorspace);
82
83 void IMB_colormanagement_colorspace_to_scene_linear_v3(float pixel[3], struct ColorSpace *colorspace);
84 void IMB_colormanagement_colorspace_to_scene_linear_v4(float pixel[4], bool predivide, struct ColorSpace *colorspace);
85
86 void IMB_colormanagement_scene_linear_to_colorspace_v3(float pixel[3], struct ColorSpace *colorspace);
87
88 void IMB_colormanagement_colorspace_to_scene_linear(float *buffer, int width, int height, int channels, struct ColorSpace *colorspace, bool predivide);
89
90 void IMB_colormanagement_scene_linear_to_color_picking_v3(float pixel[3]);
91 void IMB_colormanagement_color_picking_to_scene_linear_v3(float pixel[3]);
92
93 void IMB_colormanagement_scene_linear_to_srgb_v3(float pixel[3]);
94 void IMB_colormanagement_srgb_to_scene_linear_v3(float pixel[3]);
95
96 void IMB_colormanagement_scene_linear_to_display_v3(float pixel[3], struct ColorManagedDisplay *display);
97 void IMB_colormanagement_display_to_scene_linear_v3(float pixel[3], struct ColorManagedDisplay *display);
98
99 void IMB_colormanagement_pixel_to_display_space_v4(float result[4], const float pixel[4],  const struct ColorManagedViewSettings *view_settings,
100                                                    const struct ColorManagedDisplaySettings *display_settings);
101
102 void IMB_colormanagement_pixel_to_display_space_v3(float result[3], const float pixel[3],  const struct ColorManagedViewSettings *view_settings,
103                                                    const struct ColorManagedDisplaySettings *display_settings);
104
105 void IMB_colormanagement_imbuf_make_display_space(struct ImBuf *ibuf, const struct ColorManagedViewSettings *view_settings,
106                                                   const struct ColorManagedDisplaySettings *display_settings);
107
108 struct ImBuf *IMB_colormanagement_imbuf_for_write(struct ImBuf *ibuf, bool save_as_render, bool allocate_result,
109                                                   const struct ColorManagedViewSettings *view_settings,
110                                                   const struct ColorManagedDisplaySettings *display_settings,
111                                                   struct ImageFormatData *image_format_data);
112
113 void IMB_colormanagement_buffer_make_display_space(float *buffer, unsigned char *display_buffer,
114                                                    int width, int height, int channels, float dither,
115                                                    const struct ColorManagedViewSettings *view_settings,
116                                                    const struct ColorManagedDisplaySettings *display_settings);
117
118 /* ** Public display buffers interfaces ** */
119
120 void IMB_colormanagement_display_settings_from_ctx(const struct bContext *C,
121                                                    struct ColorManagedViewSettings **view_settings_r,
122                                                    struct ColorManagedDisplaySettings **display_settings_r);
123
124 const char *IMB_colormanagement_get_display_colorspace_name(const struct ColorManagedViewSettings *view_settings,
125                                                             const struct ColorManagedDisplaySettings *display_settings);
126
127 unsigned char *IMB_display_buffer_acquire(struct ImBuf *ibuf, const struct ColorManagedViewSettings *view_settings,
128                                           const struct ColorManagedDisplaySettings *display_settings, void **cache_handle);
129 unsigned char *IMB_display_buffer_acquire_ctx(const struct bContext *C, struct ImBuf *ibuf, void **cache_handle);
130
131 void IMB_display_buffer_transform_apply(unsigned char *display_buffer, float *linear_buffer, int width, int height,
132                                         int channels, const struct ColorManagedViewSettings *view_settings,
133                                         const struct ColorManagedDisplaySettings *display_settings, bool predivide);
134
135 void IMB_display_buffer_release(void *cache_handle);
136
137 /* ** Display functions ** */
138 int IMB_colormanagement_display_get_named_index(const char *name);
139 const char *IMB_colormanagement_display_get_indexed_name(int index);
140 const char *IMB_colormanagement_display_get_default_name(void);
141 struct ColorManagedDisplay *IMB_colormanagement_display_get_named(const char *name);
142 const char *IMB_colormanagement_display_get_none_name(void);
143 const char *IMB_colormanagement_display_get_default_view_transform_name(
144         struct ColorManagedDisplay *display);
145
146 /* ** View functions ** */
147 int IMB_colormanagement_view_get_named_index(const char *name);
148 const char *IMB_colormanagement_view_get_indexed_name(int index);
149
150 /* ** Look functions ** */
151 int IMB_colormanagement_look_get_named_index(const char *name);
152 const char *IMB_colormanagement_look_get_indexed_name(int index);
153
154 /* ** Color space functions ** */
155 int IMB_colormanagement_colorspace_get_named_index(const char *name);
156 const char *IMB_colormanagement_colorspace_get_indexed_name(int index);
157 const char *IMB_colormanagement_view_get_default_name(const char *display_name);
158
159 void IMB_colormanagement_colorspace_from_ibuf_ftype(struct ColorManagedColorspaceSettings *colorspace_settings, struct ImBuf *ibuf);
160
161 /* ** RNA helper functions ** */
162 void IMB_colormanagement_display_items_add(struct EnumPropertyItem **items, int *totitem);
163 void IMB_colormanagement_view_items_add(struct EnumPropertyItem **items, int *totitem, const char *display_name);
164 void IMB_colormanagement_look_items_add(struct EnumPropertyItem **items, int *totitem, const char *view_name);
165 void IMB_colormanagement_colorspace_items_add(struct EnumPropertyItem **items, int *totitem);
166
167 /* ** Tile-based buffer management ** */
168 void IMB_partial_display_buffer_update(struct ImBuf *ibuf, const float *linear_buffer, const unsigned char *buffer_byte,
169                                        int stride, int offset_x, int offset_y,
170                                        const struct ColorManagedViewSettings *view_settings,
171                                        const struct ColorManagedDisplaySettings *display_settings,
172                                        int xmin, int ymin, int xmax, int ymax,
173                                        bool copy_display_to_byte_buffer);
174
175 void IMB_partial_display_buffer_update_threaded(struct ImBuf *ibuf,
176                                                 const float *linear_buffer,
177                                                 const unsigned char *buffer_byte,
178                                                 int stride,
179                                                 int offset_x, int offset_y,
180                                                 const struct ColorManagedViewSettings *view_settings,
181                                                 const struct ColorManagedDisplaySettings *display_settings,
182                                                 int xmin, int ymin, int xmax, int ymax,
183                                                 bool copy_display_to_byte_buffer);
184
185 void IMB_partial_display_buffer_update_delayed(struct ImBuf *ibuf, int xmin, int ymin, int xmax, int ymax);
186
187 /* ** Pixel processor functions ** */
188 struct ColormanageProcessor *IMB_colormanagement_display_processor_new(const struct ColorManagedViewSettings *view_settings,
189                                                                        const struct ColorManagedDisplaySettings *display_settings);
190 struct ColormanageProcessor *IMB_colormanagement_colorspace_processor_new(const char *from_colorspace, const char *to_colorspace);
191 void IMB_colormanagement_processor_apply_v4(struct ColormanageProcessor *cm_processor, float pixel[4]);
192 void IMB_colormanagement_processor_apply_v4_predivide(struct ColormanageProcessor *cm_processor, float pixel[4]);
193 void IMB_colormanagement_processor_apply_v3(struct ColormanageProcessor *cm_processor, float pixel[3]);
194 void IMB_colormanagement_processor_apply_pixel(struct ColormanageProcessor *cm_processor, float *pixel, int channels);
195 void IMB_colormanagement_processor_apply(struct ColormanageProcessor *cm_processor, float *buffer, int width, int height,
196                                          int channels, bool predivide);
197 void IMB_colormanagement_processor_apply_byte(struct ColormanageProcessor *cm_processor,
198                                               unsigned char *buffer, int width, int height, int channels);
199 void IMB_colormanagement_processor_free(struct ColormanageProcessor *cm_processor);
200
201 /* ** OpenGL drawing routines using GLSL for color space transform ** */
202
203 /* Test if GLSL drawing is supported for combination of graphics card and this configuration */
204 bool IMB_colormanagement_support_glsl_draw(const struct ColorManagedViewSettings *view_settings);
205 /* Configures GLSL shader for conversion from scene linear to display space */
206 bool IMB_colormanagement_setup_glsl_draw(const struct ColorManagedViewSettings *view_settings,
207                                          const struct ColorManagedDisplaySettings *display_settings,
208                                          float dither, bool predivide);
209 /* Same as above, but display space conversion happens from a specified space */
210 bool IMB_colormanagement_setup_glsl_draw_from_space(const struct ColorManagedViewSettings *view_settings,
211                                                     const struct ColorManagedDisplaySettings *display_settings,
212                                                     struct ColorSpace *colorspace,
213                                                     float dither, bool predivide);
214 /* Same as setup_glsl_draw, but color management settings are guessing from a given context */
215 bool IMB_colormanagement_setup_glsl_draw_ctx(const struct bContext *C, float dither, bool predivide);
216 /* Same as setup_glsl_draw_from_space, but color management settings are guessing from a given context */
217 bool IMB_colormanagement_setup_glsl_draw_from_space_ctx(const struct bContext *C, struct ColorSpace *colorspace,
218                                                         float dither, bool predivide);
219 /* Finish GLSL-based display space conversion */
220 void IMB_colormanagement_finish_glsl_draw(void);
221
222 /* ** View transform ** */
223 void IMB_colormanagement_init_default_view_settings(
224         struct ColorManagedViewSettings *view_settings,
225         const struct ColorManagedDisplaySettings *display_settings);
226
227 /* Roles */
228 enum {
229         COLOR_ROLE_SCENE_LINEAR = 0,
230         COLOR_ROLE_COLOR_PICKING,
231         COLOR_ROLE_TEXTURE_PAINTING,
232         COLOR_ROLE_DEFAULT_SEQUENCER,
233         COLOR_ROLE_DEFAULT_BYTE,
234         COLOR_ROLE_DEFAULT_FLOAT,
235 };
236
237 #include "intern/colormanagement_inline.c"
238
239 #endif  /* __IMB_COLORMANAGEMENT_H__ */