public class BufferedRGB24Image extends java.lang.Object implements RGB24Image
BufferedImage
objects (class defined
in the standard runtime library, package java.awt.image
) as
RGB24Image
objects within JIU.
This class encapsulates a single BufferedImage
object.
It enables reusing existing BufferedImage objects as input or
output of JIU operations,
removing the necessity for the conversion step from java.awt.Image
to net.sourceforge.jiu.data.PixelImage
(or vice versa)
and thus reducing memory consumption.
The name of this class is a combination of BufferedImage (the class of the object
that is encapsulated) and RGB24Image (the JIU image data interface).
Internally, this class uses BufferedImage
's getRGB and
setRGB methods to access image data.
This approach is slower than working directly on the BufferedImage's data
buffers.
However, using getRGB and setRGB, this class will work with all types of BufferedImage objects.
Note that while the abstract java.awt.Image
class existed from the very
beginning (version 1.0) of the Java runtime library, java.awt.image.BufferedImage
has not been added until version 1.2.
import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import net.sourceforge.jiu.color.Invert; import net.sourceforge.jiu.data.PixelImage; import net.sourceforge.jiu.gui.awt.BufferedRGB24Image; ... BufferedImage bufferedImage = ImageIO.read(new File("image.jpg")); BufferedRGB24Image image = new BufferedRGB24Image(bufferedImage); Invert invert = new Invert(); invert.setInputImage(image); invert.process(); PixelImage outputImage = invert.getOutputImage();If you can be sure that an image object can be input and output image at the same time (as is the case with some operations), you can even work with only one BufferedRGB24Image object. Invert is one of these operations, so the following would work:
Invert invert = new Invert(); invert.setInputImage(image); invert.setOutputImage(image); invert.process(); // image now is inverted
Modifier and Type | Field and Description |
---|---|
private static int |
BLUE_SHIFT |
private static int |
GREEN_SHIFT |
private int |
HEIGHT |
private java.awt.image.BufferedImage |
image |
private static int |
RED_SHIFT |
private static int[] |
RGB_CLEAR
Masks for the three RGB channels.
|
private static int[] |
RGB_SHIFT |
private int |
WIDTH |
INDEX_BLUE, INDEX_GREEN, INDEX_RED
Constructor and Description |
---|
BufferedRGB24Image(java.awt.image.BufferedImage bufferedImage)
Creates a new BufferedRGB24Image object, storing the argument
BufferedImage object internally.
|
Modifier and Type | Method and Description |
---|---|
void |
clear(byte newValue)
Sets all the RGB samples in this image to the argument, keeping
the alpha value.
|
void |
clear(int newValue)
Sets all samples in the first channel to the argument value.
|
void |
clear(int channelIndex,
byte newValue)
Sets all samples of one channel to a new value.
|
void |
clear(int channelIndex,
int newValue)
Sets all samples of the
channelIndex 'th channel to newValue . |
PixelImage |
createCompatibleImage(int width,
int height)
Creates an instance of the same class as this one, with width and height
given by the arguments.
|
PixelImage |
createCopy()
Creates an new image object that will be of the same type as this one,
with the same image data, using entirely new resources.
|
long |
getAllocatedMemory()
Returns the number of bytes that were dynamically allocated for
this image object.
|
int |
getBitsPerPixel()
Returns the number of bits per pixel of this image.
|
byte |
getByteSample(int x,
int y)
Returns a single byte sample from the first channel and the specified position.
|
byte |
getByteSample(int channelIndex,
int x,
int y)
Returns a single byte sample from the image.
|
void |
getByteSamples(int x,
int y,
int w,
int h,
byte[] dest,
int destOffset) |
void |
getByteSamples(int channelIndex,
int x,
int y,
int w,
int h,
byte[] dest,
int destOffset)
Copies samples from this image to a byte array.
|
int |
getHeight()
Returns the vertical resolution of the image in pixels.
|
java.lang.Class |
getImageType()
If there is a single interface or class that describes the image data type
of this class, the
Class object associated with that
interface (or class) is returned (or null otherwise). |
int |
getMaxSample(int channel)
Returns the maximum value for one of the image's channels.
|
int |
getNumChannels()
Returns the number of channels in this image.
|
int |
getSample(int x,
int y)
Returns one sample of the first channel (index 0).
|
int |
getSample(int channelIndex,
int x,
int y)
Returns one sample, specified by its channel index and location.
|
void |
getSamples(int x,
int y,
int w,
int h,
int[] dest,
int destOffs) |
void |
getSamples(int channelIndex,
int x,
int y,
int w,
int h,
int[] dest,
int destOffs)
Copies a number of samples from this image to an
int[] object. |
int |
getWidth()
Returns the horizontal resolution of the image in pixels.
|
void |
putByteSample(int x,
int y,
byte newValue)
Sets one byte sample in the first channel (index
0 ) to a new value. |
void |
putByteSample(int channelIndex,
int x,
int y,
byte newValue)
Sets one byte sample in one channel to a new value.
|
void |
putByteSamples(int x,
int y,
int w,
int h,
byte[] src,
int srcOffset) |
void |
putByteSamples(int channelIndex,
int x,
int y,
int w,
int h,
byte[] src,
int srcOffset)
Copies a number of samples from the argument array to this image.
|
void |
putSample(int x,
int y,
int newValue)
This method sets one sample of the first channel (index 0) to a new value.
|
void |
putSample(int channelIndex,
int x,
int y,
int newValue)
This method sets one sample to a new value.
|
void |
putSamples(int channelIndex,
int x,
int y,
int w,
int h,
int[] src,
int srcOffset)
Copies a number of samples from an
int[] array to this image. |
private static final int RED_SHIFT
private static final int GREEN_SHIFT
private static final int BLUE_SHIFT
private static final int[] RGB_CLEAR
private static final int[] RGB_SHIFT
private final int HEIGHT
private final java.awt.image.BufferedImage image
private final int WIDTH
public BufferedRGB24Image(java.awt.image.BufferedImage bufferedImage)
bufferedImage
- the underlying BufferedImage object for this BufferedRGB24Image objectpublic void clear(byte newValue)
clear
in interface ByteChannelImage
newValue
- all samples in the image will be set to this valueByteChannelImage.clear(int, byte)
,
IntegerImage.clear(int)
,
IntegerImage.clear(int, int)
public void clear(int newValue)
IntegerImage
clear(0, newValue);
:clear
in interface IntegerImage
public void clear(int channelIndex, byte newValue)
ByteChannelImage
clear
in interface ByteChannelImage
channelIndex
- zero-based index of the channel to be cleared (must be smaller than PixelImage.getNumChannels()
newValue
- all samples in the channel will be set to this valuepublic void clear(int channelIndex, int newValue)
IntegerImage
channelIndex
'th channel to newValue
.clear
in interface IntegerImage
public PixelImage createCompatibleImage(int width, int height)
PixelImage
createCompatibleImage
in interface PixelImage
width
- the horizontal resolution of the new imageheight
- the vertical resolution of the new imagepublic PixelImage createCopy()
PixelImage
createCopy
in interface PixelImage
public long getAllocatedMemory()
PixelImage
getAllocatedMemory
in interface PixelImage
public int getBitsPerPixel()
PixelImage
getBitsPerPixel
in interface PixelImage
public byte getByteSample(int x, int y)
ByteChannelImage
getByteSample(0, x, y)
.getByteSample
in interface ByteChannelImage
x
- horizontal position of the sample to be returned (must be between 0
and PixelImage.getWidth()
- 1
y
- vertical position of the sample to be returned (must be between 0
and PixelImage.getHeight()
- 1
public byte getByteSample(int channelIndex, int x, int y)
ByteChannelImage
ByteChannelImage.getByteSamples(int, int, int, int, int, byte[], int)
).getByteSample
in interface ByteChannelImage
channelIndex
- the number of the channel of the sample; must be from 0
to PixelImage.getNumChannels()
- 1
x
- the column of the sample to be returned; must be from 0
to PixelImage.getWidth()
- 1
y
- the row of the sample; must be from 0
to PixelImage.getHeight()
- 1
ByteChannelImage.getByteSamples(int, int, int, int, int, byte[], int)
public void getByteSamples(int channelIndex, int x, int y, int w, int h, byte[] dest, int destOffset)
ByteChannelImage
num
samples in row y
of channel
channel
, starting at horizontal offset x
.
Data will be written to the dest
array, starting at
offset destOffset
.
Data will be copied from one row only, so a maximum of
getWidth()
samples can be copied with a call to this method.getByteSamples
in interface ByteChannelImage
channelIndex
- the index of the channel to be copied from; must be
from 0
to getNumChannels() - 1
x
- the horizontal offset where copying will start; must be from
0
to getWidth() - 1
y
- the row from which will be copied; must be from
0
to getHeight() - 1
w
- the number of columns to be copiedh
- the number of rows to be copieddest
- the array where the data will be copied to; must have a
length of at least destOffset + num
destOffset
- the offset into dest
where this method
will start copying datapublic void getByteSamples(int x, int y, int w, int h, byte[] dest, int destOffset)
public int getHeight()
PixelImage
getHeight
in interface PixelImage
public java.lang.Class getImageType()
PixelImage
Class
object associated with that
interface (or class) is returned (or null
otherwise).
This Class
object, if available for two image objects,
can be used to find out if they are compatible.
Example: MemoryGray8Image
returns
net.sourceforge.jiu.data.Gray8Image.class
.getImageType
in interface PixelImage
public int getMaxSample(int channel)
IntegerImage
0
.getMaxSample
in interface IntegerImage
channel
- zero-based index of the channel, from 0
to PixelImage.getNumChannels()
- 1
public int getNumChannels()
PixelImage
getNumChannels
in interface PixelImage
public int getSample(int x, int y)
IntegerImage
getSample(0, x, y);
.getSample
in interface IntegerImage
x
- the horizontal position of the sample, from 0
to PixelImage.getWidth()
- 1
y
- the vertical position of the sample, from 0
to PixelImage.getHeight()
- 1
public int getSample(int channelIndex, int x, int y)
IntegerImage
getSample
in interface IntegerImage
channelIndex
- the number of the channel, from 0
to PixelImage.getNumChannels()
- 1
x
- the horizontal position of the sample, from 0
to PixelImage.getWidth()
- 1
y
- the vertical position of the sample, from 0
to PixelImage.getHeight()
- 1
public void getSamples(int x, int y, int w, int h, int[] dest, int destOffs)
public void getSamples(int channelIndex, int x, int y, int w, int h, int[] dest, int destOffs)
IntegerImage
int[]
object.
A rectangular part of one channel is copied.
The channel index is given by - the upper left corner of
that rectangle is given by the point x / y.
Width and height of that rectangle are given by w and h.
Each sample will be stored as one int
value dest,
starting at index destOffs.getSamples
in interface IntegerImage
channelIndex
- zero-based index of the channel from which data is to be copied (valid values: 0 to PixelImage.getNumChannels()
- 1)x
- horizontal position of upper left corner of the rectangle to be copiedy
- vertical position of upper left corner of the rectangle to be copiedw
- width of rectangle to be copiedh
- height of rectangle to be copieddest
- int array to which the samples will be copieddestOffs
- int index into the dest array for the position to which the samples will be copiedpublic int getWidth()
PixelImage
getWidth
in interface PixelImage
public void putByteSample(int channelIndex, int x, int y, byte newValue)
ByteChannelImage
putByteSample
in interface ByteChannelImage
public void putByteSample(int x, int y, byte newValue)
ByteChannelImage
0
) to a new value.
Result is equal to putByteSample(0, x, y, newValue);
.putByteSample
in interface ByteChannelImage
public void putByteSamples(int channelIndex, int x, int y, int w, int h, byte[] src, int srcOffset)
ByteChannelImage
putByteSamples
in interface ByteChannelImage
public void putByteSamples(int x, int y, int w, int h, byte[] src, int srcOffset)
public void putSample(int x, int y, int newValue)
IntegerImage
putSample(0, x, y)
.
The sample location is given by the spatial coordinates, x and y.putSample
in interface IntegerImage
x
- the horizontal position of the sample, from 0
to PixelImage.getWidth()
- 1
y
- the vertical position of the sample, from 0
to PixelImage.getHeight()
- 1
newValue
- the new value of the samplepublic void putSample(int channelIndex, int x, int y, int newValue)
IntegerImage
putSample
in interface IntegerImage
channelIndex
- the number of the channel, from 0
to PixelImage.getNumChannels()
- 1
x
- the horizontal position of the sample, from 0
to PixelImage.getWidth()
- 1
y
- the vertical position of the sample, from 0
to PixelImage.getHeight()
- 1
newValue
- the new value of the samplepublic void putSamples(int channelIndex, int x, int y, int w, int h, int[] src, int srcOffset)
IntegerImage
int[]
array to this image.
A rectangular part of one channel is copied - the upper left corner of
that rectangle is given by the point x / y.
Width and height of that rectangle are given by w and h.
Each sample will be stored as one int
value src,
starting at index srcOffset.putSamples
in interface IntegerImage
channelIndex
- int (from 0 to getNumChannels() - 1) to indicate the channel to which data is copiedx
- horizontal position of upper left corner of the rectangle to be copiedy
- vertical position of upper left corner of the rectangle to be copiedw
- width of rectangle to be copiedh
- height of rectangle to be copiedsrc
- int array from which the samples will be copiedsrcOffset
- int index into the src array for the position from which the samples will be copied