rravif

rravif is an R package for encoding images in AVIF format using the ravif Rust crate.

Originally, this package was a small utility for writing AVIF images from nativeRaster objects. While AVIF encoding can also be done via other packages such as magick, this package provides a minimal Rust-backed implementation with few dependencies.

In addition, it now includes a small helper for displaying nativeRaster objects directly in the terminal using terminal graphics protocols.

Usage

Encoding AVIF images

library(rravif)

cap <- ragg::agg_capture(width = 480, height = 360, background = "cyan")

grid::grid.newpage()
grid::grid.circle(
  x = 0.5,
  y = 0.5,
  r = 0.3,
  gp = grid::gpar(
    col = "snow",
    fill = "hotpink",
    lwd = 12
  )
)

rast <- cap(native = TRUE)
dev.off()
#> pdf
#>   2

avif_img <- encode(rast)
writeBin(avif_img, "man/figures/test.avif")

test.avif

Terminal preview

rravif also provides a simple way to preview nativeRaster images directly in the terminal:

if (interactive()) {
  print_nr(rast)
}

This renders the image inline using terminal graphics protocols (such as Sixel, Kitty, or iTerm2), depending on what your terminal supports.

Note that terminal support varies across environments. For best results, use a terminal emulator with image protocol support (e.g., Kitty, WezTerm, or recent versions of Windows Terminal / VS Code terminal with images enabled).

Related work

Similar ideas are explored in other packages:

Compared to these, rravif does not implement a full graphics device. Instead, it focuses on lightweight, ad-hoc display of nativeRaster objects, making it suitable as a simple preview tool.

License

MIT License.