encodeJpeg

fun encodeJpeg(bitmap: KiteBitmap, quality: Int = 90): ByteArray(source)

Encode bitmap as a baseline JPEG at quality 1..100 (default 90). Alpha is discarded (JPEG has none); quality ≤ 90 uses 4:2:0 chroma subsampling, above that 4:4:4; stb_image_write's behavior.

Throws

if quality is outside 1..100, or a side of bitmap is larger than 65535, which the frame header cannot store


fun encodeJpeg(bitmap: KiteBitmap, quality: Int, metadata: ImageMetadata): ByteArray(source)

encodeJpeg that also writes metadata: the EXIF block as an APP1 Exif segment, the XMP packet as an APP1 Adobe XMP segment and the ICC profile as APP2 ICC_PROFILE chunks, after the JFIF header (#129). An EXIF block or XMP packet too long for one segment, about 64 KB, is left out. Pixels that a decode with applyOrientation = true turned upright take ImageMetadata.upright, or the copied orientation turns them again.

A ImageMetadata.gainMap goes unchanged after the image, with an MPF segment and an XMP directory that point to it, when bitmap has its aspect ratio; otherwise it is left out together with the hdrgm fields of the XMP packet (#130).

Throws


fun encodeJpeg(bitmap: KiteBitmap, quality: Int, encoder: JpegEncoderKind): ByteArray(source)

encodeJpeg with the encoder encoder. JpegEncoderKind.Jpegli ports Google's Jpegli (#133) with the settings its library gives by default: a baseline YCbCr file with 4:2:0 chroma at every quality, Jpegli's quantization matrices scaled by the distance quality maps to, adaptive quantization and the standard Huffman tables, and no JFIF header. Its files are smaller than JpegEncoderKind.Fast's at the same quality, which means something else to each encoder, and it is several times slower.

Throws


fun encodeJpeg(bitmap: KiteBitmap, quality: Int, metadata: ImageMetadata, encoder: JpegEncoderKind): ByteArray(source)

encodeJpeg with the encoder encoder that also writes metadata, as the overload without an encoder does. JpegEncoderKind.Jpegli writes no JFIF header, so its segments come right after SOI.

Throws


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

Encode bitmap as a JPEG the way options ask (#183): baseline, progressive, arithmetic coded or lossless, one component or three at the subsampling chosen, with optimized Huffman tables and restart markers, and metadata written as the overload with metadata writes it. Alpha is discarded. The defaults of JpegEncodeOptions write what encodeJpeg(bitmap, 90) writes, except that a gray bitmap becomes one component.

A lossless file from an 8-bit bitmap keeps every sample: decode returns the same pixels. cancellationCheck, when given, runs once a row of MCUs and once a scan, and may throw to stop the encode; what it throws comes out of this call as it is.

Throws

if JpegEncodeOptions.precision is not 8, which encodeJpeg16 writes, if a side of bitmap is larger than 65535, or if a lossless restart interval does not hold whole rows