Skip to content

Commit bf2c3b3

Browse files
tlnagytimholy
authored andcommitted
add support for loading OME-TIFFs (#147)
1 parent bf7756c commit bf2c3b3

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/registry.jl

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,6 @@ add_format(
8181
[:ImageMagick],
8282
[:MimeWriter, SAVE]
8383
)
84-
add_format(
85-
format"TIFF",
86-
(UInt8[0x4d,0x4d,0x00,0x2a], UInt8[0x4d,0x4d,0x00,0x2b], UInt8[0x49,0x49,0x2a,0x00],UInt8[0x49,0x49,0x2b,0x00]),
87-
[".tiff", ".tif"],
88-
[:QuartzImageIO, OSX],
89-
[:ImageMagick]
90-
)
9184
add_format(
9285
format"JPEG",
9386
UInt8[0xff,0xd8,0xff],
@@ -154,6 +147,25 @@ add_format(format"FLAC","fLaC",".flac",[:FLAC])
154147

155148
### Complex cases
156149

150+
# Handle OME-TIFFs, which are identical to normal TIFFs with the primary difference being the filename and embedded XML metadata
151+
const tiff_magic = (UInt8[0x4d,0x4d,0x00,0x2a], UInt8[0x4d,0x4d,0x00,0x2b], UInt8[0x49,0x49,0x2a,0x00],UInt8[0x49,0x49,0x2b,0x00])
152+
function detecttiff(io)
153+
seekstart(io)
154+
magic = read(io, UInt8, 4)
155+
# do any of the first 4 bytes match any of the 4 possible combinations of tiff magics
156+
return any(map(x->all(magic .== x), tiff_magic))
157+
end
158+
# OME-TIFF
159+
detect_ometiff(io) = detecttiff(io) && (endswith(io.name, ".ome.tif>") || endswith(io.name, ".ome.tiff>"))
160+
add_format(format"OMETIFF", detect_ometiff, [".tif", ".tiff"], [:OMETIFF])
161+
# normal TIFF
162+
detect_noometiff(io) = detecttiff(io) && !(endswith(io.name, ".ome.tif>") || endswith(io.name, ".ome.tiff>"))
163+
add_format(format"TIFF", detect_noometiff, [".tiff", ".tif"], [:QuartzImageIO, OSX], [:ImageMagick])
164+
165+
# custom skipmagic functions for function-based tiff magic detection
166+
skipmagic(io, ::typeof(detect_ometiff)) = seek(io, 4)
167+
skipmagic(io, ::typeof(detect_noometiff)) = seek(io, 4)
168+
157169
# AVI is a subtype of RIFF, as is WAV
158170
function detectavi(io)
159171
seekstart(io)

0 commit comments

Comments
 (0)