Jeremy
Jeremy

Reputation: 1364

Brighten black pixels in a BufferedImage

I'm using RescaleOp to brighten my image, my problem is, if I use black(0,0,0) and white(255,255,255) they come back exactly the same.

I can guess why, 0(black RGB values) x 1.3 (brightness factor) = 0, and the white pixels can't go higher than 255 so they remain the same, and I'm satisfied with the white pixels remaining the same.

On the other end, if I darken the image, I get desired results because the white pixels get darker, 255(white RGB values) * .8 (brightness factor) =/= 255, and the black pixels can't go lower than 0 so they remain the same.

How do I make the black pixels get lighter in the same way the white pixels get darker with a BufferedImageOp?

Here is the rescale operation

        float scale = 1.3f;//This is the aformentioned "Brightness Factor"
        RescaleOp op = new RescaleOp(new float[] {scale,scale,scale, 1}, new float[4], null);
        BufferedImage brightImage = op.filter(...);

Thanks for the current answers, but I don't see how to apply them to my problem.

Upvotes: 2

Views: 901

Answers (3)

Phil Hayward
Phil Hayward

Reputation: 1303

Options:

  • Write a custom RasterOp that extends RescaleOp to provide the behavior you want (in particular, if value = 0 && scale factor > 1, new value = 1 * scale factor = scale factor)
  • Use two RasterOps in serial. The first will have a scale factor of 1 and an offset of 1 (making everything slightly brighter). The second will be the existing RasterOp you've already defined. It's messier and probably slower than the first, but could be simpler to implement.

Upvotes: 0

Mukul Goel
Mukul Goel

Reputation: 8465

Anything multiplied by 0 would be 0. Use negative approach.

Maxvalue - (maxvalue/brightness factor).

255-255/1.3 in ur case.

Store that in an integer.

Upvotes: 1

Keppil
Keppil

Reputation: 46239

You will have to go from the max value instead. It's not obvious what the brightness factor would represent, so you will probably have to do some trial & error research here. One way could be:

int newValue = (int) (255 - (255 / 1.3));

Upvotes: 2

Related Questions