Open In App

Java.awt.image.RescaleOp Class in Java with Examples

RescaleOp is a class in java.awt.image package which implements BufferedImageOp and RasterOp interface. This class performs a pixel-by-pixel rescaling of the data in the source image by multiplying the sample values for each pixel by a scale factor and then adding an offset. The scaled sample values are clipped to the minimum/maximum representation in the destination image. This class is used in picture processing.

Syntax:



public class RescaleOp
extends Object
implements BufferedImageOp, RasterOp

Constructors: 

Methods:



Example 1: In the given example we will be setting contrast for a picture using a single scale factor and offset. The following code will reduce the brightness by 25% and darker the pixel by a factor of 3.6. 




import java.awt.image.*;
import java.net.*;
import java.awt.*;
import java.io.*;
import javax.imageio.*;
 
public class DemonRescaleop {
 
    public static void main(String[] args) throws Exception
    {
        // picking the image from the url
        URL url
            = new URL("https:// media.geeksforgeeks.org"
                      + "/wp-content/uploads/geeksforgeeks-9.png");
 
        // Reading the image from url
        Image image = ImageIO.read(url);
 
        // Setting up the scaling and
        // the offset parameters for processing
        RescaleOp rop = new RescaleOp(.75f, 3.6f, null);
 
        // applying the parameters on the image
        // by using filter() method, it takes the
        // Source and destination objects of buffered reader
        // here our destination object is null
        BufferedImage bi
            = rop.filter((BufferedImage)image, null);
        ImageIO.write(bi, "png",
                      new File("processed.png"));
    }
}

Input:

Original Image

Output:

processed.png

The method used filter(BufferedImage src, BufferedImage dst) rescales the source BufferedImage to the destination BufferedImage and returns the same. Here we have not mentioned any destination image and written null in place of it. Here we have assigned BufferedImage object with the re-scaled result. Example 2:In the given example we will be setting contrast for a picture using array of scale factor and offset. Each array will be of size 3 indicating the red, green and the blue component of each pixel.In the following code the overall brightness is increased by 45 percent, and all of the pixel colors are shifted toward the green color. The offset of 150 increases the green component of each pixel by 58.6 percent (150/256). Remember, the offset is added to the color value and must therefore be a value between 0 and 255, as opposed to the scale factor, which acts as a percentage. 




import java.awt.image.*;
import java.net.*;
import java.awt.*;
import java.io.*;
import javax.imageio.*;
public class DemonRescaleop {
 
    public static void main(String[] args) throws Exception
    {
        // picking the image from the URL
        URL url
            = new URL("https:// media.geeksforgeeks.org"
                      + "/wp-content/uploads/geeksforgeeks-9.png");
 
        // Reading the image from url
        Image image = ImageIO.read(url);
 
        // Setting up the scaling and
        // the offset parameters for processing
        float[] factors = new float[] {
 
            // RGB each value for 1 color
            1.45f, 1.45f, 1.45f
        };
 
        float[] offsets = new float[] {
            0.0f, 150.0f, 0.0f
        };
 
        RescaleOp rop
            = new RescaleOp(factors, offsets, null);
 
        // applying the parameters on the image
        // by using filter() method, it takes the
        // Source and destination objects of buffered reader
        // here our destination object is null
        BufferedImage bi
            = rop.filter((BufferedImage)image, null);
        ImageIO.write(bi, "png",
                      new File("processed.png"));
    }
}

Input:

Original Image

Output:

processed.png

The method used filter(BufferedImage src, BufferedImage dst) rescales the source BufferedImage to the destination BufferedImage and returns the same. Here we have not mentioned any destination image and written null in place of it. Here we have assigned BufferedImage object with the re-scaled result. Reference: https://docs.oracle.com/javase/9/docs/api/java/awt/image/RescaleOp.html


Article Tags :