add rgb_to_luma_y(), was being done inline in many places.
authorCampbell Barton <ideasman42@gmail.com>
Wed, 13 Jun 2012 15:05:42 +0000 (15:05 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Wed, 13 Jun 2012 15:05:42 +0000 (15:05 +0000)
source/blender/blenlib/BLI_math_color.h
source/blender/blenlib/intern/math_color.c
source/blender/compositor/operations/COM_ColorCorrectionOperation.cpp
source/blender/compositor/operations/COM_GlareThresholdOperation.cpp
source/blender/compositor/operations/COM_TonemapOperation.cpp
source/blender/nodes/composite/nodes/node_composite_glare.c
source/blender/nodes/composite/nodes/node_composite_tonemap.c
source/blender/render/intern/source/volumetric.c

index 44f54c4112909cf072e6586df56837c4a4d92db3..24c237f1130ad4a72d01c7c10c8ef2930672ea42 100644 (file)
@@ -78,6 +78,8 @@ float rgb_to_grayscale(const float rgb[3]);
 unsigned char rgb_to_grayscale_byte(const unsigned char rgb[3]);
 float rgb_to_luma(const float rgb[3]);
 unsigned char rgb_to_luma_byte(const unsigned char rgb[3]);
+float rgb_to_luma_y(const float rgb[3]);
+float rgb_to_luma_rec709_byte(const unsigned char rgb[3]);
 
 /**************** Profile Transformations *****************/
 
index 5b034bd287257871d88ad31fc6af36623b4ebf4d..f15296714923b4655ae427180e96250fd06d2403 100644 (file)
@@ -516,6 +516,17 @@ unsigned char rgb_to_luma_byte(const unsigned char rgb[3])
        return (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 */
+float rgb_to_luma_y(const float rgb[3])
+{
+       return 0.212671f * rgb[0] + 0.71516f * rgb[1] + 0.072169f * rgb[2];
+}
+
 /* ********************************* lift/gamma/gain / ASC-CDL conversion ********************************* */
 
 void lift_gamma_gain_to_asc_cdl(float *lift, float *gamma, float *gain, float *offset, float *slope, float *power)
@@ -648,9 +659,9 @@ void rgb_to_xyz(float r, float g, float b, float *x, float *y, float *z)
        g = inverse_srgb_companding(g) * 100.0f;
        b = inverse_srgb_companding(b) * 100.0f;
 
-       *x = r * 0.4124 + g * 0.3576 + b * 0.1805;
-       *y = r * 0.2126 + g * 0.7152 + b * 0.0722;
-       *z = r * 0.0193 + g * 0.1192 + b * 0.9505;
+       *x = r * 0.412453f + g * 0.357580f + b * 0.180423f;
+       *y = r * 0.212671f + g * 0.715160f + b * 0.072169f;
+       *z = r * 0.019334f + g * 0.119193f + b * 0.950227f;
 }
 
 static float xyz_to_lab_component(float v)
index 82a71f6a7a8f797ee6b7201c7280865def2bbe44..8ff58be7980c4fb164d38d7b2076f7f148caf2f0 100644 (file)
@@ -89,12 +89,13 @@ void ColorCorrectionOperation::executePixel(float *output, float x, float y, Pix
        gain *= (levelShadows*this->data->shadows.gain)+(levelMidtones*this->data->midtones.gain)+(levelHighlights*this->data->highlights.gain);
        lift += (levelShadows*this->data->shadows.lift)+(levelMidtones*this->data->midtones.lift)+(levelHighlights*this->data->highlights.lift);
        
+       float invgamma = 1.0f / gamma;
+       float luma = rgb_to_luma_y(inputImageColor);
+
        r = inputImageColor[0];
        g = inputImageColor[1];
        b = inputImageColor[2];
-       
-       float invgamma = 1.0f / gamma;
-       float luma = 0.2126f * r + 0.7152f * g + 0.0722f * b;
+
        r = (luma + saturation * (r - luma));
        g = (luma + saturation * (g - luma));
        b = (luma + saturation * (b - luma));
index 4ca5f575cfe92f1124927cc801ad8e14073f1b17..ea76e7551ad290eacbdb6b02bc3c1c4f3ff96836 100644 (file)
@@ -37,7 +37,7 @@ void GlareThresholdOperation::initExecution()
 void GlareThresholdOperation::executePixel(float *color, float x, float y, PixelSampler sampler, MemoryBuffer *inputBuffers[])
 {
        this->inputProgram->read(color, x, y, sampler, inputBuffers);
-       if ((0.212671f * color[0] + 0.71516f * color[1] + 0.072169f * color[2]) >= threshold) {
+       if (rgb_to_luma_y(color) >= threshold) {
                color[0] -= threshold, color[1] -= threshold, color[2] -= threshold;
                color[0] = MAX2(color[0], 0.0f);
                color[1] = MAX2(color[1], 0.0f);
index 9a02d6a58ca854564197247afa1775cc3fef8f86..b281fb938fda76d73c4ca70b2d2b028e8fab9815 100644 (file)
@@ -75,7 +75,7 @@ void PhotoreceptorTonemapOperation::executePixel(float *color, int x, int y, Mem
        float output[4];
        this->imageReader->read(output, x, y, inputBuffers, NULL);
 
-       const float L = 0.212671f * output[0] + 0.71516f * output[1] + 0.072169f * output[2];
+       const float L = rgb_to_luma_y(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);
@@ -133,7 +133,7 @@ void *TonemapOperation::initializeTileData(rcti *rect, MemoryBuffer **memoryBuff
                float Lav = 0.f;
                float cav[4] = {0.0f,0.0f,0.0f,0.0f};
                while (p--) {
-                       float L = 0.212671f * bc[0] + 0.71516f * bc[1] + 0.072169f * bc[2];
+                       float L = rgb_to_luma_y(bc);
                        Lav += L;
                        add_v3_v3(cav, bc);
                        lsum += logf(MAX2(L, 0.0f) + 1e-5f);
index 1e32d6f046194a3ee121c99536121f272005432c..9b1505e933326403994a57f110d8375b19ed0e21 100644 (file)
@@ -107,7 +107,7 @@ static CompBuf* BTP(CompBuf* src, float threshold, int scaledown)
        float* cr = bsrc->rect;
        for (y=0; y<bsrc->y; ++y)
                for (x=0; x<bsrc->x; ++x, cr+=4) {
-                       if ((0.212671f*cr[0] + 0.71516f*cr[1] + 0.072169f*cr[2]) >= threshold) {
+                       if (rgb_to_luma_y(cr) >= threshold) {
                                cr[0] -= threshold, cr[1] -= threshold, cr[2] -= threshold;
                                cr[0] = MAX2(cr[0], 0.f);
                                cr[1] = MAX2(cr[1], 0.f);
index a9d071ececc31d943df7e32a8c90e40d7aacbbaa..6196825c9b3761e8c60d3b6d3ed7b723de222b37 100644 (file)
@@ -51,7 +51,7 @@ static float avgLogLum(CompBuf *src, float* auto_key, float* Lav, float* Cav)
        const float sc = 1.f/(src->x*src->y);
        *Lav = 0.f;
        while (p--) {
-               float L = 0.212671f*bc[0][0] + 0.71516f*bc[0][1] + 0.072169f*bc[0][2];
+               float L = rgb_to_luma_y(bc[0]);
                *Lav += L;
                fRGB_add(Cav, bc[0]);
                lsum += (float)log((double)MAX2(L, 0.0) + 1e-5);
@@ -86,7 +86,7 @@ static void tonemap(NodeTonemap* ntm, CompBuf* dst, CompBuf* src)
                        fRGB* sp = (fRGB*)&src->rect[y*src->x*src->type];
                        fRGB* dp = (fRGB*)&dst->rect[y*src->x*src->type];
                        for (x=0; x<src->x; ++x) {
-                               const float L = 0.212671f*sp[x][0] + 0.71516f*sp[x][1] + 0.072169f*sp[x][2];
+                               const float L = rgb_to_luma_y(sp[x]);
                                float I_l = sp[x][0] + ic*(L - sp[x][0]);
                                float I_g = Cav[0] + ic*(Lav - Cav[0]);
                                float I_a = I_l + ia*(I_g - I_l);
index 75748696a72dc32dd79564064273269ce8bd582a..e4c4e905aa6296abed365def2d74d76a641884ea 100644 (file)
 extern struct Render R;
 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
 
-/* luminance rec. 709 */
-BLI_INLINE float luminance(const float col[3])
-{
-       return (0.212671f * col[0] + 0.71516f * col[1] + 0.072169f * col[2]);
-}
-
 /* tracing */
 static float vol_get_shadow(ShadeInput *shi, LampRen *lar, const float co[3])
 {
@@ -502,7 +496,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 (luminance(lacol) < 0.001f) return;
+                       if (rgb_to_luma_y(lacol) < 0.001f) return;
                }
                
                /* find minimum of volume bounds, or lamp coord */
@@ -536,7 +530,7 @@ static void vol_shade_one_lamp(struct ShadeInput *shi, const float co[3], const
                }
        }
        
-       if (luminance(lacol) < 0.001f) return;
+       if (rgb_to_luma_y(lacol) < 0.001f) return;
        
        normalize_v3(lv);
        p = vol_get_phasefunc(shi, shi->mat->vol.asymmetry, view, lv);
@@ -618,7 +612,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 (luminance(tr) < shi->mat->vol.depth_cutoff) break;
+                               if (rgb_to_luma_y(tr) < shi->mat->vol.depth_cutoff) break;
                        }
                        
                        vol_get_emission(shi, emit_col, p);
@@ -647,7 +641,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 - luminance(tr);
+       col[3] = 1.0f - rgb_to_luma_y(tr);
 }
 
 /* the main entry point for volume shading */
@@ -787,7 +781,7 @@ void shade_volume_shadow(struct ShadeInput *shi, struct ShadeResult *shr, struct
 
        
        copy_v3_v3(shr->combined, tr);
-       shr->combined[3] = 1.0f - luminance(tr);
+       shr->combined[3] = 1.0f - rgb_to_luma_y(tr);
        shr->alpha = shr->combined[3];
 }