Title: | Native Raster Image Tools |
---|---|
Description: | Tools for 'nativeRaster' images. |
Authors: | Mike Cheng [aut, cre, cph] |
Maintainer: | Mike Cheng <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.1.9025 |
Built: | 2024-11-07 12:24:00 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
if (requireNamespace('magick', quietly = TRUE)) { im <- magick::image_read(system.file("img/Rlogo.png", package = "png")) nr <- magick_to_nr(im) plot(nr) }
if (requireNamespace('magick', quietly = TRUE)) { im <- magick::image_read(system.file("img/Rlogo.png", package = "png")) 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 <- str_cols_to_packed_cols(rainbow(12)) nr <- matrix_to_nr(m, palette) plot(nr)
m <- matrix(1:12, 3, 4) palette <- str_cols_to_packed_cols(rainbow(12)) nr <- matrix_to_nr(m, palette) plot(nr)
nativeRaster
image into another at an arbitrary location.This is useful as a way of positioning sprites or icons in an image.
nr_blit( nr, x, y, src, x0 = 0L, y0 = 0L, w = -1L, h = -1L, hjust = 0, vjust = 0, respect_alpha = TRUE )
nr_blit( nr, x, y, src, x0 = 0L, y0 = 0L, w = -1L, h = -1L, hjust = 0, vjust = 0, respect_alpha = TRUE )
nr |
native raster to copy into |
x , y
|
Where in |
src |
native raster to copy from |
x0 , y0
|
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
|
respect_alpha |
Should the alpha channel be respected when blitting?
Default: TRUE. If FALSE, then contents will be blindly overwritten
which can be much much faster. If the |
nativeRaster
nr <- nr_new(50, 50, 'grey80') nr_blit(nr, x = 0, y = 0, src = deer_sprites[[1]]) plot(nr)
nr <- nr_new(50, 50, 'grey80') nr_blit(nr, x = 0, y = 0, src = deer_sprites[[1]]) plot(nr)
Blit4
nr_blit_grid( nr, x, y, src_list, idx_mat, width, height, hjust = 0, vjust = 0, respect_alpha = TRUE )
nr_blit_grid( nr, x, y, src_list, idx_mat, width, height, hjust = 0, vjust = 0, respect_alpha = TRUE )
nr |
native raster to copy into |
x , y
|
Where in |
src_list |
list of native rasters |
idx_mat |
integer matrix of indices into |
width , height
|
tile width/height (constant across all tiles) |
hjust , vjust
|
specify horizontal and vertical justification of the
|
respect_alpha |
Should the alpha channel be respected when blitting?
Default: TRUE. If FALSE, then contents will be blindly overwritten
which can be much much faster. If the |
Original nativeRaster
modified in-place
nr <- nr_new(100, 100, 'grey80') idx_mat <- matrix(c( 1, 2, 3, 4, 5, 6, 7, 8, 9 ), 3, 3, byrow = TRUE) nr_blit_grid(nr, 0, 0, src_list = deer_sprites, idx_mat = idx_mat, width = 32, height = 32)
nr <- nr_new(100, 100, 'grey80') idx_mat <- matrix(c( 1, 2, 3, 4, 5, 6, 7, 8, 9 ), 3, 3, byrow = TRUE) nr_blit_grid(nr, 0, 0, src_list = deer_sprites, idx_mat = idx_mat, width = 32, height = 32)
Blit from a list of native rasters
nr_blit_list( nr, x, y, src_list, src_idx, hjust = 0, vjust = 0, respect_alpha = TRUE )
nr_blit_list( nr, x, y, src_list, src_idx, hjust = 0, vjust = 0, respect_alpha = TRUE )
nr |
native raster to copy into |
x , y
|
Where in |
src_list |
list of native rasters |
src_idx |
indices into the list of the native raster |
hjust , vjust
|
specify horizontal and vertical justification of the
|
respect_alpha |
Should the alpha channel be respected when blitting?
Default: TRUE. If FALSE, then contents will be blindly overwritten
which can be much much faster. If the |
nr <- nr_new(50, 50, 'grey80') nr_blit_list(nr, x = c(0, 25), y = c(0, 25), src_list = deer_sprites, src_idx = c(1, 2)) plot(nr)
nr <- nr_new(50, 50, 'grey80') nr_blit_list(nr, x = c(0, 25), y = c(0, 25), src_list = deer_sprites, src_idx = c(1, 2)) plot(nr)
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 <- 21 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)
N <- 21 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)
nativeRaster
into another.The source and destination nativeRaster
images must have the same dimensions.
nr_copy_into(dst, src)
nr_copy_into(dst, src)
src , dst
|
Source and destination |
If the nativeRaster
images are of different sizes, 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)
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 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, x0, y0, x1, y1, color = "black")
nr_line(nr, x0, y0, x1, y1, color = "black")
nr |
|
x0 , y0 , x1 , y1
|
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 |
Original nativeRaster
modified in-place
N <- 20 nr <- nr_new(N, N, 'grey80') nr_line(nr, x0 = c(0, N-1), y0 = c(0, 0), x1 = c(N-1, 0), y1 = c(N-1, N-1), color = c('red', 'black')) plot(nr)
N <- 20 nr <- nr_new(N, N, 'grey80') nr_line(nr, x0 = c(0, N-1), y0 = c(0, 0), x1 = c(N-1, 0), y1 = 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)
nr_polygon(nr, x, y, id = NULL, fill = "black", color = NA)
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 |
Original nativeRaster
modified in-place
N <- 20 nr <- nr_new(N, N, 'grey80') nr_polygon(nr, x = c(0, N-1, 0), y = c(0, 0, N-1), fill = 'blue', color = 'red') plot(nr)
N <- 20 nr <- nr_new(N, N, 'grey80') nr_polygon(nr, x = c(0, N-1, 0), y = c(0, 0, N-1), fill = 'blue', color = 'red') plot(nr)
nativeRaster
imageDraw polyline on a nativeRaster
image
nr_polyline(nr, x, y, color = "black", close = FALSE)
nr_polyline(nr, x, y, color = "black", 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 |
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)
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)
nativeRaster
imageDraw rectangles on a nativeRaster
image
nr_rect(nr, x, y, w, h, fill = "black", color = NA, hjust = 0, vjust = 0)
nr_rect(nr, x, y, w, h, fill = "black", color = NA, hjust = 0, vjust = 0)
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
|
Original nativeRaster
modified in-place
N <- 20 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')) plot(nr)
N <- 20 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')) plot(nr)
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)
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)
Magick conversion - possibly won't be in final version of 'nara' pkg
nrs_to_gif(nr_list, gif_name, verbose = FALSE, framerate = 30, ...)
nrs_to_gif(nr_list, gif_name, verbose = FALSE, framerate = 30, ...)
nr_list |
list of nativeRasters |
gif_name |
name of mp4 file to save |
verbose |
logical. default FALSE |
framerate |
frames per second |
... |
other arguments passed to |
Magick conversion - possible won't be in final version of 'nara' pkg
nrs_to_mp4(nr_list, mp4_name, verbose = FALSE, ...)
nrs_to_mp4(nr_list, mp4_name, verbose = FALSE, ...)
nr_list |
list of nativeRasters |
mp4_name |
name of mp4 file to save |
verbose |
logical. default FALSE |
... |
other arguments passed to |
Convert packed colors (integer values containing RGBA bytes) to hex colors
packed_cols_to_hex_cols(packed_cols)
packed_cols_to_hex_cols(packed_cols)
packed_cols |
integer values each containing packed RGBA color information |
character vector of hex colors
packed_cols_to_hex_cols(c(-16776961L, -1L, -65536L, 16777215L, 16777215L))
packed_cols_to_hex_cols(c(-16776961L, -1L, -65536L, 16777215L, 16777215L))
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)
Convert colors (R colors and hex colors) into packed colors (integer values containing RGBA bytes)
str_cols_to_packed_cols(colors)
str_cols_to_packed_cols(colors)
colors |
character vector of R color names and hex colors e.g.
|
Integer vector. Each integer value contains a packed color i.e. RGBA bytes.
str_cols_to_packed_cols(c('red', 'white', 'blue', NA, 'transparent'))
str_cols_to_packed_cols(c('red', 'white', 'blue', NA, 'transparent'))