| Title: | Native Raster Image Tools |
|---|---|
| Description: | Native rasters are a core R image format which use a compact color representation. This color representation closely aligns with graphics device internals meaning that these images can be rendered quickly. This package provides functions to quickly create, manipulate and composite native rasters. |
| Authors: | Mike Cheng [aut, cre, cph], Julien Vernay [ctb] (Author of public domain code for thick polylines) |
| Maintainer: | Mike Cheng <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.0.2 |
| Built: | 2026-05-28 11:08:17 UTC |
| Source: | https://github.com/coolbutuseless/nara |
The 'deer' sprites are by Calciumtrice and licensed under CC BY 3.0. See 'inst/LICENSE-deer.txt' for more details. To view license information:
deerdeer
An object of class list of length 15.
cat(readLines(system.file('LICENSE-deer.txt', package = 'nara')), sep = "\n")
Check if object is a native raster image
is_nativeraster(x)is_nativeraster(x)
x |
object to check |
logical. TRUE if object is a native raster image
is_nativeraster(mtcars) nr <- nr_new(100, 100) is_nativeraster(nr)is_nativeraster(mtcars) nr <- nr_new(100, 100) is_nativeraster(nr)
Convert a 'magick' image to native raster image
magick_to_nr(im, dst = NULL) nr_to_magick(nr)magick_to_nr(im, dst = NULL) nr_to_magick(nr)
im |
image from the |
dst |
destination native raster image. If NULL (the default) a new native raster image will be created If a native raster image is supplied here, it must have the exact dimensions to match the matrix |
nr |
native raster image |
New native raster image
Other conversion functions:
matrix_to_nr(),
nr_to_raster(),
nrs_to_gif(),
nrs_to_mp4()
im <- magick::logo nr <- magick_to_nr(im) plot(nr)im <- magick::logo nr <- magick_to_nr(im) plot(nr)
Convert a numeric matrix to native raster image
matrix_to_nr(mat, palette, fill = "transparent", min = 0, max = 1, dst = NULL)matrix_to_nr(mat, palette, fill = "transparent", min = 0, max = 1, dst = NULL)
mat |
integer matrix |
palette |
vector of colors. For an integer matrix, this palette must
contain at least as many colors as the maximum integer value in |
fill |
Color to be used for values < 1 when input is an integer matrix. Default: 'transparent'. |
min, max
|
assumed range for the numeric data. values from the palette will be interpolated using this range as the extents. An error will occur if a value lies outside this range. Default: (0, 1) |
dst |
destination native raster image. If NULL (the default) a new native raster image will be created If a native raster image is supplied here, it must have the exact dimensions to match the matrix |
native raster image
Other conversion functions:
magick_to_nr(),
nr_to_raster(),
nrs_to_gif(),
nrs_to_mp4()
# integer matrix m <- matrix(1:12, 3, 4) m palette <- rainbow(12) nr <- matrix_to_nr(m, palette) plot(nr) # numeric matrix m <- matrix(runif(20 * 30), 20, 30) palette <- c('red', 'blue', 'black', 'green', 'blue', 'yellow') nr <- matrix_to_nr(m, palette) plot(nr)# integer matrix m <- matrix(1:12, 3, 4) m palette <- rainbow(12) nr <- matrix_to_nr(m, palette) plot(nr) # numeric matrix m <- matrix(runif(20 * 30), 20, 30) palette <- c('red', 'blue', 'black', 'green', 'blue', 'yellow') nr <- matrix_to_nr(m, palette) plot(nr)
Copy a whole image, or subset of an image, into the destination image and configure the angle and size of the copied image.
nr_blit( dst, src, x, y, xsrc = 0L, ysrc = 0L, w = -1L, h = -1L, hjust = 0.5, vjust = 0.5, angle = 0, scale = 1, use_alpha = TRUE )nr_blit( dst, src, x, y, xsrc = 0L, ysrc = 0L, w = -1L, h = -1L, hjust = 0.5, vjust = 0.5, angle = 0, scale = 1, use_alpha = TRUE )
dst, src
|
source and destination native raster images |
x, y
|
Location in |
xsrc, ysrc
|
Upper-left coordinates of the bounding box within the |
w, h
|
Width and height of the bounding box within the |
hjust, vjust
|
specify horizontal and vertical justification of the
handle on the |
angle |
Rotation angle (clockwise) in radians. Default: 0.
If this is a vector, then the |
scale |
Zoom factor. Default: 1. IF this is a vector, then the |
use_alpha |
Use alpha channel when drawing? Logical. Default: TRUE |
This operation is vectorised such that a single src image can be
pasted multiple times into the dst image with varying location,
angle and scale.
Invisibly return the supplied dst native raster image which was been
modified in-place
Other blitting functions:
nr_blit_multi()
nr <- nr_new(50, 50, 'grey80') nr_blit(dst = nr, src = deer[[1]], x = 25, y = 25) plot(nr) nr <- nr_new(300, 200, 'grey80') sq <- nr_new(20, 20, 'darkblue') nr_blit(nr, src = sq, x = 100, y = 100, angle = pi/3, scale = 5) plot(nr) nr <- nr_new(800, 600, 'grey80') sq <- fastpng::read_png(system.file("image/deer-1.png", package="nara"), type = 'nativeraster') nr_blit(nr, src = sq, x = 300, y = 240, angle = pi/2, scale = 1) plot(nr)nr <- nr_new(50, 50, 'grey80') nr_blit(dst = nr, src = deer[[1]], x = 25, y = 25) plot(nr) nr <- nr_new(300, 200, 'grey80') sq <- nr_new(20, 20, 'darkblue') nr_blit(nr, src = sq, x = 100, y = 100, angle = pi/3, scale = 5) plot(nr) nr <- nr_new(800, 600, 'grey80') sq <- fastpng::read_png(system.file("image/deer-1.png", package="nara"), type = 'nativeraster') nr_blit(nr, src = sq, x = 300, y = 240, angle = pi/2, scale = 1) plot(nr)
Multiple blit operations in a single call
nr_blit_multi(dst, src, config)nr_blit_multi(dst, src, config)
dst |
destination native raster |
src |
list of native raster images. |
config |
data.frame of configuration information for each blit.
Columns in this data.frame match the arguments to
Optional columns which take a default value if not present:
|
Invisibly return the supplied dst native raster image which was been
modified in-place
Other blitting functions:
nr_blit()
nr <- nr_new(90, 90, 'grey60') config <- data.frame( idx = c(1, 2, 3, 4), x = c(10, 10, 40, 40) + 15, y = c(10, 40, 40, 10) + 15, xsrc = 0L, ysrc = 0L, w = -1L, h = -1L, hjust = 0.5, vjust = 0.5, angle = c(0, 0, 0, pi/4), scale = c(0.5, 1, 1, 1), use_alpha = TRUE, render = TRUE ) nr_blit_multi(dst = nr, src = deer, config = config) plot(nr)nr <- nr_new(90, 90, 'grey60') config <- data.frame( idx = c(1, 2, 3, 4), x = c(10, 10, 40, 40) + 15, y = c(10, 40, 40, 10) + 15, xsrc = 0L, ysrc = 0L, w = -1L, h = -1L, hjust = 0.5, vjust = 0.5, angle = c(0, 0, 0, pi/4), scale = c(0.5, 1, 1, 1), use_alpha = TRUE, render = TRUE ) nr_blit_multi(dst = nr, src = deer, config = config) plot(nr)
Draw circles on a native raster image
nr_circle(nr, x, y, r, fill = "black", color = NA, use_alpha = TRUE)nr_circle(nr, x, y, r, fill = "black", color = NA, use_alpha = TRUE)
nr |
native raster image |
x, y
|
coordinates of centre of circle. [vector] |
r |
radius [vector] |
fill |
interior fill color [vector] |
color |
outline color. Default: NA. [vector] |
use_alpha |
Use alpha channel when drawing? Logical. Default: TRUE |
Invisibly return the supplied native raster image which was been modified in-place
Other drawing functions:
nr_polygon(),
nr_polyline(),
nr_rect(),
nr_tri_mesh()
set.seed(1) w <- 200 h <- 150 nr <- nr_new(w, h, 'black') N <- 10 coords <- expand.grid(x = seq(2, w, length.out = N), y = seq(2, h, length.out = N)) cols <- sample(grDevices::terrain.colors(nrow(coords))) nr_circle( nr, x = coords$x, y = coords$y, r = w/N/1.3, fill = cols ) plot(nr)set.seed(1) w <- 200 h <- 150 nr <- nr_new(w, h, 'black') N <- 10 coords <- expand.grid(x = seq(2, w, length.out = N), y = seq(2, h, length.out = N)) cols <- sample(grDevices::terrain.colors(nrow(coords))) nr_circle( nr, x = coords$x, y = coords$y, r = w/N/1.3, fill = cols ) plot(nr)
Replace colors in a native raster image
nr_color_replace(nr, old, new)nr_color_replace(nr, old, new)
nr |
native raster image |
old |
Vector of old colors |
new |
Vector of replacement colors |
Invisibly return the supplied native raster image which was been modified in-place
Other color manipulation functions:
nr_desaturate(),
nr_dither(),
nr_threshold()
nr <- nr_new(10, 10, 'hotpink') nr_color_replace(nr, 'hotpink', 'grey80') plot(nr)nr <- nr_new(10, 10, 'hotpink') nr_color_replace(nr, 'hotpink', 'grey80') plot(nr)
Create a new native raster image and copy the dimensions and contents from an existing image
nr_copy(nr)nr_copy(nr)
nr |
native raster image |
New native raster image
Other image creation functions:
nr_new()
nr1 <- nr_new(200, 200, 'hotpink') nr2 <- nr_copy(nr1) plot(nr2)nr1 <- nr_new(200, 200, 'hotpink') nr2 <- nr_copy(nr1) plot(nr2)
The source and destination native raster images must have the same dimensions.
nr_copy_into(dst, src)nr_copy_into(dst, src)
src, dst
|
Source and destination native raster images |
If the native raster images are of different sizes or alpha blending is
required, use the nr_blit() function.
Invisibly return the supplied 'dst' native raster image which was been modified in-place
nr1 <- nr_new(200, 100, 'hotpink') nr2 <- nr_new(200, 100, 'green') nr_copy_into(nr1, nr2) plot(nr1)nr1 <- nr_new(200, 100, 'hotpink') nr2 <- nr_new(200, 100, 'green') nr_copy_into(nr1, nr2) plot(nr1)
Crop a section out of a native raster image into a new image
nr_crop(nr, x, y, w, h) nr_crop2(nr, loc)nr_crop(nr, x, y, w, h) nr_crop2(nr, loc)
nr |
native raster image |
x, y, w, h
|
dimensions of cropped section |
loc |
dimensions of cropped section. A vector of 4 values
i.e. |
New native raster image
nr <- deer[[1]] dim(nr) plot(nr) nr2 <- nr_crop(nr, 16, 0, 16, 16) dim(nr2) plot(nr2)nr <- deer[[1]] dim(nr) plot(nr) nr2 <- nr_crop(nr, 16, 0, 16, 16) dim(nr2) plot(nr2)
Move image colors to gray
nr_desaturate(nr, factor = 1)nr_desaturate(nr, factor = 1)
nr |
native raster image |
factor |
desaturation factor. Default: 1 (fully desaturate) |
Invisibly return the supplied native raster image which was been modified in-place
Other color manipulation functions:
nr_color_replace(),
nr_dither(),
nr_threshold()
plot(deer[[1]]) nr <- deer[[1]] |> nr_copy() nr_desaturate(nr) plot(nr)plot(deer[[1]]) nr <- deer[[1]] |> nr_copy() nr_desaturate(nr) plot(nr)
Dither to binary image
nr_dither(nr, value = 0.5, algo = "fs")nr_dither(nr, value = 0.5, algo = "fs")
nr |
native raster image |
value |
Threshold value. Default: 0.5 (valid range [0, 1]) |
algo |
Dithering algorithm. 'fs' (floyd-steinberg) or 'atkinson'. Default: 'fs' |
Invisibly return the supplied native raster image which was been modified in-place
Other color manipulation functions:
nr_color_replace(),
nr_desaturate(),
nr_threshold()
nr <- nr_copy(deer[[1]]) plot(nr) nr_dither(nr, 0.99) plot(nr)nr <- nr_copy(deer[[1]]) plot(nr) nr_dither(nr, 0.99) plot(nr)
Fill a native raster image with the given color
nr_fill(nr, color)nr_fill(nr, color)
nr |
native raster image |
color |
Color as a character string. Either a standard R color
(e.g. 'blue', 'white')
or a hex color of the form |
Invisibly return the supplied native raster image which was been modified in-place
nr <- nr_new(400, 300, 'hotpink') nr_fill(nr, 'blue') plot(nr)nr <- nr_new(400, 300, 'hotpink') nr_fill(nr, 'blue') plot(nr)
Flip a native raster image horizontally
nr_fliph(nr)nr_fliph(nr)
nr |
native raster image |
Invisibly return the supplied native raster image which was been modified in-place
Other transformation functions:
nr_flipv(),
nr_rotate(),
nr_transpose()
nr <- nr_new(400, 200, 'white') nr_rect(nr, 0, 0, 30, 15) plot(nr) nr_fliph(nr) plot(nr)nr <- nr_new(400, 200, 'white') nr_rect(nr, 0, 0, 30, 15) plot(nr) nr_fliph(nr) plot(nr)
Flip a native raster image vertically
nr_flipv(nr)nr_flipv(nr)
nr |
native raster image |
Invisibly return the supplied native raster image which was been modified in-place
Other transformation functions:
nr_fliph(),
nr_rotate(),
nr_transpose()
nr <- deer[[1]] |> nr_copy() plot(nr) nr_flipv(nr) plot(nr)nr <- deer[[1]] |> nr_copy() plot(nr) nr_flipv(nr) plot(nr)
Uses Bresenham's algorithm to draw lines. No antialiasing.
nr_line(nr, x1, y1, x2, y2, color = "black", linewidth = 1, use_alpha = TRUE)nr_line(nr, x1, y1, x2, y2, color = "black", linewidth = 1, use_alpha = TRUE)
nr |
native raster image |
x1, y1, x2, y2
|
Vectors of coordinates of endpoints of line |
color |
Color as a character string. Either a standard R color
(e.g. 'blue', 'white')
or a hex color of the form |
linewidth |
Line linewidth. Default: 1. If |
use_alpha |
Use alpha channel when drawing? Logical. Default: TRUE |
Invisibly return the supplied native raster image which was been modified in-place
w <- 200 h <- 150 nr <- nr_new(w, h, 'grey80') N <- 40 nr_line( nr, x1 = seq(0, 2*w, length.out = N), y1 = 0, x2 = 0, y2 = seq(0, 2 * h, length.out = N), color = rainbow(N) ) plot(nr)w <- 200 h <- 150 nr <- nr_new(w, h, 'grey80') N <- 40 nr_line( nr, x1 = seq(0, 2*w, length.out = N), y1 = 0, x2 = 0, y2 = seq(0, 2 * h, length.out = N), color = rainbow(N) ) plot(nr)
These functions are used to globally set a mask which determines where pixels are affected by drawing functions.
nr_mask_begin(nr, mask) nr_mask_end(nr)nr_mask_begin(nr, mask) nr_mask_end(nr)
nr |
native raster image |
mask |
native raster image to use as mask. Must be the same size as
|
Drawing operations between
nr_mask_begin() and nr_mask_end() calls will only affect pixels
where the mask is not transparent.
Invisibly return the original native raster image which has been modified in-place
mask <- deer[[1]] |> nr_resize(600, 400) plot(mask) nr <- nr_new_from(mask) nr_mask_begin(nr, mask = mask) nr_circle(nr, 300, 200, 200, fill = 'blue', color = 'black') nr_circle(nr, 350, 200, 100, fill = 'darkgreen', color = 'black') nr_mask_end(nr) plot(nr)mask <- deer[[1]] |> nr_resize(600, 400) plot(mask) nr <- nr_new_from(mask) nr_mask_begin(nr, mask = mask) nr_circle(nr, 300, 200, 200, fill = 'blue', color = 'black') nr_circle(nr, 350, 200, 100, fill = 'darkgreen', color = 'black') nr_mask_end(nr) plot(nr)
A native raster image in R looks like an integer matrix, but is interpreted differently by graphics devices:
nr_new(width, height, fill = "white") nr_new_from(nr, fill = "white")nr_new(width, height, fill = "white") nr_new_from(nr, fill = "white")
width, height
|
Image dimensions in pixels |
fill |
Background fill color as a character string. Either a standard R color
(e.g. 'blue', 'white')
or a hex color of the form |
nr |
native raster image to use as the template for the size of the
new image in |
The data should be treated as RGBA pixels in row-major ordering
Each 32-bit integer should be interpreted as 4-bytes - one for each of the R, G, B and A color channels
native raster image
Other image creation functions:
nr_copy()
nr <- nr_new(400, 300, 'hotpink') plot(nr)nr <- nr_new(400, 300, 'hotpink') plot(nr)
Draw points on a native raster image
nr_point(nr, x, y, color = "black", use_alpha = TRUE)nr_point(nr, x, y, color = "black", use_alpha = TRUE)
nr |
native raster image |
x, y
|
Vectors of point coordinates |
color |
Vector of colors |
use_alpha |
Use alpha channel when drawing? Logical. Default: TRUE |
Invisibly return the supplied native raster image which was been modified in-place
w <- 200 h <- 150 nr <- nr_new(w, h, 'grey80') coords <- expand.grid(x = seq(w) - 1, y = seq(h) - 1) cols <- sample(rainbow(nrow(coords))) nr_point(nr, x = coords$x, y = coords$y, color = cols) plot(nr)w <- 200 h <- 150 nr <- nr_new(w, h, 'grey80') coords <- expand.grid(x = seq(w) - 1, y = seq(h) - 1) cols <- sample(rainbow(nrow(coords))) nr_point(nr, x = coords$x, y = coords$y, color = cols) plot(nr)
Draw multiple polygons on a native raster image
nr_polygon( nr, x, y, id = NULL, fill = "black", color = NA, linewidth = 1, mitre_limit = linewidth, use_alpha = TRUE )nr_polygon( nr, x, y, id = NULL, fill = "black", color = NA, linewidth = 1, mitre_limit = linewidth, use_alpha = TRUE )
nr |
native raster image |
x, y
|
Vectors of point coordinates |
id |
integer vector used to separate coordinates into
multiple polygons. Consecutive runs of the same |
fill |
fill color |
color |
Color as a character string. Either a standard R color
(e.g. 'blue', 'white')
or a hex color of the form |
linewidth |
Line linewidth. Default: 1. If |
mitre_limit |
Limit the size of the mitre when two lines meet at an acute angle and linewidth is greater than 1. Default: same as line linewidth which mostly looks OK. |
use_alpha |
Use alpha channel when drawing? Logical. Default: TRUE |
Invisibly return the supplied native raster image which was been modified in-place
Other drawing functions:
nr_circle(),
nr_polyline(),
nr_rect(),
nr_tri_mesh()
w <- 200 h <- 150 nr <- nr_new(w, h, 'grey80') N <- 20 theta <- seq(0, 2 * pi, length.out = N) xs <- w/2 + 50 * cos(theta) ys <- h/2 + 50 * sin(theta) nr_polygon(nr, xs, ys, fill = 'grey50', col = 'black') nr_point(nr, xs, ys, col = 'red') plot(nr)w <- 200 h <- 150 nr <- nr_new(w, h, 'grey80') N <- 20 theta <- seq(0, 2 * pi, length.out = N) xs <- w/2 + 50 * cos(theta) ys <- h/2 + 50 * sin(theta) nr_polygon(nr, xs, ys, fill = 'grey50', col = 'black') nr_point(nr, xs, ys, col = 'red') plot(nr)
Draw a polyline on a native raster image
nr_polyline( nr, x, y, color = "black", linewidth = 1, mitre_limit = linewidth, close = FALSE, use_alpha = TRUE )nr_polyline( nr, x, y, color = "black", linewidth = 1, mitre_limit = linewidth, close = FALSE, use_alpha = TRUE )
nr |
native raster image |
x, y
|
Vectors of point coordinates |
color |
Color as a character string. Either a standard R color
(e.g. 'blue', 'white')
or a hex color of the form |
linewidth |
Line linewidth. Default: 1. If |
mitre_limit |
Limit the size of the mitre when two lines meet at an acute angle and linewidth is greater than 1. Default: same as line linewidth which mostly looks OK. |
close |
Should the polyline be closed? I.e. should a line be drawn between the last point and the first point? Default: FALSE |
use_alpha |
Use alpha channel when drawing? Logical. Default: TRUE |
Invisibly return the supplied native raster image which was been modified in-place
Other drawing functions:
nr_circle(),
nr_polygon(),
nr_rect(),
nr_tri_mesh()
w <- 200 h <- 150 nr <- nr_new(w, h, 'grey80') N <- 20 theta <- seq(0, 2 * pi, length.out = N) xs <- w/2 + 50 * cos(theta) ys <- h/2 + 50 * sin(theta) nr_polyline(nr, xs, ys) nr_point(nr, xs, ys, col = 'red') plot(nr)w <- 200 h <- 150 nr <- nr_new(w, h, 'grey80') N <- 20 theta <- seq(0, 2 * pi, length.out = N) xs <- w/2 + 50 * cos(theta) ys <- h/2 + 50 * sin(theta) nr_polyline(nr, xs, ys) nr_point(nr, xs, ys, col = 'red') plot(nr)
Draw rectangles on a native raster image
nr_rect( nr, x, y, w, h, fill = "black", color = NA, hjust = 0, vjust = 0, linewidth = 1, use_alpha = TRUE )nr_rect( nr, x, y, w, h, fill = "black", color = NA, hjust = 0, vjust = 0, linewidth = 1, use_alpha = TRUE )
nr |
native raster image |
x, y
|
coordinates of lower left corner of rectangle. [vector] |
w, h
|
width and height of rectangle. [vector] |
fill |
interior fill color [vector] |
color |
outline color. Default: NA. [vector] |
hjust, vjust
|
specify horizontal and vertical justification of the
handle on the |
linewidth |
Line linewidth. Default: 1. If |
use_alpha |
Use alpha channel when drawing? Logical. Default: TRUE |
Invisibly return the supplied native raster image which was been modified in-place
Other drawing functions:
nr_circle(),
nr_polygon(),
nr_polyline(),
nr_tri_mesh()
set.seed(1) w <- 200 h <- 150 nr <- nr_new(w, h, 'grey80') N <- 10 coords <- expand.grid(x = seq(2, w, length.out = N), y = seq(2, h, length.out = N)) cols <- sample(grDevices::heat.colors(nrow(coords))) nr_rect( nr, x = coords$x, y = coords$y, w = w/N - 1, h = h/N - 1, fill = cols ) plot(nr)set.seed(1) w <- 200 h <- 150 nr <- nr_new(w, h, 'grey80') N <- 10 coords <- expand.grid(x = seq(2, w, length.out = N), y = seq(2, h, length.out = N)) cols <- sample(grDevices::heat.colors(nrow(coords))) nr_rect( nr, x = coords$x, y = coords$y, w = w/N - 1, h = h/N - 1, fill = cols ) plot(nr)
Resize a native raster by specifying the output dimensions
nr_resize(nr, width, height, algo = "nn")nr_resize(nr, width, height, algo = "nn")
nr |
native raster image |
width, height
|
dimensions for output image |
algo |
'nn' for nearest neighbor (the default), or 'bilinear' for bilinear interpolation. |
New native raster image
Other resizing functions:
nr_scale()
stretched <- deer[[1]] |> nr_copy() |> nr_resize(100, 40, algo = 'nn') plot(stretched)stretched <- deer[[1]] |> nr_copy() |> nr_resize(100, 40, algo = 'nn') plot(stretched)
Rotate a native raster image by 90,180,270 degrees
nr_rotate(nr, angle)nr_rotate(nr, angle)
nr |
native raster image |
angle |
one of 0,90,180,270 |
Invisibly return the supplied native raster image which was been modified in-place
Other transformation functions:
nr_fliph(),
nr_flipv(),
nr_transpose()
nr <- nr_new(20, 10, 'hotpink') dim(nr) nr_rotate(nr, 90) dim(nr)nr <- nr_new(20, 10, 'hotpink') dim(nr) nr_rotate(nr, 90) dim(nr)
Resize a native raster image using a scale factor
nr_scale(nr, scale, algo = "nn")nr_scale(nr, scale, algo = "nn")
nr |
native raster image |
scale |
scale factor |
algo |
'nn' for nearest neighbor (the default), or 'bilinear' for bilinear interpolation. |
New native raster image
Other resizing functions:
nr_resize()
big <- deer[[1]] |> nr_scale(2) plot(big)big <- deer[[1]] |> nr_scale(2) plot(big)
The only font currently available is 'spleen' - a monospace bitmap font from: https://github.com/fcambus/spleen
nr_text_mono( nr, x, y, str, color = "black", fontsize = 8L, hjust = 0.5, vjust = 0.5, use_alpha = TRUE )nr_text_mono( nr, x, y, str, color = "black", fontsize = 8L, hjust = 0.5, vjust = 0.5, use_alpha = TRUE )
nr |
native raster image |
x, y
|
coordinates of lower-left corner of text |
str |
character string |
color |
Color as a character string. Either a standard R color
(e.g. 'blue', 'white')
or a hex color of the form |
fontsize |
height of font in pizels. Only valid values are 8, 12 and 16. Default: 8. |
hjust, vjust
|
Justification of text relative to its |
use_alpha |
Use alpha channel when drawing? Logical. Default: TRUE |
The 'spleen' font is licensed under BSD and the license is included in this package as "LICENSE-spleen.txt". To view LICENSE:
cat(readLines(system.file('LICENSE-spleen.txt', package = 'nara')),
sep = "\n")
Invisibly return the supplied native raster image which was been modified in-place
w <- 150 h <- 30 nr <- nr_new(w, h, 'grey80') nr_text_mono(nr, x = w/2, y = h/2, str = "Hello RStats!", fontsize = 16) plot(nr) nr <- nr_new(w, h, 'grey80') nr_text_mono(nr, x = 0, y = 0, str = "Hello RStats!", hjust = 0, vjust = 1, fontsize = 16) plot(nr)w <- 150 h <- 30 nr <- nr_new(w, h, 'grey80') nr_text_mono(nr, x = w/2, y = h/2, str = "Hello RStats!", fontsize = 16) plot(nr) nr <- nr_new(w, h, 'grey80') nr_text_mono(nr, x = 0, y = 0, str = "Hello RStats!", hjust = 0, vjust = 1, fontsize = 16) plot(nr)
Threshold to binary image
nr_threshold(nr, value = 0.5)nr_threshold(nr, value = 0.5)
nr |
native raster image |
value |
Threshold value. Default: 0.5 (valid range [0, 1]) |
Invisibly return the supplied native raster image which was been modified in-place
Other color manipulation functions:
nr_color_replace(),
nr_desaturate(),
nr_dither()
nr <- nr_copy(deer[[1]]) plot(nr) nr_threshold(nr, 0.9) plot(nr)nr <- nr_copy(deer[[1]]) plot(nr) nr_threshold(nr, 0.9) plot(nr)
Convert native raster images to/from other R objects
nr_to_raster(nr) raster_to_nr(ras, dst = NULL) nr_to_array(nr) array_to_nr(arr, dst = NULL)nr_to_raster(nr) raster_to_nr(ras, dst = NULL) nr_to_array(nr) array_to_nr(arr, dst = NULL)
nr |
native raster image |
ras |
standard R raster i.e. a character matrix of hex color values |
dst |
destination native raster image. If NULL (the default) a new |
arr |
3d numeric array representing R,G,B,A values with dimensions [nrow, ncol, 4] or [nrow, ncol, 3]. Each value is in range [0,1]. |
raster, array or native raster image
Other conversion functions:
magick_to_nr(),
matrix_to_nr(),
nrs_to_gif(),
nrs_to_mp4()
nr <- nr_new(12, 8, 'hotpink') nr_to_raster(nr) nr_to_array(nr)nr <- nr_new(12, 8, 'hotpink') nr_to_raster(nr) nr_to_array(nr)
Transpose
nr_transpose(nr)nr_transpose(nr)
nr |
native raster image |
Invisibly return the supplied native raster image which was been modified in-place
Other transformation functions:
nr_fliph(),
nr_flipv(),
nr_rotate()
nr <- nr_new(20, 10, 'hotpink') dim(nr) nr_transpose(nr) dim(nr)nr <- nr_new(20, 10, 'hotpink') dim(nr) nr_transpose(nr) dim(nr)
Mesh data is a similar format to the mesh3d class of data
defined in the rgl package.
nr_tri_mesh(nr, vertices, indices, color, tris = "all", use_alpha = TRUE) nr_tri_coords(nr, coords, color, tris = "all", use_alpha = TRUE)nr_tri_mesh(nr, vertices, indices, color, tris = "all", use_alpha = TRUE) nr_tri_coords(nr, coords, color, tris = "all", use_alpha = TRUE)
nr |
native raster image |
vertices |
Wide matrix of vertex coordinates where the first two rows are x and y coordinates. Extra coordinates are ignored. |
indices |
3xN wide integer matrix of index information. Each column
holds the 3 indices which indicate the column index of the triangle vertices defined in the
|
color |
color specification. Single color or one color per tri |
tris |
Which triangles should be drawn? Valid options: 'all' (default), 'ccw', 'cw'. The options 'ccw' and 'cw' limit plotting to those triangles where the order of the vertices are defined in a counter-clockwise or clockwise manner respectively. Note: when considering orientation or triangles remember that the y-axis is defined vertically down the screen. |
use_alpha |
Use alpha channel when drawing? Logical. Default: TRUE |
coords |
Wide numeric matrix of direct coordinate data for each triangle where the first two rows are x and y coordinates (extra rows are ignored) Each group of 3 columns defines the (x,y) coordinates for one triangle. |
Invisibly return the supplied native raster image which was been modified in-place
Other drawing functions:
nr_circle(),
nr_polygon(),
nr_polyline(),
nr_rect()
#' Using direct coordinates set.seed(1) w <- 100 h <- 80 nr <- nr_new(w, h) n_tri <- 10 xs <- runif(n_tri * 3, 0, w - 1) ys <- runif(n_tri * 3, 0, h - 1) coords <- rbind(xs, ys) cols <- rainbow(n_tri) nr_tri_coords(nr, coords, cols, tris = 'all') plot(nr) # Using a standard mesh structure # i.e. a matrix of vertices, and a matrix of indices nr <- nr_new(w, h) # Matrix of coordinates xs <- rep(seq(0, w - 1, length.out = 10), 10) ys <- rep(seq(0, h - 1, length.out = 10), each = 10) vertices <- rbind(xs, ys) nr_point(nr, xs, ys) plot(nr) # Matrix of indices indicating which vertices make up each triangle indices <- matrix(sample(length(xs), 3 * n_tri), nrow = 3) nr_tri_mesh(nr, vertices, indices, cols, tris = 'all') plot(nr)#' Using direct coordinates set.seed(1) w <- 100 h <- 80 nr <- nr_new(w, h) n_tri <- 10 xs <- runif(n_tri * 3, 0, w - 1) ys <- runif(n_tri * 3, 0, h - 1) coords <- rbind(xs, ys) cols <- rainbow(n_tri) nr_tri_coords(nr, coords, cols, tris = 'all') plot(nr) # Using a standard mesh structure # i.e. a matrix of vertices, and a matrix of indices nr <- nr_new(w, h) # Matrix of coordinates xs <- rep(seq(0, w - 1, length.out = 10), 10) ys <- rep(seq(0, h - 1, length.out = 10), each = 10) vertices <- rbind(xs, ys) nr_point(nr, xs, ys) plot(nr) # Matrix of indices indicating which vertices make up each triangle indices <- matrix(sample(length(xs), 3 * n_tri), nrow = 3) nr_tri_mesh(nr, vertices, indices, cols, tris = 'all') plot(nr)
Convert a list of native rasters to an animated gif
nrs_to_gif(nr_list, filename, verbose = FALSE, framerate = 30, ...)nrs_to_gif(nr_list, filename, verbose = FALSE, framerate = 30, ...)
nr_list |
list of native raster image |
filename |
filename of gif |
verbose |
logical. default FALSE |
framerate |
frames per second |
... |
other arguments passed to |
None.
Other conversion functions:
magick_to_nr(),
matrix_to_nr(),
nr_to_raster(),
nrs_to_mp4()
im <- magick::logo nr1 <- magick_to_nr(im) nr2 <- nr_copy(nr1) nrs_to_gif(list(nr1, nr2), "nothing.gif")im <- magick::logo nr1 <- magick_to_nr(im) nr2 <- nr_copy(nr1) nrs_to_gif(list(nr1, nr2), "nothing.gif")
Convert a list of native rasters to an mp4 video file
nrs_to_mp4(nr_list, filename, verbose = FALSE, ...)nrs_to_mp4(nr_list, filename, verbose = FALSE, ...)
nr_list |
list of native raster image |
filename |
mp4 filename |
verbose |
logical. default FALSE |
... |
other arguments passed to |
None.
Other conversion functions:
magick_to_nr(),
matrix_to_nr(),
nr_to_raster(),
nrs_to_gif()
im <- magick::logo nr1 <- magick_to_nr(im) nr2 <- nr_copy(nr1) nrs_to_mp4(list(nr1, nr2), "nothing.mp4")im <- magick::logo nr1 <- magick_to_nr(im) nr2 <- nr_copy(nr1) nrs_to_mp4(list(nr1, nr2), "nothing.mp4")
Plot a native raster image (after first clearning the device)
## S3 method for class 'nativeRaster' plot(x, y, ...)## S3 method for class 'nativeRaster' plot(x, y, ...)
x |
native raster image |
y |
ignored |
... |
other arguments passed to |
Invisibly return the supplied native raster image
nr <- nr_new(200, 100, 'hotpink') plot(nr)nr <- nr_new(200, 100, 'hotpink') plot(nr)
Print method
## S3 method for class 'nativeRaster' print(x, ...)## S3 method for class 'nativeRaster' print(x, ...)
x |
native raster image |
... |
ignored |
None
nr <- nr_new(500, 400) print(nr)nr <- nr_new(500, 400) print(nr)
The tiles in this list are of a common base size and can be conbined to construct various scenes.
tilesettileset
An object of class list of length 44.
nr_blit_multi() and the tileset data.Use this configuration wtih nr_blit_multi()
tileset_configtileset_config
An object of class spec_tbl_df (inherits from tbl_df, tbl, data.frame) with 51 rows and 6 columns.