Title: | Read and Write PNG Files with Configurable Decoder/Encoder Options |
---|---|
Description: | Read and write PNG images with arrays, rasters, native rasters, numeric arrays, integer arrays, raw vectors and indexed values. This PNG encoder exposes configurable internal options enabling the user to select a speed-size tradeoff. For example, disabling compression can speed up writing PNG by a factor of 50. Multiple image formats are supported including raster, native rasters, and integer and numeric arrays at color depths of 1, 2, 3 or 4. 16-bit images are also supported. This implementation uses the 'libspng' 'C' library which is available from <https://github.com/randy408/libspng/>. |
Authors: | Mike Cheng [aut, cre, cph], Randy408 [aut, cph] (Author of bundled libspng), The PNG Reference Library Authors [aut, cph], Cosmin Truta [cph] (SSE2 optimised filter functions, NEON optimised filter functions, NEON optimised palette expansion functions), Glenn Randers-Pehrson [cph] (SSE2 optimised filter functions), Andreas Dilger [cph], Guy Eric Schalnat [cph], Mike Klein [ctb] (SSE2 optimised filter functions), Matt Sarett [ctb] (SSE2 optimised filter functions), James Yu [ctb] (NEON optimised filter functions), Mars Rullgard [ctb] (NEON optimised filter functions), Arm Holdings [cph] (NEON optimised palette expansion functions), Richard Townsend [ctb] (NEON optimised palette expansion functions) |
Maintainer: | Mike Cheng <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.5 |
Built: | 2024-10-27 05:50:41 UTC |
Source: | https://github.com/coolbutuseless/fastpng |
Get information about a PNG file
get_png_info(src)
get_png_info(src)
src |
PNG filename or raw vector containing PNG data |
Name list of information about the PNG image:
Dimensions of PNG
Bit depth. 8 or 16 bits
color type and its description
Compression setting
Filter method and description
Interlace method and description
# Create a small grayscale PNG image and fetch its PNG info mat <- matrix(c(0L, 255L), 3, 4) png_data <- write_png(mat) get_png_info(png_data)
# Create a small grayscale PNG image and fetch its PNG info mat <- matrix(c(0L, 255L), 3, 4) png_data <- write_png(mat) get_png_info(png_data)
write_png()
Create a specification for how raw bytes should be interpreted when passed
to write_png()
raw_spec(width, height, depth, bits)
raw_spec(width, height, depth, bits)
width , height
|
image dimensions |
depth |
number of colour channels. Integer value in range [1, 4] |
bits |
number of bits for each colour channel. Either 8 or 16. |
named list to pass to the write_png(..., raw_spec = )
raw_spec(100, 20, 3, 8)
raw_spec(100, 20, 3, 8)
Read a PNG
read_png( src, type = c("array", "raster", "nativeraster", "indexed", "raw"), rgba = FALSE, flags = 1L, avoid_transpose = FALSE, array_type = c("dbl", "int") )
read_png( src, type = c("array", "raster", "nativeraster", "indexed", "raw"), rgba = FALSE, flags = 1L, avoid_transpose = FALSE, array_type = c("dbl", "int") )
src |
PNG image provided as either a file path, or a raw vector containing encoded PNG data |
type |
type of R object in which to store image data. Valid types are 'array', 'raster', "nativeraster", 'indexed' and 'raw'. Note that indexed image objects can only be loaded from indexed PNGs. |
rgba |
Should the result be forced into RGBA? Default: FALSE
means to use the most appropriate format of the given R image type
to store the data. If |
flags |
Flags to apply when reading PNG. Default: 1 (always decode transparency from tRNS chunks).
See |
avoid_transpose |
Default: FALSE. If |
array_type |
'dbl' or 'int'. Default: dbl. When reading PNG into an array, should the data be stored as a double (i.e. real) in the range [0, 1] or an integer in the range [0,255] (for 8 bit images) or [0,65535] (for 16 bit images). |
R image object of the specified type
# create a small greyscale matrix, and write it to a PNG file ras <- matrix(c('#880000', '#000088'), 3, 4) ras <- grDevices::as.raster(ras) pngfile <- tempfile() write_png(ras, file = pngfile) ras2 <- read_png(pngfile, type = 'raster') plot(ras2, interpolate = FALSE)
# create a small greyscale matrix, and write it to a PNG file ras <- matrix(c('#880000', '#000088'), 3, 4) ras <- grDevices::as.raster(ras) pngfile <- tempfile() write_png(ras, file = pngfile) ras2 <- read_png(pngfile, type = 'raster') plot(ras2, interpolate = FALSE)
Apply transparency
Apply gamma correction
spng_decode_flags
spng_decode_flags
An object of class list
of length 2.
A nested named list of test images (300 x 200 pixels).
test_image
test_image
An object of class list
of length 9.
Possible image color spaces within each image type
Gray pixels representing intensity only
Gray pixels with an alpha channel
RGB color image
RGB color image with alpha channel
A description of the image data within each image type
Arrays of numeric values in the range [0, 1]
A 2D matrix
A 3D array with 2 planes i.e. dim(x)[3] == 2
A 3D array with 3 planes i.e. dim(x)[3] == 2
A 3D array with 4 planes i.e. dim(x)[3] == 2
Same as test_image$array
data except values contain 16 bits of
signficant color information.
Arrays of integer values in the range [0, 255]
A 2D matrix
A 3D array with 2 planes i.e. dim(x)[3] == 2
A 3D array with 3 planes i.e. dim(x)[3] == 2
A 3D array with 4 planes i.e. dim(x)[3] == 2
Same as test_image$array_int
data except values are in the
range [0, 65535]
Raster image of color values given as hex codes #RRGGBB
Raster image of color values given as hex codes #RRGGBBAA
Raster image of color values given as R color names e.g. 'red', 'blue'
Integer matrix of integer values. Each 32-bit numeric value holds a packed RGBA pixel
An integer matrix. Each value is an index into a separately specified color-lookup table
A numeric matrix. Each value is an index into a separately specified color-lookup table
An example color palette to use with indexed images. 256 colors.
Sequences of raw bytes with attributes specifying 'width', 'height', 'depth' (i.e. number of colors) and 'bits' (number of bits for each color)
Sequence of gray pixels i.e. GGGG
Sequence of GA pixels i.e. GAGAGA...
Sequence of RGB pixels i.e. RGBRGBRGB...
Sequence of RGB pixels i.e. RGBARGBARGBA...
The same as test_image$raw
except each color takes 2 raw bytes.
Write PNG
write_png( image, file = NULL, palette = NULL, use_filter = TRUE, compression_level = -1L, avoid_transpose = FALSE, bits = 8, trns = NULL, raw_spec = NULL )
write_png( image, file = NULL, palette = NULL, use_filter = TRUE, compression_level = -1L, avoid_transpose = FALSE, bits = 8, trns = NULL, raw_spec = NULL )
image |
image. Supported image types:
|
file |
If NULL (the default) then return PNG data as raw vector, otherwise write to the given file path. |
palette |
character vector of up to 256 colors. If specified, and the image is a 2D matrix of integer or numeric values, then an indexed PNG is written where the matrix values indicate the colour palette value to use. The values in the matrix must range from 0 (for the first colour) |
use_filter |
Use PNG filtering to help reduce size? Default: TRUE. If FALSE, then filtering will be disabled which can make image writing faster. |
compression_level |
compression level for PNG. Default: -1 means
to use the default compression level. Other valid
values are in range [0, 9]. In general, lower compression levels
result in faster compression, but larger image sizes. For fastest
image writing, set |
avoid_transpose |
Should transposition be avoided if possible so as to
maximise the speed of writing the PNG? Default: FALSE.
PNG is a row-major image format, but R stores data in column-major
ordering. When writing data to PNG, it is often necessary to transpose
the R data to match what PNG requires. If this option is set
to |
bits |
bit depth. default 8. Valid values are 8 and 16. This option only has an effect when image to output is a numeric array. |
trns |
color to be treated as transparent
in RGB and Greyscale images - without specifying a full alpha channel.
Only a single color can be specified and it will be treated as a
fully transparent color in the image. This setting is only used
when writing RGB and Greyscale images. For 8-bit RGB images, the value
may be a hex color value i.e. |
raw_spec |
named list of image specifications for encoding a raw vector
to PNG. Use |
If file
argument provided, function writes to file and returns
nothing, otherwise it returns a raw vector holding the PNG
encoded data.
# create a small greyscale integer matrix, and write it to a PNG file mat <- matrix(c(0L, 255L), 3, 4) pngfile <- tempfile() write_png(mat, file = pngfile) im <- read_png(pngfile, type = 'raster') plot(im, interpolate = FALSE)
# create a small greyscale integer matrix, and write it to a PNG file mat <- matrix(c(0L, 255L), 3, 4) pngfile <- tempfile() write_png(mat, file = pngfile) im <- read_png(pngfile, type = 'raster') plot(im, interpolate = FALSE)