Class ContourPlot.Img
- java.lang.Object
-
- gov.nih.mipav.model.algorithms.ContourPlot.Img
-
- All Implemented Interfaces:
ContourPlot.ImgBase<ContourPlot.Pixel>
,java.lang.Iterable<ContourPlot.Pixel>
- Enclosing class:
- ContourPlot
public class ContourPlot.Img extends java.lang.Object implements ContourPlot.ImgBase<ContourPlot.Pixel>
Image class with data stored in an int array.In contrast to
BufferedImage
the Img class only offers pixel data to be stored as integer values simplifying data retrieval and increasing performance due to less overhead and omitting color model conversions.
However the Img class can be easily used together with BufferedImages offering convenience methods likeImg(BufferedImage)
,ContourPlot.ImgBase.toBufferedImage()
orcreateRemoteImg(BufferedImage)
.Moreover the Img class targets lambda expressions introduced in Java 8 useful for per pixel operations by implementing the
Iterable
interface and providing-
ContourPlot.ImgBase.iterator()
-
ContourPlot.ImgBase.spliterator()
-
ContourPlot.ImgBase.forEach(Consumer action)
- and
ContourPlot.ImgBase.forEach(boolean parallel, Consumer action)
.
Since version 1.1 it is also possible to iterate over a specified area of the Img using
-
ContourPlot.ImgBase.iterator(int x, int y, int w, int h)
-
ContourPlot.ImgBase.spliterator(int x, int y, int w, int h)
-
ContourPlot.ImgBase.forEach(int x, int y, int w, int h, Consumer action)
- and
ContourPlot.ImgBase.forEach(boolean parallel, int x, int y, int w, int h, Consumer action)
.
Here is an example of a parallelized per pixel operation:
Img img = new Img(1024, 1024); img.forEach(true, px -> { double x = (px.getX()-512)/512.0; double y = (px.getY()-512)/512.0; double len = Math.max(Math.abs(x),Math.abs(y)); double angle = (Math.atan2(x,y)+Math.PI)*(180/Math.PI); double r = 255*Math.max(0,1-Math.abs((angle-120)/120.0)); double g = 255*Math.max(0, 1-Math.abs((angle-240)/120.0)); double b = 255*Math.max(0, angle <= 120 ? 1-Math.abs((angle)/120.0):1-Math.abs((angle-360)/120.0)); px.setRGB((int)(r*(1-len)), (int)(g*(1-len)), (int)(b*(1-len))); }); ImageSaver.saveImage(img.getRemoteBufferedImage(), "polar_colors.png");
- Since:
- 1.0
- Author:
- hageldave
-
-
Field Summary
Fields Modifier and Type Field Description static int
boundary_mode_mirror
boundary mode that will mirror the Img for out of bounds positionsstatic int
boundary_mode_repeat_edge
boundary mode that will repeat the edge of of an Img for out of bounds positions.static int
boundary_mode_repeat_image
boundary mode that will repeat the Img for out of bounds positions.static int
boundary_mode_zero
boundary mode that will return 0 for out of bounds positions.private int[]
data
data array of this Img containing a value for each pixel in row major orderprivate int
height
width and height of this imageprivate int
spliteratorMinimumSplitSize
minimum number of elements this Img'sSpliterator
s can be split to.private int
width
width and height of this image
-
Constructor Summary
Constructors Constructor Description Img(int width, int height)
Creates a new Img of specified dimensions.Img(int width, int height, int[] data)
Creates a new Img of specified dimensions.Img(java.awt.Dimension dimension)
Creates a new Img of specified Dimension.Img(java.awt.Dimension dim, int[] data)
Creates a new Img of specified dimensions.Img(java.awt.image.BufferedImage bimg)
Creates a new Img of same dimensions as provided BufferedImage.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private int
blend(int channel1, int channel2, double m)
ContourPlot.Img
copy()
Returns a deep copy of this image.ContourPlot.Img
copyArea(int x, int y, int w, int h, ContourPlot.Img dest, int destX, int destY)
Copies specified area of this Img to the specified destination Img at specified destination coordinates.ContourPlot.Img
createRemoteImg(java.awt.image.BufferedImage bimg)
Creates an Img sharing the specified BufferedImage's data.ContourPlot.Img
fill(int value)
Fills the whole image with the specified value.int[]
getData()
int
getHeight()
ContourPlot.Pixel
getPixel()
Creates a new Pixel object for this Img with position {0,0}.ContourPlot.Pixel
getPixel(int x, int y)
Creates a new Pixel object for this Img at specified position.java.awt.image.BufferedImage
getRemoteBufferedImage()
Creates a BufferedImage that shares the data of this Img.int
getSpliteratorMinimumSplitSize()
Returns the minimum number of elements in a split of aSpliterator
of this Img.int
getValue(int x, int y)
Returns the value of this Img at the specified position.int
getValue(int x, int y, int boundaryMode)
Returns the value of this Img at the specified position.int
getWidth()
int
interpolateARGB(double xNormalized, double yNormalized)
Returns a bilinearly interpolated ARGB value of the image for the specified normalized position (x and y within [0,1]).private int
interpolateColors(int c00, int c01, int c10, int c11, double mx, double my)
int
numValues()
void
setSpliteratorMinimumSplitSize(int size)
Sets the minimum number of elements in a split of aSpliterator
of this Img.void
setValue(int x, int y, int value)
Sets value at the specified position.boolean
supportsRemoteBufferedImage()
Returns true when this implementation ofContourPlot.ImgBase
supports theContourPlot.ImgBase.getRemoteBufferedImage()
method.java.awt.image.BufferedImage
toBufferedImage(java.awt.image.BufferedImage bimg)
Copies this image's data to the specifiedBufferedImage
.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface gov.nih.mipav.model.algorithms.ContourPlot.ImgBase
colSpliterator, createGraphics, forEach, forEach, forEach, forEach, forEach, forEach, forEach, forEach, forEach, forEach, forEach_defaultimpl, getDimension, iterator, iterator, paint, rowSpliterator, spliterator, spliterator, stream, stream, stream, stream, stream, stream, toBufferedImage
-
-
-
-
Field Detail
-
boundary_mode_zero
public static final int boundary_mode_zero
boundary mode that will return 0 for out of bounds positions.- Since:
- 1.0
- See Also:
getValue(int, int, int)
, Constant Field Values
-
boundary_mode_repeat_edge
public static final int boundary_mode_repeat_edge
boundary mode that will repeat the edge of of an Img for out of bounds positions.- Since:
- 1.0
- See Also:
getValue(int, int, int)
, Constant Field Values
-
boundary_mode_repeat_image
public static final int boundary_mode_repeat_image
boundary mode that will repeat the Img for out of bounds positions.- Since:
- 1.0
- See Also:
getValue(int, int, int)
, Constant Field Values
-
boundary_mode_mirror
public static final int boundary_mode_mirror
boundary mode that will mirror the Img for out of bounds positions- Since:
- 1.0
- See Also:
getValue(int, int, int)
, Constant Field Values
-
data
private final int[] data
data array of this Img containing a value for each pixel in row major order- Since:
- 1.0
-
width
private final int width
width and height of this image
-
height
private final int height
width and height of this image
-
spliteratorMinimumSplitSize
private int spliteratorMinimumSplitSize
minimum number of elements this Img'sSpliterator
s can be split to. Default value is 1024.- Since:
- 1.3
-
-
Constructor Detail
-
Img
public Img(int width, int height)
Creates a new Img of specified dimensions. Values are initialized to 0.- Parameters:
width
- of the Imgheight
- of the Img- Since:
- 1.0
-
Img
public Img(java.awt.Dimension dimension)
Creates a new Img of specified Dimension. Values are initilaized to 0.- Parameters:
dimension
- extend of the Img (width and height)- Since:
- 1.0
-
Img
public Img(java.awt.image.BufferedImage bimg)
Creates a new Img of same dimensions as provided BufferedImage. Values are copied from argument Image- Parameters:
bimg
- the BufferedImage- Since:
- 1.0
- See Also:
createRemoteImg(BufferedImage)
-
Img
public Img(int width, int height, int[] data)
Creates a new Img of specified dimensions. Provided data array will be used as this images data.- Parameters:
width
- of the Imgheight
- of the Imgdata
- values (pixels) that will be used as the content of this Img- Throws:
java.lang.IllegalArgumentException
- when the number of pixels of this Img resulting from width*height does not match the number of provided data values.- Since:
- 1.0
-
Img
public Img(java.awt.Dimension dim, int[] data)
Creates a new Img of specified dimensions. Provided data array will be used as this images data.- Parameters:
dim
- extend of the image (width and height)data
- values (pixels) that will be used as the content of this Img- Throws:
java.lang.IllegalArgumentException
- when the number of pixels of this Img resulting from width*height does not match the number of provided data values.- Since:
- 1.0
-
-
Method Detail
-
getWidth
public int getWidth()
- Specified by:
getWidth
in interfaceContourPlot.ImgBase<ContourPlot.Pixel>
- Returns:
- width of this Img
- Since:
- 1.0
- See Also:
ContourPlot.ImgBase.getHeight()
,ContourPlot.ImgBase.getDimension()
,ContourPlot.ImgBase.numValues()
-
getHeight
public int getHeight()
- Specified by:
getHeight
in interfaceContourPlot.ImgBase<ContourPlot.Pixel>
- Returns:
- height of this Img
- Since:
- 1.0
- See Also:
ContourPlot.ImgBase.getWidth()
,ContourPlot.ImgBase.getDimension()
,ContourPlot.ImgBase.numValues()
-
numValues
public int numValues()
- Specified by:
numValues
in interfaceContourPlot.ImgBase<ContourPlot.Pixel>
- Returns:
- number of values (pixels) of this Img
- Since:
- 1.0
- See Also:
ContourPlot.ImgBase.getWidth()
,ContourPlot.ImgBase.getHeight()
,ContourPlot.ImgBase.getDimension()
-
getData
public int[] getData()
- Returns:
- data array of this Img
- Since:
- 1.0
-
getValue
public int getValue(int x, int y)
Returns the value of this Img at the specified position. No bounds checks will be performed, positions outside of this image's dimension can either result in a value for a different position or an ArrayIndexOutOfBoundsException.- Parameters:
x
- coordinatey
- coordinate- Returns:
- value for specified position
- Throws:
java.lang.ArrayIndexOutOfBoundsException
- if resulting index from x and y is not within the data arrays bounds.- Since:
- 1.0
- See Also:
getValue(int, int, int)
,getPixel(int, int)
,setValue(int, int, int)
-
getValue
public int getValue(int x, int y, int boundaryMode)
Returns the value of this Img at the specified position. Bounds checks will be performed and positions outside of this image's dimensions will be handled according to the specified boundary mode.Boundary Modes
boundary_mode_zero
will return 0 for out of bounds positions.
-boundary_mode_repeat_edge
will return the same value as the nearest edge value.
-boundary_mode_repeat_image
will return a value of the image as if the if the image was repeated on all sides.
-boundary_mode_mirror
will return a value of the image as if the image was mirrored on all sides.
-other values for boundary mode
will be used as default color for out of bounds positions. It is safe to use opaque colors (0xff000000 - 0xffffffff) and transparent colors above 0x0000000f which will not collide with one of the boundary modes (number of boundary modes is limited to 16 for the future).- Parameters:
x
- coordinatey
- coordinateboundaryMode
- one of the boundary modes e.g. boundary_mode_mirror- Returns:
- value at specified position or a value depending on the boundary mode for out of bounds positions.
- Since:
- 1.0
-
interpolateARGB
public int interpolateARGB(double xNormalized, double yNormalized)
Returns a bilinearly interpolated ARGB value of the image for the specified normalized position (x and y within [0,1]). Position {0,0} denotes the image's origin (top left corner), position {1,1} denotes the opposite corner (pixel at {width-1, height-1}).An ArrayIndexOutOfBoundsException may be thrown for x and y greater than 1 or less than 0.
- Parameters:
xNormalized
- coordinate within [0,1]yNormalized
- coordinate within [0,1]- Returns:
- bilinearly interpolated ARGB value.
- Throws:
java.lang.ArrayIndexOutOfBoundsException
- when a resulting index is out of the data array's bounds, which can only happen for x and y values less than 0 or greater than 1.- Since:
- 1.0
-
interpolateColors
private int interpolateColors(int c00, int c01, int c10, int c11, double mx, double my)
-
blend
private int blend(int channel1, int channel2, double m)
-
getPixel
public ContourPlot.Pixel getPixel()
Creates a new Pixel object for this Img with position {0,0}.- Specified by:
getPixel
in interfaceContourPlot.ImgBase<ContourPlot.Pixel>
- Returns:
- a Pixel object for this Img.
- Since:
- 1.0
- See Also:
ContourPlot.ImgBase.getPixel(int, int)
-
getPixel
public ContourPlot.Pixel getPixel(int x, int y)
Creates a new Pixel object for this Img at specified position. No bounds checks are performed for x and y.Tip:
Do not use this method repeatedly while iterating the image. UseContourPlot.Pixel.setPosition(int, int)
instead to avoid excessive allocation of Pixel objects.You can also use
for(Pixel px: img){...}
syntax or theContourPlot.ImgBase.forEach(Consumer)
method to iterate this image.- Specified by:
getPixel
in interfaceContourPlot.ImgBase<ContourPlot.Pixel>
- Parameters:
x
- coordinatey
- coordinate- Returns:
- a Pixel object for this Img at {x,y}.
- Since:
- 1.0
- See Also:
getValue(int, int)
-
copyArea
public ContourPlot.Img copyArea(int x, int y, int w, int h, ContourPlot.Img dest, int destX, int destY)
Copies specified area of this Img to the specified destination Img at specified destination coordinates. If destination Img is null a new Img with the areas size will be created and the destination coordinates will be ignored so that the Img will contain all the values of the area.The specified area has to be within the bounds of this image or otherwise an IllegalArgumentException will be thrown. Only the intersecting part of the area and the destination image is copied which allows for an out of bounds destination area origin.
- Parameters:
x
- area origin in this image (x-coordinate)y
- area origin in this image (y-coordinate)w
- width of areah
- height of areadest
- destination ImgdestX
- area origin in destination Img (x-coordinate)destY
- area origin in destination Img (y-coordinate)- Returns:
- the destination Img
- Throws:
java.lang.IllegalArgumentException
- if the specified area is not within the bounds of this Img or if the size of the area is not positive.- Since:
- 1.0
-
setValue
public void setValue(int x, int y, int value)
Sets value at the specified position. No bounds checks will be performed, positions outside of this images dimension can either result in a value for a different position or an ArrayIndexOutOfBoundsException.- Parameters:
x
- coordinatey
- coordinatevalue
- to be set at specified position. e.g. 0xff0000ff for blue color- Throws:
java.lang.ArrayIndexOutOfBoundsException
- if resulting index from x and y is not within the data arrays bounds.- Since:
- 1.0
- See Also:
getValue(int, int)
-
fill
public ContourPlot.Img fill(int value)
Fills the whole image with the specified value.- Parameters:
value
- for filling image- Returns:
- this for chaining
- Since:
- 1.0
-
copy
public ContourPlot.Img copy()
Description copied from interface:ContourPlot.ImgBase
Returns a deep copy of this image. 'Deep' means that changes made to this image are NOT reflected in the copy.- Specified by:
copy
in interfaceContourPlot.ImgBase<ContourPlot.Pixel>
- Returns:
- a deep copy of this Img.
- Since:
- 1.0
-
toBufferedImage
public java.awt.image.BufferedImage toBufferedImage(java.awt.image.BufferedImage bimg)
Description copied from interface:ContourPlot.ImgBase
Copies this image's data to the specifiedBufferedImage
. This method will preserve theRaster
of the specified BufferedImage and will only modify the contents of it.- Specified by:
toBufferedImage
in interfaceContourPlot.ImgBase<ContourPlot.Pixel>
- Parameters:
bimg
- the BufferedImage- Returns:
- the specified BufferedImage
- See Also:
ContourPlot.ImgBase.toBufferedImage()
,ContourPlot.ImgBase.getRemoteBufferedImage()
-
getRemoteBufferedImage
public java.awt.image.BufferedImage getRemoteBufferedImage()
Creates a BufferedImage that shares the data of this Img. Changes in this Img are reflected in the created BufferedImage and vice versa. The created BufferedImage uses an ARGB DirectColorModel with an underlying DataBufferInt (similar toBufferedImage.TYPE_INT_ARGB
)- Specified by:
getRemoteBufferedImage
in interfaceContourPlot.ImgBase<ContourPlot.Pixel>
- Returns:
- BufferedImage sharing this Img's data.
- Since:
- 1.0
- See Also:
createRemoteImg(BufferedImage)
,ContourPlot.ImgBase.toBufferedImage()
-
supportsRemoteBufferedImage
public boolean supportsRemoteBufferedImage()
Description copied from interface:ContourPlot.ImgBase
Returns true when this implementation ofContourPlot.ImgBase
supports theContourPlot.ImgBase.getRemoteBufferedImage()
method. This by default also indicates the support for the following methods:- Specified by:
supportsRemoteBufferedImage
in interfaceContourPlot.ImgBase<ContourPlot.Pixel>
- Returns:
- true when supported, false otherwise.
-
createRemoteImg
public ContourPlot.Img createRemoteImg(java.awt.image.BufferedImage bimg)
Creates an Img sharing the specified BufferedImage's data. Changes in the BufferdImage are reflected in the created Img and vice versa.Only BufferedImages with DataBuffer of
DataBuffer.TYPE_INT
can be used since the Img class uses an int[] to store its data. An IllegalArgumentException will be thrown if a BufferedImage with a different DataBufferType is provided.- Parameters:
bimg
- BufferedImage with TYPE_INT DataBuffer.- Returns:
- Throws:
java.lang.IllegalArgumentException
- if a BufferedImage with a DataBufferType other thanDataBuffer.TYPE_INT
is provided.- Since:
- 1.0
- See Also:
getRemoteBufferedImage()
,Img(BufferedImage)
-
getSpliteratorMinimumSplitSize
public int getSpliteratorMinimumSplitSize()
Returns the minimum number of elements in a split of aSpliterator
of this Img. Spliterators will only split if they contain more elements than specified by this value. Default is 1024.- Specified by:
getSpliteratorMinimumSplitSize
in interfaceContourPlot.ImgBase<ContourPlot.Pixel>
- Returns:
- minimum number of elements of a Spliterator to allow for splitting.
- Since:
- 1.3
-
setSpliteratorMinimumSplitSize
public void setSpliteratorMinimumSplitSize(int size)
Sets the minimum number of elements in a split of aSpliterator
of this Img. Spliterators will only split if they contain more elements than specified by this value. Default is 1024.It is advised that this number is chosen carefully and with respect to the Img's size and application of the spliterator, as it can decrease performance of the parallelized methods
ContourPlot.ImgBase.forEach(boolean parallel, Consumer action)
,
ContourPlot.ImgBase.forEach(boolean parallel, int x, int y, int w, int h, Consumer action)
or
ContourPlot.ImgBase.stream(boolean parallel)
etc.
Small values cause a Spliterator to be split more often which will consume more memory compared to higher values. Special applications on small Imgs using sophisticated consumers or stream operations may justify the use of small split sizes. High values cause a Spliterator to be split less often which may cause the work items to be badly apportioned among the threads and lower throughput.- Parameters:
size
- the minimum number of elements a split covers- Throws:
java.lang.IllegalArgumentException
- if specified size is less than 1- Since:
- 1.3
-
-