Skip to content

Commit

Permalink
offset generic array back to 1-based indexing array (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnychen94 authored May 19, 2021
1 parent dc3f05f commit aca328b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Sixel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Sixel
export sixel_encode, sixel_decode

using ImageCore
using OffsetArrays
using IndirectArrays # sixel sequence is actually an indexed image format

import REPL: Terminals
Expand Down
5 changes: 5 additions & 0 deletions src/encoder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ sixel_encode(io::IO, img::AbstractVector, enc::SEC=default_encoder(img); kwargs.
sixel_encode(io, reshape(img, :, 1), enc; kwargs...)

function sixel_encode(io::IO, img::AbstractArray, enc::SEC=default_encoder(img); transpose=false, kwargs...)
# Conversion from OffsetArrays to Array is not very well supported, so we have to de-offset first.
img = OffsetArrays.no_offset_view(img)

# make sure it always tiles along row order
@assert ndims(img) >= 3
nrow = transpose ? prod(size(img)[3:end]) : 1
Expand All @@ -76,6 +79,8 @@ function sixel_encode(
img::AbstractMatrix,
enc::SEC=default_encoder(img);
transpose=false, kwargs...)
# Conversion from OffsetArrays to Array is not very well supported, so we have to de-offset first.
img = OffsetArrays.no_offset_view(img)
if enc isa LibSixelEncoder
T = canonical_sixel_eltype(enc, eltype(img))
AT = Array{T, ndims(img)}
Expand Down
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ ImageMagick = "6218d12a-5da1-5696-b52f-db25d2ecc6d1"
ImageQualityIndexes = "2996bd0c-7a13-11e9-2da2-2f5ce47296a9"
IndirectArrays = "9b13fd28-a010-5f03-acff-a1bbcff69959"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TestImages = "5e47fb64-e119-507b-a336-dd2b206d9990"
19 changes: 19 additions & 0 deletions test/backend/libsixel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,25 @@
sixel_encode(io, img, transpose=false)
@test bufferdata == String(take!(io))
end

@testset "OffsetArray" begin
enc = Sixel.LibSixelEncoder()
for img in [
repeat(distinguishable_colors(10), inner=(10, )),
repeat(distinguishable_colors(10), inner=(10, 50)),
repeat(distinguishable_colors(5), inner=(20, 50, 3))
]
io = IOBuffer()
sixel_encode(io, img, enc)
ref = String(take!(io))

imgo = OffsetArray(img, OffsetArrays.Origin(0))
io = IOBuffer()
sixel_encode(io, imgo, enc)
actual = String(take!(io))
@test ref == actual
end
end
end

@testset "decoder" begin
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Sixel
using Test
using ImageCore, IndirectArrays
using ImageCore, IndirectArrays, OffsetArrays
using ImageQualityIndexes
using LinearAlgebra
using FileIO, TestImages
Expand Down

0 comments on commit aca328b

Please sign in to comment.