Skip to content

Commit

Permalink
Merge pull request #131 from davidanthoff/mime-save
Browse files Browse the repository at this point in the history
Add mime save for eps, pdf, png and svg
  • Loading branch information
SimonDanisch authored Jun 15, 2017
2 parents a94f376 + 11e6e84 commit 8919166
Show file tree
Hide file tree
Showing 9 changed files with 1,052 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/FileIO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import Base.showerror
include("query.jl")
include("error_handling.jl")
include("loadsave.jl")
include("mimesave.jl")
include("registry.jl")

"""
Expand Down
39 changes: 39 additions & 0 deletions src/mimesave.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function FileIO.save(file::File{format"PNG"}, data)
if mimewritable("image/png", data)
open(file.filename, "w") do s
show(IOContext(s, :full_fidelity=>true), "image/png", data)
end
else
throw(ArgumentError("Argument does not support conversion to png."))
end
end

function FileIO.save(file::File{format"SVG"}, data)
if mimewritable("image/svg+xml", data)
open(file.filename, "w") do s
show(IOContext(s, :full_fidelity=>true), "image/svg+xml", data)
end
else
throw(ArgumentError("Argument does not support conversion to svg."))
end
end

function FileIO.save(file::File{format"PDF"}, data)
if mimewritable("application/pdf", data)
open(file.filename, "w") do s
show(IOContext(s, :full_fidelity=>true), "application/pdf", data)
end
else
throw(ArgumentError("Argument does not support conversion to pdf."))
end
end

function FileIO.save(file::File{format"EPS"}, data)
if mimewritable("application/eps", data)
open(file.filename, "w") do s
show(IOContext(s, :full_fidelity=>true), "application/eps", data)
end
else
throw(ArgumentError("Argument does not support conversion to eps."))
end
end
13 changes: 10 additions & 3 deletions src/registry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ add_format(format"CRW", UInt8[0x49,0x49,0x1a,0x00,0x00,0x00,0x48,0x45], ".crw",
add_format(format"CUR", UInt8[0x00,0x00,0x02,0x00], ".cur", [:ImageMagick])
add_format(format"DCX", UInt8[0xb1,0x68,0xde,0x3a], ".dcx", [:ImageMagick])
add_format(format"DOT", UInt8[0xd0,0xcf,0x11,0xe0,0xa1,0xb1,0x1a,0xe1], ".dot", [:ImageMagick])
add_format(format"EPS", UInt8[0x25,0x21,0x50,0x53,0x2d,0x41,0x64,0x6f], ".eps", [:ImageMagick])
add_format(format"EPS", UInt8[0x25,0x21,0x50,0x53,0x2d,0x41,0x64,0x6f], ".eps", [:ImageMagick], [:FileIO, SAVE])
add_format(format"HDR", UInt8[0x23,0x3f,0x52,0x41,0x44,0x49,0x41,0x4e], ".hdr", [:ImageMagick])
add_format(format"ICO", UInt8[0x00,0x00,0x01,0x00], ".ico", [:ImageMagick])
add_format(format"INFO", UInt8[0x7a,0x62,0x65,0x78], ".info",[:ImageMagick])
add_format(format"JP2", UInt8[0x00,0x00,0x00,0x0c,0x6a,0x50,0x20,0x20], ".jp2", [:ImageMagick])
add_format(format"PDB", UInt8[0x73,0x7a,0x65,0x7a], ".pdb", [:ImageMagick])
add_format(format"PDF", UInt8[0x25,0x50,0x44,0x46], ".pdf", [:ImageMagick])
add_format(format"PDF", UInt8[0x25,0x50,0x44,0x46], ".pdf", [:ImageMagick], [:FileIO, SAVE])
add_format(format"PGM", UInt8[0x50,0x35,0x0a], ".pgm", [:ImageMagick])
add_format(format"PSD", UInt8[0x38,0x42,0x50,0x53], ".psd", [:ImageMagick])
add_format(format"RGB", UInt8[0x01,0xda,0x01,0x01,0x00,0x03], ".rgb", [:ImageMagick])
Expand All @@ -76,7 +76,8 @@ add_format(
UInt8[0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a],
".png",
[:QuartzImageIO, OSX],
[:ImageMagick]
[:ImageMagick],
[:FileIO, SAVE]
)
add_format(
format"TIFF",
Expand Down Expand Up @@ -105,6 +106,12 @@ add_format(
".pcx",
[:ImageMagick]
)
add_format(
format"SVG",
(),
".svg",
[:FileIO, SAVE]
)

#=
add_format(format"NPY", UInt8[0x93, 'N', 'U', 'M', 'P', 'Y'], ".npy")
Expand Down
950 changes: 950 additions & 0 deletions test/files/mimesavetest.eps

Large diffs are not rendered by default.

Binary file added test/files/mimesavetest.pdf
Binary file not shown.
Binary file added test/files/mimesavetest.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions test/files/mimesavetest.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using FileIO
using Base.Test

immutable MimeSaveTestType
end

@testset "FileIO" begin
include("query.jl")
include("loadsave.jl")
include("error_handling.jl")
include("test_mimesave.jl")
end
45 changes: 45 additions & 0 deletions test/test_mimesave.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using FileIO
using Base.Test

@testset "Mime save" begin

function Base.show(io::IO, m::MIME"image/svg+xml", data::MimeSaveTestType)
content = read(joinpath(@__DIR__, "files", "mimesavetest.svg"))
write(io, content)
end

function Base.show(io::IO, m::MIME"application/pdf", data::MimeSaveTestType)
content = read(joinpath(@__DIR__, "files", "mimesavetest.pdf"))
write(io, content)
end

function Base.show(io::IO, m::MIME"application/eps", data::MimeSaveTestType)
content = read(joinpath(@__DIR__, "files", "mimesavetest.eps"))
write(io, content)
end

function Base.show(io::IO, m::MIME"image/png", data::MimeSaveTestType)
content = read(joinpath(@__DIR__, "files", "mimesavetest.png"))
write(io, content)
end

data = MimeSaveTestType()

output_filename = tempname()

for filetype in [".svg", ".pdf", ".eps", ".png"]

try
save(output_filename * filetype, data)

content_original = read(joinpath(@__DIR__, "files", "mimesavetest$filetype"))
content_new = read(output_filename * filetype)

@test content_new == content_original
finally
rm(output_filename * filetype)
end

end

end

0 comments on commit 8919166

Please sign in to comment.