|
| 1 | +#' Title |
| 2 | +#' |
| 3 | +#' @param image |
| 4 | +#' @param file |
| 5 | +#' @param x |
| 6 | +#' @param y |
| 7 | +#' @param adj |
| 8 | +#' @param size |
| 9 | +#' @param col |
| 10 | +#' @param alpha |
| 11 | +#' @param angle |
| 12 | +#' @param fill |
| 13 | +#' @param border |
| 14 | +#' @param padding |
| 15 | +#' @param add |
| 16 | +#' @param ... |
| 17 | +#' |
| 18 | +#' @return |
| 19 | +#' @export |
| 20 | +#' |
| 21 | +#' @examples |
| 22 | + |
| 23 | + |
| 24 | +add_image <- function(image, file, x, y, adj = c(0.5, 0.5), size = NULL, |
| 25 | + col = NA, alpha = NULL, angle = 0, fill = NA, border = NA, |
| 26 | + padding = NULL, add = TRUE, ...) { |
| 27 | + |
| 28 | + if (missing(image)) image <- NULL |
| 29 | + if (missing(file)) file <- NULL |
| 30 | + |
| 31 | + if (missing(x)) x <- NULL |
| 32 | + if (missing(y)) y <- NULL |
| 33 | + |
| 34 | + if (is.null(image) & is.null(file)) { |
| 35 | + stop("One of 'image' or 'file' is required.") |
| 36 | + } |
| 37 | + |
| 38 | + if (!is.null(image) & !is.null(file)) { |
| 39 | + stop("Only one of 'image' or 'file' is required.") |
| 40 | + } |
| 41 | + |
| 42 | + if (is.null(image)) { |
| 43 | + |
| 44 | + if (sum(is.na(file))) { |
| 45 | + stop("Argument 'file' cannot contain missing value.") |
| 46 | + } |
| 47 | + |
| 48 | + if (length(file) != 1 | !is.character(file)) { |
| 49 | + stop("Argument 'file' must be a character (filename) of length 1.") |
| 50 | + } |
| 51 | + |
| 52 | + file_formats <- c("jpg$", "jpeg$", "png$") |
| 53 | + file_format <- unlist(lapply(file_formats, grepl, file)) |
| 54 | + |
| 55 | + if (!sum(file_format)) stop("Picture must be a JP(E)G or a PNG.") |
| 56 | + |
| 57 | + if (grepl("^https?://", file)) { |
| 58 | + |
| 59 | + file_ext <- switch(which(file_format), "jpg", "jpg", "png") |
| 60 | + |
| 61 | + path <- tempfile(fileext = file_ext) |
| 62 | + download.file(url = file, destfile = path, quiet = TRUE) |
| 63 | + |
| 64 | + file <- path |
| 65 | + |
| 66 | + } else { |
| 67 | + |
| 68 | + if (!file.exists(file)) stop("File does not exist.") |
| 69 | + } |
| 70 | + |
| 71 | + image <- switch( |
| 72 | + which(file_format), |
| 73 | + jpeg::readJPEG(file, native = FALSE), |
| 74 | + jpeg::readJPEG(file, native = FALSE), |
| 75 | + png::readPNG(file, native = FALSE) |
| 76 | + ) |
| 77 | + } |
| 78 | + |
| 79 | + if (class(image) != "array") { |
| 80 | + stop("Argument 'image' must be a '3D array' (use `native = FALSE`).") |
| 81 | + } |
| 82 | + |
| 83 | + if (dim(image)[3] < 3 | dim(image)[3] > 4) { |
| 84 | + stop("Wrong image format: Must a 3-channels (RGB) or 4-channels (RGBA).") |
| 85 | + } |
| 86 | + |
| 87 | + if (is.null(padding)) padding <- rep(0, 4) |
| 88 | + |
| 89 | + if (sum(is.na(padding))) stop("Argument 'padding' cannot contain NA.") |
| 90 | + if (!is.numeric(padding)) stop("Argument 'padding' must be a numeric.") |
| 91 | + |
| 92 | + if (length(padding) == 1) { |
| 93 | + padding <- rep(padding, 4) |
| 94 | + |
| 95 | + } else { |
| 96 | + |
| 97 | + if (length(padding) == 2) { |
| 98 | + padding <- rep(padding, 2) |
| 99 | + |
| 100 | + } else { |
| 101 | + |
| 102 | + if (length(padding) != 4) { |
| 103 | + stop("Argument 'padding' must be a numeric of length 1, 2, or 4.") |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + if (!add) { |
| 109 | + |
| 110 | + plot(0, xlim = c(-1, 1), ylim = c(-1, 1), type = "n", ann = FALSE, |
| 111 | + bty = "n", axes = FALSE) |
| 112 | + } |
| 113 | + |
| 114 | + if (is.null(dev.list())) stop("No open graphical device (use `add = TRUE`).") |
| 115 | + |
| 116 | + if (is.null(x)) x <- par()$usr[1] + (par()$usr[2] - par()$usr[1]) / 2 |
| 117 | + if (is.null(y)) y <- par()$usr[3] + (par()$usr[4] - par()$usr[3]) / 2 |
| 118 | + |
| 119 | + if (sum(is.na(x))) { |
| 120 | + stop("Argument 'x' cannot contain missing value.") |
| 121 | + } |
| 122 | + |
| 123 | + if (!is.numeric(x)) { |
| 124 | + stop("Argument 'x' must be a numeric.") |
| 125 | + } |
| 126 | + |
| 127 | + if (sum(is.na(y))) { |
| 128 | + stop("Argument 'y' cannot contain missing value.") |
| 129 | + } |
| 130 | + |
| 131 | + if (!is.numeric(y)) { |
| 132 | + stop("Argument 'y' must be a numeric.") |
| 133 | + } |
| 134 | + |
| 135 | + if (length(x) != length(y)) { |
| 136 | + stop("Arguments 'x and 'y' must have the same length.") |
| 137 | + } |
| 138 | + |
| 139 | + |
| 140 | + aspect_ratio <- dim(image)[1] / dim(image)[2] |
| 141 | + |
| 142 | + |
| 143 | + if (is.null(size)) size <- 100 |
| 144 | + size <- rep(size, length(x)) |
| 145 | + |
| 146 | + if (!is.na(col)) col <- rep(col, length(x)) |
| 147 | + if (!is.null(alpha)) alpha <- rep(alpha, length(x)) |
| 148 | + |
| 149 | + original <- image |
| 150 | + |
| 151 | + for (i in 1:length(x)) { |
| 152 | + |
| 153 | + image <- original |
| 154 | + |
| 155 | + ## Change Image Colors (monochromatic transformation) --- |
| 156 | + |
| 157 | + if (!is.na(col[i])) { |
| 158 | + |
| 159 | + col_i <- col2rgb(col[i], alpha = TRUE) |
| 160 | + |
| 161 | + for (channel in 1:3) { |
| 162 | + |
| 163 | + image[ , , channel][image[ , , channel] != 1] <- col_i[channel] / 255 |
| 164 | + } |
| 165 | + |
| 166 | + if (dim(image)[3] == 4) image[ , , 4][image[ , , 4] != 0] <- col_i[4] / 255 |
| 167 | + } |
| 168 | + |
| 169 | + if (!is.null(alpha)) { |
| 170 | + |
| 171 | + alpha_i <- alpha[i] |
| 172 | + |
| 173 | + if (alpha_i > 1) alpha_i <- alpha_i / 255 |
| 174 | + |
| 175 | + if (dim(image)[3] == 4) image[ , , 4][image[ , , 4] != 0] <- alpha_i |
| 176 | + } |
| 177 | + |
| 178 | + |
| 179 | + ## Adjusting Image Dimensions ---- |
| 180 | + |
| 181 | + size_i <- size[i] / 100 |
| 182 | + |
| 183 | + width <- ifelse(aspect_ratio > 1, NA, size_i * (par()$usr[2] - par()$usr[1])) |
| 184 | + height <- ifelse(aspect_ratio <= 1, NA, size_i * (par()$usr[4] - par()$usr[3])) |
| 185 | + |
| 186 | + if (is.na(width)) { |
| 187 | + |
| 188 | + height_in <- height * par()$pin[2] / (par()$usr[4] - par()$usr[3]) |
| 189 | + width_in <- height_in / aspect_ratio |
| 190 | + width <- width_in * (par()$usr[2] - par()$usr[1]) / par()$pin[1] |
| 191 | + |
| 192 | + } |
| 193 | + |
| 194 | + if (is.na(height)) { |
| 195 | + |
| 196 | + width_in <- width / (par()$usr[2] - par()$usr[1]) * par()$pin[1] |
| 197 | + height_in <- width_in * aspect_ratio |
| 198 | + height <- height_in / par()$pin[2] * (par()$usr[4] - par()$usr[3]) |
| 199 | + } |
| 200 | + |
| 201 | + ## Adjusting Image Position ---- |
| 202 | + |
| 203 | + x1 <- x[i] - ((1 - adj[1]) * width) |
| 204 | + y1 <- y[i] - ((1 - adj[2]) * height) |
| 205 | + x2 <- x1 + width |
| 206 | + y2 <- y1 + height |
| 207 | + |
| 208 | + |
| 209 | + ## Add a background ---- |
| 210 | + |
| 211 | + if (!is.na(fill) || !is.na(border)) { |
| 212 | + |
| 213 | + padding[1] <- padding[1] * height |
| 214 | + padding[2] <- padding[2] * width |
| 215 | + padding[3] <- padding[3] * height |
| 216 | + padding[4] <- padding[4] * width |
| 217 | + |
| 218 | + rect( |
| 219 | + xleft = x1 - padding[2], |
| 220 | + xright = x2 + padding[4], |
| 221 | + ybottom = y1 - padding[1], |
| 222 | + ytop = y2 + padding[3], |
| 223 | + col = fill, |
| 224 | + border = border, |
| 225 | + ... |
| 226 | + ) |
| 227 | + } |
| 228 | + |
| 229 | + |
| 230 | + ## Plot Image ---- |
| 231 | + |
| 232 | + graphics::rasterImage( |
| 233 | + image = as.raster(image), |
| 234 | + xleft = x1, |
| 235 | + xright = x2, |
| 236 | + ybottom = y1, |
| 237 | + ytop = y2, |
| 238 | + interpolate = TRUE, |
| 239 | + angle = angle |
| 240 | + ) |
| 241 | + } |
| 242 | + |
| 243 | + invisible(c(x1, y1, x2, y2)) |
| 244 | +} |
0 commit comments