@@ -50,6 +50,11 @@ function _convert(::Type{U}, ::Type{T}, x) where {U <: Normed,T}
50
50
(0 <= y) & (y <= typemax (T)) || throw_converterror (U, x)
51
51
U (_unsafe_trunc (T, y), 0 )
52
52
end
53
+ # Prevent overflow (https://discourse.julialang.org/t/saving-greater-than-8-bit-images/6057)
54
+ _convert (:: Type{U} , :: Type{T} , x:: Float16 ) where {U <: Normed ,T} =
55
+ _convert (U, T, Float32 (x))
56
+ _convert (:: Type{U} , :: Type{UInt128} , x:: Float16 ) where {U <: Normed } =
57
+ _convert (U, UInt128, Float32 (x))
53
58
function _convert (:: Type{U} , :: Type{UInt128} , x) where {U <: Normed }
54
59
y = round (rawone (U)* x) # for UInt128, we can't widen
55
60
(0 <= y) & (y <= typemax (UInt128)) & (x <= Float64 (typemax (U))) || throw_converterror (U, x)
59
64
rem (x:: T , :: Type{T} ) where {T <: Normed } = x
60
65
rem (x:: Normed , :: Type{T} ) where {T <: Normed } = reinterpret (T, _unsafe_trunc (rawtype (T), round ((rawone (T)/ rawone (x))* reinterpret (x))))
61
66
rem (x:: Real , :: Type{T} ) where {T <: Normed } = reinterpret (T, _unsafe_trunc (rawtype (T), round (rawone (T)* x)))
67
+ rem (x:: Float16 , :: Type{T} ) where {T <: Normed } = rem (Float32 (x), T) # avoid overflow
62
68
63
69
# convert(::Type{AbstractFloat}, x::Normed) = convert(floattype(x), x)
64
70
float (x:: Normed ) = convert (floattype (x), x)
0 commit comments