GifEncoder

actual class GifEncoder @JvmOverloads constructor(sink: Sink, colorDifferenceTolerance: Double, quantizedColorDifferenceTolerance: Double, loopCount: Int, maxColors: Int, colorQuantizer: ColorQuantizer, colorSimilarityChecker: ColorSimilarityChecker, comment: String, transparentAlphaThreshold: Int, alphaFill: Int, cropTransparent: Boolean, minimumFrameDurationCentiseconds: Int, onFrameWritten: (framesWritten: Int, writtenDuration: Duration) -> Unit) : AutoCloseable(source)

A class for encoding GIF files. The dimensions of the created GIF are determined by the first frame written. Subsequent frames will have the same dimensions as the first frame, cropping or padding the frame to fit if necessary. The encoder must be closed after use to ensure all data is written correctly.

Usage:

// Obtain a Path to write the GIF data to
val path: Path = ...
val sink = SystemFileSystem.sink(path).buffered()
val encoder = GifEncoder(sink)

val argb: IntArray = ...
val width: Int = ...
val height: Int = ...
val duration: Duration = ...
encoder.writeFrame(argb, width, height, duration)

encoder.close()

Parameters

sink

The Sink to write the GIF data to.

colorDifferenceTolerance

The tolerance for color difference used by colorSimilarityChecker when performing transparency optimization. This optimization works by making pixels that are similar to the pixel in the previous frame transparent, resulting in only pixels that are different being saved. Higher values will result in a smaller file size at the cost of visual artifacts. This optimization is most effective on animations with large static areas.

The default value of 0 slightly reduces file size with no visual quality loss.

A value of around 0.01 provides a good tradeoff between file size and quality.

Set to -1 to disable transparency optimization.

quantizedColorDifferenceTolerance

The tolerance for color difference used by colorSimilarityChecker when performing transparency optimization after quantization. This optimization works by making pixels that are similar to the pixel in the previous frame transparent, resulting in only pixels that are different being saved. Higher values will result in a smaller file size at the cost of visual artifacts. This optimization is most effective on animations with large static areas.

A value of around 0.02 provides a good tradeoff between file size and quality.

Set to -1 to disable transparency optimization.

loopCount

The number of times the GIF should loop.

Set to 0 for infinite looping.

Set to -1 for no looping.

maxColors

The maximum number of colors in each frame.

Must be between 1 and 256 inclusive.

colorQuantizer

The ColorQuantizer to use for reducing the number of colors in each frame to maxColors.

colorSimilarityChecker

The ColorSimilarityChecker to use for determining if two frames are similar enough to merge.

comment

An optional comment to include in the GIF comment block metadata.

transparentAlphaThreshold

The alpha threshold for a pixel to be considered transparent. Pixels with an alpha value equal to or less than this value will be treated as fully transparent.

Must be between 0 and 255 inclusive.

alphaFill

The solid RGB color to use for filling in pixels with partial alpha transparency, as GIFs do not support partial transparency.

Set to -1 to disable filling.

cropTransparent

Whether to crop the transparent pixels from the edges of each frame. This can reduce the size of the GIF by a small amount.

minimumFrameDurationCentiseconds

The minimum duration for each frame in centiseconds. Setting this to a value less than 2 can result in the GIF being played slower than expected on some GIF viewers.

Must be positive.

onFrameWritten

A callback that is invoked after each frame is written, providing the number of frames written and the total duration of all the frames written so far. This can be used to track progress or update a UI.

Throws

If any of the parameters are invalid.

expect class GifEncoder constructor(sink: Sink, colorDifferenceTolerance: Double = 0.0, quantizedColorDifferenceTolerance: Double = -1.0, loopCount: Int = 0, maxColors: Int = GIF_MAX_COLORS, colorQuantizer: ColorQuantizer = ColorQuantizer.NEU_QUANT, colorSimilarityChecker: ColorSimilarityChecker = ColorSimilarityChecker.EUCLIDEAN_LUMINANCE_WEIGHTING, comment: String = "", transparentAlphaThreshold: Int = 20, alphaFill: Int = -1, cropTransparent: Boolean = true, minimumFrameDurationCentiseconds: Int = GIF_MINIMUM_FRAME_DURATION_CENTISECONDS, onFrameWritten: (framesWritten: Int, writtenDuration: Duration) -> Unit = { _, _ -> }) : AutoCloseable(source)

A class for encoding GIF files. The dimensions of the created GIF are determined by the first frame written. Subsequent frames will have the same dimensions as the first frame, cropping or padding the frame to fit if necessary. The encoder must be closed after use to ensure all data is written correctly.

Usage:

// Obtain a Path to write the GIF data to
val path: Path = ...
val sink = SystemFileSystem.sink(path).buffered()
val encoder = GifEncoder(sink)

val argb: IntArray = ...
val width: Int = ...
val height: Int = ...
val duration: Duration = ...
encoder.writeFrame(argb, width, height, duration)

encoder.close()

Parameters

sink

The Sink to write the GIF data to.

colorDifferenceTolerance

The tolerance for color difference used by colorSimilarityChecker when performing transparency optimization. This optimization works by making pixels that are similar to the pixel in the previous frame transparent, resulting in only pixels that are different being saved. Higher values will result in a smaller file size at the cost of visual artifacts. This optimization is most effective on animations with large static areas.

The default value of 0 slightly reduces file size with no visual quality loss.

A value of around 0.01 provides a good tradeoff between file size and quality.

Set to -1 to disable transparency optimization.

quantizedColorDifferenceTolerance

The tolerance for color difference used by colorSimilarityChecker when performing transparency optimization after quantization. This optimization works by making pixels that are similar to the pixel in the previous frame transparent, resulting in only pixels that are different being saved. Higher values will result in a smaller file size at the cost of visual artifacts. This optimization is most effective on animations with large static areas.

A value of around 0.02 provides a good tradeoff between file size and quality.

Set to -1 to disable transparency optimization.

loopCount

The number of times the GIF should loop.

Set to 0 for infinite looping.

Set to -1 for no looping.

maxColors

The maximum number of colors in each frame.

Must be between 1 and 256 inclusive.

colorQuantizer

The ColorQuantizer to use for reducing the number of colors in each frame to maxColors.

colorSimilarityChecker

The ColorSimilarityChecker to use for determining if two frames are similar enough to merge.

comment

An optional comment to include in the GIF comment block metadata.

transparentAlphaThreshold

The alpha threshold for a pixel to be considered transparent. Pixels with an alpha value equal to or less than this value will be treated as fully transparent.

Must be between 0 and 255 inclusive.

alphaFill

The solid RGB color to use for filling in pixels with partial alpha transparency, as GIFs do not support partial transparency.

Set to -1 to disable filling.

cropTransparent

Whether to crop the transparent pixels from the edges of each frame. This can reduce the size of the GIF by a small amount.

minimumFrameDurationCentiseconds

The minimum duration for each frame in centiseconds. Setting this to a value less than 2 can result in the GIF being played slower than expected on some GIF viewers.

Must be positive.

onFrameWritten

A callback that is invoked after each frame is written, providing the number of frames written and the total duration of all the frames written so far. This can be used to track progress or update a UI.

Throws

If any of the parameters are invalid.

actual class GifEncoder @JvmOverloads constructor(sink: Sink, colorDifferenceTolerance: Double, quantizedColorDifferenceTolerance: Double, loopCount: Int, maxColors: Int, colorQuantizer: ColorQuantizer, colorSimilarityChecker: ColorSimilarityChecker, comment: String, transparentAlphaThreshold: Int, alphaFill: Int, cropTransparent: Boolean, minimumFrameDurationCentiseconds: Int, onFrameWritten: (framesWritten: Int, writtenDuration: Duration) -> Unit) : AutoCloseable(source)

A class for encoding GIF files. The dimensions of the created GIF are determined by the first frame written. Subsequent frames will have the same dimensions as the first frame, cropping or padding the frame to fit if necessary. The encoder must be closed after use to ensure all data is written correctly.

Usage:

// Obtain a Path to write the GIF data to
val path: Path = ...
val sink = SystemFileSystem.sink(path).buffered()
val encoder = GifEncoder(sink)

val argb: IntArray = ...
val width: Int = ...
val height: Int = ...
val duration: Duration = ...
encoder.writeFrame(argb, width, height, duration)

encoder.close()

Parameters

sink

The Sink to write the GIF data to.

colorDifferenceTolerance

The tolerance for color difference used by colorSimilarityChecker when performing transparency optimization. This optimization works by making pixels that are similar to the pixel in the previous frame transparent, resulting in only pixels that are different being saved. Higher values will result in a smaller file size at the cost of visual artifacts. This optimization is most effective on animations with large static areas.

The default value of 0 slightly reduces file size with no visual quality loss.

A value of around 0.01 provides a good tradeoff between file size and quality.

Set to -1 to disable transparency optimization.

quantizedColorDifferenceTolerance

The tolerance for color difference used by colorSimilarityChecker when performing transparency optimization after quantization. This optimization works by making pixels that are similar to the pixel in the previous frame transparent, resulting in only pixels that are different being saved. Higher values will result in a smaller file size at the cost of visual artifacts. This optimization is most effective on animations with large static areas.

A value of around 0.02 provides a good tradeoff between file size and quality.

Set to -1 to disable transparency optimization.

loopCount

The number of times the GIF should loop.

Set to 0 for infinite looping.

Set to -1 for no looping.

maxColors

The maximum number of colors in each frame.

Must be between 1 and 256 inclusive.

colorQuantizer

The ColorQuantizer to use for reducing the number of colors in each frame to maxColors.

colorSimilarityChecker

The ColorSimilarityChecker to use for determining if two frames are similar enough to merge.

comment

An optional comment to include in the GIF comment block metadata.

transparentAlphaThreshold

The alpha threshold for a pixel to be considered transparent. Pixels with an alpha value equal to or less than this value will be treated as fully transparent.

Must be between 0 and 255 inclusive.

alphaFill

The solid RGB color to use for filling in pixels with partial alpha transparency, as GIFs do not support partial transparency.

Set to -1 to disable filling.

cropTransparent

Whether to crop the transparent pixels from the edges of each frame. This can reduce the size of the GIF by a small amount.

minimumFrameDurationCentiseconds

The minimum duration for each frame in centiseconds. Setting this to a value less than 2 can result in the GIF being played slower than expected on some GIF viewers.

Must be positive.

onFrameWritten

A callback that is invoked after each frame is written, providing the number of frames written and the total duration of all the frames written so far. This can be used to track progress or update a UI.

Throws

If any of the parameters are invalid.

actual class GifEncoder constructor(sink: Sink, colorDifferenceTolerance: Double, quantizedColorDifferenceTolerance: Double, loopCount: Int, maxColors: Int, colorQuantizer: ColorQuantizer, colorSimilarityChecker: ColorSimilarityChecker, comment: String, transparentAlphaThreshold: Int, alphaFill: Int, cropTransparent: Boolean, minimumFrameDurationCentiseconds: Int, onFrameWritten: (framesWritten: Int, writtenDuration: Duration) -> Unit) : AutoCloseable(source)

A class for encoding GIF files. The dimensions of the created GIF are determined by the first frame written. Subsequent frames will have the same dimensions as the first frame, cropping or padding the frame to fit if necessary. The encoder must be closed after use to ensure all data is written correctly.

Usage:

// Obtain a Path to write the GIF data to
val path: Path = ...
val sink = SystemFileSystem.sink(path).buffered()
val encoder = GifEncoder(sink)

val argb: IntArray = ...
val width: Int = ...
val height: Int = ...
val duration: Duration = ...
encoder.writeFrame(argb, width, height, duration)

encoder.close()

Parameters

sink

The Sink to write the GIF data to.

colorDifferenceTolerance

The tolerance for color difference used by colorSimilarityChecker when performing transparency optimization. This optimization works by making pixels that are similar to the pixel in the previous frame transparent, resulting in only pixels that are different being saved. Higher values will result in a smaller file size at the cost of visual artifacts. This optimization is most effective on animations with large static areas.

The default value of 0 slightly reduces file size with no visual quality loss.

A value of around 0.01 provides a good tradeoff between file size and quality.

Set to -1 to disable transparency optimization.

quantizedColorDifferenceTolerance

The tolerance for color difference used by colorSimilarityChecker when performing transparency optimization after quantization. This optimization works by making pixels that are similar to the pixel in the previous frame transparent, resulting in only pixels that are different being saved. Higher values will result in a smaller file size at the cost of visual artifacts. This optimization is most effective on animations with large static areas.

A value of around 0.02 provides a good tradeoff between file size and quality.

Set to -1 to disable transparency optimization.

loopCount

The number of times the GIF should loop.

Set to 0 for infinite looping.

Set to -1 for no looping.

maxColors

The maximum number of colors in each frame.

Must be between 0 and 256 inclusive.

colorQuantizer

The ColorQuantizer to use for reducing the number of colors in each frame to maxColors.

colorSimilarityChecker

The ColorSimilarityChecker to use for determining if two frames are similar enough to merge.

comment

An optional comment to include in the GIF comment block metadata.

transparentAlphaThreshold

The alpha threshold for a pixel to be considered transparent. Pixels with an alpha value equal to or less than this value will be treated as fully transparent.

Must be between 1 and 256 inclusive.

alphaFill

The solid RGB color to use for filling in pixels with partial alpha transparency, as GIFs do not support partial transparency.

Set to -1 to disable filling.

cropTransparent

Whether to crop the transparent pixels from the edges of each frame. This can reduce the size of the GIF by a small amount.

minimumFrameDurationCentiseconds

The minimum duration for each frame in centiseconds. Setting this to a value less than 2 can result in the GIF being played slower than expected on some GIF viewers.

Must be positive.

onFrameWritten

A callback that is invoked after each frame is written, providing the number of frames written and the total duration of all the frames written so far. This can be used to track progress or update a UI.

Throws

If any of the parameters are invalid.

Constructors

Link copied to clipboard
actual constructor(sink: Sink, colorDifferenceTolerance: Double, quantizedColorDifferenceTolerance: Double, loopCount: Int, maxColors: Int, colorQuantizer: ColorQuantizer, colorSimilarityChecker: ColorSimilarityChecker, comment: String, transparentAlphaThreshold: Int, alphaFill: Int, cropTransparent: Boolean, minimumFrameDurationCentiseconds: Int, onFrameWritten: (framesWritten: Int, writtenDuration: Duration) -> Unit)
expect constructor(sink: Sink, colorDifferenceTolerance: Double = 0.0, quantizedColorDifferenceTolerance: Double = -1.0, loopCount: Int = 0, maxColors: Int = GIF_MAX_COLORS, colorQuantizer: ColorQuantizer = ColorQuantizer.NEU_QUANT, colorSimilarityChecker: ColorSimilarityChecker = ColorSimilarityChecker.EUCLIDEAN_LUMINANCE_WEIGHTING, comment: String = "", transparentAlphaThreshold: Int = 20, alphaFill: Int = -1, cropTransparent: Boolean = true, minimumFrameDurationCentiseconds: Int = GIF_MINIMUM_FRAME_DURATION_CENTISECONDS, onFrameWritten: (framesWritten: Int, writtenDuration: Duration) -> Unit = { _, _ -> })
actual constructor(sink: Sink, colorDifferenceTolerance: Double, quantizedColorDifferenceTolerance: Double, loopCount: Int, maxColors: Int, colorQuantizer: ColorQuantizer, colorSimilarityChecker: ColorSimilarityChecker, comment: String, transparentAlphaThreshold: Int, alphaFill: Int, cropTransparent: Boolean, minimumFrameDurationCentiseconds: Int, onFrameWritten: (framesWritten: Int, writtenDuration: Duration) -> Unit)
actual constructor(sink: Sink, colorDifferenceTolerance: Double, quantizedColorDifferenceTolerance: Double, loopCount: Int, maxColors: Int, colorQuantizer: ColorQuantizer, colorSimilarityChecker: ColorSimilarityChecker, comment: String, transparentAlphaThreshold: Int, alphaFill: Int, cropTransparent: Boolean, minimumFrameDurationCentiseconds: Int, onFrameWritten: (framesWritten: Int, writtenDuration: Duration) -> Unit)

Types

Link copied to clipboard
actual object Companion
expect object Companion
actual object Companion
actual object Companion

Functions

Link copied to clipboard
actual open override fun close()

Closes the encoder, ensuring all data is written. Closing the encoder also closes the underlying sink.

expect open override fun close()

Closes the encoder, ensuring all data is written. Closing the encoder also closes the underlying sink.

actual open override fun close()

Closes the encoder, ensuring all data is written. Closing the encoder also closes the underlying sink.

actual open override fun close()

Closes the encoder, ensuring all data is written. Closing the encoder also closes the underlying sink.

Link copied to clipboard
actual fun writeFrame(frame: ImageFrame)

Writes a single frame to the GIF. The frame may be skipped if the duration is below minimumFrameDurationCentiseconds, or if the frame is the same as or similar enough to the previous frame, determined by colorDifferenceTolerance, quantizedColorDifferenceTolerance, and colorSimilarityChecker.

fun writeFrame(image: Bitmap, duration: Duration)
fun writeFrame(image: Bitmap, duration: Duration)
actual fun writeFrame(argb: IntArray, width: Int, height: Int, duration: Duration)

Writes a single frame to the GIF. The frame may be skipped if the duration is below minimumFrameDurationCentiseconds, or if the frame is the same as or similar enough to the previous frame, determined by colorDifferenceTolerance, quantizedColorDifferenceTolerance, and colorSimilarityChecker.

fun writeFrame(argb: IntArray, width: Int, height: Int, duration: Duration)

Writes a single frame to the GIF. The frame may be skipped if the duration is below minimumFrameDurationCentiseconds, or if the frame is the same as or similar enough to the previous frame, determined by colorDifferenceTolerance, quantizedColorDifferenceTolerance, and colorSimilarityChecker.

expect fun writeFrame(frame: ImageFrame)

Writes a single frame to the GIF. The frame may be skipped if the duration is below minimumFrameDurationCentiseconds, or if the frame is the same as or similar enough to the previous frame, determined by colorDifferenceTolerance, quantizedColorDifferenceTolerance, and colorSimilarityChecker.

expect fun writeFrame(argb: IntArray, width: Int, height: Int, duration: Duration)

Writes a single frame to the GIF. The frame may be skipped if the duration is below minimumFrameDurationCentiseconds, or if the frame is the same as or similar enough to the previous frame, determined by colorDifferenceTolerance, quantizedColorDifferenceTolerance, and colorSimilarityChecker.

actual fun writeFrame(frame: ImageFrame)

Writes a single frame to the GIF. The frame may be skipped if the duration is below minimumFrameDurationCentiseconds, or if the frame is the same as or similar enough to the previous frame, determined by colorDifferenceTolerance, quantizedColorDifferenceTolerance, and colorSimilarityChecker.

fun writeFrame(image: BufferedImage, duration: Duration)
fun writeFrame(image: BufferedImage, duration: Duration)
actual fun writeFrame(argb: IntArray, width: Int, height: Int, duration: Duration)

Writes a single frame to the GIF. The frame may be skipped if the duration is below minimumFrameDurationCentiseconds, or if the frame is the same as or similar enough to the previous frame, determined by colorDifferenceTolerance, quantizedColorDifferenceTolerance, and colorSimilarityChecker.

fun writeFrame(argb: IntArray, width: Int, height: Int, duration: Duration)

Writes a single frame to the GIF. The frame may be skipped if the duration is below minimumFrameDurationCentiseconds, or if the frame is the same as or similar enough to the previous frame, determined by colorDifferenceTolerance, quantizedColorDifferenceTolerance, and colorSimilarityChecker.

actual fun writeFrame(frame: ImageFrame)

Writes a single frame to the GIF. The frame may be skipped if the duration is below minimumFrameDurationCentiseconds, or if the frame is the same as or similar enough to the previous frame, determined by colorDifferenceTolerance, quantizedColorDifferenceTolerance, and colorSimilarityChecker.

actual fun writeFrame(argb: IntArray, width: Int, height: Int, duration: Duration)

Writes a single frame to the GIF. The frame may be skipped if the duration is below minimumFrameDurationCentiseconds, or if the frame is the same as or similar enough to the previous frame, determined by colorDifferenceTolerance, quantizedColorDifferenceTolerance, and colorSimilarityChecker.