Skip to content

Commit

Permalink
rayimage v0.11.0: Image read/write functions, TIFF support, clamping,…
Browse files Browse the repository at this point in the history
… and B&W conversion

-Add `ray_read_image()` to standardize loading images into 3/4 layer RGBA arrays
-Add `ray_write_image()` to standardize writing files
-Add `render_clamp()` to clamp images
-Add `render_bw()` to generate B&W images
-Add TIFF support via `tiff` package
-Significant clean-up of internal code to remove un-needed transposition and orientation changes, and standardize reading in images
-`plot_image()` Ignore alpha when plotting image
  • Loading branch information
tylermorganwall committed May 30, 2024
1 parent 8497883 commit fd26b28
Show file tree
Hide file tree
Showing 47 changed files with 829 additions and 442 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@
^\.github$
^CODE_OF_CONDUCT\.md$
^CONTRIBUTING\.md$
^tests/.*$
^.*\.DS_Store$
95 changes: 95 additions & 0 deletions .github/workflows/rhub.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# R-hub's generic GitHub Actions workflow file. It's canonical location is at
# https://github.com/r-hub/actions/blob/v1/workflows/rhub.yaml
# You can update this file to a newer version using the rhub2 package:
#
# rhub::rhub_setup()
#
# It is unlikely that you need to modify this file manually.

name: R-hub
run-name: "${{ github.event.inputs.id }}: ${{ github.event.inputs.name || format('Manually run by {0}', github.triggering_actor) }}"

on:
workflow_dispatch:
inputs:
config:
description: 'A comma separated list of R-hub platforms to use.'
type: string
default: 'linux,windows,macos'
name:
description: 'Run name. You can leave this empty now.'
type: string
id:
description: 'Unique ID. You can leave this empty now.'
type: string

jobs:

setup:
runs-on: ubuntu-latest
outputs:
containers: ${{ steps.rhub-setup.outputs.containers }}
platforms: ${{ steps.rhub-setup.outputs.platforms }}

steps:
# NO NEED TO CHECKOUT HERE
- uses: r-hub/actions/setup@v1
with:
config: ${{ github.event.inputs.config }}
id: rhub-setup

linux-containers:
needs: setup
if: ${{ needs.setup.outputs.containers != '[]' }}
runs-on: ubuntu-latest
name: ${{ matrix.config.label }}
strategy:
fail-fast: false
matrix:
config: ${{ fromJson(needs.setup.outputs.containers) }}
container:
image: ${{ matrix.config.container }}

steps:
- uses: r-hub/actions/checkout@v1
- uses: r-hub/actions/platform-info@v1
with:
token: ${{ secrets.RHUB_TOKEN }}
job-config: ${{ matrix.config.job-config }}
- uses: r-hub/actions/setup-deps@v1
with:
token: ${{ secrets.RHUB_TOKEN }}
job-config: ${{ matrix.config.job-config }}
- uses: r-hub/actions/run-check@v1
with:
token: ${{ secrets.RHUB_TOKEN }}
job-config: ${{ matrix.config.job-config }}

other-platforms:
needs: setup
if: ${{ needs.setup.outputs.platforms != '[]' }}
runs-on: ${{ matrix.config.os }}
name: ${{ matrix.config.label }}
strategy:
fail-fast: false
matrix:
config: ${{ fromJson(needs.setup.outputs.platforms) }}

steps:
- uses: r-hub/actions/checkout@v1
- uses: r-hub/actions/setup-r@v1
with:
job-config: ${{ matrix.config.job-config }}
token: ${{ secrets.RHUB_TOKEN }}
- uses: r-hub/actions/platform-info@v1
with:
token: ${{ secrets.RHUB_TOKEN }}
job-config: ${{ matrix.config.job-config }}
- uses: r-hub/actions/setup-deps@v1
with:
job-config: ${{ matrix.config.job-config }}
token: ${{ secrets.RHUB_TOKEN }}
- uses: r-hub/actions/run-check@v1
with:
job-config: ${{ matrix.config.job-config }}
token: ${{ secrets.RHUB_TOKEN }}
8 changes: 5 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: rayimage
Type: Package
Title: Image Processing for Simulated Cameras
Version: 0.10.0
Version: 0.11.0
Authors@R: c(person("Tyler", "Morgan-Wall", email = "[email protected]",
role = c("aut", "cph", "cre"), comment = c(ORCID = "0000-0002-3131-3814")),
person("Sean", "Barrett", role = c("ctb", "cph")))
Expand All @@ -15,13 +15,15 @@ Imports:
png,
jpeg,
grDevices,
grid
grid,
tiff
Suggests:
magick,
testthat (>= 3.0.0)
LinkingTo: Rcpp, RcppArmadillo, progress
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Roxygen: list(markdown = TRUE)
Encoding: UTF-8
URL: https://www.rayimage.dev, https://github.com/tylermorganwall/rayimage
BugReports: https://github.com/tylermorganwall/rayimage/issues
Config/testthat/edition: 3
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ export(generate_2d_gaussian)
export(interpolate_array)
export(plot_image)
export(plot_image_grid)
export(ray_read_image)
export(ray_write_image)
export(render_bokeh)
export(render_boolean_distance)
export(render_bw)
export(render_clamp)
export(render_convolution)
export(render_convolution_fft)
export(render_reorient)
export(render_resized)
export(run_documentation)
import(grDevices)
import(stats)
importFrom(Rcpp,evalCpp)
Expand Down
56 changes: 9 additions & 47 deletions R/add_image_overlay.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
#'@import grDevices
#'@export
#'@examples
#'if(rayimage:::run_documentation()){
#'if(run_documentation()){
#'#Plot the dragon
#'plot_image(dragon)
#'}
#'if(rayimage:::run_documentation()){
#'if(run_documentation()){
#'#Add an overlay of a red semi-transparent circle:
#'circlemat = generate_2d_disk(min(dim(dragon)[1:2]))
#'circlemat = circlemat/max(circlemat)
Expand All @@ -41,53 +41,15 @@
add_image_overlay = function(image, image_overlay = NULL, rescale_original = FALSE,
alpha = NULL, filename = NULL, preview = FALSE) {
imagetype = get_file_type(image)
image_overlay_type = get_file_type(image)
image_overlay_type = get_file_type(image_overlay)

temp = tempfile(fileext = ".png")
temp_overlay = tempfile(fileext = ".png")
image_overlay = ray_read_image(image_overlay)

ray_write_image(image, temp)
ray_write_image(image_overlay, temp_overlay)

if(imagetype == "array") {
#Clip HDR images
image[image > 1] = 1
png::writePNG(image,temp)
image = temp
} else if (imagetype == "matrix") {
newarray = array(1,dim=c(nrow(image),ncol(image),4))
newarray[,,1] = image
newarray[,,2] = image
newarray[,,3] = image
#Clip HDR images
newarray[newarray > 1] = 1
png::writePNG(newarray,temp)
image = temp
} else if (imagetype == "png") {
image = png::readPNG(image)
png::writePNG(image,temp)
} else if (imagetype == "jpg") {
image = jpeg::readJPEG(image)
png::writePNG(image,temp)
}
if(image_overlay_type == "array") {
#Clip HDR images
image_overlay[image_overlay > 1] = 1
png::writePNG(image_overlay,temp_overlay)
image_overlay = temp_overlay
} else if (image_overlay_type == "matrix") {
newarray = array(1,dim=c(nrow(image_overlay),ncol(image_overlay),4))
newarray[,,1] = image_overlay
newarray[,,2] = image_overlay
newarray[,,3] = image_overlay
#Clip HDR images
newarray[newarray > 1] = 1
png::writePNG(newarray,temp_overlay)
image_overlay = temp_overlay
} else if (image_overlay_type == "png") {
image_overlay = png::readPNG(image_overlay)
png::writePNG(image_overlay,temp_overlay)
} else if (image_overlay_type == "jpg") {
image_overlay = jpeg::readJPEG(image_overlay)
png::writePNG(image_overlay,temp_overlay)
}
tempmap = aperm(png::readPNG(temp),c(2,1,3))

dimensions = dim(tempmap)
Expand Down Expand Up @@ -198,10 +160,10 @@ add_image_overlay = function(image, image_overlay = NULL, rescale_original = FAL
if(!preview) {
return(temp)
}
plot_image(temp)
plot_image(render_clamp(temp))
return(invisible(temp))
} else {
save_png(temp, filename)
ray_write_image(render_clamp(temp), filename)
return(invisible(temp))
}
}
45 changes: 13 additions & 32 deletions R/add_title.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,34 @@
#'@import grDevices
#'@export
#'@examples
#'if(rayimage:::run_documentation()){
#'if(run_documentation()){
#'#Plot the dragon
#'add_title(dragon, preview = TRUE, title_text = "Dragon", title_size=20)
#'}
#'if(rayimage:::run_documentation()){
#'if(run_documentation()){
#'#That's hard to see--let's add a title bar:
#'add_title(dragon, preview = TRUE, title_text = "Dragon", title_size=20,
#' title_bar_color="white")
#'}
#'if(rayimage:::run_documentation()){
#'if(run_documentation()){
#'#Change the width of the bar:
#'add_title(dragon, preview = TRUE, title_text = "Dragon", title_size=20,
#' title_bar_color="white", title_offset = c(12,12))
#' title_bar_color="white", title_offset = c(8,8))
#'}
#'if(rayimage:::run_documentation()){
#'if(run_documentation()){
#'#The width of the bar will also automatically adjust for newlines:
#'add_title(dragon, preview = TRUE, title_text = "Dragon\n(Blue)", title_size=20,
#' title_bar_color="white", title_offset = c(12,12))
#' title_bar_color="white")
#'}
#'if(rayimage:::run_documentation()){
#'if(run_documentation()){
#'#Change the color and title color:
#'add_title(dragon, preview = TRUE, title_text = "Dragon", title_size=20,
#' title_bar_color="red", title_color = "white", title_offset = c(12,12))
#' title_bar_color="red", title_color = "white")
#'}
#'if(rayimage:::run_documentation()){
#'if(run_documentation()){
#'#Change the transparency:
#'add_title(dragon, preview = TRUE, title_text = "Dragon", title_size=20, title_bar_alpha = 0.8,
#' title_bar_color="red", title_color = "white", title_offset = c(12,12))
#' title_bar_color="red", title_color = "white")
#'}
add_title = function(image,
title_text = "", title_offset = c(15,15),
Expand All @@ -62,27 +62,8 @@ add_title = function(image,
filename = NULL, preview = FALSE) {
imagetype = get_file_type(image)
temp = tempfile(fileext = ".png")
if(imagetype == "array") {
#Clip HDR images
image[image > 1] = 1
png::writePNG(image,temp)
image = temp
} else if (imagetype == "matrix") {
newarray = array(0,dim=c(nrow(image),ncol(image),3))
newarray[,,1] = image
newarray[,,2] = image
newarray[,,3] = image
#Clip HDR images
newarray[newarray > 1] = 1
png::writePNG(newarray,temp)
image = temp
} else if (imagetype == "png") {
image = png::readPNG(image)
png::writePNG(image,temp)
} else if (imagetype == "jpg") {
image = jpeg::readJPEG(image)
png::writePNG(image,temp)
}
ray_write_image(image, temp)

tempmap = png::readPNG(temp)
dimensions = dim(tempmap)

Expand Down Expand Up @@ -159,7 +140,7 @@ add_title = function(image,
plot_image(temp)
return(invisible(temp))
} else {
save_png(temp, filename)
ray_write_image(temp, filename)
return(invisible(temp))
}
}
40 changes: 10 additions & 30 deletions R/add_vignette.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,56 +19,36 @@
#'@import grDevices
#'@export
#'@examples
#'if(rayimage:::run_documentation()){
#'if(run_documentation()){
#'#Plot the dragon
#'plot_image(dragon)
#'}
#'if(rayimage:::run_documentation()){
#'if(run_documentation()){
#'#Add a vignette effect:
#'add_vignette(dragon, preview = TRUE, vignette = 0.5)
#'}
#'if(rayimage:::run_documentation()){
#'if(run_documentation()){
#'#Darken the vignette effect:
#'add_vignette(dragon, preview = TRUE, vignette = 1)
#'}
#'if(rayimage:::run_documentation()){
#'if(run_documentation()){
#'#Change the radius:
#'add_vignette(dragon, preview = TRUE, vignette = 1, radius=1.5)
#'add_vignette(dragon, preview = TRUE, vignette = 1, radius=0.5)
#'}
#'if(rayimage:::run_documentation()){
#'if(run_documentation()){
#'#Change the color:
#'add_vignette(dragon, preview = TRUE, vignette = 1, color="white")
#'}
#'if(rayimage:::run_documentation()){
#'if(run_documentation()){
#'#Increase the width of the blur by 50%:
#'add_vignette(dragon, preview = TRUE, vignette = c(1,1.5))
#'}
add_vignette = function(image, vignette = 0.5, color = "#000000", radius = 1.3,
filename = NULL, preview = FALSE) {
imagetype = get_file_type(image)
temp = tempfile(fileext = ".png")
if(imagetype == "array") {
#Clip HDR images
image[image > 1] = 1
png::writePNG(image,temp)
image = temp
} else if (imagetype == "matrix") {
newarray = array(0,dim=c(nrow(image),ncol(image),3))
newarray[,,1] = image
newarray[,,2] = image
newarray[,,3] = image
#Clip HDR images
newarray[newarray > 1] = 1
png::writePNG(newarray,temp)
image = temp
} else if (imagetype == "png") {
image = png::readPNG(image)
png::writePNG(image,temp)
} else if (imagetype == "jpg") {
image = jpeg::readJPEG(image)
png::writePNG(image,temp)
}
ray_write_image(image, temp)

tempmap = png::readPNG(temp)
dimensions = dim(tempmap)

Expand Down Expand Up @@ -115,10 +95,10 @@ add_vignette = function(image, vignette = 0.5, color = "#000000", radius = 1.3,
if(!preview) {
return(temp)
}
plot_image(temp)
plot_image(render_clamp(temp))
return(invisible(temp))
} else {
save_png(temp, filename)
ray_write_image(render_clamp(temp), filename)
return(invisible(temp))
}
}
Loading

0 comments on commit fd26b28

Please sign in to comment.