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 for WebP #67

Merged
merged 4 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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, 0.9, 0.10"
WebP = "0.1.2"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
WebP = "0.1.2"
WebP = "0.1"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The restriction to >= 0.1.2 was on purpose - there were some bad bugs in 0.1.0 and 0.1.1.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be >=0.1.2 then ? I always forget if the >= is implied or if it pins the version.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0.1.2 means >=0.1.2, <0.2.0 which is semantically differnt from >=0.1.2.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always forget this one, thanks for the correction 😌.

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 WebP" 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
Loading