From dd38dce7f0ae604396d1e96bc49500369fdedf29 Mon Sep 17 00:00:00 2001 From: Antony Riakiotakis Date: Tue, 17 Mar 2015 15:20:33 +0100 Subject: Part 2 of D1082 by Troy Sobotka, remove our functions that do luma calculations and use the OCIO one instead. --- source/blender/blenkernel/intern/colortools.c | 6 +-- source/blender/blenlib/BLI_math_color.h | 7 --- source/blender/blenlib/intern/math_color_inline.c | 55 ---------------------- .../operations/COM_CalculateMeanOperation.cpp | 6 ++- .../COM_CalculateStandardDeviationOperation.cpp | 6 ++- .../operations/COM_ColorCorrectionOperation.cpp | 6 ++- .../compositor/operations/COM_ConvertOperation.cpp | 5 +- .../operations/COM_GlareThresholdOperation.cpp | 6 ++- .../compositor/operations/COM_TonemapOperation.cpp | 8 +++- source/blender/editors/interface/interface_draw.c | 2 +- .../blender/editors/interface/interface_regions.c | 6 +-- .../blender/editors/interface/interface_widgets.c | 3 +- .../blender/editors/sculpt_paint/paint_image_2d.c | 2 +- .../editors/sculpt_paint/paint_image_proj.c | 4 +- source/blender/editors/sculpt_paint/paint_vertex.c | 5 +- .../editors/space_sequencer/sequencer_scopes.c | 5 +- source/blender/imbuf/IMB_colormanagement.h | 3 ++ source/blender/imbuf/intern/colormanagement.c | 36 ++++++++++++++ source/blender/imbuf/intern/divers.c | 4 +- source/blender/imbuf/intern/png.c | 4 +- .../nodes/shader/nodes/node_shader_valToRgb.c | 3 +- .../nodes/texture/nodes/node_texture_valToRgb.c | 3 +- .../blender/render/intern/source/render_texture.c | 38 +++++++-------- source/blender/render/intern/source/shadeoutput.c | 22 +++++---- source/blender/render/intern/source/volumetric.c | 12 +++-- 25 files changed, 131 insertions(+), 126 deletions(-) (limited to 'source/blender') diff --git a/source/blender/blenkernel/intern/colortools.c b/source/blender/blenkernel/intern/colortools.c index 89c3e4b0cfc..616e8a0486b 100644 --- a/source/blender/blenkernel/intern/colortools.c +++ b/source/blender/blenkernel/intern/colortools.c @@ -994,7 +994,7 @@ void BKE_histogram_update_sample_line(Histogram *hist, ImBuf *ibuf, const ColorM copy_v3_v3(rgb, fp); IMB_colormanagement_processor_apply_v3(cm_processor, rgb); - hist->data_luma[i] = rgb_to_luma(rgb); + hist->data_luma[i] = IMB_colormanagement_get_luminance(rgb); hist->data_r[i] = rgb[0]; hist->data_g[i] = rgb[1]; hist->data_b[i] = rgb[2]; @@ -1002,7 +1002,7 @@ void BKE_histogram_update_sample_line(Histogram *hist, ImBuf *ibuf, const ColorM } else if (ibuf->rect) { cp = (unsigned char *)(ibuf->rect + y * ibuf->x + x); - hist->data_luma[i] = (float)rgb_to_luma_byte(cp) / 255.0f; + hist->data_luma[i] = (float)IMB_colormanagement_get_luminance_byte(cp) / 255.0f; hist->data_r[i] = (float)cp[0] / 255.0f; hist->data_g[i] = (float)cp[1] / 255.0f; hist->data_b[i] = (float)cp[2] / 255.0f; @@ -1124,7 +1124,7 @@ void scopes_update(Scopes *scopes, ImBuf *ibuf, const ColorManagedViewSettings * } /* we still need luma for histogram */ - luma = rgb_to_luma(rgba); + luma = IMB_colormanagement_get_luminance(rgba); /* check for min max */ if (ycc_mode == -1) { diff --git a/source/blender/blenlib/BLI_math_color.h b/source/blender/blenlib/BLI_math_color.h index 55e57b14d09..5652c8056c0 100644 --- a/source/blender/blenlib/BLI_math_color.h +++ b/source/blender/blenlib/BLI_math_color.h @@ -80,13 +80,6 @@ void rgb_to_xyz(float r, float g, float b, float *x, float *y, float *z); unsigned int rgb_to_cpack(float r, float g, float b); unsigned int hsv_to_cpack(float h, float s, float v); -MINLINE float rgb_to_bw(const float rgb[3]); -MINLINE float rgb_to_grayscale(const float rgb[3]); -MINLINE unsigned char rgb_to_grayscale_byte(const unsigned char rgb[3]); -MINLINE float rgb_to_luma(const float rgb[3]); -MINLINE unsigned char rgb_to_luma_byte(const unsigned char rgb[3]); -MINLINE float rgb_to_luma_y(const float rgb[3]); - /**************** Profile Transformations *****************/ float srgb_to_linearrgb(float c); diff --git a/source/blender/blenlib/intern/math_color_inline.c b/source/blender/blenlib/intern/math_color_inline.c index dc62d04ad55..96aa4b55201 100644 --- a/source/blender/blenlib/intern/math_color_inline.c +++ b/source/blender/blenlib/intern/math_color_inline.c @@ -200,61 +200,6 @@ MINLINE void cpack_cpy_3ub(unsigned char r_col[3], const unsigned int pack) r_col[2] = ((pack) >> 16) & 0xFF; } -/* TODO: - * - * regarding #rgb_to_bw vs #rgb_to_grayscale, - * it seems nobody knows why we have both functions which convert color to grays - * but with different influences, this is quite stupid, and should be resolved - * by someone who knows this stuff: see this thread - * http://lists.blender.org/pipermail/bf-committers/2012-June/037180.html - * - * Only conclusion is that rgb_to_grayscale is used more for compositing. - */ -MINLINE float rgb_to_bw(const float rgb[3]) -{ - return 0.35f * rgb[0] + 0.45f * rgb[1] + 0.2f * rgb[2]; -} - -/* non-linear luma from ITU-R BT.601-2 - * see: http://www.poynton.com/notes/colour_and_gamma/ColorFAQ.html#RTFToC11 - * note: the values used for are not exact matches to those documented above, - * but they are from the same */ -MINLINE float rgb_to_grayscale(const float rgb[3]) -{ - return 0.3f * rgb[0] + 0.58f * rgb[1] + 0.12f * rgb[2]; -} - -MINLINE unsigned char rgb_to_grayscale_byte(const unsigned char rgb[3]) -{ - return (unsigned char)(((76 * (unsigned short)rgb[0]) + - (148 * (unsigned short)rgb[1]) + - (31 * (unsigned short)rgb[2])) / 255); -} - -/* luma from defined by 'YCC_JFIF', see #rgb_to_ycc */ -MINLINE float rgb_to_luma(const float rgb[3]) -{ - return 0.299f * rgb[0] + 0.587f * rgb[1] + 0.114f * rgb[2]; -} - -MINLINE unsigned char rgb_to_luma_byte(const unsigned char rgb[3]) -{ - return (unsigned char)(((76 * (unsigned short)rgb[0]) + - (150 * (unsigned short)rgb[1]) + - (29 * (unsigned short)rgb[2])) / 255); -} - -/* gamma-corrected RGB --> CIE XYZ - * for this function we only get the Y component - * see: http://software.intel.com/sites/products/documentation/hpc/ipp/ippi/ippi_ch6/ch6_color_models.html - * - * also known as: - * luminance rec. 709 */ -MINLINE float rgb_to_luma_y(const float rgb[3]) -{ - return 0.212671f * rgb[0] + 0.71516f * rgb[1] + 0.072169f * rgb[2]; -} - MINLINE int compare_rgb_uchar(const unsigned char col_a[3], const unsigned char col_b[3], const int limit) { const int r = (int)col_a[0] - (int)col_b[0]; diff --git a/source/blender/compositor/operations/COM_CalculateMeanOperation.cpp b/source/blender/compositor/operations/COM_CalculateMeanOperation.cpp index a2954a20e90..54b0e37c791 100644 --- a/source/blender/compositor/operations/COM_CalculateMeanOperation.cpp +++ b/source/blender/compositor/operations/COM_CalculateMeanOperation.cpp @@ -24,7 +24,9 @@ #include "BLI_math.h" #include "BLI_utildefines.h" - +extern "C" { +#include "IMB_colormanagement.h" +} CalculateMeanOperation::CalculateMeanOperation() : NodeOperation() { @@ -96,7 +98,7 @@ void CalculateMeanOperation::calculateMean(MemoryBuffer *tile) switch (this->m_setting) { case 1: { - sum += rgb_to_bw(&buffer[offset]); + sum += IMB_colormanagement_get_luminance(&buffer[offset]); break; } case 2: diff --git a/source/blender/compositor/operations/COM_CalculateStandardDeviationOperation.cpp b/source/blender/compositor/operations/COM_CalculateStandardDeviationOperation.cpp index 0c67da2d552..32c5fc1adc7 100644 --- a/source/blender/compositor/operations/COM_CalculateStandardDeviationOperation.cpp +++ b/source/blender/compositor/operations/COM_CalculateStandardDeviationOperation.cpp @@ -24,7 +24,9 @@ #include "BLI_math.h" #include "BLI_utildefines.h" - +extern "C" { +#include "IMB_colormanagement.h" +} CalculateStandardDeviationOperation::CalculateStandardDeviationOperation() : CalculateMeanOperation() { @@ -55,7 +57,7 @@ void *CalculateStandardDeviationOperation::initializeTileData(rcti *rect) switch (this->m_setting) { case 1: /* rgb combined */ { - float value = rgb_to_bw(&buffer[offset]); + float value = IMB_colormanagement_get_luminance(&buffer[offset]); sum += (value - mean) * (value - mean); break; } diff --git a/source/blender/compositor/operations/COM_ColorCorrectionOperation.cpp b/source/blender/compositor/operations/COM_ColorCorrectionOperation.cpp index 19209951005..54e0fb41abf 100644 --- a/source/blender/compositor/operations/COM_ColorCorrectionOperation.cpp +++ b/source/blender/compositor/operations/COM_ColorCorrectionOperation.cpp @@ -23,6 +23,10 @@ #include "COM_ColorCorrectionOperation.h" #include "BLI_math.h" +extern "C" { +#include "IMB_colormanagement.h" +} + ColorCorrectionOperation::ColorCorrectionOperation() : NodeOperation() { this->addInputSocket(COM_DT_COLOR); @@ -90,7 +94,7 @@ void ColorCorrectionOperation::executePixelSampled(float output[4], float x, flo lift += (levelShadows * this->m_data->shadows.lift) + (levelMidtones * this->m_data->midtones.lift) + (levelHighlights * this->m_data->highlights.lift); float invgamma = 1.0f / gamma; - float luma = rgb_to_luma_y(inputImageColor); + float luma = IMB_colormanagement_get_luminance(inputImageColor); r = inputImageColor[0]; g = inputImageColor[1]; diff --git a/source/blender/compositor/operations/COM_ConvertOperation.cpp b/source/blender/compositor/operations/COM_ConvertOperation.cpp index 977586acb60..8b8e8408208 100644 --- a/source/blender/compositor/operations/COM_ConvertOperation.cpp +++ b/source/blender/compositor/operations/COM_ConvertOperation.cpp @@ -22,6 +22,9 @@ #include "COM_ConvertOperation.h" +extern "C" { +#include "IMB_colormanagement.h" +} ConvertBaseOperation::ConvertBaseOperation() { @@ -84,7 +87,7 @@ void ConvertColorToBWOperation::executePixelSampled(float output[4], float x, fl { float inputColor[4]; this->m_inputOperation->readSampled(inputColor, x, y, sampler); - output[0] = rgb_to_bw(inputColor); + output[0] = IMB_colormanagement_get_luminance(inputColor); } diff --git a/source/blender/compositor/operations/COM_GlareThresholdOperation.cpp b/source/blender/compositor/operations/COM_GlareThresholdOperation.cpp index 78e1e80cafc..d2bd7cbeeab 100644 --- a/source/blender/compositor/operations/COM_GlareThresholdOperation.cpp +++ b/source/blender/compositor/operations/COM_GlareThresholdOperation.cpp @@ -23,6 +23,10 @@ #include "COM_GlareThresholdOperation.h" #include "BLI_math.h" +extern "C" { +#include "IMB_colormanagement.h" +} + GlareThresholdOperation::GlareThresholdOperation() : NodeOperation() { this->addInputSocket(COM_DT_COLOR, COM_SC_FIT); @@ -47,7 +51,7 @@ void GlareThresholdOperation::executePixelSampled(float output[4], float x, floa const float threshold = this->m_settings->threshold; this->m_inputProgram->readSampled(output, x, y, sampler); - if (rgb_to_luma_y(output) >= threshold) { + if (IMB_colormanagement_get_luminance(output) >= threshold) { output[0] -= threshold, output[1] -= threshold, output[2] -= threshold; output[0] = max(output[0], 0.0f); output[1] = max(output[1], 0.0f); diff --git a/source/blender/compositor/operations/COM_TonemapOperation.cpp b/source/blender/compositor/operations/COM_TonemapOperation.cpp index e8a578fa131..54ba218b802 100644 --- a/source/blender/compositor/operations/COM_TonemapOperation.cpp +++ b/source/blender/compositor/operations/COM_TonemapOperation.cpp @@ -24,6 +24,10 @@ #include "BLI_math.h" #include "BLI_utildefines.h" +extern "C" { +#include "IMB_colormanagement.h" +} + TonemapOperation::TonemapOperation() : NodeOperation() { this->addInputSocket(COM_DT_COLOR, COM_SC_NO_RESIZE); @@ -69,7 +73,7 @@ void PhotoreceptorTonemapOperation::executePixel(float output[4], int x, int y, this->m_imageReader->read(output, x, y, NULL); - const float L = rgb_to_luma_y(output); + const float L = IMB_colormanagement_get_luminance(output); float I_l = output[0] + ic * (L - output[0]); float I_g = avg->cav[0] + ic * (avg->lav - avg->cav[0]); float I_a = I_l + ia * (I_g - I_l); @@ -125,7 +129,7 @@ void *TonemapOperation::initializeTileData(rcti *rect) float Lav = 0.f; float cav[4] = {0.0f, 0.0f, 0.0f, 0.0f}; while (p--) { - float L = rgb_to_luma_y(bc); + float L = IMB_colormanagement_get_luminance(bc); Lav += L; add_v3_v3(cav, bc); lsum += logf(MAX2(L, 0.0f) + 1e-5f); diff --git a/source/blender/editors/interface/interface_draw.c b/source/blender/editors/interface/interface_draw.c index 07d580b9138..414903fa194 100644 --- a/source/blender/editors/interface/interface_draw.c +++ b/source/blender/editors/interface/interface_draw.c @@ -1453,7 +1453,7 @@ void ui_draw_but_CURVE(ARegion *ar, uiBut *but, uiWidgetColors *wcol, const rcti glEnd(); } else if (cumap->cur == 3) { - float lum = rgb_to_bw(cumap->sample); + float lum = IMB_colormanagement_get_luminance(cumap->sample); glColor3ub(240, 240, 240); glBegin(GL_LINES); diff --git a/source/blender/editors/interface/interface_regions.c b/source/blender/editors/interface/interface_regions.c index 6e5f6af3c5f..adcd615368b 100644 --- a/source/blender/editors/interface/interface_regions.c +++ b/source/blender/editors/interface/interface_regions.c @@ -237,8 +237,8 @@ static void ui_tooltip_region_draw_cb(const bContext *UNUSED(C), ARegion *ar) /* find the brightness difference between background and text colors */ - tone_bg = rgb_to_grayscale(background_color); - /* tone_fg = rgb_to_grayscale(main_color); */ + tone_bg = IMB_colormanagement_get_luminance(background_color); + /* tone_fg = IMB_colormanagement_get_luminance(main_color); */ /* mix the colors */ rgb_tint(value_color, 0.0f, 0.0f, tone_bg, 0.2f); /* light grey */ @@ -261,7 +261,7 @@ static void ui_tooltip_region_draw_cb(const bContext *UNUSED(C), ARegion *ar) /* override text-style */ fstyle_header.shadow = 1; - fstyle_header.shadowcolor = rgb_to_luma(tip_colors[UI_TIP_LC_MAIN]); + fstyle_header.shadowcolor = IMB_colormanagement_get_luminance(tip_colors[UI_TIP_LC_MAIN]); fstyle_header.shadx = fstyle_header.shady = 0; fstyle_header.shadowalpha = 1.0f; diff --git a/source/blender/editors/interface/interface_widgets.c b/source/blender/editors/interface/interface_widgets.c index 91186a14b49..9020522bb59 100644 --- a/source/blender/editors/interface/interface_widgets.c +++ b/source/blender/editors/interface/interface_widgets.c @@ -56,6 +56,7 @@ #include "UI_interface.h" #include "UI_interface_icons.h" +#include "IMB_colormanagement.h" #include "interface_intern.h" @@ -3027,7 +3028,7 @@ static void widget_swatch(uiBut *but, uiWidgetColors *wcol, rcti *rect, int stat float width = rect->xmax - rect->xmin; float height = rect->ymax - rect->ymin; /* find color luminance and change it slightly */ - float bw = rgb_to_bw(col); + float bw = IMB_colormanagement_get_luminance(col); bw += (bw < 0.5f) ? 0.5f : -0.5f; diff --git a/source/blender/editors/sculpt_paint/paint_image_2d.c b/source/blender/editors/sculpt_paint/paint_image_2d.c index eb96d1d20d5..4f71bea7cd2 100644 --- a/source/blender/editors/sculpt_paint/paint_image_2d.c +++ b/source/blender/editors/sculpt_paint/paint_image_2d.c @@ -889,7 +889,7 @@ static void paint_2d_lift_soften(ImagePaintState *s, ImBuf *ibuf, ImBuf *ibufb, /* now rgba_ub contains the edge result, but this should be converted to luminance to avoid * colored speckles appearing in final image, and also to check for threshold */ - outrgb[0] = outrgb[1] = outrgb[2] = rgb_to_grayscale(outrgb); + outrgb[0] = outrgb[1] = outrgb[2] = IMB_colormanagement_get_luminance(outrgb); if (fabsf(outrgb[0]) > threshold) { float mask = BKE_brush_alpha_get(s->scene, s->brush); float alpha = rgba[3]; diff --git a/source/blender/editors/sculpt_paint/paint_image_proj.c b/source/blender/editors/sculpt_paint/paint_image_proj.c index 098f0d04d78..805c271f2d1 100644 --- a/source/blender/editors/sculpt_paint/paint_image_proj.c +++ b/source/blender/editors/sculpt_paint/paint_image_proj.c @@ -4207,7 +4207,7 @@ static void do_projectpaint_soften_f(ProjPaintState *ps, ProjPixel *projPixel, f /* now rgba_ub contains the edge result, but this should be converted to luminance to avoid * colored speckles appearing in final image, and also to check for threshold */ - rgba[0] = rgba[1] = rgba[2] = rgb_to_grayscale(rgba); + rgba[0] = rgba[1] = rgba[2] = IMB_colormanagement_get_luminance(rgba); if (fabsf(rgba[0]) > ps->brush->sharp_threshold) { float alpha = projPixel->pixel.f_pt[3]; projPixel->pixel.f_pt[3] = rgba[3] = mask; @@ -4268,7 +4268,7 @@ static void do_projectpaint_soften(ProjPaintState *ps, ProjPixel *projPixel, flo sub_v3_v3v3(rgba, rgba_pixel, rgba); /* now rgba_ub contains the edge result, but this should be converted to luminance to avoid * colored speckles appearing in final image, and also to check for threshold */ - rgba[0] = rgba[1] = rgba[2] = rgb_to_grayscale(rgba); + rgba[0] = rgba[1] = rgba[2] = IMB_colormanagement_get_luminance(rgba); if (fabsf(rgba[0]) > ps->brush->sharp_threshold) { float alpha = rgba_pixel[3]; rgba[3] = rgba_pixel[3] = mask; diff --git a/source/blender/editors/sculpt_paint/paint_vertex.c b/source/blender/editors/sculpt_paint/paint_vertex.c index e87dd6c2810..9bca0fd2976 100644 --- a/source/blender/editors/sculpt_paint/paint_vertex.c +++ b/source/blender/editors/sculpt_paint/paint_vertex.c @@ -37,6 +37,7 @@ #include "IMB_imbuf.h" #include "IMB_imbuf_types.h" +#include "IMB_colormanagement.h" #include "DNA_armature_types.h" #include "DNA_mesh_types.h" @@ -768,7 +769,7 @@ BLI_INLINE unsigned int mcol_lighten(unsigned int col1, unsigned int col2, int f /* See if are lighter, if so mix, else don't do anything. * if the paint col is darker then the original, then ignore */ - if (rgb_to_grayscale_byte(cp1) > rgb_to_grayscale_byte(cp2)) { + if (IMB_colormanagement_get_luminance_byte(cp1) > IMB_colormanagement_get_luminance_byte(cp2)) { return col1; } @@ -801,7 +802,7 @@ BLI_INLINE unsigned int mcol_darken(unsigned int col1, unsigned int col2, int fa /* See if were darker, if so mix, else don't do anything. * if the paint col is brighter then the original, then ignore */ - if (rgb_to_grayscale_byte(cp1) < rgb_to_grayscale_byte(cp2)) { + if (IMB_colormanagement_get_luminance_byte(cp1) < IMB_colormanagement_get_luminance_byte(cp2)) { return col1; } diff --git a/source/blender/editors/space_sequencer/sequencer_scopes.c b/source/blender/editors/space_sequencer/sequencer_scopes.c index 24b3776da6d..b4bcfb4eb07 100644 --- a/source/blender/editors/space_sequencer/sequencer_scopes.c +++ b/source/blender/editors/space_sequencer/sequencer_scopes.c @@ -32,6 +32,7 @@ #include "BLI_math_color.h" #include "BLI_utildefines.h" +#include "IMB_colormanagement.h" #include "IMB_imbuf_types.h" #include "IMB_imbuf.h" @@ -167,7 +168,7 @@ static ImBuf *make_waveform_view_from_ibuf_byte(ImBuf *ibuf) for (x = 0; x < ibuf->x; x++) { const unsigned char *rgb = src + 4 * (ibuf->x * y + x); - float v = (float)rgb_to_luma_byte(rgb) / 255.0f; + float v = (float)IMB_colormanagement_get_luminance_byte(rgb) / 255.0f; unsigned char *p = tgt; p += 4 * (w * ((int) (v * (h - 3)) + 1) + x + 1); @@ -207,7 +208,7 @@ static ImBuf *make_waveform_view_from_ibuf_float(ImBuf *ibuf) for (x = 0; x < ibuf->x; x++) { const float *rgb = src + 4 * (ibuf->x * y + x); - float v = rgb_to_luma(rgb); + float v = IMB_colormanagement_get_luminance(rgb); unsigned char *p = tgt; CLAMP(v, 0.0f, 1.0f); diff --git a/source/blender/imbuf/IMB_colormanagement.h b/source/blender/imbuf/IMB_colormanagement.h index 262e87bedf5..9407750c039 100644 --- a/source/blender/imbuf/IMB_colormanagement.h +++ b/source/blender/imbuf/IMB_colormanagement.h @@ -71,6 +71,9 @@ void IMB_colormanagement_assign_rect_colorspace(struct ImBuf *ibuf, const char * const char *IMB_colormanagement_get_float_colorspace(struct ImBuf *ibuf); const char *IMB_colormanagement_get_rect_colorspace(struct ImBuf *ibuf); +float IMB_colormanagement_get_luminance(const float rgb[3]); +unsigned char IMB_colormanagement_get_luminance_byte(const unsigned char[3]); + /* ** Color space transformation functions ** */ void IMB_colormanagement_transform(float *buffer, int width, int height, int channels, const char *from_colorspace, const char *to_colorspace, bool predivide); diff --git a/source/blender/imbuf/intern/colormanagement.c b/source/blender/imbuf/intern/colormanagement.c index 95e166b2f8b..588d81f9a75 100644 --- a/source/blender/imbuf/intern/colormanagement.c +++ b/source/blender/imbuf/intern/colormanagement.c @@ -90,6 +90,11 @@ static int global_tot_display = 0; static int global_tot_view = 0; static int global_tot_looks = 0; +/* Set to ITU-BT.709 / sRGB primaries weight. Brute force stupid, but only + * option with no colormanagement in place. + */ +static float luma_coefficients[3] = { 0.2126729f, 0.7151522f, 0.0721750f }; + /* lock used by pre-cached processors getters, so processor wouldn't * be created several times * LOCK_COLORMANAGE can not be used since this mutex could be needed to @@ -545,6 +550,9 @@ static void colormanage_load_config(OCIO_ConstConfigRcPtr *config) colormanage_look_add(name, process_space, false); } + + /* Load luminance coefficients. */ + OCIO_configGetDefaultLumaCoefs(config, luma_coefficients); } static void colormanage_free_config(void) @@ -1222,6 +1230,34 @@ const char *IMB_colormanagement_get_rect_colorspace(ImBuf *ibuf) return ibuf->rect_colorspace->name; } +/* Convert a float RGB triplet to the correct luminance weighted average. + * + * Grayscale, or Luma is a distillation of RGB data values down to a weighted average + * based on the luminance positions of the red, green, and blue primaries. + * Given that the internal reference space may be arbitrarily set, any + * effort to glean the luminance coefficients must be aware of the reference + * space primaries. + * + * See http://wiki.blender.org/index.php/User:Nazg-gul/ColorManagement#Luminance + */ + +float IMB_colormanagement_get_luminance(const float rgb[3]) +{ + return dot_v3v3(luma_coefficients, rgb); +} + +/* Byte equivalent of IMB_colormanagement_get_luminance(). */ +unsigned char IMB_colormanagement_get_luminance_byte(const unsigned char rgb[3]) +{ + float rgbf[3]; + + rgbf[0] = (float) rgb[0] / 255.0f; + rgbf[1] = (float) rgb[1] / 255.0f; + rgbf[2] = (float) rgb[2] / 255.0f; + + return FTOCHAR(dot_v3v3(luma_coefficients, rgbf)); +} + /*********************** Threaded display buffer transform routines *************************/ typedef struct DisplayBufferThread { diff --git a/source/blender/imbuf/intern/divers.c b/source/blender/imbuf/intern/divers.c index 65abf22ff2c..3417fe2fc1d 100644 --- a/source/blender/imbuf/intern/divers.c +++ b/source/blender/imbuf/intern/divers.c @@ -775,12 +775,12 @@ void IMB_color_to_bw(ImBuf *ibuf) if (rct_fl) { for (i = ibuf->x * ibuf->y; i > 0; i--, rct_fl += 4) - rct_fl[0] = rct_fl[1] = rct_fl[2] = rgb_to_grayscale(rct_fl); + rct_fl[0] = rct_fl[1] = rct_fl[2] = IMB_colormanagement_get_luminance(rct_fl); } if (rct) { for (i = ibuf->x * ibuf->y; i > 0; i--, rct += 4) - rct[0] = rct[1] = rct[2] = rgb_to_grayscale_byte(rct); + rct[0] = rct[1] = rct[2] = IMB_colormanagement_get_luminance_byte(rct); } } diff --git a/source/blender/imbuf/intern/png.c b/source/blender/imbuf/intern/png.c index 683bdabcd6c..f3ff6659e1c 100644 --- a/source/blender/imbuf/intern/png.c +++ b/source/blender/imbuf/intern/png.c @@ -312,7 +312,7 @@ int imb_savepng(struct ImBuf *ibuf, const char *name, int flags) rgb[0] = chanel_colormanage_cb(from_straight[0]); rgb[1] = chanel_colormanage_cb(from_straight[1]); rgb[2] = chanel_colormanage_cb(from_straight[2]); - to16[0] = ftoshort(rgb_to_bw(rgb)); + to16[0] = ftoshort(IMB_colormanagement_get_luminance(rgb)); to16++; from_float += 4; } } @@ -321,7 +321,7 @@ int imb_savepng(struct ImBuf *ibuf, const char *name, int flags) rgb[0] = chanel_colormanage_cb(from_float[0]); rgb[1] = chanel_colormanage_cb(from_float[1]); rgb[2] = chanel_colormanage_cb(from_float[2]); - to16[0] = ftoshort(rgb_to_bw(rgb)); + to16[0] = ftoshort(IMB_colormanagement_get_luminance(rgb)); to16++; from_float += 3; } } diff --git a/source/blender/nodes/shader/nodes/node_shader_valToRgb.c b/source/blender/nodes/shader/nodes/node_shader_valToRgb.c index fa5d9b43ee7..72942cce9c5 100644 --- a/source/blender/nodes/shader/nodes/node_shader_valToRgb.c +++ b/source/blender/nodes/shader/nodes/node_shader_valToRgb.c @@ -31,6 +31,7 @@ #include "node_shader_util.h" +#include "IMB_colormanagement.h" /* **************** VALTORGB ******************** */ static bNodeSocketTemplate sh_node_valtorgb_in[] = { @@ -106,7 +107,7 @@ static void node_shader_exec_rgbtobw(void *UNUSED(data), int UNUSED(thread), bNo float col[3]; nodestack_get_vec(col, SOCK_VECTOR, in[0]); - out[0]->vec[0] = rgb_to_bw(col); + out[0]->vec[0] = IMB_colormanagement_get_luminance(col); } static int gpu_shader_rgbtobw(GPUMaterial *mat, bNode *UNUSED(node), bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out) diff --git a/source/blender/nodes/texture/nodes/node_texture_valToRgb.c b/source/blender/nodes/texture/nodes/node_texture_valToRgb.c index 7f8adab2da5..a49d82d27a9 100644 --- a/source/blender/nodes/texture/nodes/node_texture_valToRgb.c +++ b/source/blender/nodes/texture/nodes/node_texture_valToRgb.c @@ -32,6 +32,7 @@ #include "node_texture_util.h" #include "NOD_texture.h" +#include "IMB_colormanagement.h" /* **************** VALTORGB ******************** */ static bNodeSocketTemplate valtorgb_in[] = { @@ -91,7 +92,7 @@ static void rgbtobw_valuefn(float *out, TexParams *p, bNode *UNUSED(node), bNode { float cin[4]; tex_input_rgba(cin, in[0], p, thread); - *out = rgb_to_bw(cin); + *out = IMB_colormanagement_get_luminance(cin); } static void rgbtobw_exec(void *data, int UNUSED(thread), bNode *node, bNodeExecData *execdata, bNodeStack **in, bNodeStack **out) diff --git a/source/blender/render/intern/source/render_texture.c b/source/blender/render/intern/source/render_texture.c index 17039bca63a..531c31848a6 100644 --- a/source/blender/render/intern/source/render_texture.c +++ b/source/blender/render/intern/source/render_texture.c @@ -1982,14 +1982,14 @@ static int ntap_bump_compute(NTapBump *ntap_bump, ShadeInput *shi, MTex *mtex, T /* use texres for the center sample, set rgbnor */ rgbnor = multitex_mtex(shi, mtex, STll, dxt, dyt, texres, pool, skip_load_image); - Hll = (fromrgb) ? rgb_to_grayscale(&texres->tr) : texres->tin; + Hll = (fromrgb) ? IMB_colormanagement_get_luminance(&texres->tr) : texres->tin; /* use ttexr for the other 2 taps */ multitex_mtex(shi, mtex, STlr, dxt, dyt, &ttexr, pool, skip_load_image); - Hlr = (fromrgb) ? rgb_to_grayscale(&ttexr.tr) : ttexr.tin; + Hlr = (fromrgb) ? IMB_colormanagement_get_luminance(&ttexr.tr) : ttexr.tin; multitex_mtex(shi, mtex, STul, dxt, dyt, &ttexr, pool, skip_load_image); - Hul = (fromrgb) ? rgb_to_grayscale(&ttexr.tr) : ttexr.tin; + Hul = (fromrgb) ? IMB_colormanagement_get_luminance(&ttexr.tr) : ttexr.tin; dHdx = Hscale*(Hlr - Hll); dHdy = Hscale*(Hul - Hll); @@ -2020,17 +2020,17 @@ static int ntap_bump_compute(NTapBump *ntap_bump, ShadeInput *shi, MTex *mtex, T /* use texres for the center sample, set rgbnor */ rgbnor = multitex_mtex(shi, mtex, STc, dxt, dyt, texres, pool, skip_load_image); - /* Hc = (fromrgb) ? rgb_to_grayscale(&texres->tr) : texres->tin; */ /* UNUSED */ + /* Hc = (fromrgb) ? IMB_colormanagement_get_luminance(&texres->tr) : texres->tin; */ /* UNUSED */ /* use ttexr for the other taps */ multitex_mtex(shi, mtex, STl, dxt, dyt, &ttexr, pool, skip_load_image); - Hl = (fromrgb) ? rgb_to_grayscale(&ttexr.tr) : ttexr.tin; + Hl = (fromrgb) ? IMB_colormanagement_get_luminance(&ttexr.tr) : ttexr.tin; multitex_mtex(shi, mtex, STr, dxt, dyt, &ttexr, pool, skip_load_image); - Hr = (fromrgb) ? rgb_to_grayscale(&ttexr.tr) : ttexr.tin; + Hr = (fromrgb) ? IMB_colormanagement_get_luminance(&ttexr.tr) : ttexr.tin; multitex_mtex(shi, mtex, STd, dxt, dyt, &ttexr, pool, skip_load_image); - Hd = (fromrgb) ? rgb_to_grayscale(&ttexr.tr) : ttexr.tin; + Hd = (fromrgb) ? IMB_colormanagement_get_luminance(&ttexr.tr) : ttexr.tin; multitex_mtex(shi, mtex, STu, dxt, dyt, &ttexr, pool, skip_load_image); - Hu = (fromrgb) ? rgb_to_grayscale(&ttexr.tr) : ttexr.tin; + Hu = (fromrgb) ? IMB_colormanagement_get_luminance(&ttexr.tr) : ttexr.tin; dHdx = Hscale*(Hr - Hl); dHdy = Hscale*(Hu - Hd); @@ -2328,7 +2328,7 @@ void do_material_tex(ShadeInput *shi, Render *re) /* texture output */ if ((rgbnor & TEX_RGB) && (mtex->texflag & MTEX_RGBTOINT)) { - texres.tin = rgb_to_grayscale(&texres.tr); + texres.tin = IMB_colormanagement_get_luminance(&texres.tr); rgbnor -= TEX_RGB; } if (mtex->texflag & MTEX_NEGATIVE) { @@ -2568,7 +2568,7 @@ void do_material_tex(ShadeInput *shi, Render *re) } if (rgbnor & TEX_RGB) { - texres.tin = rgb_to_grayscale(&texres.tr); + texres.tin = IMB_colormanagement_get_luminance(&texres.tr); } factt= (0.5f-texres.tin)*mtex->dispfac*stencilTin; facmm= 1.0f-factt; @@ -2596,7 +2596,7 @@ void do_material_tex(ShadeInput *shi, Render *re) if (rgbnor & TEX_RGB) { if (texres.talpha) texres.tin = texres.ta; - else texres.tin = rgb_to_grayscale(&texres.tr); + else texres.tin = IMB_colormanagement_get_luminance(&texres.tr); } if (mtex->mapto & MAP_REF) { @@ -2767,7 +2767,7 @@ void do_volume_tex(ShadeInput *shi, const float *xyz, int mapto_flag, float col_ /* texture output */ if ((rgbnor & TEX_RGB) && (mtex->texflag & MTEX_RGBTOINT)) { - texres.tin = rgb_to_grayscale(&texres.tr); + texres.tin = IMB_colormanagement_get_luminance(&texres.tr); rgbnor -= TEX_RGB; } if (mtex->texflag & MTEX_NEGATIVE) { @@ -2836,7 +2836,7 @@ void do_volume_tex(ShadeInput *shi, const float *xyz, int mapto_flag, float col_ /* convert RGB to intensity if intensity info isn't provided */ if (rgbnor & TEX_RGB) { if (texres.talpha) texres.tin = texres.ta; - else texres.tin = rgb_to_grayscale(&texres.tr); + else texres.tin = IMB_colormanagement_get_luminance(&texres.tr); } if ((mapto_flag & MAP_EMISSION) && (mtex->mapto & MAP_EMISSION)) { @@ -2934,7 +2934,7 @@ void do_halo_tex(HaloRen *har, float xn, float yn, float col_r[4]) /* texture output */ if (rgb && (mtex->texflag & MTEX_RGBTOINT)) { - texres.tin = rgb_to_bw(&texres.tr); + texres.tin = IMB_colormanagement_get_luminance(&texres.tr); rgb= 0; } if (mtex->texflag & MTEX_NEGATIVE) { @@ -3006,7 +3006,7 @@ void do_halo_tex(HaloRen *har, float xn, float yn, float col_r[4]) texres.tin = texres.ta; } else { - texres.tin = rgb_to_bw(&texres.tr); + texres.tin = IMB_colormanagement_get_luminance(&texres.tr); } } @@ -3140,7 +3140,7 @@ void do_sky_tex(const float rco[3], float lo[3], const float dxyview[2], float h /* texture output */ if (rgb && (mtex->texflag & MTEX_RGBTOINT)) { - texres.tin = rgb_to_bw(&texres.tr); + texres.tin = IMB_colormanagement_get_luminance(&texres.tr); rgb= 0; } if (mtex->texflag & MTEX_NEGATIVE) { @@ -3215,7 +3215,7 @@ void do_sky_tex(const float rco[3], float lo[3], const float dxyview[2], float h } } if (mtex->mapto & WOMAP_BLEND) { - if (rgb) texres.tin = rgb_to_bw(&texres.tr); + if (rgb) texres.tin = IMB_colormanagement_get_luminance(&texres.tr); *blend= texture_value_blend(mtex->def_var, *blend, texres.tin, mtex->blendfac, mtex->blendtype); } @@ -3356,7 +3356,7 @@ void do_lamp_tex(LampRen *la, const float lavec[3], ShadeInput *shi, float col_r /* texture output */ if (rgb && (mtex->texflag & MTEX_RGBTOINT)) { - texres.tin = rgb_to_bw(&texres.tr); + texres.tin = IMB_colormanagement_get_luminance(&texres.tr); rgb= 0; } if (mtex->texflag & MTEX_NEGATIVE) { @@ -3455,7 +3455,7 @@ int externtex(MTex *mtex, const float vec[3], float *tin, float *tr, float *tg, rgb = multitex(tex, texvec, dxt, dyt, 0, &texr, thread, mtex->which_output, pool, skip_load_image); if (rgb) { - texr.tin = rgb_to_bw(&texr.tr); + texr.tin = IMB_colormanagement_get_luminance(&texr.tr); } else { texr.tr= mtex->r; diff --git a/source/blender/render/intern/source/shadeoutput.c b/source/blender/render/intern/source/shadeoutput.c index 63a97a04b39..586345d1eb4 100644 --- a/source/blender/render/intern/source/shadeoutput.c +++ b/source/blender/render/intern/source/shadeoutput.c @@ -55,6 +55,8 @@ #include "shading.h" /* own include */ +#include "IMB_colormanagement.h" + /* could enable at some point but for now there are far too many conversions */ #ifdef __GNUC__ # pragma GCC diagnostic ignored "-Wdouble-promotion" @@ -948,7 +950,7 @@ static void ramp_diffuse_result(float *diff, ShadeInput *shi) if (ma->ramp_col) { if (ma->rampin_col==MA_RAMP_IN_RESULT) { - float fac = rgb_to_grayscale(diff); + float fac = IMB_colormanagement_get_luminance(diff); do_colorband(ma->ramp_col, fac, col); /* blending method */ @@ -980,7 +982,7 @@ static void add_to_diffuse(float *diff, ShadeInput *shi, float is, float r, floa /* input */ switch (ma->rampin_col) { case MA_RAMP_IN_ENERGY: - /* should use 'rgb_to_grayscale' but we only have a vector version */ + /* should use 'IMB_colormanagement_get_luminance' but we only have a vector version */ fac= 0.3f*r + 0.58f*g + 0.12f*b; break; case MA_RAMP_IN_SHADER: @@ -1023,7 +1025,7 @@ static void ramp_spec_result(float spec_col[3], ShadeInput *shi) if (ma->ramp_spec && (ma->rampin_spec==MA_RAMP_IN_RESULT)) { float col[4]; - float fac = rgb_to_grayscale(spec_col); + float fac = IMB_colormanagement_get_luminance(spec_col); do_colorband(ma->ramp_spec, fac, col); @@ -1619,10 +1621,10 @@ static void shade_lamp_loop_only_shadow(ShadeInput *shi, ShadeResult *shr) if (shi->mat->shadowonly_flag == MA_SO_OLD) { /* Old "Shadows Only" */ - accum+= (1.0f-visifac) + (visifac)*rgb_to_grayscale(shadfac)*shadfac[3]; + accum+= (1.0f-visifac) + (visifac)*IMB_colormanagement_get_luminance(shadfac)*shadfac[3]; } else { - shaded += rgb_to_grayscale(shadfac)*shadfac[3] * visifac * lar->energy; + shaded += IMB_colormanagement_get_luminance(shadfac)*shadfac[3] * visifac * lar->energy; if (shi->mat->shadowonly_flag == MA_SO_SHADOW) { lightness += visifac * lar->energy; @@ -1671,26 +1673,26 @@ static void shade_lamp_loop_only_shadow(ShadeInput *shi, ShadeResult *shr) if (R.wrld.aomix==WO_AOADD) { if (shi->mat->shadowonly_flag == MA_SO_OLD) { - f= f*(1.0f - rgb_to_grayscale(shi->ao)); + f= f*(1.0f - IMB_colormanagement_get_luminance(shi->ao)); shr->alpha= (shr->alpha + f)*f; } else { - shr->alpha -= f*rgb_to_grayscale(shi->ao); + shr->alpha -= f*IMB_colormanagement_get_luminance(shi->ao); if (shr->alpha<0.0f) shr->alpha=0.0f; } } else /* AO Multiply */ - shr->alpha= (1.0f - f)*shr->alpha + f*(1.0f - (1.0f - shr->alpha)*rgb_to_grayscale(shi->ao)); + shr->alpha= (1.0f - f)*shr->alpha + f*(1.0f - (1.0f - shr->alpha)*IMB_colormanagement_get_luminance(shi->ao)); } if (R.wrld.mode & WO_ENV_LIGHT) { if (shi->mat->shadowonly_flag == MA_SO_OLD) { - f= R.wrld.ao_env_energy*shi->amb*(1.0f - rgb_to_grayscale(shi->env)); + f= R.wrld.ao_env_energy*shi->amb*(1.0f - IMB_colormanagement_get_luminance(shi->env)); shr->alpha= (shr->alpha + f)*f; } else { f= R.wrld.ao_env_energy*shi->amb; - shr->alpha -= f*rgb_to_grayscale(shi->env); + shr->alpha -= f*IMB_colormanagement_get_luminance(shi->env); if (shr->alpha<0.0f) shr->alpha=0.0f; } } diff --git a/source/blender/render/intern/source/volumetric.c b/source/blender/render/intern/source/volumetric.c index d5c4c407bf6..2c0917243a3 100644 --- a/source/blender/render/intern/source/volumetric.c +++ b/source/blender/render/intern/source/volumetric.c @@ -41,6 +41,8 @@ #include "RE_shader_ext.h" +#include "IMB_colormanagement.h" + #include "DNA_material_types.h" #include "DNA_group_types.h" #include "DNA_lamp_types.h" @@ -504,7 +506,7 @@ static void vol_shade_one_lamp(struct ShadeInput *shi, const float co[3], const if (shi->mat->vol.shadeflag & MA_VOL_RECV_EXT_SHADOW) { mul_v3_fl(lacol, vol_get_shadow(shi, lar, co)); - if (rgb_to_luma_y(lacol) < 0.001f) return; + if (IMB_colormanagement_get_luminance(lacol) < 0.001f) return; } /* find minimum of volume bounds, or lamp coord */ @@ -538,7 +540,7 @@ static void vol_shade_one_lamp(struct ShadeInput *shi, const float co[3], const } } - if (rgb_to_luma_y(lacol) < 0.001f) return; + if (IMB_colormanagement_get_luminance(lacol) < 0.001f) return; normalize_v3(lv); p = vol_get_phasefunc(shi, shi->mat->vol.asymmetry, view, lv); @@ -620,7 +622,7 @@ static void volumeintegrate(struct ShadeInput *shi, float col[4], const float co if (t0 > t1 * 0.25f) { /* only use depth cutoff after we've traced a little way into the volume */ - if (rgb_to_luma_y(tr) < shi->mat->vol.depth_cutoff) break; + if (IMB_colormanagement_get_luminance(tr) < shi->mat->vol.depth_cutoff) break; } vol_get_emission(shi, emit_col, p); @@ -649,7 +651,7 @@ static void volumeintegrate(struct ShadeInput *shi, float col[4], const float co add_v3_v3(col, radiance); /* alpha <-- transmission luminance */ - col[3] = 1.0f - rgb_to_luma_y(tr); + col[3] = 1.0f - IMB_colormanagement_get_luminance(tr); } /* the main entry point for volume shading */ @@ -790,7 +792,7 @@ void shade_volume_shadow(struct ShadeInput *shi, struct ShadeResult *shr, struct copy_v3_v3(shr->combined, tr); - shr->combined[3] = 1.0f - rgb_to_luma_y(tr); + shr->combined[3] = 1.0f - IMB_colormanagement_get_luminance(tr); shr->alpha = shr->combined[3]; } -- cgit v1.2.3