Skip to content

Commit 3db0a9f

Browse files
committed
Switch to using the png() dev graphics.
Change image parsing to search all channels of the array. Close #1
1 parent c64767b commit 3db0a9f

File tree

7 files changed

+22
-50
lines changed

7 files changed

+22
-50
lines changed

DESCRIPTION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ RoxygenNote: 7.3.2
2020
URL: https://github.com/coatless-rpkg/surreal, https://r-pkg.thecoatlessprofessor.com/surreal/
2121
BugReports: https://github.com/coatless-rpkg/surreal/issues
2222
LazyData: true
23+
Imports:
24+
png

NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
export(border_augmentation)
44
export(surreal)
55
export(surreal_text)
6-
importFrom(grDevices,bitmap)
76
importFrom(grDevices,dev.off)
7+
importFrom(grDevices,png)
88
importFrom(graphics,pairs)
99
importFrom(graphics,plot)
1010
importFrom(graphics,text)

R/surreal-text.R

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#' Create a Temporary Text Plot
22
#'
3-
#' This function creates a temporary bitmap image file containing a plot of the given text.
3+
#' This function creates a temporary png image file containing a plot of the
4+
#' given text.
45
#'
56
#' @param text A character string to be plotted.
67
#' @param cex A numeric value specifying the relative size of the text. Default is 4.
78
#'
89
#' @return
9-
#' A character vector containing the temporary image file.
10+
#' An array containing data from the temporary image file.
1011
#'
11-
#' @importFrom grDevices bitmap dev.off
12+
#' @importFrom grDevices png dev.off
1213
#' @importFrom graphics plot text
1314
#'
1415
#' @noRd
@@ -22,13 +23,13 @@ temporary_text_plot <- function(text, cex = 4) {
2223

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

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

3031
# Create a bitmap image
31-
bitmap(temp_file, "ppm", height = 5, width = 5)
32+
png(temp_file, antialias = "none")
3233

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

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

4546
image
4647
}
@@ -67,30 +68,21 @@ temporary_text_plot <- function(text, cex = 4) {
6768
#' image_data <- process_image(temp_file)
6869
process_image <- function(image) {
6970

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

73-
# Extract pixel data (skip first 4 lines of metadata)
74-
pixel_data <- image[-(1:4)]
75-
pixel_data <- unlist(lapply(strsplit(pixel_data, " "), as.numeric))
76-
77-
# Convert pixel data to a matrix
78-
pixel_matrix <- matrix(pixel_data, ncol = 3, byrow = TRUE)
79-
80-
# Convert to binary (text pixels are where any RGB value is 0)
81-
pixel_matrix <- pixel_matrix[,1] == 0 | pixel_matrix[,2] == 0 | pixel_matrix[,3] == 0
82-
pixel_matrix <- matrix(pixel_matrix, nrow = size[1], ncol = size[2], byrow = TRUE)
83-
84-
# Remove empty rows and columns
85-
pixel_matrix <- pixel_matrix[apply(pixel_matrix, 1, any), ]
86-
pixel_matrix <- pixel_matrix[, apply(pixel_matrix, 2, any)]
87-
88-
# Generate x and y coordinates
89-
x <- col(pixel_matrix)
90-
y <- nrow(pixel_matrix) + 1 - row(pixel_matrix)
74+
# Find black points (where all channels are 0)
75+
activated_points <- which(
76+
img_array[,,1] == 0 &
77+
img_array[,,2] == 0 &
78+
img_array[,,3] == 0,
79+
arr.ind = TRUE)
9180

9281
# Return coordinates of text pixels
93-
list(x = x[pixel_matrix], y = y[pixel_matrix])
82+
list(
83+
x = activated_points[, 2], # Column index represents x
84+
y = nrow(img_array) - activated_points[, 1] + 1 # Row index represents y, but flipped
85+
)
9486
}
9587

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

131-
if (.Platform$OS.type != "unix") {
132-
message("This function is only supported on macOS and Linux versions of R due to limitations in GhostScript.")
133-
return(NULL)
134-
}
135-
136119
# Create temporary plot of the text
137120
temp_file <- temporary_text_plot(text = text, cex = cex)
138121

README.Rmd

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,6 @@ The residual plot reveals the original R logo with a slight border, enhancing th
9898

9999
## Creating Custom Hidden Images
100100

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

107103
```{r}

README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,6 @@ enhancing the image recovery.
100100

101101
## Creating Custom Hidden Images
102102

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

-6.96 KB
Loading

man/surreal_text.Rd

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)