Skip to content

Commit 4bb6075

Browse files
committed
Added support for WebP
Also: * Moved QOI to Image formats section. * Changed AVI and WAV detectors (`detectavi`, and `detectwav`) to also use `detect_riff` - `detect_riff` is a bit more elaborate, as it also validates the RIFF payload size (little-endian UInt32 length of the file excluding fourcc, payload_length, and optional padding).
1 parent e6385d9 commit 4bb6075

File tree

1 file changed

+34
-21
lines changed

1 file changed

+34
-21
lines changed

src/registry.jl

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,23 @@ const idLibSndFile = :LibSndFile => UUID("b13ce0c6-77b0-50c6-a2db-140568b8d1a5")
1717
const idJpegTurbo = :JpegTurbo => UUID("b835a17e-a41a-41e7-81f0-2f016b05efe0")
1818
const idNPZ = :NPZ => UUID("15e1cf62-19b3-5cfa-8e77-841668bca605")
1919

20+
function detect_riff(io::IO, expected_magic::AbstractVector{UInt8})
21+
file_length = getlength(io)
22+
file_length >= 12 || return false
23+
buf = Vector{UInt8}(undef, 4)
24+
25+
fourcc = read!(io, buf)
26+
fourcc == b"RIFF" || return false
27+
28+
# Cf. https://developers.google.com/speed/webp/docs/riff_container#riff_file_format, and https://learn.microsoft.com/en-us/windows/win32/xaudio2/resource-interchange-file-format--riff-#chunks
29+
payload_length = ltoh(read(io, UInt32))
30+
padded_payload_length = payload_length + payload_length % 2
31+
padded_payload_length == file_length - 8 || return false
32+
33+
magic = read!(io, buf)
34+
return magic == expected_magic
35+
end
36+
2037
### Simple cases
2138

2239
# data formats
@@ -204,6 +221,13 @@ add_format(
204221
".pcx",
205222
[idImageMagick]
206223
)
224+
add_format(
225+
format"QOI",
226+
"qoif",
227+
".qoi",
228+
[:QOI => UUID("4b34888f-f399-49d4-9bb3-47ed5cae4e65")],
229+
[idImageIO]
230+
)
207231
add_format(
208232
format"SVG",
209233
(),
@@ -218,19 +242,18 @@ add_format(
218242
[idImageIO],
219243
[idImageMagick]
220244
)
245+
detect_webp(io) = detect_riff(io, b"WEBP")
246+
add_format(
247+
format"WebP",
248+
detect_webp,
249+
".webp",
250+
[:WebP => UUID("e3aaa7dc-3e4b-44e0-be63-ffb868ccd7c1")],
251+
[idImageIO]
252+
)
221253

222254
# Video formats
223255

224-
# AVI is a subtype of RIFF, as is WAV
225-
function detectavi(io)
226-
getlength(io) >= 12 || return false
227-
magic = read!(io, Vector{UInt8}(undef, 4))
228-
magic == b"RIFF" || return false
229-
seek(io, 8)
230-
submagic = read!(io, Vector{UInt8}(undef, 4))
231-
232-
submagic == b"AVI "
233-
end
256+
detectavi(io) = detect_riff(io, b"AVI ")
234257
add_format(format"AVI", detectavi, ".avi", [idImageMagick], [idVideoIO])
235258

236259
"""
@@ -278,15 +301,7 @@ add_format(format"OUT", "# Bundle file v0.3\n", ".out", [:BundlerIO => UUID("654
278301
add_format(format"GSLIB", (), [".gslib",".sgems"], [:GslibIO => UUID("4610876b-9b01-57c8-9ad9-06315f1a66a5")])
279302

280303
### Audio formats
281-
function detectwav(io)
282-
getlength(io) >= 12 || return false
283-
buf = Vector{UInt8}(undef, 4)
284-
read!(io, buf)
285-
buf == b"RIFF" || return false
286-
seek(io, 8)
287-
read!(io, buf)
288-
buf == b"WAVE"
289-
end
304+
detectwav(io) = detect_riff(io, b"WAVE")
290305
add_format(format"WAV", detectwav, ".wav", [:WAV => UUID("8149f6b0-98f6-5db9-b78f-408fbbb8ef88")], [idLibSndFile])
291306
add_format(format"FLAC", "fLaC", ".flac", [:FLAC => UUID("abae9e3b-a9a0-4778-b5c6-ca109b507d99")], [idLibSndFile])
292307

@@ -542,7 +557,5 @@ add_format(format"HTML", (), [".html", ".htm"], [MimeWriter, SAVE])
542557

543558
add_format(format"MIDI", "MThd", [".mid", ".midi", ".MID"], [:MIDI => UUID("f57c4921-e30c-5f49-b073-3f2f2ada663e")])
544559

545-
add_format(format"QOI", "qoif", ".qoi", [:QOI => UUID("4b34888f-f399-49d4-9bb3-47ed5cae4e65")], [idImageIO])
546-
547560
# Bibliography files.
548561
add_format(format"BIB", (), [".bib"], [:Bibliography => UUID("f1be7e48-bf82-45af-a471-ae754a193061")])

0 commit comments

Comments
 (0)