Package 'nara'

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

Help Index


List of deer native rasters

Description

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:

Usage

deer_sprites

Format

An object of class list of length 15.

Details

cat(readLines(system.file('LICENSE-deer.txt', package = 'nara')), sep = "\n")


Check if object is nativeRaster

Description

Check if object is nativeRaster

Usage

is_nativeraster(x)

Arguments

x

object to check

Value

logical. TRUE if object is a nativeRaster

Examples

is_nativeraster(mtcars)

Convert a 'magick' image to nativeRaster

Description

Convert a 'magick' image to nativeRaster

Usage

magick_to_nr(im, dst = NULL)

nr_to_magick(nr)

Arguments

im

image from the magick package

dst

destination nativeRaster object. If NULL (the default) a new nativeRaster will be created If a nativeRaster is supplied here, it must have the exact dimensions to match the matrix

nr

nativeRaster object

Value

nativeRaster

Examples

im <- magick::logo
nr <- magick_to_nr(im)
plot(nr)

Matrix to nativeRaster

Description

Matrix to nativeRaster

Usage

matrix_to_nr(mat, palette, fill = "transparent", min = 0, max = 1, dst = NULL)

Arguments

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 mat. For a numeric matrix, any length palette is allowed.

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 object. If NULL (the default) a new nativeRaster will be created If a nativeRaster is supplied here, it must have the exact dimensions to match the matrix

Value

nativeRaster

Examples

m <- matrix(1:12, 3, 4)
palette <- colorfast::col_to_int(rainbow(12))
nr <- matrix_to_nr(m, palette) 
plot(nr)

Copy one nativeRaster image into another at an arbitrary location.

Description

Single source blitted to one or more locations.

Usage

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
)

Arguments

dst, src

source and destination native rasters

x, y

Where in nr to place the sprite. These values must be vectors of the same length. If the length is greater than 1, then the sprite will be pasted into nr at multiple locations. Note that the origin of nativeraster images is the top-left where the coordinates are (0, 0).

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 src image. e.g. hjust = vjust = 0 the blitting starts at the top-left of the image. Use hjust = vjust = 0.5 to treat the centre of the src_ as the blitting origin. Default (0, 0)

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 dst will just be replaced with src pixels which can be much much faster.

Value

None. dst modified by-reference and returned invisibly.

Examples

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

Description

Multiple blit operations in a single call

Usage

nr_blit_bulk(dst, src, config)

Arguments

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

Value

None. dst modifief by-reference and returned invisibly.

Examples

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)

Draw circles on a nativeRaster image

Description

Draw circles on a nativeRaster image

Usage

nr_circle(nr, x, y, r, fill = "black", color = NA)

Arguments

nr

nativeRaster

x, y

coordinates of centre of circle. [vector]

r

radius [vector]

fill

interior fill color [vector]

color

outline color. Default: NA. [vector]

Value

Original nativeRaster modified in-place

Examples

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)

Copy the contents of one nativeRaster into another.

Description

The source and destination nativeRaster images must have the same dimensions.

Usage

nr_copy_into(dst, src, mask = NULL, color = "black", invert = FALSE)

Arguments

src, dst

Source and destination nativeRaster images

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 argument.

invert

Invert masking so that pixels are copied where the mask does not match the specified color. Default: FALSE

Details

If the nativeRaster images are of different sizes or alpha blending is required, use the nr_blit() function.

Value

The 'dst' nativeRaster

Examples

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

Description

Crop a section out of a nativeRaster into a new nativeRaster

Usage

nr_crop(nr, x, y, w, h)

nr_crop2(nr, loc)

Arguments

nr

nativeRaster

x, y, w, h

dimensions of cropped section

loc

dimensions of cropped section. A vector of 4 values i.e. c(x, y, w, h)

Value

New nativeRaster

Examples

nr <- nr_new(400, 400, 'hotpink')
nr2 <- nr_crop(nr, 0, 0, 10, 10)
dim(nr2)
plot(nr2)

Create a new nativeRaster image and copy the dimensions and contents from an existing image

Description

Create a new nativeRaster image and copy the dimensions and contents from an existing image

Usage

nr_duplicate(nr)

Arguments

nr

nativeRaster

Value

New nativeRaster

Examples

nr1 <- nr_new(200, 200, 'hotpink')
nr2 <- nr_duplicate(nr1)
plot(nr2)

Fill a nativeRaster image with the given color

Description

Fill a nativeRaster image with the given color

Usage

nr_fill(nr, color)

Arguments

nr

nativeRaster

color

Color as a character string. Either a standard R color (e.g. 'blue', 'white') or a hex color of the form #rrggbbaa, #rrggbb, #rgba or #rgb

Value

The original nativeRaster modified in-place.

Examples

nr <- nr_new(400, 300, 'hotpink')
nr_fill(nr, 'blue')
plot(nr)

Flip a nativeRaster horizontally

Description

Flip a nativeRaster horizontally

Usage

nr_fliph(nr)

Arguments

nr

nativeRaster

Value

Original nativeRaster modified in-place

Examples

nr <- nr_new(400, 200, 'white')
nr_rect(nr, 0, 0, 30, 15)
plot(nr)
nr_fliph(nr)
plot(nr)

Flip a nativeRaster vertically

Description

Flip a nativeRaster vertically

Usage

nr_flipv(nr)

Arguments

nr

nativeRaster

Value

Original nativeRaster modified in-place

Examples

nr <- nr_new(400, 200, 'white')
nr_rect(nr, 0, 0, 30, 15)
plot(nr)
nr_flipv(nr)
plot(nr)

Draw lines on a nativeRaster image

Description

Uses Bresenham's algorithm to draw lines. No antialiasing.

Usage

nr_line(nr, x1, y1, x2, y2, color = "black", linewidth = 1)

Arguments

nr

nativeRaster

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 #rrggbbaa, #rrggbb, #rgba or #rgb

linewidth

Line linewidth. Default: 1. If linewidth = 1 then a naive version of Bresenham is used to draw the points. If linewidth is greater than 1, then the line is convert to a triangle strip and rendered as polygons.

Value

Original nativeRaster modified in-place

Examples

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)

Create a nativeRaster image

Description

A nativeRaster in R looks like an integer matrix, but is interpreted differently by graphics devices:

Usage

nr_new(width, height, fill = "white")

Arguments

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 #rrggbbaa, #rrggbb, #rgba or #rgb

Details

  • 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

Value

nativeRaster

Examples

nr <- nr_new(400, 300, 'hotpink')
plot(nr)

Draw points on a nativeRaster image

Description

Draw points on a nativeRaster image

Usage

nr_point(nr, x, y, color = "black")

Arguments

nr

nativeRaster

x, y

Vectors of point coordinates

color

Vector of colors

Value

Original nativeRaster modified in-place

Examples

N <- 20
nr <- nr_new(N, N, 'grey80')
nr_point(nr, x = seq(N), y = seq(N), color = rainbow(N)) 
plot(nr)

Draw multiple polygon on a nativeRaster image

Description

Draw multiple polygon on a nativeRaster image

Usage

nr_polygon(
  nr,
  x,
  y,
  id = NULL,
  fill = "black",
  color = NA,
  linewidth = 1,
  mitre_limit = linewidth
)

Arguments

nr

nativeRaster

x, y

Vectors of point coordinates

id

integer vector used to separate coordinates into multiple polygons. Consecutive runs of the same id value belong to the same polygon. If NULL (the default) then all coordinates are assumed to be vertices of a single polygon.

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 #rrggbbaa, #rrggbb, #rgba or #rgb

linewidth

Line linewidth. Default: 1. If linewidth = 1 then a naive version of Bresenham is used to draw the points. If linewidth is greater than 1, then the line is convert to a triangle strip and rendered as polygons.

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.

Value

Original nativeRaster modified in-place

Examples

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)

Draw polyline on a nativeRaster image

Description

Draw polyline on a nativeRaster image

Usage

nr_polyline(
  nr,
  x,
  y,
  color = "black",
  linewidth = 1,
  mitre_limit = linewidth,
  close = FALSE
)

Arguments

nr

nativeRaster

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 #rrggbbaa, #rrggbb, #rgba or #rgb

linewidth

Line linewidth. Default: 1. If linewidth = 1 then a naive version of Bresenham is used to draw the points. If linewidth is greater than 1, then the line is convert to a triangle strip and rendered as polygons.

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

Value

Original nativeRaster modified in-place

Examples

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)

Draw rectangles on a nativeRaster image

Description

Draw rectangles on a nativeRaster image

Usage

nr_rect(
  nr,
  x,
  y,
  w,
  h,
  fill = "black",
  color = NA,
  hjust = 0,
  vjust = 0,
  linewidth = 1
)

Arguments

nr

nativeRaster

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 src image. e.g. hjust = vjust = 0 the blitting starts at the top-left of the image. Use hjust = vjust = 0.5 to treat the centre of the src_ as the blitting origin. Default (0, 0)

linewidth

Line linewidth. Default: 1. If linewidth = 1 then a naive version of Bresenham is used to draw the points. If linewidth is greater than 1, then the line is convert to a triangle strip and rendered as polygons.

Value

Original nativeRaster modified in-place

Examples

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

Description

Replace colours in a native raster

Usage

nr_replace(nr, old, new)

Arguments

nr

nativeRaster

old

Vector of old colours

new

Vector of replacement colours

Value

Original nativeRaster modified in-place

Examples

nr <- nr_new(10, 10, 'hotpink')
nr_replace(nr, 'hotpink', 'grey80')
plot(nr)

Scale a nativeRaster

Description

Scale a nativeRaster

Usage

nr_resize(nr, width, height, algo = "nn")

Arguments

nr

native raster

width, height

new dimensions

algo

'nn' for nearest neighbour (the default), or 'bilinear' for bilinear interpolation.

Value

New nativeRaster

Examples

stretched <- nr_resize(deer_sprites[[1]], 100, 40, algo = 'nn')
plot(stretched)

Rotate a native raster by 90,180,270 degrees

Description

Rotate a native raster by 90,180,270 degrees

Usage

nr_rotate(nr, angle)

Arguments

nr

nativeRaster

angle

one of 0,90,180,270

Value

Original nativeRaster modified in-place

Examples

nr <- nr_new(20, 10, 'hotpink')
dim(nr)
nr_rotate(nr, 90)
dim(nr)

Scale the size of a nativeRaster using Nearest Neighbour resizinng

Description

Scale the size of a nativeRaster using Nearest Neighbour resizinng

Usage

nr_scale(nr, scale, algo = "nn")

Arguments

nr

nativeRaster

scale

scale factor

algo

'nn' for nearest neighbour (the default), or 'bilinear' for bilinear interpolation.

Value

New nativeRaster

Examples

big <- nr_scale(deer_sprites[[1]], 2)
plot(big)

Draw text on a nativeRaster image using the built-in spleen bitmapped font.

Description

The only font currently available is 'spleen' - a monospace bitmap font from: https://github.com/fcambus/spleen

Usage

nr_text_basic(nr, x, y, str, color = "black", fontsize = 8L)

Arguments

nr

nativeRaster

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 #rrggbbaa, #rrggbb, #rgba or #rgb

fontsize

height of font in pizels. Only valid values are 8, 12 and 16. Default: 8.

Details

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")

Value

Original nativeRaster modified in-place

Examples

N <- 20
nr <- nr_new(N, N, 'grey80')
nr_text_basic(nr, x = 0, y = N/2, str = "Hi!")
plot(nr)

Convert nativeRaster images to/from other R objects

Description

Convert nativeRaster images to/from other R objects

Usage

nr_to_raster(nr)

raster_to_nr(ras, dst = NULL)

nr_to_array(nr)

array_to_nr(arr, dst = NULL)

Arguments

nr

nativeRaster object

ras

standard R raster i.e. a character matrix of hex color values

dst

destination nativeRaster If NULL (the default) a new nativeRaster will be created.

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].

Value

raster, array or nativeRaster

Examples

nr <- nr_new(12, 8, 'hotpink')
nr_to_raster(nr)

Transpose

Description

Transpose

Usage

nr_transpose(nr)

Arguments

nr

nativeRaster

Value

Original nativeRaster modified in-place

Examples

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

Description

Magick conversion - possibly won't be in final version of 'nara' pkg

Usage

nrs_to_gif(nr_list, filename, verbose = FALSE, framerate = 30, ...)

Arguments

nr_list

list of nativeRasters

filename

filename of gif

verbose

logical. default FALSE

framerate

frames per second

...

other arguments passed to magick::image_write_gif()

Value

None.

Examples

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

Description

Magick conversion - possible won't be in final version of 'nara' pkg

Usage

nrs_to_mp4(nr_list, filename, verbose = FALSE, ...)

Arguments

nr_list

list of nativeRasters

filename

mp4 filename

verbose

logical. default FALSE

...

other arguments passed to magick::image_write_video()

Value

None.

Examples

im <- magick::logo
nr1 <- magick_to_nr(im)
nr2 <- nr_duplicate(nr1)
nrs_to_mp4(list(nr1, nr2), "nothing.mp4")

Plot a nativeRaster as an image

Description

Plot a nativeRaster as an image

Usage

## S3 method for class 'nativeRaster'
plot(x, y, ...)

Arguments

x

nativeRaster

y

any argument here will cause grid::grid.newpage() to be called prior to drawing the nativeRaster

...

other arguments passed to grid::grid.raster()

Value

None.

Examples

nr <- nr_new(200, 100, 'hotpink')
plot(nr)