Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.

public final class RasterImage

  1. Object
  2. 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.

Constructors

public RasterImage(int width, int height)Creates a fully transparent image.
public RasterImage(int width, int height, int[] argb)Wraps an existing ARGB array without copying it.

Methods

public int getWidth()Returns the width in pixels.
public int getHeight()Returns the height in pixels.
public int[] getPixels()Returns the live pixel array; writes to it change the image.
public int getRGB(int x, int y)Returns the ARGB value at (x, y).
public void setRGB(int x, int y, int argb)Sets the ARGB value at (x, y), replacing what was there.
public void fill(int argb)Fills the whole image with one ARGB value.
public RasterImage copy()Returns a copy of this image.
public RasterImage subImage(int x, int y, int w, int h)Returns a copy of a rectangle of this image.
public RasterImage scale(int w, int h, int filter)Returns this image resized with one of the Resampler filters.
public byte[] toRGBA()Returns the pixels as R, G, B, A bytes, row by row.
public static RasterImage fromRGBA(int w, int h, byte[] rgba)Builds an image from R, G, B, A bytes, row by row.
public RasterImage rotate90()Returns this image turned 90 degrees clockwise.
public RasterImage rotate180()Returns this image turned 180 degrees.
public RasterImage rotate270()Returns this image turned 90 degrees counterclockwise.
public RasterImage flipHorizontal()Returns this image mirrored left to right.
public RasterImage flipVertical()Returns this image mirrored top to bottom.
public RasterGraphics createGraphics()Returns a RasterGraphics that draws into this image.

Inherited methods

Constructor details

RasterImage

public RasterImage(int width, int height)
Creates a fully transparent image.

Parameters

width int
width in pixels, at least 1
height int
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 int
width in pixels
height int
height in pixels
argb int[]
at least width * height non-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 int
column
y int
row

Returns

the pixel; an ArrayIndexOutOfBoundsException or IllegalArgumentException for 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 int
column
y int
row
argb int
the new pixel

fill

public void fill(int argb)
Fills the whole image with one ARGB value.

Parameters

argb int
the pixel value

copy

public RasterImage copy()
Returns a copy of this image.

subImage

public RasterImage subImage(int x, int y, int w, int h)
Returns a copy of a rectangle of this image.

Parameters

x int
left edge
y int
top edge
w int
width
h int
height; the rectangle must lie inside the image

Returns

a new image

scale

public RasterImage scale(int w, int h, int filter)

Returns this image resized with one of the Resampler filters.

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 int
new width
h int
new height
filter int
Resampler.NEAREST, BILINEAR, BICUBIC, LANCZOS, BOX or HAMMING

Returns

a new image

toRGBA

public byte[] toRGBA()
Returns the pixels as R, G, B, A bytes, row by row.

fromRGBA

public static RasterImage fromRGBA(int w, int h, byte[] rgba)
Builds an image from R, G, B, A bytes, row by row.

Parameters

w int
width
h int
height
rgba byte[]
w * h * 4 bytes, not premultiplied

Returns

a new image

rotate90

public RasterImage rotate90()
Returns this image turned 90 degrees clockwise.

rotate180

public RasterImage rotate180()
Returns this image turned 180 degrees.

rotate270

public RasterImage rotate270()
Returns this image turned 90 degrees counterclockwise.

flipHorizontal

public RasterImage flipHorizontal()
Returns this image mirrored left to right.

flipVertical

public RasterImage flipVertical()
Returns this image mirrored top to bottom.

createGraphics

public RasterGraphics createGraphics()
Returns a RasterGraphics that draws into this image.