1

I am trying to adjust the contrast of a BufferedImage using RescaleOp. Changing the contrast of an image without alpha channel works fine, but for images with an alpha channel it creates a yellow tint; what am I doing wrong?

To verify that the issue arises because of the alpha channel I loaded the original file into Gimp, added an alpha channel and then exported the file to another png image, which yielded the second image I try to change the contrast of.

public class Test
{

    public static void main(String[] args) throws IOException
    {
        changeContrast(new File("contrast/base_image_without_alpha.png"), 
                       new File("contrast/output_without_alpha.png"));
        changeContrast(new File("contrast/base_image_with_alpha.png"), 
                       new File("contrast/output_with_alpha.png"));
    }

    private static void changeContrast(File sourceImageFile, File targetImageFile) throws IOException
    {
        BufferedImage image = ImageIO.read(sourceImageFile);

        RescaleOp rescaleOp = new RescaleOp(1.8f, 0, null);
        rescaleOp.filter(image, image);

        ImageIO.write(image, "png", targetImageFile);
    }

}

base_image: base_image

output_without_alpha: output_without_alpha

output_with_alpha: output_with_alpha

images to try for yourself: http://www.mediafire.com/file/15r1zh27ccjj1w1/contrast.zip

MCVE to try it out for yourself (easily)

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import java.net.*;

public class TestAlphaRescale {

    public static void main(String[] args) throws Exception {

        File f = new File("output_without_alpha.png");
        URL url = new URL("https://i.stack.imgur.com/S3m9W.jpg");
        Desktop desktop = Desktop.getDesktop();
        changeContrast(url,f);
        desktop.open(f);
        desktop.browse(url.toURI());

        f = new File("output_with_alpha.png");
        // with alpha
        url = new URL("https://i.stack.imgur.com/memI0.png");
        changeContrast(url,f);
        desktop.open(f);
        desktop.browse(url.toURI());
    }

    private static void changeContrast(URL sourceImageURL, File targetImageFile) throws IOException {
        BufferedImage image = ImageIO.read(sourceImageURL);

        RescaleOp rescaleOp = new RescaleOp(1.8f, 0, null);
        rescaleOp.filter(image, image);

        ImageIO.write(image, "png", targetImageFile);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
domisum
  • 531
  • 1
  • 7
  • 12
  • 1
    Could you have an issue with RGBA vs ARGB? From the Javadoc _"The number of sets of scaling constants may be one, in which case the same constants are applied to all color **(but not alpha)** components"_. If your image was ARGB but interpreted by the Java code as RGBA, then the scaling you applied would increase the A, R and G by a factor of 1.8, leaving the blue alone, which would have exactly the effect you see. Not posting as an answer because, though I understand color well as a photographer and Lightroom user, I'm no expert in AWT graphics. – Jim Garrison Jan 25 '18 at 23:00
  • 1
    I ran your code on my system (Java 9, Windows 7.1, Eclipse Oxygen) and was ***unable to reproduce your issue***. The `output_with_alpha.png` file looks identical to the `output_without_alpha.png` file. Something else is happening on your system. – Jim Garrison Jan 26 '18 at 00:01
  • Do you see the same behavior using the source code I edited into the question? It is a [mcve] - please post one in future. It uses your original 'no alpha' image as well as an PNG with alpha of chess pieces from [this question](https://stackoverflow.com/q/19209650/418556). Like @JimGarrison, I did not see the problem here. – Andrew Thompson Jan 26 '18 at 05:11
  • 1
    @AndrewThompson I ran the code you provided on my system, and experienced the same issue with the alpha-chess pieces: https://i.imgur.com/KY2cHcH.png seems like this Is an issue specific to my system. I will try to debug this further and report back if I find a solution. – domisum Jan 26 '18 at 16:36
  • Glad you got it sorted. Pleas post an MCVE in future, as it makes things easier for others to test and results in faster answers. Oh, and please give test classes specific names like `TestAlphaRescale` rather than just `Test`. I reserve just one package in my IDE for testing other people's code, and short generic names cause clashes. – Andrew Thompson Jan 27 '18 at 00:47

1 Answers1

1

I fixed the issue by updating to Java 9.

This seems to be a bug in Java 8, that is now fixed in Java 9. I originally used JDK1.8.0_144 and received the yellow tint, I now use JDK-9.0.4 and no longer have the issue. Now the image looks the same with and without alpha channel.

domisum
  • 531
  • 1
  • 7
  • 12