Open In App

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

Last Updated : 21 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:

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 single 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 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

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 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

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



Similar Reads

Java AWT | Cursor class with examples
Cursor class is a part of Java AWT package and it is used to create custom cursors or inherit system or predefined cursors. Cursor class is mainly used to encapsulate the bitmap representation of the mouse cursor. Constructor of cursor class are : Cursor(int t) : Creates a cursor with specified class Cursor(String name) : Creates a custom cursor wi
4 min read
How to Place an Image in AWT in Java
AWT stands For Abstract Window Tool Kit. In the AWT we could place the image in the frame using the ToolKit class and MediaTracker class in the java.awt.* package. We can display image formats like GIF or JPG images in the AWT Frame with the help of the following steps in Java. Firstly create an Image class object and then load your convenient imag
3 min read
Java AWT | GridLayout Class
GridLayout class represents a layout manager with a specified number of rows and columns in a rectangular grid. The GridLayout container is divided into an equal-sized of rectangles, and one of the components is placed in each rectangle. Every rectangle cell has the same size therefore, they contain a component, which fills the entire cell. When th
5 min read
Java AWT | Choice Class
Choice class is part of Java Abstract Window Toolkit(AWT). The Choice class presents a pop- up menu for the user, the user may select an item from the popup menu. The selected item appears on the top. The Choice class inherits the Component. Constructor for the Choice class Choice() : creates an new empty choice menu . Different Methods for the Cho
5 min read
Java AWT | Desktop Class
The Desktop class is a part of Java AWT package. This class is used to launch associated applications registered on the native desktop to handle a URI or a file. Important Points About Desktop Class : It can open a default web browser showing a specific URI It can launch default mail client with optional mailto URI It can launch a registered applic
5 min read
Java AWT | Color Class
The Color class is a part of Java Abstract Window Toolkit(AWT) package. The Color class creates color by using the given RGBA values where RGBA stands for RED, GREEN, BLUE, ALPHA or using HSB value where HSB stands for HUE, SATURATION, BRIcomponents. The value for individual components RGBA ranges from 0 to 255 or 0.0 to 0.1. The value of alpha det
9 min read
Java AWT | Dimension Class
Dimension class is a part of Java AWT. It contains the height and width of a component in an integer as well as double precision. The use of Dimension class is that many functions of Java AWT and Swing return dimension object. Constructors of the Dimension class Dimension() : It will create a new Object with height and width set to zero.Dimension(D
4 min read
Java AWT | Canvas Class
Canvas class is a part of Java AWT. Canvas is a blank rectangular area where the user can draw or trap input from the user. Canvas class inherits the Component class.Constructor of the Canvas class are : Canvas(): Creates a new blank canvas.Canvas(GraphicsConfiguration c): Creates a new canvas with a specified graphics configuration. Commonly used
3 min read
Java AWT | BorderLayout Class
BorderLayout is the default layout for the window objects such as JFrame, JWindow, JDialog, JInternalFrame etc. BorderLayout arranges the components in the five regions. Four sides are referred to as north, south, east, and west. The middle part is called the center. Each region can contain only one component and is identified by a corresponding co
4 min read
Java AWT | CardLayout Class
The CardLayout class manages the components in such a way that only one component is visible at a time. It treats each component as a card in the container. Only one card is visible at a time, and the container acts as a stack of cards. The first component added to a CardLayout object is the visible component when the container is first displayed.
6 min read
Article Tags :
Practice Tags :