Class RasterImage

java.lang.Object
com.codename1.backend.image.RasterImage

public final class RasterImage extends Object

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 Details

    • RasterImage

      public RasterImage(int width, int height)

      Creates a fully transparent image.

      Parameters
      • width: width in pixels, at least 1
      • height: 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 pixels
      • height: height in pixels
      • argb: 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: column
      • y: 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: column
      • y: row
      • argb: the new pixel
    • fill

      public void fill(int argb)

      Fills the whole image with one ARGB value.

      Parameters
      • argb: 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: left edge
      • y: top edge
      • w: width
      • h: 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: new width
      • h: new height
      • filter: 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: width
      • h: height
      • rgba: 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.