encodePng

Encode bitmap as a PNG: 8-bit RGB, or RGBA when any pixel carries alpha. Lossless: decoding the result returns the exact same pixels.


Encode animation as an animated PNG (APNG): 8-bit RGB, or RGBA when any frame carries alpha, each frame's delay to the millisecond and the animation's loop count. Lossless: decodeAnimation returns the same frames, delays and loop count, and a reader without APNG support shows the first frame. A single frame is written as a plain PNG. Frames must all be canvas-sized, which is exactly what decodeAnimation produces.

Each frame after the first stores only the rectangle that changed, replacing the canvas there or drawn over it, whichever compresses smaller.

Throws

if the frames are not all canvas-sized, or if the play count does not fit APNG's 32 bits


fun encodePng(bitmap: KiteBitmap, options: PngEncodeOptions, metadata: ImageMetadata = ImageMetadata(), cancellationCheck: () -> Unit? = null): ByteArray(source)

Encode bitmap as a PNG in the colour type, bit depth and interlace options asks for, with metadata written as the overload without options writes it (#182). Lossless: decode returns the exact same pixels.

With PngColorType.Auto, the default, the file takes the smallest type that holds the pixels exactly: gray at 1, 2, 4 or 8 bits, a palette of up to 256 colours with their alpha in tRNS, RGB, or gray or RGB with an alpha channel. Gray and RGB carry a tRNS colour key in place of alpha when every transparent pixel is one colour that no opaque pixel has. An ICC profile in metadata keeps the type gray or colour as its colour space is. The overload without options keeps writing 8-bit RGB or RGBA, byte for byte as before.

cancellationCheck runs between rows and through the compression, and may throw to stop the encode; what it throws comes out of this call.

Throws

if the fixed PngEncodeOptions.colorType or PngEncodeOptions.bitDepth cannot hold the pixels exactly, if it disagrees with the ICC profile's colour space, or if the rows need more than 2 GB


fun encodePng(animation: KiteAnimation, options: PngEncodeOptions, metadata: ImageMetadata = ImageMetadata(), cancellationCheck: () -> Unit? = null): ByteArray(source)

Encode animation as an APNG, as the overload without options does, in the colour type, bit depth and interlace options asks for, chosen once for every frame, since a PNG has one colour type (#182). PngColorType.Auto picks it by its bits a pixel at every effort; the effort sets the filters and compression of each frame. Gray and palette frames take 8 bits unless PngEncodeOptions.bitDepth asks for fewer, as ffmpeg 6 reads the frames after the first wrong at fewer. A frame is drawn over the canvas only where the type has a transparent pixel, which it has when any frame is transparent. metadata goes before the image data, as in a still. A single frame is written as encodePng with options writes a bitmap.

Throws

as the overload without options does, or as encodePng with options does


encodePng that also writes metadata: iCCP, eXIf and an iTXt chunk with the keyword XML:com.adobe.xmp, before the image data (#129).