Title: | Native Raster Image Tools |
---|---|
Description: | Tools for 'nativeRaster' images. |
Authors: | Mike Cheng [aut, cre, cph], Julien Vernay [ctb] (Author of thick polyline algorithm which is in the public domain) |
Maintainer: | Mike Cheng <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.1.9033 |
Built: | 2025-01-06 07:21:32 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:
deer_sprites
deer_sprites
An object of class list
of length 15.
cat(readLines(system.file('LICENSE-deer.txt', package = 'nara')), sep = "\n")
nativeRaster
Check if object is nativeRaster
is_nativeraster(x)
is_nativeraster(x)
x |
object to check |
logical. TRUE if object is a nativeRaster
is_nativeraster(mtcars)
is_nativeraster(mtcars)
nativeRaster
Convert a 'magick' image to nativeRaster
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 |
nr |
|
nativeRaster
im <- magick::logo nr <- magick_to_nr(im) plot(nr)
im <- magick::logo nr <- magick_to_nr(im) plot(nr)
nativeRaster
Matrix to nativeRaster
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 |
nativeRaster
m <- matrix(1:12, 3, 4) palette <- colorfast::col_to_int(rainbow(12)) nr <- matrix_to_nr(m, palette) plot(nr)
m <- matrix(1:12, 3, 4) palette <- colorfast::col_to_int(rainbow(12)) nr <- matrix_to_nr(m, palette) plot(nr)
nativeRaster
image into another at an arbitrary location.Single source blitted to one or more locations.
nr_blit( dst, src, x, y, xsrc = 0L, ysrc = 0L, w = -1L, h = -1L, hjust = 0.5, vjust = 0.5, angle = 0, scale = 1, respect_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, respect_alpha = TRUE )
dst , src
|
source and destination native rasters |
x , y
|
Where in |
xsrc , ysrc
|
start coordiates within src |
w , h
|
size within src. If size is negative, then the actual width/height of the src is used |
hjust , vjust
|
specify horizontal and vertical justification of the
|
angle |
Rotation angle (clockwise) in radians. Default: 0 |
scale |
Zoom factor. Default: 1 |
respect_alpha |
Should the alpha channel be respected when blitting?
Default: TRUE means to carefully blend pixels at each location
using alpha values. If FALSE, then contents at the |
None. dst
modified by-reference and returned invisibly.
nr <- nr_new(50, 50, 'grey80') nr_blit(dst = nr, src = deer_sprites[[1]], x = 25, y = 25) plot(nr, T) 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, T) nr <- nr_new(300, 200, 'grey80') sq <- png::readPNG(system.file("img", "Rlogo.png", package="png"), native = TRUE) nr_blit(nr, src = sq, x = 180, y = 120, angle = pi/6, scale = 1) plot(nr, T)
nr <- nr_new(50, 50, 'grey80') nr_blit(dst = nr, src = deer_sprites[[1]], x = 25, y = 25) plot(nr, T) 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, T) nr <- nr_new(300, 200, 'grey80') sq <- png::readPNG(system.file("img", "Rlogo.png", package="png"), native = TRUE) nr_blit(nr, src = sq, x = 180, y = 120, angle = pi/6, scale = 1) plot(nr, T)
Multiple blit operations in a single call
nr_blit_bulk(dst, src, config)
nr_blit_bulk(dst, src, config)
dst |
destination native raster |
src |
list of native rasters |
config |
data.frame of configuration information for each blit which most contain: idx, x, y, xsrc, ysrc, w, h, hjust, vjust, respect_alpha, draw |
None. dst
modifief by-reference and returned invisibly.
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), respect_alpha = TRUE, render = TRUE ) nr_blit_bulk(dst = nr, src = deer_sprites, config = config) plot(nr, T)
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), respect_alpha = TRUE, render = TRUE ) nr_blit_bulk(dst = nr, src = deer_sprites, config = config) plot(nr, T)
nativeRaster
imageDraw circles on a nativeRaster
image
nr_circle(nr, x, y, r, fill = "black", color = NA)
nr_circle(nr, x, y, r, fill = "black", color = NA)
nr |
|
x , y
|
coordinates of centre of circle. [vector] |
r |
radius [vector] |
fill |
interior fill color [vector] |
color |
outline color. Default: NA. [vector] |
Original nativeRaster
modified in-place
N <- 201 nr <- nr_new(N, N, 'grey80') nr_circle(nr, x = N/2, y = N/2, r = c(N/3, N/4), fill = c('darkred', 'black')) plot(nr, TRUE)
N <- 201 nr <- nr_new(N, N, 'grey80') nr_circle(nr, x = N/2, y = N/2, r = c(N/3, N/4), fill = c('darkred', 'black')) plot(nr, TRUE)
nativeRaster
into another.The source and destination nativeRaster
images must have the same dimensions.
nr_copy_into(dst, src, mask = NULL, color = "black", invert = FALSE)
nr_copy_into(dst, src, mask = NULL, color = "black", invert = FALSE)
src , dst
|
Source and destination |
mask |
Optional nativeRaster image to use as a mask. Default: NULL (no mask) |
color |
Copy the source pixels into the destination where the mask
matches this color. See also the |
invert |
Invert masking so that pixels are copied where the mask does not match the specified color. Default: FALSE |
If the nativeRaster
images are of different sizes or alpha blending is
required, use the nr_blit()
function.
The 'dst' nativeRaster
nr1 <- nr_new(200, 100, 'hotpink') nr2 <- nr_new(200, 100, 'green') nr_copy_into(nr1, nr2) plot(nr1) # Copy with mask logo <- png::readPNG(system.file("img", "Rlogo.png", package="png"), native = TRUE) src <- nr_duplicate(logo) dst <- nr_duplicate(logo) nr_fill(src, 'hotpink') nr_copy_into(dst, src, mask = logo, col = 0L, invert = TRUE) plot(dst, T)
nr1 <- nr_new(200, 100, 'hotpink') nr2 <- nr_new(200, 100, 'green') nr_copy_into(nr1, nr2) plot(nr1) # Copy with mask logo <- png::readPNG(system.file("img", "Rlogo.png", package="png"), native = TRUE) src <- nr_duplicate(logo) dst <- nr_duplicate(logo) nr_fill(src, 'hotpink') nr_copy_into(dst, src, mask = logo, col = 0L, invert = TRUE) plot(dst, T)
Crop a section out of a nativeRaster into a new nativeRaster
nr_crop(nr, x, y, w, h) nr_crop2(nr, loc)
nr_crop(nr, x, y, w, h) nr_crop2(nr, loc)
nr |
|
x , y , w , h
|
dimensions of cropped section |
loc |
dimensions of cropped section. A vector of 4 values
i.e. |
New nativeRaster
nr <- nr_new(400, 400, 'hotpink') nr2 <- nr_crop(nr, 0, 0, 10, 10) dim(nr2) plot(nr2)
nr <- nr_new(400, 400, 'hotpink') nr2 <- nr_crop(nr, 0, 0, 10, 10) dim(nr2) plot(nr2)
nativeRaster
image and copy the dimensions and
contents from an existing imageCreate a new nativeRaster
image and copy the dimensions and
contents from an existing image
nr_duplicate(nr)
nr_duplicate(nr)
nr |
|
New nativeRaster
nr1 <- nr_new(200, 200, 'hotpink') nr2 <- nr_duplicate(nr1) plot(nr2)
nr1 <- nr_new(200, 200, 'hotpink') nr2 <- nr_duplicate(nr1) plot(nr2)
nativeRaster
image with the given colorFill a nativeRaster
image with the given color
nr_fill(nr, color)
nr_fill(nr, color)
nr |
|
color |
Color as a character string. Either a standard R color
(e.g. 'blue', 'white')
or a hex color of the form |
The original nativeRaster
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)
nativeRaster
horizontallyFlip a nativeRaster
horizontally
nr_fliph(nr)
nr_fliph(nr)
nr |
|
Original nativeRaster
modified in-place
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)
nativeRaster
verticallyFlip a nativeRaster
vertically
nr_flipv(nr)
nr_flipv(nr)
nr |
|
Original nativeRaster
modified in-place
nr <- nr_new(400, 200, 'white') nr_rect(nr, 0, 0, 30, 15) plot(nr) nr_flipv(nr) plot(nr)
nr <- nr_new(400, 200, 'white') nr_rect(nr, 0, 0, 30, 15) plot(nr) nr_flipv(nr) plot(nr)
nativeRaster
imageUses Bresenham's algorithm to draw lines. No antialiasing.
nr_line(nr, x1, y1, x2, y2, color = "black", linewidth = 1)
nr_line(nr, x1, y1, x2, y2, color = "black", linewidth = 1)
nr |
|
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 |
Original nativeRaster
modified in-place
N <- 20 nr <- nr_new(N, N, 'grey80') nr_line(nr, x1 = c(0, N-1), y1 = c(0, 0), x2 = c(N-1, 0), y2 = c(N-1, N-1), color = c('red', 'black')) plot(nr)
N <- 20 nr <- nr_new(N, N, 'grey80') nr_line(nr, x1 = c(0, N-1), y1 = c(0, 0), x2 = c(N-1, 0), y2 = c(N-1, N-1), color = c('red', 'black')) plot(nr)
nativeRaster
imageA nativeRaster
in R looks like an integer matrix, but is interpreted
differently by graphics devices:
nr_new(width, height, fill = "white")
nr_new(width, height, 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 |
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
nativeRaster
nr <- nr_new(400, 300, 'hotpink') plot(nr)
nr <- nr_new(400, 300, 'hotpink') plot(nr)
nativeRaster
imageDraw points on a nativeRaster
image
nr_point(nr, x, y, color = "black")
nr_point(nr, x, y, color = "black")
nr |
|
x , y
|
Vectors of point coordinates |
color |
Vector of colors |
Original nativeRaster
modified in-place
N <- 20 nr <- nr_new(N, N, 'grey80') nr_point(nr, x = seq(N), y = seq(N), color = rainbow(N)) plot(nr)
N <- 20 nr <- nr_new(N, N, 'grey80') nr_point(nr, x = seq(N), y = seq(N), color = rainbow(N)) plot(nr)
nativeRaster
imageDraw multiple polygon on a nativeRaster
image
nr_polygon( nr, x, y, id = NULL, fill = "black", color = NA, linewidth = 1, mitre_limit = linewidth )
nr_polygon( nr, x, y, id = NULL, fill = "black", color = NA, linewidth = 1, mitre_limit = linewidth )
nr |
|
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. |
Original nativeRaster
modified in-place
N <- 200 nr <- nr_new(N, N, 'grey80') nr_polygon(nr, x = c(10, N-10, 10), y = c(10, 10, N-10), fill = 'blue', color = 'red', linewidth = 5) plot(nr, TRUE)
N <- 200 nr <- nr_new(N, N, 'grey80') nr_polygon(nr, x = c(10, N-10, 10), y = c(10, 10, N-10), fill = 'blue', color = 'red', linewidth = 5) plot(nr, TRUE)
nativeRaster
imageDraw polyline on a nativeRaster
image
nr_polyline( nr, x, y, color = "black", linewidth = 1, mitre_limit = linewidth, close = FALSE )
nr_polyline( nr, x, y, color = "black", linewidth = 1, mitre_limit = linewidth, close = FALSE )
nr |
|
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 |
Original nativeRaster
modified in-place
N <- 20 nr <- nr_new(N, N, 'grey80') nr_polyline(nr, x = c(0, N-1, 0), y = c(0, 0, N-1), color = 'red') plot(nr, TRUE) N <- 200 nr <- nr_new(N, N, 'grey80') nr_polyline(nr, x = c(10, N-10, 10), y = c(10, 10, N-10), color = 'red', linewidth = 5, mitre_limit = 3) nr_polyline(nr, x = c(10, N-10, 10), y = c(10, 10, N-10), color = 'black') plot(nr, TRUE) N <- 200 nr <- nr_new(N, N, 'grey80') nr_polyline(nr, x = c(10, N-10, N-10, 10), y = c(10, 10, N-10, N-10), color = 'red', linewidth = 10, mitre_limit = 5, close = TRUE) nr_polyline(nr, x = c(10, N-10, N-10, 10), y = c(10, 10, N-10, N-10), color = 'black', close = TRUE) plot(nr, TRUE)
N <- 20 nr <- nr_new(N, N, 'grey80') nr_polyline(nr, x = c(0, N-1, 0), y = c(0, 0, N-1), color = 'red') plot(nr, TRUE) N <- 200 nr <- nr_new(N, N, 'grey80') nr_polyline(nr, x = c(10, N-10, 10), y = c(10, 10, N-10), color = 'red', linewidth = 5, mitre_limit = 3) nr_polyline(nr, x = c(10, N-10, 10), y = c(10, 10, N-10), color = 'black') plot(nr, TRUE) N <- 200 nr <- nr_new(N, N, 'grey80') nr_polyline(nr, x = c(10, N-10, N-10, 10), y = c(10, 10, N-10, N-10), color = 'red', linewidth = 10, mitre_limit = 5, close = TRUE) nr_polyline(nr, x = c(10, N-10, N-10, 10), y = c(10, 10, N-10, N-10), color = 'black', close = TRUE) plot(nr, TRUE)
nativeRaster
imageDraw rectangles on a nativeRaster
image
nr_rect( nr, x, y, w, h, fill = "black", color = NA, hjust = 0, vjust = 0, linewidth = 1 )
nr_rect( nr, x, y, w, h, fill = "black", color = NA, hjust = 0, vjust = 0, linewidth = 1 )
nr |
|
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
|
linewidth |
Line linewidth. Default: 1. If |
Original nativeRaster
modified in-place
N <- 200 nr <- nr_new(N, N, 'grey80') nr_rect(nr, x = c(0, N/2 - 1), y = c(0, N/2 - 1), w = N/2, h = N/4, fill = 'blue', color = c('red', 'green'), linewidth = 17) plot(nr, TRUE)
N <- 200 nr <- nr_new(N, N, 'grey80') nr_rect(nr, x = c(0, N/2 - 1), y = c(0, N/2 - 1), w = N/2, h = N/4, fill = 'blue', color = c('red', 'green'), linewidth = 17) plot(nr, TRUE)
Replace colours in a native raster
nr_replace(nr, old, new)
nr_replace(nr, old, new)
nr |
|
old |
Vector of old colours |
new |
Vector of replacement colours |
Original nativeRaster
modified in-place
nr <- nr_new(10, 10, 'hotpink') nr_replace(nr, 'hotpink', 'grey80') plot(nr)
nr <- nr_new(10, 10, 'hotpink') nr_replace(nr, 'hotpink', 'grey80') plot(nr)
Scale a nativeRaster
nr_resize(nr, width, height, algo = "nn")
nr_resize(nr, width, height, algo = "nn")
nr |
native raster |
width , height
|
new dimensions |
algo |
'nn' for nearest neighbour (the default), or 'bilinear' for bilinear interpolation. |
New nativeRaster
stretched <- nr_resize(deer_sprites[[1]], 100, 40, algo = 'nn') plot(stretched)
stretched <- nr_resize(deer_sprites[[1]], 100, 40, algo = 'nn') plot(stretched)
Rotate a native raster by 90,180,270 degrees
nr_rotate(nr, angle)
nr_rotate(nr, angle)
nr |
|
angle |
one of 0,90,180,270 |
Original nativeRaster
modified in-place
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)
nativeRaster
using Nearest Neighbour resizinngScale the size of a nativeRaster
using Nearest Neighbour resizinng
nr_scale(nr, scale, algo = "nn")
nr_scale(nr, scale, algo = "nn")
nr |
|
scale |
scale factor |
algo |
'nn' for nearest neighbour (the default), or 'bilinear' for bilinear interpolation. |
New nativeRaster
big <- nr_scale(deer_sprites[[1]], 2) plot(big)
big <- nr_scale(deer_sprites[[1]], 2) plot(big)
nativeRaster
image using the built-in spleen
bitmapped font.The only font currently available is 'spleen' - a monospace bitmap font from: https://github.com/fcambus/spleen
nr_text_basic(nr, x, y, str, color = "black", fontsize = 8L)
nr_text_basic(nr, x, y, str, color = "black", fontsize = 8L)
nr |
|
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. |
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")
Original nativeRaster
modified in-place
N <- 20 nr <- nr_new(N, N, 'grey80') nr_text_basic(nr, x = 0, y = N/2, str = "Hi!") plot(nr)
N <- 20 nr <- nr_new(N, N, 'grey80') nr_text_basic(nr, x = 0, y = N/2, str = "Hi!") plot(nr)
nativeRaster
images to/from other R objectsConvert nativeRaster
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 |
|
ras |
standard R raster i.e. a character matrix of hex color values |
dst |
destination |
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 nativeRaster
nr <- nr_new(12, 8, 'hotpink') nr_to_raster(nr)
nr <- nr_new(12, 8, 'hotpink') nr_to_raster(nr)
Transpose
nr_transpose(nr)
nr_transpose(nr)
nr |
|
Original nativeRaster
modified in-place
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)
Magick conversion - possibly won't be in final version of 'nara' pkg
nrs_to_gif(nr_list, filename, verbose = FALSE, framerate = 30, ...)
nrs_to_gif(nr_list, filename, verbose = FALSE, framerate = 30, ...)
nr_list |
list of nativeRasters |
filename |
filename of gif |
verbose |
logical. default FALSE |
framerate |
frames per second |
... |
other arguments passed to |
None.
im <- magick::logo nr1 <- magick_to_nr(im) nr2 <- nr_duplicate(nr1) nrs_to_gif(list(nr1, nr2), "nothing.gif")
im <- magick::logo nr1 <- magick_to_nr(im) nr2 <- nr_duplicate(nr1) nrs_to_gif(list(nr1, nr2), "nothing.gif")
Magick conversion - possible won't be in final version of 'nara' pkg
nrs_to_mp4(nr_list, filename, verbose = FALSE, ...)
nrs_to_mp4(nr_list, filename, verbose = FALSE, ...)
nr_list |
list of nativeRasters |
filename |
mp4 filename |
verbose |
logical. default FALSE |
... |
other arguments passed to |
None.
im <- magick::logo nr1 <- magick_to_nr(im) nr2 <- nr_duplicate(nr1) nrs_to_mp4(list(nr1, nr2), "nothing.mp4")
im <- magick::logo nr1 <- magick_to_nr(im) nr2 <- nr_duplicate(nr1) nrs_to_mp4(list(nr1, nr2), "nothing.mp4")
nativeRaster
as an imagePlot a nativeRaster
as an image
## S3 method for class 'nativeRaster' plot(x, y, ...)
## S3 method for class 'nativeRaster' plot(x, y, ...)
x |
|
y |
any argument here will cause |
... |
other arguments passed to |
None.
nr <- nr_new(200, 100, 'hotpink') plot(nr)
nr <- nr_new(200, 100, 'hotpink') plot(nr)