Image

class Image(val width: Int, val height: Int)(source)

A width by height grid of RGBA pixels with 8 bits per channel, stored row by row from the top-left corner. Every pixel starts as transparent black.

Read and write pixels with get and set, or decode a PNG or JPEG file with Image.decode. fill, fillRect, fillCircle and drawLine paint shapes and draw lays another image, or a rectangle of it, over this one, all on the CPU and clipped to the image. A game turns an image into a texture it can draw with klyph.app.Assets.createTexture.

Throws

if width or height is not positive, or the image would need more than Int.MAX_VALUE bytes.

Constructors

Link copied to clipboard
constructor(width: Int, height: Int)

Types

Link copied to clipboard
object Companion

Holds the factories that decode image files.

Properties

Link copied to clipboard
val height: Int
Link copied to clipboard
val width: Int

Functions

Link copied to clipboard
fun Image.copyFrom(image: Image, x: Int, y: Int, sourceX: Int = 0, sourceY: Int = 0, sourceWidth: Int = image.width - sourceX, sourceHeight: Int = image.height - sourceY)

Copies image, or the rectangle of it sourceWidth pixels wide and sourceHeight pixels tall whose top-left corner is at column sourceX and row sourceY, into this image with that corner at column x and row y. Texels are written as given, alpha included and nothing blended, so a transparent or half-transparent pixel replaces the one under it; draw is the call that blends.

Link copied to clipboard
fun Image.draw(image: Image, x: Int, y: Int, sourceX: Int = 0, sourceY: Int = 0, sourceWidth: Int = image.width - sourceX, sourceHeight: Int = image.height - sourceY)

Draws image, or the rectangle of it sourceWidth pixels wide and sourceHeight pixels tall whose top-left corner is at column sourceX and row sourceY, over this image with that corner at column x and row y, blending each of its pixels by its alpha.

Link copied to clipboard
fun Image.drawLine(x1: Int, y1: Int, x2: Int, y2: Int, color: Color)

Sets the pixels of a line one pixel wide from column x1 and row y1 to column x2 and row y2 to color, alpha included, as Image.set does. Nothing is blended, so a transparent color clears the pixels the line covers.

Link copied to clipboard
fun Image.fill(color: Color)

Sets every pixel to color.

Link copied to clipboard
fun Image.fillCircle(centerX: Float, centerY: Float, radius: Float, color: Color)

Paints a disc of radius pixels centered on (centerX, centerY) in color, over what the image already holds.

Link copied to clipboard
fun Image.fillRect(x: Int, y: Int, width: Int, height: Int, color: Color)

Sets every pixel of the rectangle width pixels wide and height pixels tall whose top-left corner is at column x and row y to color, alpha included, as Image.set does. Nothing is blended, so a transparent color clears the rectangle; Image.draw is the call that blends.

Link copied to clipboard
operator fun get(x: Int, y: Int): Color

Returns the color of the pixel at column x and row y, counted from the top-left corner.

Link copied to clipboard
fun Image.save(path: String)

Writes this image to the file at path as a PNG file with four 8-bit channels, creating the file or replacing its contents. Image.decode reads it back pixel for pixel.

Link copied to clipboard
operator fun set(x: Int, y: Int, color: Color)

Sets the pixel at column x and row y, counted from the top-left corner, to color.

Link copied to clipboard
open override fun toString(): String

Returns the size as Image(<width>x<height>), for example Image(64x64).