Skip to content

Commit ecf124b

Browse files
committed
Ambiguous extensions: default to first format, provide forceable fallback
This also registers the Netpbm.jl package
1 parent fd6e1e8 commit ecf124b

File tree

5 files changed

+43
-7
lines changed

5 files changed

+43
-7
lines changed

src/FileIO.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ function save(s::@compat(Union{AbstractString,IO}), data...; options...)
8686
rethrow(last_exception)
8787
end
8888

89+
# Forced format
90+
function save{sym}(::Type{DataFormat{sym}}, f::AbstractString, data...; options...)
91+
libraries = sym2saver[sym]
92+
check_saver(libraries[1])
93+
save(File(DataFormat{sym}, f), data...; options...)
94+
end
95+
96+
function save{sym}(::Type{DataFormat{sym}}, s::IO, data...; options...)
97+
libraries = sym2saver[sym]
98+
check_saver(libraries[1])
99+
save(Stream(DataFormat{sym}, s), data...; options...)
100+
end
101+
89102
function Base.writemime(io::IO, mime::MIME, x)
90103
handlers = applicable_mime(mime)
91104
last_exception = ErrorException("No package available to writemime $mime")

src/query.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ function query(filename::AbstractString)
352352
if lensym(sym) == 1 && (no_magic || !isfile(filename)) # we only found one candidate and there is no magic bytes, or no file, trust the extension
353353
return File{DataFormat{sym}}(filename)
354354
elseif !isfile(filename) && lensym(sym) > 1
355-
error("no file for check of magic bytes and multiple extensions possible: $sym")
355+
return File{DataFormat{sym[1]}}(filename)
356356
end
357357
if no_magic && !hasfunction(sym)
358358
error("Some formats with extension ", ext, " have no magic bytes; use `File{format\"FMT\"}(filename)` to resolve the ambiguity.")

src/registry.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
add_format(format"JLD", "Julia data file (HDF5)", ".jld", [:JLD])
33

44
# Image formats
5-
add_format(format"PBMText", b"P1", ".pbm")
6-
add_format(format"PGMText", b"P2", ".pgm")
7-
add_format(format"PPMText", b"P3", ".ppm")
8-
add_format(format"PBMBinary", b"P4", ".pbm")
9-
add_format(format"PGMBinary", b"P5", ".pgm")
10-
add_format(format"PPMBinary", b"P6", ".ppm")
5+
add_format(format"PBMBinary", b"P4", ".pbm", [:ImageMagick])
6+
add_format(format"PGMBinary", b"P5", ".pgm", [:Netpbm])
7+
add_format(format"PPMBinary", b"P6", ".ppm", [:Netpbm])
8+
add_format(format"PBMText", b"P1", ".pbm", [:ImageMagick, LOAD])
9+
add_format(format"PGMText", b"P2", ".pgm", [:ImageMagick, LOAD])
10+
add_format(format"PPMText", b"P3", ".ppm", [:ImageMagick, LOAD])
1111

1212
add_format(format"NRRD", "NRRD", [".nrrd", ".nhdr"], [:NRRD])
1313

test/REQUIRE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
FactCheck
2+
Images
3+
Netpbm
4+
ImageMagick

test/loadsave.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,23 @@ context("Save") do
115115
end
116116

117117
del_format(format"DUMMY")
118+
119+
# PPM/PBM can be either binary or text. Test that the defaults work,
120+
# and that we can force a choice.
121+
using Images
122+
context("Ambiguous extension") do
123+
A = rand(UInt8, 3, 2)
124+
fn = string(tempname(), ".pgm")
125+
# Test the forced version first: we wouldn't want some method in Netpbm
126+
# coming to the rescue here, we want to rely on FileIO's logic.
127+
# `save(fn, A)` will load Netpbm, which could conceivably mask a failure
128+
# in the next line.
129+
save(format"PGMBinary", fn, A)
130+
B = load(fn)
131+
@fact raw(convert(Array, B)) --> A
132+
save(fn, A)
133+
B = load(fn)
134+
@fact raw(convert(Array, B)) --> A
135+
136+
rm(fn)
137+
end

0 commit comments

Comments
 (0)