Skip to content

Commit 8919166

Browse files
authored
Merge pull request #131 from davidanthoff/mime-save
Add mime save for eps, pdf, png and svg
2 parents a94f376 + 11e6e84 commit 8919166

File tree

9 files changed

+1052
-3
lines changed

9 files changed

+1052
-3
lines changed

src/FileIO.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import Base.showerror
2929
include("query.jl")
3030
include("error_handling.jl")
3131
include("loadsave.jl")
32+
include("mimesave.jl")
3233
include("registry.jl")
3334

3435
"""

src/mimesave.jl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function FileIO.save(file::File{format"PNG"}, data)
2+
if mimewritable("image/png", data)
3+
open(file.filename, "w") do s
4+
show(IOContext(s, :full_fidelity=>true), "image/png", data)
5+
end
6+
else
7+
throw(ArgumentError("Argument does not support conversion to png."))
8+
end
9+
end
10+
11+
function FileIO.save(file::File{format"SVG"}, data)
12+
if mimewritable("image/svg+xml", data)
13+
open(file.filename, "w") do s
14+
show(IOContext(s, :full_fidelity=>true), "image/svg+xml", data)
15+
end
16+
else
17+
throw(ArgumentError("Argument does not support conversion to svg."))
18+
end
19+
end
20+
21+
function FileIO.save(file::File{format"PDF"}, data)
22+
if mimewritable("application/pdf", data)
23+
open(file.filename, "w") do s
24+
show(IOContext(s, :full_fidelity=>true), "application/pdf", data)
25+
end
26+
else
27+
throw(ArgumentError("Argument does not support conversion to pdf."))
28+
end
29+
end
30+
31+
function FileIO.save(file::File{format"EPS"}, data)
32+
if mimewritable("application/eps", data)
33+
open(file.filename, "w") do s
34+
show(IOContext(s, :full_fidelity=>true), "application/eps", data)
35+
end
36+
else
37+
throw(ArgumentError("Argument does not support conversion to eps."))
38+
end
39+
end

src/registry.jl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ add_format(format"CRW", UInt8[0x49,0x49,0x1a,0x00,0x00,0x00,0x48,0x45], ".crw",
4343
add_format(format"CUR", UInt8[0x00,0x00,0x02,0x00], ".cur", [:ImageMagick])
4444
add_format(format"DCX", UInt8[0xb1,0x68,0xde,0x3a], ".dcx", [:ImageMagick])
4545
add_format(format"DOT", UInt8[0xd0,0xcf,0x11,0xe0,0xa1,0xb1,0x1a,0xe1], ".dot", [:ImageMagick])
46-
add_format(format"EPS", UInt8[0x25,0x21,0x50,0x53,0x2d,0x41,0x64,0x6f], ".eps", [:ImageMagick])
46+
add_format(format"EPS", UInt8[0x25,0x21,0x50,0x53,0x2d,0x41,0x64,0x6f], ".eps", [:ImageMagick], [:FileIO, SAVE])
4747
add_format(format"HDR", UInt8[0x23,0x3f,0x52,0x41,0x44,0x49,0x41,0x4e], ".hdr", [:ImageMagick])
4848
add_format(format"ICO", UInt8[0x00,0x00,0x01,0x00], ".ico", [:ImageMagick])
4949
add_format(format"INFO", UInt8[0x7a,0x62,0x65,0x78], ".info",[:ImageMagick])
5050
add_format(format"JP2", UInt8[0x00,0x00,0x00,0x0c,0x6a,0x50,0x20,0x20], ".jp2", [:ImageMagick])
5151
add_format(format"PDB", UInt8[0x73,0x7a,0x65,0x7a], ".pdb", [:ImageMagick])
52-
add_format(format"PDF", UInt8[0x25,0x50,0x44,0x46], ".pdf", [:ImageMagick])
52+
add_format(format"PDF", UInt8[0x25,0x50,0x44,0x46], ".pdf", [:ImageMagick], [:FileIO, SAVE])
5353
add_format(format"PGM", UInt8[0x50,0x35,0x0a], ".pgm", [:ImageMagick])
5454
add_format(format"PSD", UInt8[0x38,0x42,0x50,0x53], ".psd", [:ImageMagick])
5555
add_format(format"RGB", UInt8[0x01,0xda,0x01,0x01,0x00,0x03], ".rgb", [:ImageMagick])
@@ -76,7 +76,8 @@ add_format(
7676
UInt8[0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a],
7777
".png",
7878
[:QuartzImageIO, OSX],
79-
[:ImageMagick]
79+
[:ImageMagick],
80+
[:FileIO, SAVE]
8081
)
8182
add_format(
8283
format"TIFF",
@@ -105,6 +106,12 @@ add_format(
105106
".pcx",
106107
[:ImageMagick]
107108
)
109+
add_format(
110+
format"SVG",
111+
(),
112+
".svg",
113+
[:FileIO, SAVE]
114+
)
108115

109116
#=
110117
add_format(format"NPY", UInt8[0x93, 'N', 'U', 'M', 'P', 'Y'], ".npy")

test/files/mimesavetest.eps

Lines changed: 950 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/files/mimesavetest.pdf

19.8 KB
Binary file not shown.

test/files/mimesavetest.png

3.55 KB
Loading

test/files/mimesavetest.svg

Lines changed: 3 additions & 0 deletions
Loading

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
using FileIO
22
using Base.Test
33

4+
immutable MimeSaveTestType
5+
end
6+
47
@testset "FileIO" begin
58
include("query.jl")
69
include("loadsave.jl")
710
include("error_handling.jl")
11+
include("test_mimesave.jl")
812
end

test/test_mimesave.jl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using FileIO
2+
using Base.Test
3+
4+
@testset "Mime save" begin
5+
6+
function Base.show(io::IO, m::MIME"image/svg+xml", data::MimeSaveTestType)
7+
content = read(joinpath(@__DIR__, "files", "mimesavetest.svg"))
8+
write(io, content)
9+
end
10+
11+
function Base.show(io::IO, m::MIME"application/pdf", data::MimeSaveTestType)
12+
content = read(joinpath(@__DIR__, "files", "mimesavetest.pdf"))
13+
write(io, content)
14+
end
15+
16+
function Base.show(io::IO, m::MIME"application/eps", data::MimeSaveTestType)
17+
content = read(joinpath(@__DIR__, "files", "mimesavetest.eps"))
18+
write(io, content)
19+
end
20+
21+
function Base.show(io::IO, m::MIME"image/png", data::MimeSaveTestType)
22+
content = read(joinpath(@__DIR__, "files", "mimesavetest.png"))
23+
write(io, content)
24+
end
25+
26+
data = MimeSaveTestType()
27+
28+
output_filename = tempname()
29+
30+
for filetype in [".svg", ".pdf", ".eps", ".png"]
31+
32+
try
33+
save(output_filename * filetype, data)
34+
35+
content_original = read(joinpath(@__DIR__, "files", "mimesavetest$filetype"))
36+
content_new = read(output_filename * filetype)
37+
38+
@test content_new == content_original
39+
finally
40+
rm(output_filename * filetype)
41+
end
42+
43+
end
44+
45+
end

0 commit comments

Comments
 (0)