Skip to content

Commit

Permalink
fix: magick giottoAffineImage realization when extent != image dims…
Browse files Browse the repository at this point in the history
… ratio
  • Loading branch information
jiajic committed Jan 22, 2025
1 parent b75063e commit 8ccfbc2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## bug fixes
- hotfix `shear()` for `giottoPolygon`
- fix `ext<-` for `spatLocsObj`
- fix {magick} `giottoAffineImage` realization when extent does not match the image dims ratio.
- fix `ext<-()` for `spatLocsObj`
- fix `ext<-()` for `giottoAffineImage`

# GiottoClass 0.4.6 (2025/01/17)
Expand Down
21 changes: 20 additions & 1 deletion R/methods-affine.R
Original file line number Diff line number Diff line change
Expand Up @@ -344,12 +344,31 @@ setMethod("affine", signature(x = "affine2d", y = "matrix"), function(x, y, inv
mg <- .spatraster_sample_values(x, output = "magick", size = size, ...)
aff <- x@affine

# account for intrinsic rescale needed to account for ext vs dim differences
# terra spatSample() ignores extent dims, and output aspect ratio is the
# same as the source image.
# Only downscale here.
sv_xy_dim <- range(ext(x@raster_object))
sv_xy_ratio <- sv_xy_dim / max(sv_xy_dim)
mg_info <- magick::image_info(mg)[c("width", "height")]
mg_xy_dim <- as.numeric(mg_info)
mg_xy_ratio <- mg_xy_dim / max(mg_xy_dim)
ext_ratio <- sv_xy_ratio / mg_xy_ratio

# create a dummy spatLocsObj to act as control points
# pt1: bottom left
# pt2: top left
# pt3: bottom right
dummy_sl <- .magick_image_corners(mg)
aff_dummy_sl <- dummy_sl %>%

# account for intrinsic scaling from when ext != dims ratio
aff_dummy_sl <- rescale(dummy_sl,
fx = ext_ratio[["x"]],
fy = ext_ratio[["y"]],
x0 = 0, y0 = 0
)

aff_dummy_sl <- aff_dummy_sl %>%
affine(.aff_linear_2d(aff)) %>%
flip() %>%
rescale(
Expand Down

0 comments on commit 8ccfbc2

Please sign in to comment.