Skip to content

Commit

Permalink
Changed encode to provide a default quality for lossy encoding
Browse files Browse the repository at this point in the history
Provides the same default for quality (75) as the cwebp tool included with libwebp.

This necessitated adding a boolean lossy kwarg to encode.
  • Loading branch information
stemann committed Apr 21, 2024
1 parent 3bbefca commit 71a2c82
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
5 changes: 1 addition & 4 deletions src/encoding.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
function encode(
image::AbstractMatrix{TColor};
quality::Union{Real, Nothing} = nothing,
transpose = false,
image::AbstractMatrix{TColor}; lossy = false, quality::Real = 75, transpose = false
)::Vector{UInt8} where {TColor <: Colorant}
lossy = !isnothing(quality)
if lossy && !(0 quality 100)
throw(ArgumentError("Quality $quality is not in the range from 0 to 100."))
end
Expand Down
12 changes: 9 additions & 3 deletions test/encoding_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,19 @@ using WebP

@testset "Lossy" begin
@testset "encode throws ArgumentError for quality outside range" begin
@test_throws ArgumentError WebP.encode(input_image; quality = -1)
@test_throws ArgumentError WebP.encode(input_image; quality = 101)
@test_throws ArgumentError WebP.encode(
input_image; lossy = true, quality = -1
)
@test_throws ArgumentError WebP.encode(
input_image; lossy = true, quality = 101
)
end
qualities = [1, 10, 50, 100]
quality_types = [Int, Float32, Float64]
for quality in qualities, TQuality in quality_types
input_kwargs = merge(kwargs, (quality = TQuality(quality),))
input_kwargs = merge(
kwargs, (lossy = true, quality = TQuality(quality))
)
@testset "WebP.encode(::Matrix{$TColor}; $input_kwargs)" begin
data = WebP.encode(input_image; input_kwargs...)
output = WebP.decode(data)
Expand Down

0 comments on commit 71a2c82

Please sign in to comment.