Class RasterImage
A mutable image held in memory as 32-bit ARGB pixels.
Pixels are not premultiplied: 0x80FF0000 is half transparent pure
red. They are stored row by row with no padding, so pixel (x, y) is
getPixels()[y * getWidth() + x].
Unlike com.codename1.ui.Image this class needs no display and no
platform: it runs on a device, in the simulator and on a Codename One
server alike. Draw into it with RasterGraphics, read and write files with
PngCodec and JpegCodec, and hand the pixels to the UI with
Image.createImage(raster.getPixels(), w, h) when you need one.
-
Constructor Summary
ConstructorsConstructorDescriptionRasterImage(int width, int height) Creates a fully transparent image.RasterImage(int width, int height, int[] argb) Wraps an existing ARGB array without copying it. -
Method Summary
Modifier and TypeMethodDescriptioncopy()Returns a copy of this image.Returns aRasterGraphicsthat draws into this image.voidfill(int argb) Fills the whole image with one ARGB value.Returns this image mirrored left to right.Returns this image mirrored top to bottom.static RasterImagefromRGBA(int w, int h, byte[] rgba) Builds an image fromR, G, B, Abytes, row by row.intReturns the height in pixels.int[]Returns the live pixel array; writes to it change the image.intgetRGB(int x, int y) Returns the ARGB value at(x, y).intgetWidth()Returns the width in pixels.Returns this image turned 180 degrees.Returns this image turned 90 degrees counterclockwise.rotate90()Returns this image turned 90 degrees clockwise.scale(int w, int h, int filter) Returns this image resized with one of theResamplerfilters.voidsetRGB(int x, int y, int argb) Sets the ARGB value at(x, y), replacing what was there.subImage(int x, int y, int w, int h) Returns a copy of a rectangle of this image.byte[]toRGBA()Returns the pixels asR, G, B, Abytes, row by row.
-
Constructor Details
-
RasterImage
public RasterImage(int width, int height) Creates a fully transparent image.
Parameters
width: width in pixels, at least 1height: height in pixels, at least 1
-
RasterImage
public RasterImage(int width, int height, int[] argb) Wraps an existing ARGB array without copying it.
Parameters
width: width in pixelsheight: height in pixelsargb: at leastwidth * heightnon-premultiplied ARGB pixels
-
-
Method Details
-
getWidth
public int getWidth()Returns the width in pixels. -
getHeight
public int getHeight()Returns the height in pixels. -
getPixels
public int[] getPixels()Returns the live pixel array; writes to it change the image. -
getRGB
public int getRGB(int x, int y) Returns the ARGB value at
(x, y).Parameters
x: columny: row
Returns
the pixel; an
ArrayIndexOutOfBoundsExceptionorIllegalArgumentExceptionfor a point outside the image -
setRGB
public void setRGB(int x, int y, int argb) Sets the ARGB value at
(x, y), replacing what was there.Parameters
x: columny: rowargb: the new pixel
-
fill
public void fill(int argb) Fills the whole image with one ARGB value.
Parameters
argb: the pixel value
-
copy
Returns a copy of this image. -
subImage
Returns a copy of a rectangle of this image.
Parameters
x: left edgey: top edgew: widthh: height; the rectangle must lie inside the image
Returns
a new image
-
scale
Returns this image resized with one of the
Resamplerfilters.Color is filtered premultiplied, so transparent pixels do not darken the edges of what is next to them. The convolution filters give the same bytes as Pillow's
resize.Parameters
w: new widthh: new heightfilter:Resampler.NEAREST,BILINEAR,BICUBIC,LANCZOS,BOXorHAMMING
Returns
a new image
-
toRGBA
public byte[] toRGBA()Returns the pixels asR, G, B, Abytes, row by row. -
fromRGBA
Builds an image from
R, G, B, Abytes, row by row.Parameters
w: widthh: heightrgba:w * h * 4bytes, not premultiplied
Returns
a new image
-
rotate90
Returns this image turned 90 degrees clockwise. -
rotate180
Returns this image turned 180 degrees. -
rotate270
Returns this image turned 90 degrees counterclockwise. -
flipHorizontal
Returns this image mirrored left to right. -
flipVertical
Returns this image mirrored top to bottom. -
createGraphics
Returns aRasterGraphicsthat draws into this image.
-