Julia can xor a cursor image with an image when both images are gray, producing the following image with the following code:
img = Array{N0f8}(reshape(range(0,stop=1,length=10^4), 100, 100))
imgx = zeros(N0f8, 21, 21)
imgx[10:12,:] .= 1.
imgx[:,10:12] .= 1.
img_num = reinterpret(UInt8, img)
imgx_num = reinterpret(UInt8, imgx)
img_num[10:30,70:90] = xor.(
img_num[10:30,70:90],
imgx_num)
save("trash3.png", img)
However, I don’t understand how to get Julia to access the UInt8 color fields for the color version of that xor operation. My first attempt was reinterpreting N0f8 from the channelview reinterpretation of the loaded image. Apparently, two layers of reinterpret cause problems with accessing ranges of pixels:
julia> img_map = load("bok.png"); # Array{RGBA{N0f8},2}
julia> img_map_num = channelview(img_map); # 4×3372×6000 reinterpret(reshape, N0f8, ::Array{RGBA{N0f8},2})
julia> img_map_num2 = reinterpret(UInt8, img_map_num); # 4×3372×6000 reinterpret(UInt8, reinterpret(reshape, N0f8, ::Array{RGBA{N0f8},2}))
julia> display(img_map_num2[1,1:10,1:10])
ERROR: MethodError: no method matching _maybe_reshape(::Base.IndexSCartesian2{4}, ::Base.ReinterpretArray{UInt8, 3, N0f8, Base.ReinterpretArray{N0f8, 3, RGBA{N0f8}, Matrix{RGBA{N0f8}}, true}, false}, ::Int64, ::UnitRange{Int64}, ::UnitRange{Int64})
Closest candidates are:
_maybe_reshape(::Base.IndexSCartesian2, ::Base.ReinterpretArray{T, N, S, A, true} where {T, N, S, A<:(AbstractArray{S})}, ::Any...) at C:\tool\Julia-1.7.3\share\julia\base\reinterpretarray.jl:255
_maybe_reshape(::IndexLinear, ::AbstractArray, ::Any...) at C:\tool\Julia-1.7.3\share\julia\base\multidimensional.jl:842
_maybe_reshape(::IndexCartesian, ::AbstractArray, ::Any...) at C:\tool\Julia-1.7.3\share\julia\base\multidimensional.jl:844
...
Stacktrace:
[1] _getindex
@ .\multidimensional.jl:839 [inlined]
[2] getindex(::Base.ReinterpretArray{UInt8, 3, N0f8, Base.ReinterpretArray{N0f8, 3, RGBA{N0f8}, Matrix{RGBA{N0f8}}, true}, false}, ::Int64, ::UnitRange{Int64}, ::UnitRange{Int64})
@ Base .\abstractarray.jl:1218
[3] top-level scope
@ REPL[301]:1
And my attempt at getting access to the UInt8 fields in a single reinterpret also doesn’t work:
julia> imgx = load("iconArrow101.png")
101×101 Array{RGB{N0f8},2} with eltype RGB{N0f8}:
RGB{N0f8}(0.0,0.157,1.0) RGB{N0f8}(0.0,0.157,1.0) … RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0)
RGB{N0f8}(0.0,0.157,1.0) RGB{N0f8}(0.0,0.157,1.0) RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0)
RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.157,1.0) RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0)
RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.157,1.0) RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0)
RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0)
RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0) … RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0)
RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0)
RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0)
RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0)
RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.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0f8}(0.0,0.0,0.0)
RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0)
RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0)
RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0) … RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0)
RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.157,1.0) RGB{N0f8}(0.0,0.0,0.0)
RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.157,1.0) RGB{N0f8}(0.0,0.0,0.0)
RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.157,1.0) RGB{N0f8}(0.0,0.0,0.0)
RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0)
RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0) … RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.0,0.0)
julia> imgx_num = reinterpret(reshape, Array{RGB{UInt8},2}, imgx)
ERROR: TypeError: in RGB, in T, expected T<:Union{AbstractFloat, FixedPoint}, got Type{UInt8}
Stacktrace:
[1] top-level scope
@ REPL[304]:1