RescaleOp Filter

RescaleOp used to multiply the color value for each pixel by a user-specified scale factor, and then to add a user-specified constant to the product. Separate scale factors and additive constants are provided for each of the red, green, and blue colors. Color values that fall outside the allowable range from 0 to 255 are simply clipped to 0 and 255.

RescaleOp can also be used to adjust the contrast and brightness of an image.

Using a RescaleOp is similar the various other BufferedImageOp classes. In all cases, we can apply a filter to a source to generate a destination. The other BufferedImageOp classes are explained in further detail in these other sections of the notes (AffineTransformOp, ColorConvertOp, ConvolveOp & LookupOp).

RescaleDemo Example (Run Applet)

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;

public class RescaleDemo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel implements ItemListener
    {
        private final float normalScale[] =
        {
            1.0f, 1.0f, 1.0f
        };
        private final float normalOffset[] =
        {
            0.0f, 0.0f, 0.0f
        };
        private final float brightScale[] =
        {
            2.0f, 2.0f, 2.0f
        };
        private final float brightOffset[] =
        {
            -100.0f, -100.0f, -100.0f
        };
        private final float darkScale[] =
        {
            -1.0f, -1.0f, -1.0f
        };
        private final float darkOffset[] =
        {
            150.0f, 150.0f, 150.0f
        };
        private final float blueScale[] =
        {
            1.0f, 1.0f, 5.0f
        };
        private final float blueOffset[] =
        {
            0.0f, 0.0f, -150.0f
        };
        private final float greenScale[] =
        {
            1.0f, -1.0f, 1.0f
        };
        private final float greenOffset[] =
        {
            0.0f, 0.0f, 0.0f
        };
        private float scale[] = normalScale;   // initialse the scale and offset to produce a normal, unadjusted, image
        private float offset[] = normalScale;
        private final float scaleList[][] =
        {
            normalScale, brightScale, darkScale, blueScale, greenScale
        };
        private final float offsetList[][] =
        {
            normalOffset, brightOffset, darkOffset, blueOffset, greenOffset
        };
        private final String rescaleNames[] =
        {
            "Normal", "Lighten", "Darken", "Lighten in Blue", "Darken in Green"
        };
        private final JComboBox rescaleList = new JComboBox(rescaleNames);

        public View()
        {
            this.add(this.rescaleList);
            this.rescaleList.addItemListener(this);
        }

        @Override
        protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            final Image image = new ImageIcon(getClass().getClassLoader().getResource("images/koala.jpg")).getImage();

            // place the original file image into a buffered image
            final BufferedImage srcBufferedImg = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
            final Graphics2D SrcG = srcBufferedImg.createGraphics();
            SrcG.drawImage(image, 0, 0, getWidth(), getHeight(), this);

            // Do the rescaleOp
            RescaleOp rescaleOp = new RescaleOp(this.scale, this.offset, null);
            final BufferedImage destBufferedImg = rescaleOp.filter(srcBufferedImg, null);

            // draw the destination buffered image onto the applet's panel
            g.drawImage(destBufferedImg, 0, 0, getWidth(), getHeight(), this);
        }

        @Override
        public void itemStateChanged(ItemEvent e)
        {
            this.scale = this.scaleList[this.rescaleList.getSelectedIndex()];
            this.offset = this.offsetList[this.rescaleList.getSelectedIndex()];
            this.repaint();
        }
    }
}

Highlighting Text

A more efficient way to highlight text is to change the colour model to HSBColor (hue, saturation, brightness) and to adjust the brightness. This is shown in the example below.

HSBColorDemo Example (Run Applet)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class HSBColorDemo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel
    {
        public View()
        {
            super();
            HighlightLabel label1 = new HighlightLabel("D.k.I.T.");
            add(label1);
        }
    }

    public class HighlightLabel extends JLabel implements MouseListener
    {
        private Color originalColor = new Color(150, 0, 0);

        public HighlightLabel(String text)
        {
            super(text);

            setForeground(this.originalColor);
            setFont(new Font("Times New Roman", Font.BOLD + Font.ITALIC, 100));

            addMouseListener(this);
        }

        public void highlight(boolean isHighlighted)
        {
            if (isHighlighted)
            {
                final int red = this.originalColor.getRed();
                final int green = this.originalColor.getGreen();
                final int blue = this.originalColor.getBlue();

                final float[] hsb = Color.RGBtoHSB(red, green, blue, null);
                final float scale = 1.5f;   // highlight scaling factor
                hsb[2] = Math.min(1.0f, hsb[2] * scale);

                final Color newColor = Color.getHSBColor(hsb[0], hsb[1], hsb[2]);
                this.setForeground(newColor);
            }
            else   // not highlighted
            {
                this.setForeground(this.originalColor);
            }
        }

        @Override
        public void mouseEntered(MouseEvent e)
        {
            this.highlight(true);
        }

        @Override
        public void mouseExited(MouseEvent e)
        {
            this.highlight(false);
        }

        @Override
        public void mousePressed(MouseEvent e)
        {
        }

        @Override
        public void mouseReleased(MouseEvent e)
        {
        }

        @Override
        public void mouseClicked(MouseEvent e)
        {
        }
    }
}
 
<div align="center"><a href="../versionC/index.html" title="DKIT Lecture notes homepage for Derek O&#39; Reilly, Dundalk Institute of Technology (DKIT), Dundalk, County Louth, Ireland. Copyright Derek O&#39; Reilly, DKIT." target="_parent" style='font-size:0;color:white;background-color:white'>&nbsp;</a></div>