Package net.sourceforge.jiu.gui.awt
Class BufferedRGB24Image
- java.lang.Object
-
- net.sourceforge.jiu.gui.awt.BufferedRGB24Image
-
- All Implemented Interfaces:
ByteChannelImage
,IntegerImage
,PixelImage
,RGB24Image
,RGBImage
,RGBIndex
,RGBIntegerImage
public class BufferedRGB24Image extends Object implements RGB24Image
A bridge class to useBufferedImage
objects (class defined in the standard runtime library, packagejava.awt.image
) asRGB24Image
objects within JIU. This class encapsulates a singleBufferedImage
object. It enables reusing existing BufferedImage objects as input or output of JIU operations, removing the necessity for the conversion step fromjava.awt.Image
tonet.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.Usage example
This code snippet demonstrates to how combine functionality from Java's runtime library with JIU by using this class. Requires Java 1.4 or higher. Obviously, BufferedRGB24Image objects can only be used with operations that work on classes implementing RGB24Image.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
- Since:
- 0.10.0
- Author:
- Marco Schmidt
-
-
Field Summary
Fields Modifier and Type Field Description private static int
BLUE_SHIFT
private static int
GREEN_SHIFT
private int
HEIGHT
private 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
-
Fields inherited from interface net.sourceforge.jiu.data.RGBIndex
INDEX_BLUE, INDEX_GREEN, INDEX_RED
-
-
Constructor Summary
Constructors Constructor Description BufferedRGB24Image(BufferedImage bufferedImage)
Creates a new BufferedRGB24Image object, storing the argument BufferedImage object internally.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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 thechannelIndex
'th channel tonewValue
.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.Class
getImageType()
If there is a single interface or class that describes the image data type of this class, theClass
object associated with that interface (or class) is returned (ornull
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 anint[]
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 (index0
) 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 anint[]
array to this image.
-
-
-
Field Detail
-
RED_SHIFT
private static final int RED_SHIFT
- See Also:
- Constant Field Values
-
GREEN_SHIFT
private static final int GREEN_SHIFT
- See Also:
- Constant Field Values
-
BLUE_SHIFT
private static final int BLUE_SHIFT
- See Also:
- Constant Field Values
-
RGB_CLEAR
private static final int[] RGB_CLEAR
Masks for the three RGB channels. RGB_CLEAR[i] is an int with all bits on, except for those occupied by the channel i. RGB_CLEAR[i] can thus be used to bitwise AND an ARGB value so that the sample for channel i will be cleared.
-
RGB_SHIFT
private static final int[] RGB_SHIFT
-
HEIGHT
private final int HEIGHT
-
image
private final BufferedImage image
-
WIDTH
private final int WIDTH
-
-
Constructor Detail
-
BufferedRGB24Image
public BufferedRGB24Image(BufferedImage bufferedImage)
Creates a new BufferedRGB24Image object, storing the argument BufferedImage object internally. All image data access will be delegated to that BufferedImage object's methods.- Parameters:
bufferedImage
- the underlying BufferedImage object for this BufferedRGB24Image object
-
-
Method Detail
-
clear
public void clear(byte newValue)
Sets all the RGB samples in this image to the argument, keeping the alpha value.- Specified by:
clear
in interfaceByteChannelImage
- Parameters:
newValue
- all samples in the image will be set to this value- See Also:
ByteChannelImage.clear(int, byte)
,IntegerImage.clear(int)
,IntegerImage.clear(int, int)
-
clear
public void clear(int newValue)
Description copied from interface:IntegerImage
Sets all samples in the first channel to the argument value. Equal toclear(0, newValue);
:- Specified by:
clear
in interfaceIntegerImage
-
clear
public void clear(int channelIndex, byte newValue)
Description copied from interface:ByteChannelImage
Sets all samples of one channel to a new value.- Specified by:
clear
in interfaceByteChannelImage
- Parameters:
channelIndex
- zero-based index of the channel to be cleared (must be smaller thanPixelImage.getNumChannels()
newValue
- all samples in the channel will be set to this value
-
clear
public void clear(int channelIndex, int newValue)
Description copied from interface:IntegerImage
Sets all samples of thechannelIndex
'th channel tonewValue
.- Specified by:
clear
in interfaceIntegerImage
-
createCompatibleImage
public PixelImage createCompatibleImage(int width, int height)
Description copied from interface:PixelImage
Creates an instance of the same class as this one, with width and height given by the arguments.- Specified by:
createCompatibleImage
in interfacePixelImage
- Parameters:
width
- the horizontal resolution of the new imageheight
- the vertical resolution of the new image- Returns:
- the new image
-
createCopy
public PixelImage createCopy()
Description copied from interface:PixelImage
Creates an new image object that will be of the same type as this one, with the same image data, using entirely new resources.- Specified by:
createCopy
in interfacePixelImage
- Returns:
- the new image object
-
getAllocatedMemory
public long getAllocatedMemory()
Description copied from interface:PixelImage
Returns the number of bytes that were dynamically allocated for this image object.- Specified by:
getAllocatedMemory
in interfacePixelImage
- Returns:
- allocated memory in bytes
-
getBitsPerPixel
public int getBitsPerPixel()
Description copied from interface:PixelImage
Returns the number of bits per pixel of this image. That is the number of bits per sample for all channels of this image. Does not include any transparency channels.- Specified by:
getBitsPerPixel
in interfacePixelImage
-
getByteSample
public byte getByteSample(int x, int y)
Description copied from interface:ByteChannelImage
Returns a single byte sample from the first channel and the specified position. A call to this method is the same asgetByteSample(0, x, y)
.- Specified by:
getByteSample
in interfaceByteChannelImage
- Parameters:
x
- horizontal position of the sample to be returned (must be between0
andPixelImage.getWidth()
- 1
y
- vertical position of the sample to be returned (must be between0
andPixelImage.getHeight()
- 1
- Returns:
- the requested byte sample
-
getByteSample
public byte getByteSample(int channelIndex, int x, int y)
Description copied from interface:ByteChannelImage
Returns a single byte sample from the image. When possible, try copying several samples at a time for higher speed (ByteChannelImage.getByteSamples(int, int, int, int, int, byte[], int)
).- Specified by:
getByteSample
in interfaceByteChannelImage
- Parameters:
channelIndex
- the number of the channel of the sample; must be from0
toPixelImage.getNumChannels()
- 1x
- the column of the sample to be returned; must be from0
toPixelImage.getWidth()
- 1y
- the row of the sample; must be from0
toPixelImage.getHeight()
- 1- Returns:
- the sample, a single byte value
- See Also:
ByteChannelImage.getByteSamples(int, int, int, int, int, byte[], int)
-
getByteSamples
public void getByteSamples(int channelIndex, int x, int y, int w, int h, byte[] dest, int destOffset)
Description copied from interface:ByteChannelImage
Copies samples from this image to a byte array. Copiesnum
samples in rowy
of channelchannel
, starting at horizontal offsetx
. Data will be written to thedest
array, starting at offsetdestOffset
. Data will be copied from one row only, so a maximum ofgetWidth()
samples can be copied with a call to this method.- Specified by:
getByteSamples
in interfaceByteChannelImage
- Parameters:
channelIndex
- the index of the channel to be copied from; must be from0
togetNumChannels() - 1
x
- the horizontal offset where copying will start; must be from0
togetWidth() - 1
y
- the row from which will be copied; must be from0
togetHeight() - 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 leastdestOffset + num
destOffset
- the offset intodest
where this method will start copying data
-
getByteSamples
public void getByteSamples(int x, int y, int w, int h, byte[] dest, int destOffset)
-
getHeight
public int getHeight()
Description copied from interface:PixelImage
Returns the vertical resolution of the image in pixels. Must be one or larger.- Specified by:
getHeight
in interfacePixelImage
- Returns:
- height in pixels
-
getImageType
public Class getImageType()
Description copied from interface:PixelImage
If there is a single interface or class that describes the image data type of this class, theClass
object associated with that interface (or class) is returned (ornull
otherwise). ThisClass
object, if available for two image objects, can be used to find out if they are compatible. Example:MemoryGray8Image
returnsnet.sourceforge.jiu.data.Gray8Image.class
.- Specified by:
getImageType
in interfacePixelImage
-
getMaxSample
public int getMaxSample(int channel)
Description copied from interface:IntegerImage
Returns the maximum value for one of the image's channels. The minimum value is always0
.- Specified by:
getMaxSample
in interfaceIntegerImage
- Parameters:
channel
- zero-based index of the channel, from0
toPixelImage.getNumChannels()
- 1
- Returns:
- maximum allowed sample value
-
getNumChannels
public int getNumChannels()
Description copied from interface:PixelImage
Returns the number of channels in this image. Must be one or larger.- Specified by:
getNumChannels
in interfacePixelImage
- Returns:
- the number of channels
-
getSample
public int getSample(int x, int y)
Description copied from interface:IntegerImage
Returns one sample of the first channel (index 0). A call to this method must have the same result as the callgetSample(0, x, y);
.- Specified by:
getSample
in interfaceIntegerImage
- Parameters:
x
- the horizontal position of the sample, from0
toPixelImage.getWidth()
- 1
y
- the vertical position of the sample, from0
toPixelImage.getHeight()
- 1
- Returns:
- the desired sample
-
getSample
public int getSample(int channelIndex, int x, int y)
Description copied from interface:IntegerImage
Returns one sample, specified by its channel index and location.- Specified by:
getSample
in interfaceIntegerImage
- Parameters:
channelIndex
- the number of the channel, from0
toPixelImage.getNumChannels()
- 1
x
- the horizontal position of the sample, from0
toPixelImage.getWidth()
- 1
y
- the vertical position of the sample, from0
toPixelImage.getHeight()
- 1
- Returns:
- the desired sample
-
getSamples
public void getSamples(int x, int y, int w, int h, int[] dest, int destOffs)
-
getSamples
public void getSamples(int channelIndex, int x, int y, int w, int h, int[] dest, int destOffs)
Description copied from interface:IntegerImage
Copies a number of samples from this image to anint[]
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 oneint
value dest, starting at index destOffs.- Specified by:
getSamples
in interfaceIntegerImage
- Parameters:
channelIndex
- zero-based index of the channel from which data is to be copied (valid values: 0 toPixelImage.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 copied
-
getWidth
public int getWidth()
Description copied from interface:PixelImage
Returns the horizontal resolution of the image in pixels. Must be one or larger.- Specified by:
getWidth
in interfacePixelImage
- Returns:
- width in pixels
-
putByteSample
public void putByteSample(int channelIndex, int x, int y, byte newValue)
Description copied from interface:ByteChannelImage
Sets one byte sample in one channel to a new value.- Specified by:
putByteSample
in interfaceByteChannelImage
-
putByteSample
public void putByteSample(int x, int y, byte newValue)
Description copied from interface:ByteChannelImage
Sets one byte sample in the first channel (index0
) to a new value. Result is equal toputByteSample(0, x, y, newValue);
.- Specified by:
putByteSample
in interfaceByteChannelImage
-
putByteSamples
public void putByteSamples(int channelIndex, int x, int y, int w, int h, byte[] src, int srcOffset)
Description copied from interface:ByteChannelImage
Copies a number of samples from the argument array to this image.- Specified by:
putByteSamples
in interfaceByteChannelImage
-
putByteSamples
public void putByteSamples(int x, int y, int w, int h, byte[] src, int srcOffset)
-
putSample
public void putSample(int x, int y, int newValue)
Description copied from interface:IntegerImage
This method sets one sample of the first channel (index 0) to a new value. This call must have the same result as the callputSample(0, x, y)
. The sample location is given by the spatial coordinates, x and y.- Specified by:
putSample
in interfaceIntegerImage
- Parameters:
x
- the horizontal position of the sample, from0
toPixelImage.getWidth()
- 1
y
- the vertical position of the sample, from0
toPixelImage.getHeight()
- 1
newValue
- the new value of the sample
-
putSample
public void putSample(int channelIndex, int x, int y, int newValue)
Description copied from interface:IntegerImage
This method sets one sample to a new value. The sample location is given by the channel index and the spatial coordinates, x and y.- Specified by:
putSample
in interfaceIntegerImage
- Parameters:
channelIndex
- the number of the channel, from0
toPixelImage.getNumChannels()
- 1
x
- the horizontal position of the sample, from0
toPixelImage.getWidth()
- 1
y
- the vertical position of the sample, from0
toPixelImage.getHeight()
- 1
newValue
- the new value of the sample
-
putSamples
public void putSamples(int channelIndex, int x, int y, int w, int h, int[] src, int srcOffset)
Description copied from interface:IntegerImage
Copies a number of samples from anint[]
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 oneint
value src, starting at index srcOffset.- Specified by:
putSamples
in interfaceIntegerImage
- Parameters:
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
-
-