ImageKodec

The ImageKodec facade. Everything is pure computation on byte arrays: no I/O and no platform types, so it behaves identically on every KMP target. It starts no threads of its own; a caller can hand the tiles of a grid image to its own threads through setTileRunner.

val bitmap = ImageKodec.decode(bytes)          // sniffs the format, dispatches
val anim = ImageKodec.decodeAnimation(bytes) // frames + delays + loop count
val format = ImageKodec.detect(bytes) // just the sniff
val info = ImageKodec.probe(bytes) // size + depth + frames, no decode
val thumb = ImageKodec.decodeScaled(bytes, 256, 256) // fitted, reduced while decoding

Functions

Link copied to clipboard

The auxiliary images of the primary image of a HEIC or AVIF file (#154): the items that an auxl reference ties to it, in the order of those references, alpha among them. Each gives its URN, the size and bit depth of its picture and the subtype bytes of its auxC; AuxiliaryImage names the URNs this library knows. A file with none, and a file of any other format, gives an empty list. No picture is decoded.

Link copied to clipboard
fun convertCmykToSrgb(width: Int, height: Int, ink: ByteArray, profile: ColorProfile, intent: RenderingIntent = RenderingIntent.Perceptual): KiteBitmap?

CMYK ink converted to opaque sRGB through the CMYK profile with intent, or null when profile is not a CMYK profile this reads. ink holds four samples a pixel, cyan, magenta, yellow and black, from no ink (0) to full (255), as a PDF's DeviceCMYK or ICCBased samples run; a CMYK JPEG's Adobe samples run the other way, and decode turns them over itself.

fun convertCmykToSrgb(width: Int, height: Int, ink: ShortArray, profile: ColorProfile, intent: RenderingIntent = RenderingIntent.Perceptual): KiteBitmap16?

convertCmykToSrgb at 16 bits a sample, as RGB at full precision.

Link copied to clipboard
fun convertToSrgb(bitmap: KiteBitmap, profile: ColorProfile, intent: RenderingIntent = RenderingIntent.Perceptual): KiteBitmap

bitmap converted to sRGB through profile with intent, as decode converts with ColorTarget.Srgb: for a bitmap decoded before, or one whose profile came from elsewhere, such as a PDF's ICCBased colour space. Every declaration of profile counts, in the order ColorProfile gives: an ICC profile, H.273 code points (HDR ones tone-mapped to SDR), and a PNG's sRGB, gAMA and cHRM. A gray profile reads the red channel. A profile this does not read, one that already is sRGB, a CMYK profile (whose ink convertCmykToSrgb takes), or a gray one with a bitmap that is not gray gives bitmap back.

fun convertToSrgb(bitmap: KiteBitmap16, profile: ColorProfile, intent: RenderingIntent = RenderingIntent.Perceptual): KiteBitmap16

convertToSrgb at 16 bits a sample, at full precision.

Link copied to clipboard
fun decode(data: ByteArray, applyOrientation: Boolean = false, colorTarget: ColorTarget = ColorTarget.Source, cancellationCheck: () -> Unit? = null): KiteBitmap

Decode data into a KiteBitmap, sniffing the format first. GIF and WebP inputs yield their first composited frame. APNG yields its default image, which may be separate from the animation; use decodeAnimation for the full sequence. A TIFF yields its first page; ImageInfo.pageCount says how many there are and decodePage decodes them.

Link copied to clipboard
fun decode16(data: ByteArray, page: Int = 0, applyOrientation: Boolean = false, colorTarget: ColorTarget = ColorTarget.Source, cancellationCheck: () -> Unit? = null): KiteBitmap16

Decode page page of data at 16 bits a sample, for the files whose low byte matters: depth maps, scientific and medical images, heavily graded photographs. decode keeps 8 bits a channel; this keeps every bit a 16-bit PNG or TIFF stores, including a TIFF palette's 16-bit ColorMap, every bit of a JPEG 2000 component up to 16 bits deep, through its palette, channel definitions and colour space, and every bit of a 12-bit JPEG or a lossless one of 9 to 16 bits, converted at that precision. A JPEG XL image keeps up to 16 bits a sample; a deeper or a float sample is rounded to 16 bits. A JPEG XR image keeps its 16-bit samples, and its linear fixed-point, half and float samples convert to sRGB at 16 bits. The channels are those the file stores once a palette is looked up (gray, gray and alpha, RGB or RGBA), so a gray depth map stays one sample a pixel. Narrower samples replicate up (an 8-bit v becomes v * 257, a 12-bit one v shl 4 or (v shr 8), a 1-bit one 0 or 65535), and every other format decodes as decode does and widens the same way, as RGB, or RGBA when the file declares transparency. KiteBitmap16.toBitmap then gives exactly what decode returns.

Link copied to clipboard
fun decodeAnimation(data: ByteArray, applyOrientation: Boolean = false, maxFrames: Int = Int.MAX_VALUE, colorTarget: ColorTarget = ColorTarget.Source, cancellationCheck: () -> Unit? = null): KiteAnimation

Decode data as an animation. GIF, APNG, animated WebP, AVIF image sequences and animated JPEG XL return every composited frame with delays and the loop count; static formats return a single zero-delay frame, so this is safe to call on anything decode accepts.

Link copied to clipboard
fun decodeAuxiliaryImage(data: ByteArray, index: Int, cancellationCheck: () -> Unit? = null): KiteBitmap16

Decode auxiliary image index of auxiliaryImages as one channel of 16-bit samples: the item's luma or gray plane as its codec gives it, at AuxiliaryImage.width by AuxiliaryImage.height, before the item's clean aperture, rotation and mirror. A coded sample v of n bits becomes v * 65535 / (2^n - 1), rounded to the nearest, so an 8-bit v is v * 257 and AuxiliaryImage.bitDepth gives v back. Nothing else changes it: a limited range is not stretched and chroma planes are left out, as alpha and depth are carried in luma alone. What a depth sample measures is in AuxiliaryImage.subtype or the file's XMP. cancellationCheck works as for decode.

Link copied to clipboard
fun decodeDownscaledTo(data: ByteArray, width: Int, height: Int, applyOrientation: Boolean = false, colorTarget: ColorTarget = ColorTarget.Source, cancellationCheck: () -> Unit? = null): KiteBitmap

Decode data at exactly width by height, the size decode(data, applyOrientation).downscaledTo(width, height) gives. The caller chooses the aspect ratio: use it for the size of a centre crop, computed from probe, or for a size a layout has already worked out. It reduces a JPEG, JPEG 2000 or JPEG XR, and a HEIC or AVIF grid, while decoding as decodeScaled does. colorTarget converts as for decode, and cancellationCheck runs as for decode.

Link copied to clipboard
fun decodeGainMap(data: ByteArray, cancellationCheck: () -> Unit? = null): HeifGainMap?

The HDR gain map of a HEIC or AVIF file, decoded, with its parameters, or null when it has none (#153): the gain map image of a tone-mapped (tmap) item that is the primary item or whose base image the primary item is, with its ISO 21496-1 metadata, else the Apple gain map auxiliary image with the MakerNote tags that go with it. HeifGainMap says what its samples mean. The map is returned, not applied. A file of any other format gives null; the gain map of an Ultra HDR JPEG comes from readMetadata. cancellationCheck works as for decode.

Link copied to clipboard
fun decodeJpegComponents(data: ByteArray, reduction: Int = 1, cancellationCheck: () -> Unit? = null): JpegComponents

Decode the JPEG in data to its stored samples: every component after the inverse DCT and the upsampling, before any color conversion. Use it when the caller, not the decoder, knows what the samples mean, such as the CMYK or YCCK ink of a PDF image. JpegComponents says how the samples are laid out.

Link copied to clipboard
fun decodeJpxComponents(data: ByteArray, reduction: Int = 1, cancellationCheck: () -> Unit? = null): JpxComponents

Decode the JPEG 2000 image in data to its stored components: every component after the inverse wavelet, the multiple component transform and the DC level shift, sampled onto the image grid, before the JP2 palette, channel definitions and colour space. Use it when the caller, not the file, says what the components mean, as a PDF's ColorSpace does for a JPXDecode image (ISO 32000-1, 8.9.5.2). JpxComponents says how the samples are laid out, and reports the file's own colour specification.

Link copied to clipboard
fun decodePage(data: ByteArray, page: Int, applyOrientation: Boolean = false, colorTarget: ColorTarget = ColorTarget.Source, cancellationCheck: () -> Unit? = null): KiteBitmap

Decode page page of data, counting from 0. A TIFF has a page for each image file directory in its main chain, as libtiff, ImageIO and libvips count them, and decode gives the first; ImageInfo.pageCount says how many there are. A HEIC or AVIF file has a page for each top-level image, the primary first, so the two eyes of a spatial photo are pages 0 and 1, as ImageInfo.stereoLeftPage and ImageInfo.stereoRightPage say. Every other format has one page. Pages are separate images with their own size, depth and orientation, not animation frames, so decodeAnimation gives a TIFF's first page alone.

Link copied to clipboard
fun decodeReduced(data: ByteArray, reduction: Int, colorTarget: ColorTarget = ColorTarget.Source, cancellationCheck: () -> Unit? = null): KiteBitmap

Decode data with each side divided by reduction, rounded up: 1, 2, 4 or 8. Use it for an image that will draw smaller than its pixels, such as a scan on a phone screen or a thumbnail. Like decode by default, it does not apply EXIF orientation.

Link copied to clipboard
fun decodeScaled(data: ByteArray, maxWidth: Int, maxHeight: Int, applyOrientation: Boolean = false, colorTarget: ColorTarget = ColorTarget.Source, cancellationCheck: () -> Unit? = null): KiteBitmap

Decode data at the size decode(data, applyOrientation).scaled(maxWidth, maxHeight) gives: fitted inside maxWidth by maxHeight with the image's own aspect ratio, and never larger than the image. Use it for a thumbnail or a preview, where the full-size pixels would only be thrown away.

Link copied to clipboard
fun decodeThumbnail(data: ByteArray, maxWidth: Int = Int.MAX_VALUE, maxHeight: Int = Int.MAX_VALUE, applyOrientation: Boolean = false, colorTarget: ColorTarget = ColorTarget.Source, cancellationCheck: () -> Unit? = null): KiteBitmap?

A thumbnail that data carries, or null when it carries none (#150): for a HEIC or AVIF file, the item with a thmb reference to its primary image, and for a JPEG, the thumbnail in its EXIF. Of several, it gives the smallest that reaches maxWidth or maxHeight, else the largest, and skips one whose codec this build lacks. The thumbnail is not scaled; use scaled for an exact box. ImageInfo.thumbnailWidth gives the largest one's size.

Link copied to clipboard

Identify data's format from its magic bytes, or null if unrecognised.

Link copied to clipboard
fun encodeAvif(bitmap: KiteBitmap, quality: Int = 50): ByteArray

Encode bitmap as an AVIF file at quality, 0 to 100 (default 50), with the other settings of HeifEncodeOptions: 8-bit 4:2:0, effort 2, tiles of 512 for a larger picture. See the overload with options.

fun encodeAvif(animation: KiteAnimation, options: HeifEncodeOptions, metadata: ImageMetadata = ImageMetadata(), cancellationCheck: () -> Unit? = null): ByteArray

Encode animation as an animated AVIF file (#177): an AV1 image sequence, a track of every frame with its delay, that decodeAnimation plays and libavif and ffmpeg read. The first frame is also the primary image item, sharing its bytes, as libavif writes a sequence, so a reader of still images shows it.

fun encodeAvif(bitmap: KiteBitmap, options: HeifEncodeOptions, metadata: ImageMetadata = ImageMetadata(), cancellationCheck: () -> Unit? = null): ByteArray

Encode bitmap as an AVIF file (#176): HEIF (ISO/IEC 23008-12) with AV1 pictures, coded by the core's own intra AV1 encoder, as the AV1 Image File Format 1.1 describes. AV1 is royalty-free, so the encoder is part of the core and needs no module.

Link copied to clipboard
fun encodeAvif16(bitmap: KiteBitmap16, options: HeifEncodeOptions, metadata: ImageMetadata = ImageMetadata(), cancellationCheck: () -> Unit? = null): ByteArray

Encode bitmap as an AVIF file of 10 or 12 bits a sample (HeifEncodeOptions.bitDepth; by default 10, or 12 when lossless), as encodeAvif writes an 8-bit bitmap. One or two channels code as gray, and so do three or four whose colour channels are equal. AV1 holds 12 bits at most, so decode16 gives each sample back rounded to the coded depth; a lossless file gives back exactly every sample that a file of its depth can give.

Link copied to clipboard

Encode bitmap as a BMP: 24-bit BI_RGB when it is opaque, or 32-bit BI_BITFIELDS under a V4 header when it carries alpha (plain 32-bit BMP alpha is officially "reserved", and half the world's readers discard it). Uncompressed and lossless either way.

Encode bitmap as a BMP of the depth options gives (#184): a palette of 1, 4 or 8 bits, with BI_RLE4 or BI_RLE8 on request, 16-bit 5-5-5, 24-bit BI_RGB or 32-bit BI_BITFIELDS with alpha. BmpEncodeOptions() gives what encodeBmp without options gives, byte for byte. Every depth but 16 is lossless.

fun encodeBmp(bitmap: KiteBitmap, options: BmpEncodeOptions, metadata: ImageMetadata): ByteArray

encodeBmp with options that also embeds metadata's ICC profile (#186): with a profile, the header is a BITMAPV5HEADER whose colour space is PROFILE_EMBEDDED, and the profile follows the pixels; without one, the file is what the overload without metadata writes. A BMP has no place for EXIF or XMP, so those are left out. readMetadata reads the profile back.

Link copied to clipboard

Encode bitmap as CCITT Group 4 fax data (ITU-T T.6), ended with EOFB, as CcittEncodeOptions gives by default. See the overload with options.

fun encodeCcitt(bitmap: KiteBitmap, options: CcittEncodeOptions, cancellationCheck: () -> Unit? = null): ByteArray

Encode bitmap as CCITT fax data (#185): Group 4, or Group 3 one- or two-dimensional, framed as options says. A pixel is black when its luma (BT.601 weights), laid over white by its alpha, is below half, so a transparent pixel is white; the coding of that bilevel page is lossless.

Link copied to clipboard
fun encodeGif(animation: KiteAnimation, dither: Boolean = true): ByteArray

Encode animation as an animated GIF89a: one shared colour table, per-frame delays, and the NETSCAPE2.0 loop count. Frames must all be canvas-sized, which is exactly what decodeAnimation produces.

fun encodeGif(bitmap: KiteBitmap, dither: Boolean = true): ByteArray

Encode bitmap as a GIF89a. Colours are quantised to the format's 256-entry limit; images that already fit encode losslessly. Alpha is reduced to GIF's single transparent index at a 50% threshold.

fun encodeGif(animation: KiteAnimation, metadata: ImageMetadata, dither: Boolean = true): ByteArray

encodeGif of animation that also writes metadata, as the still overload with metadata does.

fun encodeGif(bitmap: KiteBitmap, metadata: ImageMetadata, dither: Boolean = true): ByteArray

encodeGif that also writes metadata (#186), in application extensions before the first frame: the ICC profile in an ICCRGBG1 012 extension (ICC.1:2022, B.6), and the XMP packet in an XMP DataXMP extension with the trailer XMP Part 3 gives it. A GIF has no place for EXIF, so it is left out. The pixels are those the overload without metadata writes, and readMetadata reads both blocks back.

Link copied to clipboard
fun encodeHeic(bitmap: KiteBitmap, quality: Int = 50): ByteArray

Encode bitmap as a HEIC file at quality, 0 to 100 (default 50), with the other settings of HeifEncodeOptions: 8-bit 4:2:0, tiles of 512 for a larger picture. See the overload with options.

fun encodeHeic(animation: KiteAnimation, options: HeifEncodeOptions, metadata: ImageMetadata = ImageMetadata(), cancellationCheck: () -> Unit? = null): ByteArray

Encode animation as an animated HEIC file (#177): an HEVC image sequence written as encodeAvif writes an AV1 one, its pictures coded by the sequence encoder of the hvc1 encoder that a module registers with registerHeifEncoder (HeifEncoder.openSequence).

fun encodeHeic(bitmap: KiteBitmap, options: HeifEncodeOptions, metadata: ImageMetadata = ImageMetadata(), cancellationCheck: () -> Unit? = null): ByteArray

Encode bitmap as a HEIC file (#174): HEIF (ISO/IEC 23008-12) with HEVC pictures, coded by the intra encoder of the imagekodec-heic module, which ImageKodecHeic.install() registers.

Link copied to clipboard
fun encodeHeic16(bitmap: KiteBitmap16, options: HeifEncodeOptions, metadata: ImageMetadata = ImageMetadata(), cancellationCheck: () -> Unit? = null): ByteArray

Encode bitmap as a HEIC file of 10 or 12 bits a sample (HeifEncodeOptions.bitDepth; by default 10, or 12 when lossless), as encodeHeic writes an 8-bit bitmap. One or two channels code as gray, and so do three or four whose colour channels are equal. HEVC holds 12 bits at most, so decode16 gives each sample back rounded to the coded depth; a lossless file gives back exactly every sample that a file of its depth can give, such as the samples decode16 read from one.

Link copied to clipboard

Encode bitmap as a JBIG2 generic region with template 0 and typical prediction, in the embedded form a PDF takes, as Jbig2EncodeOptions gives by default. See the overload with options.

fun encodeJbig2(bitmap: KiteBitmap, options: Jbig2EncodeOptions, cancellationCheck: () -> Unit? = null): ByteArray

Encode bitmap as JBIG2 (ITU-T T.88, #185): one page holding one generic region, coded with the MQ arithmetic coder or, with Jbig2EncodeOptions.mmr, with T.6. A pixel is black when its luma (BT.601 weights), laid over white by its alpha, is below half, so a transparent pixel is white; the coding of that bilevel page is lossless.

Link copied to clipboard
fun encodeJp2(bitmap: KiteBitmap, quality: Int = 75): ByteArray

Encode bitmap as a lossy JPEG 2000 file (JP2) at quality 0..100 (default 75), as Jp2EncodeOptions describes with its defaults: the 9/7 wavelet over five resolution levels, code-blocks truncated until the error meets a peak signal-to-noise ratio of 20 + 0.28 * quality dB. Gray when every pixel is gray, RGB otherwise, with an opacity channel when any pixel carries alpha. decode reads it back, and so does OpenJPEG.

fun encodeJp2(bitmap: KiteBitmap, options: Jp2EncodeOptions, metadata: ImageMetadata = ImageMetadata(), cancellationCheck: () -> Unit? = null): ByteArray

Encode bitmap as JPEG 2000 with every choice of options (#180): lossy at a quality or at a compression ratio a layer, or lossless with the reversible 5/3 wavelet and colour transform, in a JP2 file or as a bare codestream, in tiles, resolution levels, code-blocks, precincts and quality layers of the sizes asked for, in any progression order. Gray when every pixel is gray, RGB otherwise, with an opacity channel, which a channel definition box (cdef) describes, when any pixel carries alpha. A lossless file decodes to bitmap exactly.

Link copied to clipboard
fun encodeJp2Wide(bitmap: KiteBitmap16, options: Jp2EncodeOptions = Jp2EncodeOptions(), metadata: ImageMetadata = ImageMetadata(), cancellationCheck: () -> Unit? = null): ByteArray

encodeJp2 for a KiteBitmap16: its channels as components, 1 gray, 2 gray and alpha, 3 RGB, 4 RGBA, at 16 bits a sample or at Jp2EncodeOptions.bitsPerSample, each sample scaled to that depth and rounded. Lossless, decode16 returns the samples of a 16-bit file exactly, and those of a shallower one as it widens them, so a bitmap decode16 read from a 12-bit file comes back the same at 12 bits.

Link copied to clipboard
fun encodeJpeg(bitmap: KiteBitmap, quality: Int = 90): ByteArray

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.

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

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.

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

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.

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

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.

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

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.

Link copied to clipboard
fun encodeJpeg16(bitmap: KiteBitmap16, options: JpegEncodeOptions, metadata: ImageMetadata = ImageMetadata(), cancellationCheck: () -> Unit? = null): ByteArray

Encode bitmap as a JPEG of JpegEncodeOptions.precision bits (#183): a 12-bit DCT file (extended sequential SOF1, progressive SOF2, or their arithmetic forms), or a lossless file of 2 to 16 bits (SOF3, or SOF11 with arithmetic coding). Each sample is the top JpegEncodeOptions.precision bits of its 16-bit value, so a lossless file of a bitmap that decode16 gave from a file of that precision comes back from decode16 the same, and one of 16 bits keeps every sample of any bitmap. One or two channels are gray, three or four RGB; alpha is discarded. An 8-bit precision writes what encodeJpeg writes of the high bytes.

Link copied to clipboard
fun encodeJxl(bitmap: KiteBitmap, quality: Int = 90): ByteArray

Encode bitmap as a lossy JPEG XL at quality 0..100 (default 90), which maps to a distance as cjxl -q maps it; see JxlEncodeOptions. The colour is coded with VarDCT in the XYB colour space and alpha, when the bitmap has any, losslessly with Modular. The result is a bare codestream (#179).

fun encodeJxl(animation: KiteAnimation, options: JxlEncodeOptions, metadata: ImageMetadata = ImageMetadata(), cancellationCheck: () -> Unit? = null): ByteArray

Encode animation as an animated JPEG XL: one frame for each of its frames, with its delay in ticks of a millisecond, and its play count (0 for forever). A frame that changes only a rectangle of the one before is coded as that rectangle over a saved copy of it. Lossy or lossless as options says (#179).

fun encodeJxl(bitmap: KiteBitmap, options: JxlEncodeOptions, metadata: ImageMetadata = ImageMetadata(), cancellationCheck: () -> Unit? = null): ByteArray

Encode bitmap as a JPEG XL with options: lossy VarDCT at a distance, or, with JxlEncodeOptions.lossless, lossless Modular, which decodes to exactly the input. A bitmap whose pixels are all gray is coded as gray when lossless, and alpha is kept only when some pixel is not opaque. metadata's ICC profile goes into the codestream, which then describes the samples; its EXIF block and XMP packet go into Exif and xml boxes, which put the codestream in the ISO BMFF container. cancellationCheck runs between the steps of the encode and may throw to stop it (#179).

Link copied to clipboard
fun encodeJxl16(bitmap: KiteBitmap16, options: JxlEncodeOptions, metadata: ImageMetadata = ImageMetadata(), cancellationCheck: () -> Unit? = null): ByteArray

encodeJxl for samples of 16 bits: gray, gray and alpha, RGB or RGBA as bitmap holds them, with a bit depth of 16 in the header. A lossless file decodes to exactly the input with decode16 (#179).

Link copied to clipboard
fun encodeJxr(bitmap: KiteBitmap, quality: Int = 90): ByteArray

Encode bitmap as a lossy JPEG XR file (#181) at quality 0..100 (default 90), which means what JxrEncApp -q means at quality divided by 100: YUV 4:4:4 and one level of overlap filtering from 50 up, 4:2:0 and two levels below. A gray opaque bitmap is written as 8bppGray, an opaque one as 24bppBGR, and one with alpha as 32bppBGRA with planar alpha, coded losslessly.

fun encodeJxr(bitmap: KiteBitmap, options: JxrEncodeOptions, metadata: ImageMetadata = ImageMetadata(), cancellationCheck: () -> Unit? = null): ByteArray

Encode bitmap as a JPEG XR file (#181) with every setting of options: lossless or at a quality, 4:4:4 or 4:2:0 chroma, the overlap mode, quantizers per channel and per tile, tiles, spatial or frequency order, and planar or interleaved alpha. The ICC profile, XMP packet and EXIF block of metadata go into the file's directory, the EXIF orientation as its Transformation tag; a ImageMetadata.gainMap is left out. cancellationCheck runs before each row of macroblocks of each codestream and may throw to stop the encode.

Link copied to clipboard
fun encodeJxr16(bitmap: KiteBitmap16, options: JxrEncodeOptions, metadata: ImageMetadata = ImageMetadata(), cancellationCheck: () -> Unit? = null): ByteArray

encodeJxr for 16-bit samples: 16bppGray, 48bppRGB or 64bppRGBA, as bitmap has one, three or four channels; gray with alpha goes as 64bppRGBA. Chroma stays 4:4:4.

Link copied to clipboard
fun encodeJxrCmyk(width: Int, height: Int, ink: ByteArray, options: JxrEncodeOptions = JxrEncodeOptions(), metadata: ImageMetadata = ImageMetadata(), cancellationCheck: () -> Unit? = null): ByteArray

Encode CMYK ink as a JPEG XR file of pixel format 32bppCMYK (#189), through T.832's reversible transform to YUVK. ink holds four samples a pixel, cyan, magenta, yellow and black, from no ink (0) to full (255), row by row from the top, as convertCmykToSrgb takes them. Give metadata the press's ICC profile, so that a reader can convert the ink; without one, decode draws it as it draws a CMYK TIFF. options works as for encodeJxr; chroma stays full, since YUVK has no subsampled form.

fun encodeJxrCmyk(width: Int, height: Int, ink: ShortArray, options: JxrEncodeOptions = JxrEncodeOptions(), metadata: ImageMetadata = ImageMetadata(), cancellationCheck: () -> Unit? = null): ByteArray

encodeJxrCmyk at 16 bits a sample, from no ink (0) to full (65535), as 64bppCMYK.

Link copied to clipboard

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.

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

fun encodePng(bitmap: KiteBitmap, metadata: ImageMetadata): ByteArray

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

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

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.

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

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.

Link copied to clipboard
fun encodePng16(bitmap: KiteBitmap16, options: PngEncodeOptions = PngEncodeOptions(), metadata: ImageMetadata = ImageMetadata(), cancellationCheck: () -> Unit? = null): ByteArray

Encode bitmap as a PNG of up to 16 bits a sample, keeping every bit decode16 gives, as encodePng with options writes an 8-bit bitmap (#182). Its channels are gray, gray and alpha, RGB or RGBA, as KiteBitmap16 holds them.

Link copied to clipboard

Encode bitmap as a TIFF: 8-bit gray when every pixel is gray, RGB otherwise, with an unassociated alpha sample when any pixel carries alpha. Lossless: decoding the result returns the exact same pixels.

Encode pages as one multi-page TIFF, a directory a page in order, each written as encodeTiff writes a single bitmap and marked with its page number. probe reports their count as pageCount, and decodePage reads each back.

fun encodeTiff(bitmap: KiteBitmap, options: TiffEncodeOptions, metadata: ImageMetadata = ImageMetadata(), cancellationCheck: () -> Unit? = null): ByteArray

Encode bitmap as a TIFF as options says (#184): the compression (none, PackBits, LZW, Deflate, CCITT Group 3 or 4, or JPEG), strips or tiles, and the samples (bi-level, gray, palette or RGB). TiffEncodeOptions() gives what encodeTiff without options gives, byte for byte. Every combination is lossless but JPEG, a bi-level page, and a gray page made from colour: decoding the result returns the exact same pixels.

fun encodeTiff(pages: List<KiteBitmap>, options: TiffEncodeOptions, metadata: ImageMetadata = ImageMetadata(), cancellationCheck: () -> Unit? = null): ByteArray

Encode pages as one multi-page TIFF, each page as encodeTiff with options writes one and marked with its page number. Every page carries metadata's ICC profile; the XMP packet and the EXIF block go with the first.

Link copied to clipboard

Encode bitmap as a 16-bit TIFF, keeping every bit decode16 gives: gray for one or two channels, RGB for three or four, the second or fourth an unassociated alpha sample. Otherwise written as encodeTiff writes an 8-bit bitmap, and lossless: decode16 returns the same samples.

Encode pages as one multi-page 16-bit TIFF, each page as encodeTiff16 writes one.

fun encodeTiff16(bitmap: KiteBitmap16, options: TiffEncodeOptions, metadata: ImageMetadata = ImageMetadata(), cancellationCheck: () -> Unit? = null): ByteArray

Encode bitmap as a 16-bit TIFF as options says: none, PackBits, LZW or Deflate, strips or tiles, gray or RGB samples. TiffPhotometric.Auto keeps the channels bitmap has, as encodeTiff16 without options does. TiffPhotometric.Rgb repeats a gray sample three times, and TiffPhotometric.Gray takes the luma of a colour pixel. Alpha is kept. Metadata and cancellation as encodeTiff with options.

fun encodeTiff16(pages: List<KiteBitmap16>, options: TiffEncodeOptions, metadata: ImageMetadata = ImageMetadata(), cancellationCheck: () -> Unit? = null): ByteArray

Encode pages as one multi-page 16-bit TIFF, each page as encodeTiff16 with options writes one.

Link copied to clipboard
fun encodeTiffCmyk(width: Int, height: Int, ink: ByteArray, options: TiffEncodeOptions = TiffEncodeOptions(), metadata: ImageMetadata = ImageMetadata(), cancellationCheck: () -> Unit? = null): ByteArray

Encode CMYK ink as a TIFF of separations (photometric 5, InkSet 1, #184), lossless. ink holds four samples a pixel, cyan, magenta, yellow and black, from no ink (0) to full (255), row by row from the top, as convertCmykToSrgb takes them. Give metadata the press's ICC profile, so that a reader can convert the ink; without one, decode draws it as libtiff's RGBA interface does.

fun encodeTiffCmyk(width: Int, height: Int, ink: ShortArray, options: TiffEncodeOptions = TiffEncodeOptions(), metadata: ImageMetadata = ImageMetadata(), cancellationCheck: () -> Unit? = null): ByteArray

encodeTiffCmyk at 16 bits a sample, from no ink (0) to full (65535).

Link copied to clipboard

Encode bitmap as a lossless WebP: decoding the result returns the exact same pixels, alpha included. It is tried as a palette when it has 256 colours or fewer, and through the subtract-green, predictor and cross-colour transforms, with LZ77 and a colour cache, and the smallest of the results is kept. WebpEncodeOptions writes lossy WebP.

fun encodeWebp(bitmap: KiteBitmap, metadata: ImageMetadata): ByteArray

encodeWebp that also writes metadata, in the extended format: VP8X, ICCP, the image, EXIF and XMP (#129).

fun encodeWebp(bitmap: KiteBitmap, quality: Int): ByteArray

Encode bitmap as a lossy WebP at quality 0..100, with the meaning of cwebp -q, and otherwise WebpEncodeOptions' defaults: a VP8 image in YUV 4:2:0, with its alpha, when any pixel carries some, kept exact in an ALPH chunk (#178).

fun encodeWebp(animation: KiteAnimation, options: WebpEncodeOptions, metadata: ImageMetadata = ImageMetadata(), cancellationCheck: () -> Unit? = null): ByteArray

Encode animation as an animated WebP (#178): an ANIM chunk with the loop count and WebpEncodeOptions.background, and an ANMF chunk for each frame with its delay to the millisecond, every frame lossless or every frame lossy as options say. Frames must all be canvas-sized, which is exactly what decodeAnimation produces. A single frame is written as a still image.

fun encodeWebp(bitmap: KiteBitmap, options: WebpEncodeOptions, metadata: ImageMetadata = ImageMetadata(), cancellationCheck: () -> Unit? = null): ByteArray

Encode bitmap as a WebP as options say (#178): lossless as encodeWebp without options writes it, or lossy, a VP8 image with its alpha in an ALPH chunk. metadata goes in the extended format, as the overload that takes only metadata writes it: VP8X, ICCP, the image, EXIF and XMP . A lossy image without alpha or metadata is the simple format, one VP8 chunk.

Link copied to clipboard

Where the video of a Motion Photo sits, or null for a file that has none (#131): a JPEG with a short MP4 video after the image, as Pixel and Samsung cameras save. Google's format names the video in XMP, with Camera:MotionPhoto and a Container:Directory item of semantic MotionPhoto and its Item:Length, or with the older Camera:MicroVideo and Camera:MicroVideoOffset, under whatever prefixes the XMP binds to their namespaces. A Samsung file puts it after a MotionPhoto_Data marker. A HEIC or AVIF file keeps it in its top-level mpvd box, with Camera:MotionPhoto 1 in its XMP (#155).

Link copied to clipboard

Read data's header and report what it says: dimensions, bit depth, declared alpha, animation frame count, EXIF orientation, page count, and whether this build can decode it. No pixels are decoded and no image-sized buffer is allocated, so this stays cheap on a 50 MP file. A TIFF reports its first page; probePage reads another.

Link copied to clipboard

probe, but null instead of throwing on unrecognised or malformed input.

Link copied to clipboard
fun probePage(data: ByteArray, page: Int): ImageInfo

probe for page page of data, counting from 0: that page's dimensions, depth, alpha, orientation and decodability, and the file's ImageInfo.pageCount. A TIFF has a page for each image file directory in its main chain, a HEIC or AVIF file one for each top-level image, the primary first, and every other format has one.

Link copied to clipboard

The EXIF block, XMP packet and ICC profile of a JPEG, PNG, WebP, JP2, AVIF, HEIC, TIFF, JPEG XL, JPEG XR, GIF or BMP file. The encoders that take an ImageMetadata write them again, so a re-encode keeps them (#129, #186). A JP2 file keeps the XMP and EXIF in UUID boxes and the profile in its colour specification (#180). A HEIF file gives those of its primary image (#149); its orientation is in irot and imir, which decode applies, never in the EXIF tag. The gain map of an Ultra HDR JPEG comes too, found through its Multi-Picture Format segment (#130); the gain map of a HEIF file, which needs a decode, comes from decodeGainMap (#153).

Link copied to clipboard

Adds codec as the decoder of its HEIF item type, so that HEIF files whose items have that type decode. A codec for a type that already has one replaces it. Safe from several threads.

Link copied to clipboard

Adds encoder as the encoder of its HEIF item type, so that encodeHeic can write HEIF files whose items have that type (#174). An encoder for a type that already has one replaces it. Safe from several threads.

Link copied to clipboard

The still photo of a Motion Photo: every byte up to the video, with the XMP that named the video rewritten to say there is none: GCamera:MotionPhoto 0, the video's directory item and the MicroVideo fields removed. A gain map item and its bytes stay. A Samsung file is cut at the end of its JPEG, with its trailer. A HEIC or AVIF file loses its mpvd box and keeps its XMP, as Motion Photo 1.0 allows. A file with no video comes back as a copy, and the photo decodes to the same pixels (#131, #155).

Link copied to clipboard
fun repackJpeg(bytes: ByteArray, progressive: Boolean = false): ByteArray

bytes, a JPEG, with the same quantized coefficients written again with Huffman tables built for the image (#132), as jpegtran -optimize -copy all does: the file gets smaller, and it decodes to the same pixels, byte for byte. Every segment outside the entropy coding stays in its order, the APPn and COM segments, the quantization tables and the frame header, and so does whatever follows the image, such as an Ultra HDR gain map, whose MPF offsets move with it. The restart interval goes.

Link copied to clipboard

Sets the TileRunner that every decode from now on hands its independent jobs to: the tiles of a HEIC or AVIF grid, in decode, decode16, decodeScaled and the other decodes of a grid image (#152), and the tiles that encodeHeic and encodeHeic16 code (#174). The default, TileRunner.Serial, runs them in order on the calling thread, which is all JS and Wasm can do. Set it once, at startup, before any decode: a decode reads the runner each time it hands out a set of jobs, so a change while it runs can apply to its later rows of tiles.

Link copied to clipboard
fun ssimulacra2(reference: KiteBitmap, distorted: KiteBitmap): Double

How close distorted looks to reference, by SSIMULACRA 2 (#134): 100 for identical images, 90 for visually lossless, 70 for artifacts hard to notice without the original, 50 for a JPEG of about quality 50, and lower, even below 0, for worse. It is a port of tools/ssimulacra2.cc of libjxl: both images go to linear sRGB, then to the XYB colour space, and six scales of SSIM and edge maps are weighed as the reference weighs them. As libjxl's ssimulacra2 tool does, a reference with alpha is scored blended onto a dark and onto a bright gray, and the worse score is returned; otherwise alpha in distorted is blended onto a mid gray.