Skip to content

Commit

Permalink
support transparency
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg committed Aug 11, 2022
1 parent 07011d3 commit 5f4970b
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/backend/libsixel/encoder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function (enc::LibSixelEncoder)(io::T, bytes::Matrix) where {T<:IO}
depth = 3 # unused

dither = SixelDither(bytes, height, width, pixelformat, quality_mode; allocator=enc.allocator)
C.sixel_dither_set_transparent(dither.ptr, hasfield(eltype(bytes), :alpha) ? 0 : -1)

fn_write_cb = @cfunction(sixel_write_callback_function, Cint, (Ptr{Cchar}, Cint, Ref{T}))
output = SixelOutput(fn_write_cb, Ref{T}(io); allocator=enc.allocator)
Expand Down Expand Up @@ -62,9 +63,10 @@ default_pixelformat(::Type{T}) where T<:Union{N0f8, UInt8} = C.SIXEL_PIXELFORMA
default_quality_mode(img)
default_quality_mode(CT)
Infer the default quality mode that used to encode pixel.
Infer the default quality mode that is used to encode pixel.
"""
default_quality_mode(::AbstractArray{CT}) where CT<:Union{Colorant, Real} = default_quality_mode(CT)
default_quality_mode(::Type{CT}) where CT<:TransparentRGB = C.SIXEL_QUALITY_AUTO
default_quality_mode(::Type{CT}) where CT<:AbstractRGB = C.SIXEL_QUALITY_AUTO
# CHECK: highcolor is needed for iTerm2 on macOS, check if this works on other terminal
default_quality_mode(::Type{CT}) where CT<:AbstractGray = C.SIXEL_QUALITY_HIGHCOLOR
Expand All @@ -73,11 +75,10 @@ default_quality_mode(::Type{T}) where T<:Union{N0f8, UInt8} = C.SIXEL_QUALITY_HI

canonical_sixel_eltype(::LibSixelEncoder, ::Type{CT}) where CT<:Colorant = n0f8(CT)
canonical_sixel_eltype(::LibSixelEncoder, ::Type{CT}) where CT<:Color3 = RGB{N0f8}
canonical_sixel_eltype(::LibSixelEncoder, ::Type{CT}) where CT<:Transparent3 = RGBA{N0f8}
canonical_sixel_eltype(::LibSixelEncoder, ::Type{T}) where T<:Real = N0f8
canonical_sixel_eltype(::LibSixelEncoder, ::Type{T}) where T<:Integer = UInt8
# strip the alpha channel before sending into libsixel
canonical_sixel_eltype(lib::LibSixelEncoder, ::Type{CT}) where CT<:Union{ColorAlpha, AlphaColor} =
canonical_sixel_eltype(lib, base_color_type(CT))

# TODO: these special types might have native libsixel support, but I haven't
# figured it out yet.
# canonical_sixel_eltype(::LibSixelEncoder, ::Type{Bool}) = Gray{N0f8}
Expand All @@ -88,3 +89,13 @@ canonical_sixel_eltype(lib::LibSixelEncoder, ::Type{CT}) where CT<:Union{ColorAl
# TODO: For unknown reasons, AGray and GrayA encoded by libsixel is not correctly displayed
# in iTerm. Thus here we convert it to `ARGB` types.
# canonical_sixel_eltype(::LibSixelEncoder, ::Union{AGray, GrayA}) = ARGB{N0f8}

"""
default_colorbits(img)
default_colorbits(CT)
Infer the default color mode that is used to encode pixel.
"""
# default_colorbits(::AbstractArray{CT}) where CT<:Union{Colorant, Real} = default_colorbits(CT)
# default_colorbits(::Type{CT}) where CT<:TransparentRGB = ...
# default_colorbits(::Type{CT}) where CT<:AbstractRGB = ...

0 comments on commit 5f4970b

Please sign in to comment.