Skip to content

Commit

Permalink
Switch to using the png() dev graphics.
Browse files Browse the repository at this point in the history
Change image parsing to search all channels of the array.

Close #1
  • Loading branch information
coatless committed Sep 10, 2024
1 parent c64767b commit 3db0a9f
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 50 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ RoxygenNote: 7.3.2
URL: https://github.com/coatless-rpkg/surreal, https://r-pkg.thecoatlessprofessor.com/surreal/
BugReports: https://github.com/coatless-rpkg/surreal/issues
LazyData: true
Imports:
png
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
export(border_augmentation)
export(surreal)
export(surreal_text)
importFrom(grDevices,bitmap)
importFrom(grDevices,dev.off)
importFrom(grDevices,png)
importFrom(graphics,pairs)
importFrom(graphics,plot)
importFrom(graphics,text)
Expand Down
55 changes: 19 additions & 36 deletions R/surreal-text.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#' Create a Temporary Text Plot
#'
#' This function creates a temporary bitmap image file containing a plot of the given text.
#' This function creates a temporary png image file containing a plot of the
#' given text.
#'
#' @param text A character string to be plotted.
#' @param cex A numeric value specifying the relative size of the text. Default is 4.
#'
#' @return
#' A character vector containing the temporary image file.
#' An array containing data from the temporary image file.
#'
#' @importFrom grDevices bitmap dev.off
#' @importFrom grDevices png dev.off
#' @importFrom graphics plot text
#'
#' @noRd
Expand All @@ -22,13 +23,13 @@ temporary_text_plot <- function(text, cex = 4) {

# Create a temporary file path using a known directory
temp_dir <- tempdir()
temp_file <- tempfile(tmpdir = temp_dir, fileext = ".ppm")
temp_file <- tempfile(tmpdir = temp_dir, fileext = ".png")

# Ensure the temporary file is removed when the function exits
on.exit(unlink(temp_file))

# Create a bitmap image
bitmap(temp_file, "ppm", height = 5, width = 5)
png(temp_file, antialias = "none")

# Create a blank plot
plot(1, 1, type = "n", axes = FALSE, xlab = "", ylab = "")
Expand All @@ -40,7 +41,7 @@ temporary_text_plot <- function(text, cex = 4) {
dev.off()

# Read the image file
image <- scan(temp_file, "", sep = "\n", quiet = TRUE)
image <- png::readPNG(temp_file)

image
}
Expand All @@ -67,30 +68,21 @@ temporary_text_plot <- function(text, cex = 4) {
#' image_data <- process_image(temp_file)
process_image <- function(image) {

# Extract image size from the third line
size <- as.numeric(unlist(strsplit(image[3], " ")))
# Convert the array to integer values between 0 and 255
img_array <- round(image * 255)

# Extract pixel data (skip first 4 lines of metadata)
pixel_data <- image[-(1:4)]
pixel_data <- unlist(lapply(strsplit(pixel_data, " "), as.numeric))

# Convert pixel data to a matrix
pixel_matrix <- matrix(pixel_data, ncol = 3, byrow = TRUE)

# Convert to binary (text pixels are where any RGB value is 0)
pixel_matrix <- pixel_matrix[,1] == 0 | pixel_matrix[,2] == 0 | pixel_matrix[,3] == 0
pixel_matrix <- matrix(pixel_matrix, nrow = size[1], ncol = size[2], byrow = TRUE)

# Remove empty rows and columns
pixel_matrix <- pixel_matrix[apply(pixel_matrix, 1, any), ]
pixel_matrix <- pixel_matrix[, apply(pixel_matrix, 2, any)]

# Generate x and y coordinates
x <- col(pixel_matrix)
y <- nrow(pixel_matrix) + 1 - row(pixel_matrix)
# Find black points (where all channels are 0)
activated_points <- which(
img_array[,,1] == 0 &
img_array[,,2] == 0 &
img_array[,,3] == 0,
arr.ind = TRUE)

# Return coordinates of text pixels
list(x = x[pixel_matrix], y = y[pixel_matrix])
list(
x = activated_points[, 2], # Column index represents x
y = nrow(img_array) - activated_points[, 1] + 1 # Row index represents y, but flipped
)
}

#' Apply the surreal method to a text string
Expand All @@ -106,10 +98,6 @@ process_image <- function(image) {
#' @return
#' A data.frame containing the results of the surreal method application.
#'
#' @details
#' This function is not supported on Windows due to the `ppm` image format
#' not being supported by the version of GhostScript included with R.
#'
#' @examples
#' # Create a surreal plot of the text "R is fun" appearing on one line
#' r_is_fun_result <- surreal_text("R is fun", verbose = TRUE)
Expand All @@ -128,11 +116,6 @@ surreal_text <- function(text = "hello world",
n_add_points = 40,
max_iter = 100, tolerance = 0.01, verbose = FALSE) {

if (.Platform$OS.type != "unix") {
message("This function is only supported on macOS and Linux versions of R due to limitations in GhostScript.")
return(NULL)
}

# Create temporary plot of the text
temp_file <- temporary_text_plot(text = text, cex = cex)

Expand Down
4 changes: 0 additions & 4 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@ The residual plot reveals the original R logo with a slight border, enhancing th

## Creating Custom Hidden Images

> [!IMPORTANT]
>
> This function is unable to work on Windows as the version of GhostScript included with R does not support the `ppm` type.
You can also create datasets with custom hidden images or text. Here's a quick example using text:

```{r}
Expand Down
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,6 @@ enhancing the image recovery.

## Creating Custom Hidden Images

> \[!IMPORTANT\]
>
> This function is unable to work on Windows as the version of
> GhostScript included with R does not support the `ppm` type.
You can also create datasets with custom hidden images or text. Here’s a
quick example using text:

Expand Down
Binary file modified man/figures/README-custom-text-example-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 0 additions & 4 deletions man/surreal_text.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3db0a9f

Please sign in to comment.