copyFrom

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)(source)

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.

Unlike draw, nothing is clipped: both rectangles must lie inside their images. Without a source rectangle the whole of image is copied; sourceWidth and sourceHeight default to the rest of image right of and below the source corner. image is never changed, and may be this image, which is then read as it was before the call. A sourceWidth or sourceHeight of 0 copies nothing. Pass sourceX, sourceY, sourceWidth and sourceHeight by name; x and y are a conventional pair.

Throws

if sourceX, sourceY, sourceWidth or sourceHeight is negative, if the source rectangle runs past the right or bottom edge of image, or if the rectangle at x and y does not lie inside this image.

Samples

val stamp = Image(2, 2)
stamp.fill(Color.RED.withAlpha(0.5f))
val sheet = Image(4, 4)
sheet.fill(Color.WHITE)
sheet.copyFrom(stamp, x = 1, y = 1)
// Every texel is written as it is, so the half-transparent red replaces the white instead of blending with it.
assertPrints(sheet[1, 1], "Color(#ff000080)")
assertPrints(sheet[0, 0], "Color(#ffffffff)")
// Nothing is clipped: a rectangle that does not fit is rejected.
assertFailsWith<IllegalArgumentException> { sheet.copyFrom(stamp, x = 3, y = 3) }