Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By jim829

Thread: Image Brightness

  1. #1
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    157
    Rep Power
    2

    Default Image Brightness

    Is there any class which lets me set brightness to an image, preferably BufferedImage but it doesn't really matter, and then whenever desired remove the brightness. I have tried using RescaleOp, but it doesn't satisfy the latter criterion. When I have brightened the image, I can't seem to get it back to normal.

    Java Code:
    RescaleOp op;
    		
    if(image should be brightened){
    	float scales[] = {2f, 2f, 2f, 1f}, offsets[] = new float[4];
    		
    	op = new RescaleOp(scales, offsets, null);
    	op.filter(getImage(), getImage());
    }else{
    	float scales[] = {1f, 1f, 1f, 1f}, offsets[] = new float[4];
    			
    	op = new RescaleOp(scales, offsets, null);
    	op.filter(getImage(), getImage());
    }
    Thanks in advance!
    Last edited by Zelaine; 08-01-2014 at 04:09 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: Image Brightness

    I did this once but have forgotten how. As a quick solution, just keep the original
    image around. When you want to increase or decrease brightness, use the original
    image. I suspect that using the changed image may introduce subtle errors as you
    keep changing it. I know this happens in other graphics applications. Especially when
    using floats or doubles to calculate changes.

    The downside is having to keep twice as many images around. Others may offer
    a more efficient solution.

    Edit: Upon further reflection I think RescaleOp should work. Please include sufficient code
    that I can compile the program. I will provide the image.

    Regards,
    Jim
    Last edited by jim829; 08-01-2014 at 04:36 PM.
    gimbal2 likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default Re: Image Brightness

    For an real 'el sicko' test, use an image that is all black (RGB == (0,0,0)) and brighten it; a real black body, heated up to a temperature of 6500K shows up as bright white; see what your brighten( ... ) method does.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: Image Brightness

    Ok, I read the class documentation and clearly your scale values are incorrect (as Jos has already alluded to).
    Here's a hint. A scale value of 1 doesn't do anything. And as far as I was able to tell, my initial assumption was correct.
    I simply maintain a cummulative scale value as an int then convert it to float prior to scaling. Also, remember that if you
    increase something by 1/2 you need to decrease that "something" by 1/3 to get back to the original value.

    But I did not do it that way as I could not get satisfactory results altering an altered image. I simply kept altering the
    original to maintain consistency in the floating point calculations.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    157
    Rep Power
    2

    Default Re: Image Brightness

    Hi!

    I tried altering the image's values back to normal so that the image would not be bright anymore. The values are correct, at least to my understanding, and the image is not bright, but the colors seem to have melted together.

    Before
    Image Brightness-before.png

    After
    Image Brightness-after.png

    So I instead kept the original image around and applied a new bright image whenever I wanted to brighten something. I am however worried how I will accomplish that with animations. A brightened version of every single layer of the animation? That doesn't seem very efficient at all.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: Image Brightness

    Brightness is nothing more than altering the rgb components of the color. And when you animate you repaint and
    usually you need to set the color in the graphics contexts. So just change the color to a dimmer version of the original.
    And I don't know what you mean by layer? Are you drawing this to animate or simply drawing a bunch of images (similar
    to how a movie projector works)? I guess I don't see the problem.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    157
    Rep Power
    2

    Default Re: Image Brightness

    These are the values, so I think I have altered them correctly.

    Java Code:
    if(should be brightened){
    	float scales[] = {2f, 2f, 2f, 1f}, offsets[] = new float[4];
    	RescaleOp op = new RescaleOp(scales, offsets, null);
    	setImage(op.filter(getImage(), null));
    }else if(should be turned back to normal){
    	float scales[] = {0.5f, 0.5f, 0.5f, 1f}, offsets[] = new float[4];
    	RescaleOp op = new RescaleOp(scales, offsets, null);
    	setImage(op.filter(getImage(), null));
    }

    I said "layer" because I couldn't come up with the right word. But you know in animations, there are several images that follow each other. If I want to brighten the animation I will need an original and brightened version of every single image of the animation, because the colors seem to melt together with RescaleOp.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default Re: Image Brightness

    Suppose a color component was 200 before brightening; after brightening it would be 400 which is too large, i.e. it clips at 255; if you 'debrighten' that value, you get 127 which definitely isn't 200.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    157
    Rep Power
    2

    Default Re: Image Brightness

    Ah, I get what you mean. But how do I tell what value the image currently has? Because I want to increase it as much as possible.

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: Image Brightness

    I am not certain how you would do that. Because, as Jos pointed out, the values for rgb
    elements are between 0 and 255 inclusive. So eventually you may get to a point where you can't
    go back because the values are pegged at their maximum or minimum values. I now believe this
    is much more difficult to do than I first thought. Here is a snippet that illustrates the problem.

    Java Code:
    public static void main(String[] args) {
          Color myColor = new Color(255, 200, 80);
          System.out.println(myColor); // displays java.awt.Color[r=255,g=200,b=80]
          myColor = myColor.brighter();
          myColor = myColor.brighter();
          myColor = myColor.darker();
          myColor = myColor.darker();
          System.out.println(myColor);//displays java.awt.Color[r=124,g=124,b=79]
    }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Setting Brightness with RescaleOp
    By Zelaine in forum New To Java
    Replies: 2
    Last Post: 07-04-2014, 12:47 AM
  2. Replies: 4
    Last Post: 08-09-2012, 06:18 PM
  3. Auto contrast and auto brightness
    By oxxxis in forum Java 2D
    Replies: 0
    Last Post: 01-21-2010, 08:32 PM
  4. (Trying to) fill HSB (Hue|Saturation|Brightness) array
    By courteous in forum New To Java
    Replies: 1
    Last Post: 01-19-2009, 07:49 AM
  5. Replies: 0
    Last Post: 01-08-2008, 05:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •