Skip to content

Commit

Permalink
Added support for WebP
Browse files Browse the repository at this point in the history
  • Loading branch information
stemann committed Apr 26, 2024
1 parent 583389c commit dd6dfba
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ QOI = "4b34888f-f399-49d4-9bb3-47ed5cae4e65"
Sixel = "45858cf5-a6b0-47a3-bbea-62219f50df47"
TiffImages = "731e570b-9d59-4bfa-96dc-6df516fadf69"
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
WebP = "e3aaa7dc-3e4b-44e0-be63-ffb868ccd7c1"

[compat]
FileIO = "1.2"
Expand All @@ -28,6 +29,7 @@ PNGFiles = "0.3, 0.4"
QOI = "1"
Sixel = "0.1.2"
TiffImages = "0.3, 0.4, 0.5, 0.6, 0.7, 0.8"
WebP = "0.1.2"
julia = "1.6"

[extras]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ FileIO.jl integration for image files
| [QOI (Quite Okay Image)](https://qoiformat.org/) format | `.qoi` | [QOI.jl](https://github.com/KristofferC/QOI.jl) | pure Julia | |
| DEC SIXEL (six-pixels) graphics | `.six`, `.sixel` | [Sixel.jl](https://github.com/johnnychen94/Sixel.jl) | Julia wrapper of [libsixel](https://github.com/libsixel/libsixel) | |
| TIFF (Tag Image File Format) | `.tiff`, `.tif` | [TiffImages.jl](https://github.com/tlnagy/TiffImages.jl) | pure Julia | check [OMETIFF.jl](https://github.com/tlnagy/OMETIFF.jl) for OMETIFF support |

| WebP | `.webp` | [WebP.jl](https://github.com/stemann/WebP.jl) | Julia wrapper of [libwebp](https://developers.google.com/speed/webp/docs/api) | |

## Installation

Expand Down
20 changes: 19 additions & 1 deletion src/ImageIO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ using LazyModules # @lazy macro is used to delay the package loading to its firs
@lazy import OpenEXR = "52e1d378-f018-4a11-a4be-720524705ac7"
@lazy import QOI = "4b34888f-f399-49d4-9bb3-47ed5cae4e65"
@lazy import JpegTurbo = "b835a17e-a41a-41e7-81f0-2f016b05efe0"
@lazy import WebP = "e3aaa7dc-3e4b-44e0-be63-ffb868ccd7c1"

# Enforce a type conversion to be backend independent (issue #25)
# Note: If the backend does not provide efficient `convert` implementation,
Expand All @@ -24,7 +25,8 @@ for FMT in (
:EXR,
:QOI,
:SIXEL,
:JPEG
:JPEG,
:WebP,
)
@eval canonical_type(::DataFormat{$(Expr(:quote, FMT))}, ::AbstractArray{T, N}) where {T,N} =
Array{T,N}
Expand Down Expand Up @@ -186,6 +188,22 @@ function save(s::Stream{DataFormat{:JPEG}}, image::AbstractArray; kwargs...)
JpegTurbo.fileio_save(s, image; kwargs...)
end

## WebP
function load(f::File{DataFormat{:WebP}}; kwargs...)
data = WebP.fileio_load(f, kwargs...)
return enforce_canonical_type(f, data)
end
function load(s::Stream{DataFormat{:WebP}}; kwargs...)
data = WebP.fileio_load(s, kwargs...)
return enforce_canonical_type(s, data)
end
function save(f::File{DataFormat{:WebP}}, image::AbstractArray; kwargs...)
WebP.fileio_save(f, image; kwargs...)
end
function save(s::Stream{DataFormat{:WebP}}, image::AbstractArray; kwargs...)
WebP.fileio_save(s, image; kwargs...)
end

## Function names labelled for FileIO. Makes FileIO lookup quicker
const fileio_save = save
const fileio_load = load
Expand Down
18 changes: 18 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,22 @@ Threads.nthreads() <= 1 && @info "Threads.nthreads() = $(Threads.nthreads()), mu
end
end
end

@testset "WebP" begin
for typ in [RGBA{N0f8}, RGB{N0f8}]
@testset "$typ JPEG" begin
img = rand(typ, 10, 10)
f = File{format"WebP"}(joinpath(tmpdir, "test_fpath.webp"))
ImageIO.save(f, img)
img_saveload = ImageIO.load(f)
@test eltype(img_saveload) == n0f8(typ) # WebP uses 8bit
@test typeof(img_saveload) == ImageIO.canonical_type(f, img_saveload)

open(io->ImageIO.save(Stream{format"WebP"}(io), img), joinpath(tmpdir, "test_io.webp"), "w")
img_saveload = open(io->ImageIO.load(Stream{format"WebP"}(io)), joinpath(tmpdir, "test_io.webp"))
@test eltype(img_saveload) == n0f8(typ) # WebP uses 8bit
@test typeof(img_saveload) == ImageIO.canonical_type(f, img_saveload)
end
end
end
end

0 comments on commit dd6dfba

Please sign in to comment.