I'm not working on directly with the Gimp source, but I did fix the
bug with Preserve Luminosity. The offending code in color-balance.c
lines 182-187:

          if (cb->preserve_luminosity)
            {
              gimp_rgb_to_hsl_int (&r_n, &g_n, &b_n);
              b_n = gimp_rgb_to_l_int (r, g, b);
              gimp_hsl_to_rgb_int (&r_n, &g_n, &b_n);
            }

I'm working in Objective-C, so my code isn't copy paste ready.
Basically, if cb-> preserve_luminosity, re-scale the slider values so
that the lightness of R1+G1+B1 = the lightness of the base pixel
R+G+B, now use these modified slider values when changing the pixel
values:

-(NSBitmapImageRep *)colorAdjustedImage:(NSBitmapImageRep *)baseImageRep
                                          
preserveLumenance:(BOOL)preserveLumenance
                                                          
colorTone:(LSColorTone)colorTone
{
    int w,h,x,y = 0;
    unsigned char *srcData, *p1;
    int n, red, green, blue;
        double shadowScale, midScale, highlightScale;

        shadowScale = midScale = highlightScale = 1.0;
        
        if (preserveLumenance == YES) {
                shadowScale =    1.0 / ( ([_shadowToneRed doubleValue] / 100.0) 
+
([_shadowToneGreen doubleValue] / 100.0) + ([_shadowToneBlue
doubleValue] / 100.0) );
                midScale =       1.0 / ( (   [_midToneRed doubleValue] / 100.0) 
+ ( 
 [_midToneGreen doubleValue] / 100.0) + (   [_midToneBlue doubleValue]
/ 100.0) );
                highlightScale = 1.0 / ( (  [_highToneRed doubleValue] / 100.0) 
+ ( 
[_highToneGreen doubleValue] / 100.0) + (  [_highToneBlue doubleValue]
/ 100.0) );
                
                shadowScale = shadowScale < -1/3 ? -1/3 : shadowScale;
                shadowScale = shadowScale > 1/3 ? 1/3 : shadowScale;
                shadowScale = shadowScale == 0 ? 1.0 : shadowScale;

                midScale = midScale < -1/3 ? -1/3 : midScale;
                midScale = midScale > 1/3 ? 1/3 : midScale;
                midScale = midScale == 0 ? 1.0 : midScale;

                highlightScale = highlightScale < -1/3 ? -1/3 : highlightScale;
                highlightScale = highlightScale > 1/3 ? 1/3 : highlightScale;
                highlightScale = highlightScale == 0 ? 1.0 : highlightScale;
        }
        
    // update the tonal range values from any changes in the UI
    [self applyToneToColorAdjustment:colorToneMatrix];

    // now set the slider values for the different tonal ranges
    cyan_red[LSColorToneShadow]      = shadowScale * [_shadowToneRed
doubleValue];
    magenta_green[LSColorToneShadow] = shadowScale * [_shadowToneGreen
doubleValue];
    yellow_blue[LSColorToneShadow]   = shadowScale * [_shadowToneBlue
doubleValue];

    cyan_red[LSColorToneMidtone]      = midScale * [_midToneRed doubleValue];
    magenta_green[LSColorToneMidtone] = midScale * [_midToneGreen doubleValue];
    yellow_blue[LSColorToneMidtone]   = midScale * [_midToneBlue doubleValue];

    cyan_red[LSColorToneHighlights]      = highlightScale *
[_highToneRed doubleValue];
    magenta_green[LSColorToneHighlights] = highlightScale *
[_highToneGreen doubleValue];
    yellow_blue[LSColorToneHighlights]   = highlightScale *
[_highToneBlue doubleValue];


    // create the color lookup tables for the current slider values
and tonal ranges
    [self color_balance_create_lookup_tables];

    // change the pixels
    w = [baseImageRep pixelsWide];
    h = [baseImageRep pixelsHigh];

    srcData = [baseImageRep bitmapData];
    n = [baseImageRep bitsPerPixel] / 8;

    for ( y = 0 ; y < h ; y++) {
        for ( x = 0 ; x < w ; x++) {
            int r, g, b = 0;
            p1 = srcData + n * (y * w + x);

            // get the pixels color components
            r = p1[0];
            g = p1[1];
            b = p1[2];

            // lookup the new color values in the LUTS contructed from
slider values and tonal selections
            red = r_lookup[r];
            green = g_lookup[g];
            blue = b_lookup[b];

            // preserve lumenance if requested to do so
                        // original code from adapted from the Gimp, which does 
not work correctly.
//            if (preserveLumenance == YES) {
//                gimp_rgb_to_hsl_int (&red, &green, &blue); // red
becomes hue, green becomes saturation, blue becomes lightness
//                blue = gimp_rgb_to_l_int (r, g, b); // get the
lightness, set it
//                gimp_hsl_to_rgb_int (&red, &green, &blue);
//            }

            // stomp the pixel component values
            p1[0] = red;
            p1[1] = green;
            p1[2] = blue;
        }
    }

    return baseImageRep;
}
_______________________________________________
Gimp-developer mailing list
Gimp-developer@lists.XCF.Berkeley.EDU
https://lists.XCF.Berkeley.EDU/mailman/listinfo/gimp-developer

Reply via email to