{fastpng}
reads and writes PNG images.
{fastpng}
exposes configuration options so that the user
can make a trade-off between speed of writing and PNG size. These
options include:
For example, writing uncompressed PNG images can be 100x faster than writing with regular compression settings.
fastpng
uses libspng - current
v0.7.4
Supported images each have examples in the test_image
as
part of this package.
{png}
library{fastpng} |
{png} |
|
---|---|---|
Numeric arrays | Yes | Yes |
Native rasters | Yes | Yes |
Rasters | Yes | |
Integer Arrays | Yes | |
Indexed PNGs | Yes | |
tRNS transparency |
Yes | |
Configurable compression | Yes | |
Configurable filtering | Yes | |
Configurable transposition | Yes |
The following graph shows the speed of writing and the compression
ratio for various settings in fastpng
. A data point for the
png
package is also shown.
library(fastpng)
png_file <- system.file("img", "Rlogo.png", package="png")
fastpng::get_png_info(png_file)
#> $width
#> [1] 100
#>
#> $height
#> [1] 76
#>
#> $bit_depth
#> [1] 8
#>
#> $color_type
#> [1] 6
#>
#> $compression_method
#> [1] 0
#>
#> $filter_method
#> [1] 0
#>
#> $interlace_method
#> [1] 0
#>
#> $color_desc
#> [1] "SPNG_COLOR_TYPE_TRUECOLOR_ALPHA"
#>
#> $filter_desc
#> [1] "SPNG_FILTER_NONE"
#>
#> $interlate_desc
#> [1] "SPNG_INTERLACE_NONE"
ras <- fastpng::read_png(png_file, type = 'raster')
grid::grid.raster(ras, interpolate = FALSE)
ras <- fastpng::read_png(png_file, type = "raster")
ras[7:11, 79:83]
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] "#686D6597" "#7579711F" "#00000000" "#00000000" "#00000000"
#> [2,] "#5F645CFF" "#5D635AF9" "#63696098" "#7B80781C" "#00000000"
#> [3,] "#686D64FF" "#61665DFF" "#5B6158FF" "#5C6158F5" "#656A6280"
#> [4,] "#71766EFF" "#6B6F67FF" "#656A61FF" "#5E635BFF" "#595E55FF"
#> [5,] "#797D75FF" "#747971FF" "#6E736AFF" "#686D64FF" "#60655DFF"
ras <- fastpng::read_png(png_file, type = "array")
ras[7:11, 79:83, 1] # red channel
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 0.4078431 0.4588235 0.0000000 0.0000000 0.0000000
#> [2,] 0.3725490 0.3647059 0.3882353 0.4823529 0.0000000
#> [3,] 0.4078431 0.3803922 0.3568627 0.3607843 0.3960784
#> [4,] 0.4431373 0.4196078 0.3960784 0.3686275 0.3490196
#> [5,] 0.4745098 0.4549020 0.4313725 0.4078431 0.3764706
indices <- test_image$indexed$integer_index
palette <- test_image$indexed$palette
dim(indices)
#> [1] 200 300
indices[1:10, 1:10]
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#> [1,] 0 0 0 0 0 0 0 0 0 0
#> [2,] 0 0 0 0 0 0 0 0 0 0
#> [3,] 0 0 0 0 0 0 0 0 0 1
#> [4,] 0 0 0 0 0 0 0 1 1 1
#> [5,] 0 0 0 0 0 1 1 1 1 1
#> [6,] 0 0 0 0 1 1 1 1 1 1
#> [7,] 0 0 0 0 1 1 1 1 1 1
#> [8,] 0 0 0 1 1 1 1 1 1 1
#> [9,] 0 0 0 1 1 1 1 1 1 2
#> [10,] 0 0 1 1 1 1 1 1 2 2
palette[1:10]
#> [1] "#440154FF" "#440256FF" "#450457FF" "#450559FF" "#46075AFF" "#46085CFF"
#> [7] "#460A5DFF" "#460B5EFF" "#470D60FF" "#470E61FF"