From 952ab7936f3afe11882ec87e13139c38a2026751 Mon Sep 17 00:00:00 2001 From: Hugo Gruson Date: Mon, 4 May 2026 17:41:03 +0200 Subject: [PATCH 1/6] Fix transpose in normalization step --- R/plotImage.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/plotImage.R b/R/plotImage.R index ce950fd..282c510 100644 --- a/R/plotImage.R +++ b/R/plotImage.R @@ -101,8 +101,8 @@ NULL cl <- matrix(cl, ncol=2) } colors_rgb <- col2rgb(c) - normed_a <- (t(linear_a) - cl[, 1]) / (cl[, 2] - cl[, 1]) - flat_img <- tcrossprod(colors_rgb, normed_a) / d + normed_a <- (linear_a - cl[, 1]) / (cl[, 2] - cl[, 1]) + flat_img <- (colors_rgb %*% normed_a) / d flat_img |> t() |> farver::encode_colour() |> From 60ea42afb46d8d249e12fc6e791efb63430fe787 Mon Sep 17 00:00:00 2001 From: Hugo Gruson Date: Mon, 4 May 2026 18:26:21 +0200 Subject: [PATCH 2/6] Apply special channel selection and normalization to rgb images --- R/plotImage.R | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/R/plotImage.R b/R/plotImage.R index 282c510..fafa3c4 100644 --- a/R/plotImage.R +++ b/R/plotImage.R @@ -142,7 +142,6 @@ NULL #' @importFrom SpatialData channels #' @noRd .ch_idx <- \(x, ch) { - if (.is_rgb(x)) return(seq_len(3)) if (is.null(ch)) return(1) lbs <- channels(x) if (all(ch %in% lbs)) { @@ -162,6 +161,11 @@ NULL #' @importFrom SpatialData data_type .df_i <- \(x, k=NULL, ch=NULL, c=NULL, cl=NULL) { a <- .get_multiscale_data(x, k) + if (.is_rgb(x)) { + # RGB: we plot everything by default and we don't normalize + ch <- ch %||% c("r", "g", "b") + cl <- cl %||% c(0, 1/3) + } a <- a[.ch_idx(x, ch),,,drop=FALSE] a <- .norm_ia(a, data_type(x)) a <- .prep_ia(a, c, cl) From 3c0dea9984c2923a01308e40dadec02fff114b3c Mon Sep 17 00:00:00 2001 From: Hugo Gruson Date: Mon, 4 May 2026 19:20:28 +0200 Subject: [PATCH 3/6] Ensure rgb settings get applied to pal --- R/plotImage.R | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/R/plotImage.R b/R/plotImage.R index fafa3c4..161301c 100644 --- a/R/plotImage.R +++ b/R/plotImage.R @@ -161,11 +161,6 @@ NULL #' @importFrom SpatialData data_type .df_i <- \(x, k=NULL, ch=NULL, c=NULL, cl=NULL) { a <- .get_multiscale_data(x, k) - if (.is_rgb(x)) { - # RGB: we plot everything by default and we don't normalize - ch <- ch %||% c("r", "g", "b") - cl <- cl %||% c(0, 1/3) - } a <- a[.ch_idx(x, ch),,,drop=FALSE] a <- .norm_ia(a, data_type(x)) a <- .prep_ia(a, c, cl) @@ -209,6 +204,11 @@ setMethod("plotImage", "SpatialData", \(x, i=1, j=1, k=NULL, ch=NULL, c=NULL, cl j <- CTname(y)[j] y <- transform(y, j) wh <- .get_wh(y) + if (.is_rgb(y)) { + # RGB: we plot everything by default and we don't normalize + ch <- ch %||% c("r", "g", "b") + cl <- cl %||% c(0, 1/3) + } df <- .df_i(y, k, ch, c, cl) pal <- if (is.null(c)) .DEFAULT_COLORS else c if (dim(y)[1] > 1) { From df18584d98c9b39331b539b61074d4c700ca8965 Mon Sep 17 00:00:00 2001 From: Hugo Gruson Date: Mon, 4 May 2026 19:31:03 +0200 Subject: [PATCH 4/6] Use %||% shorthand to define pal --- R/plotImage.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/plotImage.R b/R/plotImage.R index 161301c..0152bcc 100644 --- a/R/plotImage.R +++ b/R/plotImage.R @@ -210,7 +210,7 @@ setMethod("plotImage", "SpatialData", \(x, i=1, j=1, k=NULL, ch=NULL, c=NULL, cl cl <- cl %||% c(0, 1/3) } df <- .df_i(y, k, ch, c, cl) - pal <- if (is.null(c)) .DEFAULT_COLORS else c + pal <- c %||% .DEFAULT_COLORS if (dim(y)[1] > 1) { nms <- unlist(channels(y))[idx <- .ch_idx(y, ch)] pal <- pal[seq_along(idx)]; names(pal) <- nms From 3175d95d481a177a73ace3138311bbb193ee8e52 Mon Sep 17 00:00:00 2001 From: Hugo Gruson Date: Mon, 4 May 2026 19:31:21 +0200 Subject: [PATCH 5/6] Do not plot legend in RGB imgs --- R/plotImage.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/plotImage.R b/R/plotImage.R index 0152bcc..4dd4589 100644 --- a/R/plotImage.R +++ b/R/plotImage.R @@ -211,7 +211,7 @@ setMethod("plotImage", "SpatialData", \(x, i=1, j=1, k=NULL, ch=NULL, c=NULL, cl } df <- .df_i(y, k, ch, c, cl) pal <- c %||% .DEFAULT_COLORS - if (dim(y)[1] > 1) { + if (dim(y)[1] > 1 && !.is_rgb(y)) { nms <- unlist(channels(y))[idx <- .ch_idx(y, ch)] pal <- pal[seq_along(idx)]; names(pal) <- nms } From 08451f2f576c9bdbdae0d8840747f38340f119d6 Mon Sep 17 00:00:00 2001 From: Hugo Gruson Date: Tue, 5 May 2026 18:39:26 +0200 Subject: [PATCH 6/6] Fix channel selection in rgb images --- R/plotImage.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/plotImage.R b/R/plotImage.R index 4dd4589..af48765 100644 --- a/R/plotImage.R +++ b/R/plotImage.R @@ -206,7 +206,7 @@ setMethod("plotImage", "SpatialData", \(x, i=1, j=1, k=NULL, ch=NULL, c=NULL, cl wh <- .get_wh(y) if (.is_rgb(y)) { # RGB: we plot everything by default and we don't normalize - ch <- ch %||% c("r", "g", "b") + ch <- ch %||% channels(y) cl <- cl %||% c(0, 1/3) } df <- .df_i(y, k, ch, c, cl)