Package com.sksamuel.scrimage
Class AwtImage
- java.lang.Object
-
- com.sksamuel.scrimage.AwtImage
-
- Direct Known Subclasses:
MutableImage
public class AwtImage extends java.lang.Object
Wraps an AWT BufferedImage with some basic helper functions related to sizes, pixels etc.It also includes methods to write out the image.
None of the operations in this class will mutate the underlying awt buffer.
-
-
Constructor Summary
Constructors Constructor Description AwtImage(java.awt.image.BufferedImage awt)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description int[][]
argb()
Returns the ARGB components for all pixels in this imageint[]
argb(int x, int y)
Returns the ARGB components for the pixel at the given coordinatesint[]
argbints()
Returns the ARGB components for all pixels in this image as packed ints.RGBColor
average()
Returns the average colour of all pixels in this imagejava.awt.image.BufferedImage
awt()
Returns the [BufferedImage] that this Image is wrapping.Pixel
bottomLeftPixel()
Pixel
bottomRightPixel()
byte[]
bytes(ImageWriter writer)
java.awt.Point
center()
The centre coordinates for the image.int
centreX()
int
centreY()
AwtImage
clone(int imageType)
Returns a copy of this image with the backing image type set to the given value.Pixel[]
col(int x)
RGBColor
color(int x, int y)
Returns the color at the given coordinates.RGBColor[]
colors()
Returns the colors of this image represented as an array of RGBColor.java.util.Set<RGBColor>
colours()
Returns a set of the distinct colours used in this image.boolean
contains(java.awt.Color color)
Returns true if a pixel with the given color existsint
count()
Returns the number of pixels in the image.long
count(java.awt.Color color)
Counts the number of pixels with the given colour.long
count(java.util.function.Predicate<Pixel> p)
Counts the number of pixels that are true for the given predicateDimension
dimensions()
AwtImage
empty()
Returns a new AWTImage with the same dimensions and same AWT type.boolean
equals(java.lang.Object other)
boolean
exists(java.util.function.Predicate<Pixel> p)
Returns true if the predicate is true for at least one pixel on the image.protected java.awt.image.BufferedImage
fastScaleAwt(int targetWidth, int targetHeight)
Returns a new AWT BufferedImage scaled using nearest-neighbour.protected java.awt.image.BufferedImage
fastScaleScrimage(int targetWidth, int targetHeight)
boolean
forAll(java.util.function.Predicate<Pixel> predicate)
Returns true if the given predicate holds for all pixels in the image.void
forEach(java.util.function.Consumer<Pixel> fn)
Executes the given side effecting function on each pixel.WriteContext
forWriter(ImageWriter writer)
int
getType()
Returns the AWT type of this image.boolean
hasAlpha()
Returns true if this image supports transparency/alpha in its underlying data model.int
hashCode()
boolean
hasTransparency()
Deprecated.boolean
isFilled(java.awt.Color color)
Returns true if all the pixels on this image are a single color.java.util.Iterator<Pixel>
iterator()
Returns the pixels of the image as an iterator.int
offset(int x, int y)
java.io.File
output(ImageWriter writer, java.io.File file)
java.nio.file.Path
output(ImageWriter writer, java.lang.String path)
java.nio.file.Path
output(ImageWriter writer, java.nio.file.Path path)
Pixel[]
patch(int x, int y, int patchWidth, int patchHeight)
Pixel[][]
patches(int patchWidth, int patchHeight)
Returns all the patches of a given size in the image, assuming pixel alignment (no subpixel extraction).Pixel
pixel(int x, int y)
Returns the pixel at the given coordinates.Pixel
pixel(java.awt.Point p)
Returns the pixel at the given coordinate.Pixel[]
pixels()
Returns the pixels of this image represented as an array of Pixels.Pixel[]
pixels(int x, int y, int w, int h)
Returns a rectangular region within the given boundaries as a single dimensional array of Pixels.java.awt.Point[]
points()
Returns an array of every point in the image, useful if you want to be able to iterate over all the coordinates.int
radius()
The radius of the image defined as the centre to the corners.double
ratio()
int[][]
rgb()
int[]
rgb(int x, int y)
protected java.awt.image.BufferedImage
rotateByRadians(Radians angle, java.awt.Color bgcolor)
Returns a new AWT Image rotated with the given angle (in radians)Pixel[]
row(int y)
Pixel[][]
rows()
Returns an array of rows, where each row is itself an array of pixels in that row.protected java.awt.image.BufferedImage
scale(int targetWidth, int targetHeight, Scale scale)
java.io.ByteArrayInputStream
stream(ImageWriter writer)
Deprecated.pointless method that simply wraps bytes()int
subpixel(double x, double y)
Uses linear interpolation to get a sub-pixel.ImmutableImage
toImmutableImage()
java.awt.image.BufferedImage
toNewBufferedImage(int type)
Returns a new AWT BufferedImage from this image.Pixel
topLeftPixel()
Programatically returns the origin point of top left.Pixel
topRightPixel()
java.lang.String
toString()
-
-
-
Method Detail
-
toImmutableImage
public ImmutableImage toImmutableImage()
-
awt
public java.awt.image.BufferedImage awt()
Returns the [BufferedImage] that this Image is wrapping.
-
center
public java.awt.Point center()
The centre coordinates for the image.
-
centreX
public int centreX()
-
centreY
public int centreY()
-
radius
public int radius()
The radius of the image defined as the centre to the corners.
-
dimensions
public Dimension dimensions()
-
ratio
public double ratio()
- Returns:
- Returns the aspect ratio for this image.
-
getType
public int getType()
Returns the AWT type of this image.
-
colors
public RGBColor[] colors()
Returns the colors of this image represented as an array of RGBColor.
-
pixels
public Pixel[] pixels()
Returns the pixels of this image represented as an array of Pixels.
-
iterator
public java.util.Iterator<Pixel> iterator()
Returns the pixels of the image as an iterator.The iterator is the most efficient way to lazily iterator over the pixels as the pixels will only be fetched from the raster as needed.
- Returns:
- the iterator
-
pixel
public Pixel pixel(int x, int y)
Returns the pixel at the given coordinates.- Parameters:
x
- the x coordinate of the pixel to graby
- the y coordinate of the pixel to grab- Returns:
- the Pixel at the location
-
pixel
public Pixel pixel(java.awt.Point p)
Returns the pixel at the given coordinate.- Parameters:
p
- the pixel as an integer tuple- Returns:
- the pixel
-
points
public java.awt.Point[] points()
Returns an array of every point in the image, useful if you want to be able to iterate over all the coordinates.If you want the actual pixel values of every point then use pixels().
-
count
public int count()
Returns the number of pixels in the image.- Returns:
- the number of pixels
-
forEach
public void forEach(java.util.function.Consumer<Pixel> fn)
Executes the given side effecting function on each pixel.- Parameters:
fn
- a function that accepts a pixel
-
row
public Pixel[] row(int y)
- Returns:
- the pixels in the row identified by the y coordinate. 0 indexed.
-
rows
public Pixel[][] rows()
Returns an array of rows, where each row is itself an array of pixels in that row.
-
col
public Pixel[] col(int x)
- Returns:
- the pixels for the column identified by the x co-ordinate. 0 indexed.
-
contains
public boolean contains(java.awt.Color color)
Returns true if a pixel with the given color exists- Parameters:
color
- the pixel colour to look for.- Returns:
- true if there exists at least one pixel that has the given pixels color
-
exists
public boolean exists(java.util.function.Predicate<Pixel> p)
Returns true if the predicate is true for at least one pixel on the image.- Parameters:
p
- a predicate- Returns:
- true if p holds for at least one pixel
-
color
public RGBColor color(int x, int y)
Returns the color at the given coordinates.- Returns:
- the RGBColor value at the coords
-
argb
public int[] argb(int x, int y)
Returns the ARGB components for the pixel at the given coordinates- Parameters:
x
- the x coordinate of the pixel component to graby
- the y coordinate of the pixel component to grab- Returns:
- an array containing ARGB components in that order.
-
argb
public int[][] argb()
Returns the ARGB components for all pixels in this image- Returns:
- an array containing an array for each ARGB components in that order.
-
argbints
public int[] argbints()
Returns the ARGB components for all pixels in this image as packed ints.- Returns:
- an array containing the packed ARGB int for each pixel.
-
rgb
public int[] rgb(int x, int y)
-
rgb
public int[][] rgb()
-
pixels
public Pixel[] pixels(int x, int y, int w, int h)
Returns a rectangular region within the given boundaries as a single dimensional array of Pixels.Eg, pixels(10, 10, 30, 20) would result in an array of size 600 with the first row of the region in indexes 0,..,29, second row 30,..,59 etc.
- Parameters:
x
- the start x coordinatey
- the start y coordinatew
- the width of the regionh
- the height of the region- Returns:
- an Array of pixels for the region
-
patch
public Pixel[] patch(int x, int y, int patchWidth, int patchHeight)
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
subpixel
public int subpixel(double x, double y)
Uses linear interpolation to get a sub-pixel.Legal values for `x` and `y` are in [0, width) and [0, height), respectively.
-
patches
public Pixel[][] patches(int patchWidth, int patchHeight)
Returns all the patches of a given size in the image, assuming pixel alignment (no subpixel extraction).The patches are returned as an array of of pixel matrices arrays.
-
offset
public int offset(int x, int y)
-
colours
public java.util.Set<RGBColor> colours()
Returns a set of the distinct colours used in this image.- Returns:
- the set of distinct Colors
-
count
public long count(java.awt.Color color)
Counts the number of pixels with the given colour.- Parameters:
color
- the colour to detect.- Returns:
- the number of pixels that matched the colour of the given pixel
-
count
public long count(java.util.function.Predicate<Pixel> p)
Counts the number of pixels that are true for the given predicate- Parameters:
p
- a predicate- Returns:
- the number of pixels that evaluated true
-
toNewBufferedImage
public java.awt.image.BufferedImage toNewBufferedImage(int type)
Returns a new AWT BufferedImage from this image.- Parameters:
type
- the type of buffered image to create, if not specified then defaults to the current image type- Returns:
- a new, non-shared, BufferedImage with the same data as this Image.
-
empty
public AwtImage empty()
Returns a new AWTImage with the same dimensions and same AWT type. The data is uninitialized.
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object other)
- Overrides:
equals
in classjava.lang.Object
-
scale
protected java.awt.image.BufferedImage scale(int targetWidth, int targetHeight, Scale scale)
-
fastScaleScrimage
protected java.awt.image.BufferedImage fastScaleScrimage(int targetWidth, int targetHeight)
-
fastScaleAwt
protected java.awt.image.BufferedImage fastScaleAwt(int targetWidth, int targetHeight)
Returns a new AWT BufferedImage scaled using nearest-neighbour.
-
rotateByRadians
protected java.awt.image.BufferedImage rotateByRadians(Radians angle, java.awt.Color bgcolor)
Returns a new AWT Image rotated with the given angle (in radians)
-
forAll
public boolean forAll(java.util.function.Predicate<Pixel> predicate)
Returns true if the given predicate holds for all pixels in the image.
-
topLeftPixel
public Pixel topLeftPixel()
Programatically returns the origin point of top left.
-
bottomLeftPixel
public Pixel bottomLeftPixel()
-
topRightPixel
public Pixel topRightPixel()
-
bottomRightPixel
public Pixel bottomRightPixel()
-
hasAlpha
public boolean hasAlpha()
Returns true if this image supports transparency/alpha in its underlying data model.
-
hasTransparency
@Deprecated public boolean hasTransparency()
Deprecated.Returns true if this image supports transparency/alpha in its underlying data model.
-
average
public RGBColor average()
Returns the average colour of all pixels in this image
-
isFilled
public boolean isFilled(java.awt.Color color)
Returns true if all the pixels on this image are a single color.- Parameters:
color
- the color to test pixels against
-
output
public java.nio.file.Path output(ImageWriter writer, java.lang.String path) throws java.io.IOException
- Throws:
java.io.IOException
-
output
public java.io.File output(ImageWriter writer, java.io.File file) throws java.io.IOException
- Throws:
java.io.IOException
-
output
public java.nio.file.Path output(ImageWriter writer, java.nio.file.Path path) throws java.io.IOException
- Throws:
java.io.IOException
-
bytes
public byte[] bytes(ImageWriter writer) throws java.io.IOException
- Throws:
java.io.IOException
-
forWriter
public WriteContext forWriter(ImageWriter writer)
-
stream
@Deprecated public java.io.ByteArrayInputStream stream(ImageWriter writer) throws java.io.IOException
Deprecated.pointless method that simply wraps bytes()Returns a byte array stream consisting of the pixels of this image written out using the supplied writer.- Throws:
java.io.IOException
-
clone
public AwtImage clone(int imageType)
Returns a copy of this image with the backing image type set to the given value.
-
-