RFPNG wraps fpng, a very fast C++ .PNG image reader/writer for 24/32bpp images See the blog post by the creator Rich Geldreich for more technical details. ## Installation
You can install the development version of RFPNG from GitHub with:
# install.packages("devtools")
devtools::install_github("schochastics/RFPNG")library(RFPNG)RFPNG can only read pngs that were encoded with RFPNG. For regular pngs,
use png::readPNG. The function png2fpng can be used to convertinga
regular png to be compatible with fpng.
img <- png::readPNG("bear.png")Before writing with FPNG, the array needs to be reshaped, which includes converting from [0,1] values to [0,255].
fimg <- reshape_to_fpng(img)writeFPNG is significantly faster than png::writePNG.
ftmp <- tempfile(fileext = "png")
tmp <- tempfile(fileext = "png")
bench::mark(
writeFPNG(fimg, ftmp),
png::writePNG(img, tmp),
check = FALSE
)
#> # A tibble: 2 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 writeFPNG(fimg, ftmp) 30.7ms 33.5ms 29.7 16MB 9.90
#> 2 png::writePNG(img, tmp) 545.3ms 545.3ms 1.83 4.02MB 0same goes for the reading
bench::mark(
readFPNG("fbear.png"),
png::readPNG("bear.png"),
png::readPNG("fbear.png"),
check = FALSE
)
# A tibble: 3 × 13
#> expression min median `itr/sec` mem_alloc `gc/sec` n_itr n_gc total_time result memory
#> <bch:expr> <bch:> <bch:t> <dbl> <bch:byt> <dbl> <int> <dbl> <bch:tm> <list> <list>
#> 1 "readFPNG(… 29.8ms 33.6ms 29.7 16MB 13.2 9 4 303.1ms <NULL> <Rprofmem>
#> 2 "png::read… 75.7ms 100.9ms 10.7 36MB 10.7 3 3 279.5ms <NULL> <Rprofmem>
#> 3 "png::read… 97.3ms 97.3ms 10.3 36MB 41.1 1 4 97.3ms <NULL> <Rprofmem>
#> # ℹ 2 more variables: time <list>, gc <list>