no.hiof.imagepr
Class RGBImage

java.lang.Object
  extended byno.hiof.imagepr.RGBImage
All Implemented Interfaces:
Image, java.io.Serializable

public class RGBImage
extends java.lang.Object
implements Image, java.io.Serializable

RGBImage is a class for images which is supposed to be easier to use than the classes for image processing in the Java SDK API. The RGImage class contains three two-dimensional short arrays (matrices) called red green and blue which contains the pixel values of the image. Other colours then the three primary colours are composed as a combination of red, green and blue.

In order to be viewed properly, the colour values must be set in the interval from 0 to 255.

The RGBImage class contains methods for setting and getting the color matrices, showing the image, saving the image to a file and reading the images from a file or from a web-location.

Author:
Per-Olav Rusås
See Also:
IntensityImage, BinaryImage, HSIImage, Serialized Form

Field Summary
protected  short[][] blue
          The Matrix with the blue component of the image
protected  short[][] green
          The Matrix with the green component of the image
protected  int imageType
          The image type used when saving the image.
protected  short[][] red
          The Matrix with the red component of the image
 
Fields inherited from interface no.hiof.imagepr.Image
BLUE, GREEN, RED
 
Constructor Summary
RGBImage()
          A constructor that creates an empty RGBImage object.
RGBImage(BinaryImage binImage)
          A constructor which creates an image based on a BinaryImage (black and white image).
RGBImage(java.awt.image.BufferedImage bim)
          A constructor which creates an image based on a BufferedImage.
RGBImage(HSIImage hsiImage)
          A constructor which creates an image based on a HSIImage.
RGBImage(IntensityImage intensImage)
          A constructor which creates an image based on an IntensityImage (gray scale image).
RGBImage(int rows, int cols)
          A constructor that creates a black image of given size.
RGBImage(RGBImage otherImage)
          A constructor which copy another RGBImage.
RGBImage(short[][] red, short[][] green, short[][] blue)
          A constructor that sets the color matrices.
RGBImage(java.lang.String location)
          A constructor which read an image from an URL or a file.
 
Method Summary
 void copyFrom(RGBImage otherImage)
          Copy another RGBImage.
 Image createScaledImage(double scale, int interpolType)
          Creates a scaled image.
 void draw(java.awt.Graphics g, int x, int y)
          Draws the image to a Graphics object at a given position.
 void draw(java.awt.Graphics g, int x, int y, double scaleX, double scaleY, double rotateAngle)
          Draws the image to a Graphics object at a given position with a given scale and rotation.
 short[][] getBlue()
          Gets the reference to the matrix with the blue colour component of the image.
 short[][] getGreen()
          Gets the reference to the matrix with the green colour component of the image.
 int getHeight()
          The height of the image.
 short[][] getRed()
          Gets the reference to the matrix with the red colour component of the image.
 int getWidth()
          The width of the image.
 void loadFile(java.lang.String filename)
          Load an image from a file.
 void loadURL(java.lang.String url)
          Load an image from a URL.
 java.awt.image.BufferedImage makeBufferedImage()
          Make a BufferedImage of the RGBImage based on the contents of the RGBImage.
 boolean rgbEqual()
          Returns true if the R, G and B components are all equal, which means that the image is a gray scale image.
 void save(java.lang.String filename)
          Saves the image to a file.
 void saveAsJPEG(java.lang.String filename, double quality)
          Saves the image to a JPEG-file.
 void setBImage(java.awt.image.BufferedImage bImage)
          Set the BufferedImage of the RGBImage.
 void setBlue(short[][] blue)
          Sets the blue component of the image.
 void setGreen(short[][] green)
          Sets the green component of the image.
 void setRed(short[][] red)
          Sets the red component of the image.
 void show()
          Shows a JOptionPane-window including the image.
 void show(java.lang.String title)
          Shows a JOptionPane-window including the image.
 void show(java.lang.String title, double scale)
          Shows the RGBImage scaled with use of nearest neighbor interpolation.
 void showBL(java.lang.String title, double scale)
          Shows the RGBImage scaled with use of bilinear interpolation.
 java.lang.String toString()
          Returns a description of the image.
 void useGrayColormap()
          Force the image to be shown with a colormap usually used for gray scale images.
 void useRGBColormap()
          Force the image to be shown with a colormap usually used for color images.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

red

protected short[][] red
The Matrix with the red component of the image


green

protected short[][] green
The Matrix with the green component of the image


blue

protected short[][] blue
The Matrix with the blue component of the image


imageType

protected int imageType
The image type used when saving the image. Used to save gray scale images with an adjusted color map

Constructor Detail

RGBImage

public RGBImage()
A constructor that creates an empty RGBImage object.


RGBImage

public RGBImage(int rows,
                int cols)
A constructor that creates a black image of given size.

Parameters:
rows - Number of rows.
cols - Number of colums.

RGBImage

public RGBImage(short[][] red,
                short[][] green,
                short[][] blue)
A constructor that sets the color matrices.

Parameters:
red - The red component of the image.
green - The green component of the image.
blue - The blue component of the image.

RGBImage

public RGBImage(RGBImage otherImage)
A constructor which copy another RGBImage.

Parameters:
otherImage - Another RGBImage.

RGBImage

public RGBImage(IntensityImage intensImage)
A constructor which creates an image based on an IntensityImage (gray scale image).

Parameters:
intensImage - Original image

RGBImage

public RGBImage(BinaryImage binImage)
A constructor which creates an image based on a BinaryImage (black and white image).

Parameters:
binImage - Original image

RGBImage

public RGBImage(HSIImage hsiImage)
A constructor which creates an image based on a HSIImage.

Parameters:
hsiImage - Original image

RGBImage

public RGBImage(java.awt.image.BufferedImage bim)
A constructor which creates an image based on a BufferedImage.

Parameters:
bim - Original image

RGBImage

public RGBImage(java.lang.String location)
         throws java.io.IOException
A constructor which read an image from an URL or a file. Allowed file formats are at least jpeg, png and gif.

Parameters:
location - The URL or filename
Method Detail

copyFrom

public void copyFrom(RGBImage otherImage)
Copy another RGBImage.

Parameters:
otherImage - Image to copy

getRed

public short[][] getRed()
Gets the reference to the matrix with the red colour component of the image. This method does not make a copy of the the matrix, but returns the reference to the RGBImage's colour matrix. This means that the returned reference can be used to manipulate the colour of the RGBImage.

Returns:
The red colour matrix.

getGreen

public short[][] getGreen()
Gets the reference to the matrix with the green colour component of the image. This method does not make a copy of the the matrix, but returns the reference to the RGBImage's colour matrix. This means that the returned reference can be used to manipulate the colour of the RGBImage.

Returns:
The green colour matrix.

getBlue

public short[][] getBlue()
Gets the reference to the matrix with the blue colour component of the image. This method does not make a copy of the the matrix, but returns the reference to the RGBImage's colour matrix. This means that the returned reference can be used to manipulate the colour of the RGBImage.

Returns:
The blue colour matrix.

setRed

public void setRed(short[][] red)
Sets the red component of the image.

Parameters:
red - Matrix with the red colour values.

setGreen

public void setGreen(short[][] green)
Sets the green component of the image.

Parameters:
green - Matrix with the green colour values.

setBlue

public void setBlue(short[][] blue)
Sets the blue component of the image.

Parameters:
blue - Matrix with the blue colour values.

rgbEqual

public boolean rgbEqual()
Returns true if the R, G and B components are all equal, which means that the image is a gray scale image.

Returns:
true if an only if the color components are equal

getWidth

public int getWidth()
The width of the image. If some of the matrices are missing, the method returns -1.

Specified by:
getWidth in interface Image
Returns:
The width of the image if all the matrices exist.

getHeight

public int getHeight()
The height of the image. If some of the matrixes are missing, the method returns -1.

Specified by:
getHeight in interface Image
Returns:
The height of the image if all the matrices exist.

toString

public java.lang.String toString()
Returns a description of the image.

Returns:
A description.

setBImage

public void setBImage(java.awt.image.BufferedImage bImage)
Set the BufferedImage of the RGBImage. The matrices with the red, green and blue components are updated.

Parameters:
bImage - the BufferedImage.

makeBufferedImage

public java.awt.image.BufferedImage makeBufferedImage()
Make a BufferedImage of the RGBImage based on the contents of the RGBImage.

Specified by:
makeBufferedImage in interface Image
Returns:
a BufferedImage representing the RGBImage.

loadURL

public void loadURL(java.lang.String url)
             throws java.io.IOException
Load an image from a URL.

Parameters:
url - the URL of the web-location containing an image file
Throws:
java.io.IOException

loadFile

public void loadFile(java.lang.String filename)
              throws java.io.IOException
Load an image from a file.

Parameters:
filename - The name of the image file.
Throws:
java.io.IOException

save

public void save(java.lang.String filename)
          throws java.io.IOException
Saves the image to a file. The format of the file is determined by the extension of the filename (jpg, gif or png).

Parameters:
filename - the name of the file.
Throws:
java.io.IOException

saveAsJPEG

public void saveAsJPEG(java.lang.String filename,
                       double quality)
                throws java.io.IOException
Saves the image to a JPEG-file.

Parameters:
filename - the name of the file.
quality - the quality of the image, between 0.0 and 1.0
Throws:
java.io.IOException

draw

public void draw(java.awt.Graphics g,
                 int x,
                 int y)
Draws the image to a Graphics object at a given position.

Specified by:
draw in interface Image
Parameters:
g - The Graphics object to draw on.
x - The horizontal position of the image.
y - The vertical position of the image.

draw

public void draw(java.awt.Graphics g,
                 int x,
                 int y,
                 double scaleX,
                 double scaleY,
                 double rotateAngle)
Draws the image to a Graphics object at a given position with a given scale and rotation.

Specified by:
draw in interface Image
Parameters:
g - The Graphics object to draw on.
x - The horizontal position of the image.
y - The vertical position of the image.
scaleX - The scale in x-direction. 1.0 means unscaled.
scaleY - The scale in y-direction. 1.0 means unscaled.
rotateAngle - Rotation of the image in degrees (0-360).

show

public void show(java.lang.String title)
Shows a JOptionPane-window including the image. The window is modal, which means that the program waits until it is closed.

Specified by:
show in interface Image
Parameters:
title - A text placed on the bar on top of the window.

show

public void show()
Shows a JOptionPane-window including the image. The window is modal, which means that the program waits until it is closed.


show

public void show(java.lang.String title,
                 double scale)
Shows the RGBImage scaled with use of nearest neighbor interpolation. This method utilizes an ImageScaler to perform the scaling.

Specified by:
show in interface Image
Parameters:
scale - The scale.
title - The title on top of the window.

showBL

public void showBL(java.lang.String title,
                   double scale)
Shows the RGBImage scaled with use of bilinear interpolation. This method utilizes an ImageScaler to perform the scaling.

Parameters:
scale - The scale.
title - The title on top of the window.

createScaledImage

public Image createScaledImage(double scale,
                               int interpolType)
Creates a scaled image. This method utilizes an ImageScaler to perform the scaling. The parameter interpolType may be one of the constants ImageScaler.NN_INTERPOL for nearest neighbor interpolation or ImageScaler.BL_INTERPOL for bilinear interpolation.

Specified by:
createScaledImage in interface Image
Parameters:
scale - The scale.
interpolType - The type of interpolation

useGrayColormap

public void useGrayColormap()
Force the image to be shown with a colormap usually used for gray scale images. It is usually unnecessary to call this method.


useRGBColormap

public void useRGBColormap()
Force the image to be shown with a colormap usually used for color images. It is usually unnecessary to call this method.