public class RGBImage

  1. Object
  2. Image
  3. RGBImage

ImplementsActionSource

An image that stores its data as an integer RGB array internally, this image cannot be manipulated via Graphics primitives however its array is accessible and modifiable programmatically. This is very useful for 2 distinct use cases.

The first use case allows us to manipulate images in a custom way while still preserving alpha information where applicable.

The second use case allows us to store images in the Java heap which is useful for some constrained devices. In small devices images are often stored in a separate “heap” which runs out eventually, this allows us to place the image in the Java heap which is potentially more wasteful but might sometimes be more abundant.

Note that unless specified otherwise most methods inherited from Image will fail when invoked on this subclass often with a NullPointerException. This image can be drawn on graphics as usual

Constructors

public RGBImage(Image img)Converts an image to an RGB image after which the original image can be GC’d
public RGBImage(int[] rgb, int width, int height)Creates an RGB image from scratch the array isn’t copied and can be saved and manipulated

Methods

public Image subImage(int x, int y, int width, int height, boolean processAlpha)Extracts a subimage from the given image allowing us to breakdown a single large image into multiple smaller images in RAM, this actually creates a standalone version of the image for use.
public Image scaled(int width, int height)Returns a scaled version of this image image using the given width and height, this is a fast algorithm that preserves translucent information.
public void scale(int width, int height)Scale the image to the given width and height, this is a fast algorithm that preserves translucent information
public Image rotate(int degrees)Unsupported in the current version, this method will be implemented in a future release
public Image modifyAlpha(byte alpha)Creates a new image instance with the alpha channel of opaque/translucent pixels within the image using the new alpha value.
public Graphics getGraphics()This method is unsupported in this image type
public int[] getRGB()Returns a mutable array that can be used to change the appearance of the image arranged as AARRGGBB.
protected void drawImage(Graphics g, Object nativeGraphics, int x, int y)Callback invoked internally by Codename One to draw the image/frame onto the display.
protected void drawImage(Graphics g, Object nativeGraphics, int x, int y, int w, int h)Callback invoked internally by Codename One to draw the image/frame onto the display.
public boolean isOpaque()Indicates if an image should be treated as opaque, this can improve support for fast drawing of RGB images without alpha support.
public void setOpaque(boolean opaque)Sets whether this image should be treated as fully opaque.
public int getWidth()Returns the width of the image
public int getHeight()Returns the height of the image
public boolean requiresDrawImage()New label optimizations don’t invoke drawImage and instead just pass the native image directly to the underlying renderer.

Inherited methods

Constructor details

RGBImage

public RGBImage(Image img)
Converts an image to an RGB image after which the original image can be GC’d

Parameters

img Image
the image to convert to an RGB image

RGBImage

public RGBImage(int[] rgb, int width, int height)
Creates an RGB image from scratch the array isn’t copied and can be saved and manipulated

Parameters

rgb int[]
AARRGGBB array
width int
width of image
height int
height of image

Method details

subImage

public Image subImage(int x, int y, int width, int height, boolean processAlpha)
Extracts a subimage from the given image allowing us to breakdown a single large image into multiple smaller images in RAM, this actually creates a standalone version of the image for use.

Parameters

x int
the x offset from the image
y int
the y offset from the image
width int
the width of internal images
height int
the height of internal images
processAlpha boolean
whether alpha should be processed as well as part of the cutting

Returns

An array of all the possible images that can be created from the source

scaled

public Image scaled(int width, int height)
Returns a scaled version of this image image using the given width and height, this is a fast algorithm that preserves translucent information. The method accepts -1 to preserve aspect ratio in the given axis.

Parameters

width int
width for the scaling
height int
height of the scaled image

Returns

new image instance scaled to the given height and width

scale

public void scale(int width, int height)
Scale the image to the given width and height, this is a fast algorithm that preserves translucent information

Parameters

width int
width for the scaling
height int
height of the scaled image

rotate

public Image rotate(int degrees)
Unsupported in the current version, this method will be implemented in a future release

Parameters

degrees int
A degree in right angle must be larger than 0 and up to 359 degrees

Returns

new image instance with the closest possible rotation

modifyAlpha

public Image modifyAlpha(byte alpha)
Creates a new image instance with the alpha channel of opaque/translucent pixels within the image using the new alpha value. Transparent (alpha == 0) pixels remain transparent. All other pixels will have the new alpha value.

Parameters

alpha byte
New value for the entire alpha channel

Returns

Translucent/Opaque image based on the alpha value and the pixels of this image

getGraphics

public Graphics getGraphics()
This method is unsupported in this image type

Returns

Graphics object allowing us to manipulate the content of a mutable image

getRGB

public int[] getRGB()
Returns a mutable array that can be used to change the appearance of the image arranged as AARRGGBB.

Returns

ARGB int array

drawImage

protected void drawImage(Graphics g, Object nativeGraphics, int x, int y)
Callback invoked internally by Codename One to draw the image/frame onto the display. Image subclasses can override this method to perform drawing of custom image types.

Parameters

g Graphics
the graphics object
nativeGraphics Object
the underlying native graphics which might be essential for some image types
x int
the x coordinate
y int
the y coordinate

drawImage

protected void drawImage(Graphics g, Object nativeGraphics, int x, int y, int w, int h)

Callback invoked internally by Codename One to draw the image/frame onto the display. Image subclasses can override this method to perform drawing of custom image types.

RGBImage has no native peer, so the inherited scaled-draw path (g.drawImageWH(image, ...)) renders nothing. Instead, build a translate + scale affine transform on top of the graphics context’s current transform and emit drawRGB at the image’s native size – the platform pipeline (iOS Metal, Android Skia, Graphics2D, …) performs the actual scaling in hardware / native code.

Graphics.setTransform is used (rather than translateMatrix + scale) because on ports where impl.isTranslationSupported() is false (iOS), prior g.translate(int, int) calls accumulate into a per-Graphics integer translate that is baked into draw coordinates before the impl matrix is applied. A naked translateMatrix / scale composition would therefore multiply that accumulator by the scale factor, shifting the on-screen position. setTransform conjugates the matrix with T(xTranslate, yTranslate), cancelling the accumulator so the result lands at the requested coordinates on every port.

Parameters

g Graphics
the graphics object
nativeGraphics Object
the underlying native graphics which might be essential for some image types
x int
the x coordinate
y int
the y coordinate
w int
the width to occupy
h int
the height to occupy

isOpaque

public boolean isOpaque()
Indicates if an image should be treated as opaque, this can improve support for fast drawing of RGB images without alpha support.

Returns

true if the image is completely opqaque which allows for some heavy optimizations

setOpaque

public void setOpaque(boolean opaque)
Sets whether this image should be treated as fully opaque.

Parameters

opaque boolean
true to treat this image as opaque.

getWidth

public int getWidth()
Returns the width of the image

Returns

the width of the image

getHeight

public int getHeight()
Returns the height of the image

Returns

the height of the image

requiresDrawImage

public boolean requiresDrawImage()
New label optimizations don’t invoke drawImage and instead just pass the native image directly to the underlying renderer. This is problematic for some image types specifically timeline & FontImage and this method allows these classes to indicate that they need that legacy behavior of calling drawImage.

Returns

true if a drawImage call is a required