Skip to content

Commit 78adbd0

Browse files
authored
Fix imrotate for Irrational θ (#149)
1 parent 516dbf6 commit 78adbd0

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ ImageIO = "82e4d734-157c-48bb-816b-45c225c6df19"
3131
ImageMagick = "6218d12a-5da1-5696-b52f-db25d2ecc6d1"
3232
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
3333
ReferenceTests = "324d217c-45ce-50fc-942e-d289b448e8cf"
34+
Tau = "c544e3c2-d3e5-5802-ac44-44683f340e4a"
3435
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
3536
TestImages = "5e47fb64-e119-507b-a336-dd2b206d9990"
3637

3738
[targets]
38-
test = ["EndpointRanges", "ImageIO", "ImageMagick", "LinearAlgebra", "ReferenceTests", "Test", "TestImages"]
39+
test = ["EndpointRanges", "ImageIO", "ImageMagick", "LinearAlgebra", "ReferenceTests", "Tau", "Test", "TestImages"]

src/warp.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,19 @@ mosaicview([imgr for _ in 1:9]; nrow=3)
244244
"""
245245
function imrotate(img::AbstractArray{T}, θ::Real, inds::Union{Tuple, Nothing} = nothing; kwargs...) where T
246246
# TODO: expose rotation center as a keyword
247-
Δ = eps(θ)
248-
θ = mod2pi(θ)
247+
Δ = eps(float(θ))
248+
θ = _mod2pi(θ)
249249
if abs(θ) <= Δ || abs- 2π) <= Δ
250250
θ = zero(θ)
251251
end
252-
tform = recenter(RotMatrix{2}(θ), center(img))
252+
tform = recenter(rotmtrx2(θ), center(img))
253253
# Use the `nothing` trick here because moving the `autorange` as default value is not type-stable
254254
inds = isnothing(inds) ? autorange(img, inv(tform)) : inds
255255
warp(img, tform, inds; kwargs...)
256256
end
257+
_mod2pi(θ) = mod2pi(θ)
258+
_mod2pi::Irrational{:π}) = θ
259+
_mod2pi::Irrational) = mod2pi(float(θ))
260+
261+
rotmtrx2(θ) = RotMatrix{2}(θ)
262+
rotmtrx2::Irrational{:π}) = RotMatrix(@SMatrix [-1 0; 0 -1])

test/warp.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using CoordinateTransformations, Rotations, TestImages, ImageCore, StaticArrays, OffsetArrays, Interpolations, LinearAlgebra
22
using EndpointRanges
3+
using Tau
34
using Test, ReferenceTests
45

56
include("twoints.jl")
@@ -465,5 +466,23 @@ NaN NaN NaN NaN NaN NaN NaN
465466
@test !any(isnan, imrotate(img, π/3, axes(img); fillvalue=0.0))
466467
@test !any(isnan, imrotate(img, π/3, axes(img), fillvalue=0.0))
467468
end
469+
470+
@testset "Examples from #79" begin
471+
@test axes(imrotate(img_camera, pi/2)) == axes(img_camera)
472+
@test imrotate(imrotate(imrotate(imrotate(img_camera, pi/2), pi/2), pi/2), pi/2) img_camera
473+
@test imrotate(imrotate(img_camera, pi), pi) == img_camera
474+
# Also check a "generic" irrational (one that happens to be quite special)
475+
## First, check that this hits the generic code path (if we change that, pick a different irrational)
476+
@test ImageTransformations._mod2pi(τ) isa Float64
477+
## Now check that it gives the expected result
478+
@test imrotate(img_camera, τ) img_camera
479+
480+
# check special rotation degrees
481+
img = rand(Gray{N0f8}, 100, 50)
482+
@test size(imrotate(img, pi/2)) == (50, 100)
483+
@test size(imrotate(img, pi)) == (100, 50)
484+
@test size(imrotate(img, 3pi/2)) == (50, 100)
485+
@test size(imrotate(img, 2pi)) == (100, 50)
486+
end
468487
end
469488
end

0 commit comments

Comments
 (0)