JuliaImages / DitherPunk.jl

Dithering algorithms in Julia.
MIT License
60 stars 3 forks source link

error diffusion method errors if requesting more than 255 cmap colors #74

Closed johnnychen94 closed 2 years ago

johnnychen94 commented 2 years ago
using Clustering, DitherPunk, ImageCore, TestImages

img = testimage("lighthouse")

dither(img, FloydSteinberg(), 32); # works
dither(img, FloydSteinberg(), 256); # fails with encrypted error message
adrhill commented 2 years ago

This is probably because of https://github.com/JuliaImages/DitherPunk.jl/blob/a8b3a623cc145fecb88c95212306cf0641710dd8/src/error_diffusion.jl#L82 and can be replicated by

julia> index = Array{UInt8}(undef, 2)
2-element Vector{UInt8}:
 0xf0
 0x6b

julia> index[1] = 32
32

julia> index[2] = 256
ERROR: InexactError: trunc(UInt8, 256)
Stacktrace:
 [1] throw_inexacterror(f::Symbol, #unused#::Type{UInt8}, val::Int64)
   @ Core ./boot.jl:614
 [2] checked_trunc_uint
   @ ./boot.jl:644 [inlined]
 [3] toUInt8
   @ ./boot.jl:706 [inlined]
 [4] UInt8
   @ ./boot.jl:766 [inlined]
 [5] convert
   @ ./number.jl:7 [inlined]
 [6] setindex!(A::Vector{UInt8}, x::Int64, i1::Int64)
   @ Base ./array.jl:966
 [7] top-level scope
   @ REPL[10]:1