Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support transparency #18

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
t-bltg marked this conversation as resolved.
Show resolved Hide resolved

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 = ...
t-bltg marked this conversation as resolved.
Show resolved Hide resolved