C API

library(nara)
library(callme)

C function declaration for drawing a line on a native raster

void nr_line(uint32_t *nr, int nr_width, int nr_height, int x1, int y1, int x2, int y2, uint32_t color, double linewidth);

code <- r"(
#include <stdint.h>
#include "nara.h"

SEXP my_func(SEXP nr_) {

  int nr_width = Rf_ncols(nr_);
  int nr_height = Rf_nrows(nr_);
  uint32_t *nr = (uint32_t *)INTEGER(nr_);
  uint32_t col = 0xFF0000FFu; // 0xAABBGGRR Red

  //      nr, nr_width, nr_height, x1, y1,  x2,  y2, color, linewidth
  nr_line(nr, nr_width, nr_height,  0,  0, 200, 150, col  , 1);

  return nr_;
}
)"

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Find the location of nara.h and include its directory in the search path
# using C Pre-Processor flags (PKG_CPPFLAGS)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nara_h <- system.file("include", "nara.h", package = "nara", mustWork = TRUE)
cpp_include = paste0("-I", dirname(nara_h))
callme::compile(code, PKG_CPPFLAGS = cpp_include)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Create a native raster and call my function
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nr <- nr_new(400, 300, 'grey70')
nr <- my_func(nr)
plot(nr, T)