Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Last Updated : 06 Dec, 2021

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.

  • For Rasters, rescaling operates on bands. The number of sets of scaling constants may be one, in which case the same constants are applied to all bands, or it must equal the number of Source Raster bands.
  • For BufferedImages, rescaling operates on color. The number of sets of scaling constants may be one, in which case the same constants are applied to all color components.
  • Images with an IndexColorModel cannot be rescaled.
  • If a RenderingHints object is specified in the constructor, the color rendering hint and the dithering hint may be used when color conversion is required.

Syntax:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

public class RescaleOp
extends Object
implements BufferedImageOp, RasterOp

Constructors:

  • public RescaleOp(float[] scaleFactors, float[] offsets, RenderingHints hints): Constructor with desired scaleFactors and offsets. hints can be null.
  • public RescaleOp(float scaleFactor, float offset, RenderingHints hints): Constructor with singlt scaleFactor and offset. hints can be null.

Methods:



  • BufferedImage createCompatibleDestImage?(BufferedImage src, ColorModel destCM): This method creates a zeroed destination image with the correct size and number of bands.
  • WritableRaster createCompatibleDestRaster?(Raster src): This method creates a zeroed-destination Raster with the correct size and number of bands, given this source.
  • BufferedImage filter?(BufferedImage src, BufferedImage dst): This method rescales the source BufferedImage.
  • WritableRaster filter?(Raster src, WritableRaster dst): This method rescales the pixel data in the source Raster.
  • Rectangle2D getBounds2D?(BufferedImage src): This method returns the bounding box of the rescaled destination image.
  • Rectangle2D getBounds2D?(Raster src): This method returns the bounding box of the rescaled destination Raster.
  • int getNumFactors?(): This method returns the number of scaling factors and offsets used in this RescaleOp.
  • float[] getOffsets?(float[] offsets): This method returns the offsets in the given array.
  • Point2D getPoint2D?(Point2D srcPt, Point2D dstPt): This method returns the location of the destination point given a point in the source.
  • RenderingHints getRenderingHints?(): This method returns the rendering hints for this op.
  • float[] getScaleFactors?(float[] scaleFactors): This method returns the scale factors in the given array.

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.

Java




import java.awt.image.*;
import java.net.*;
import java.awt.*;
import java.io.*;
import javax.imageio.*;
  
public class DemoonRescaleop {
  
    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

Original Image

Output:

processed.png

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.

Java




import java.awt.image.*;
import java.net.*;
import java.awt.*;
import java.io.*;
import javax.imageio.*;
public class DemoonRescaleop {
  
    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

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!