diff --git a/src/libmagickwand.jl b/src/libmagickwand.jl index 3039638..e9c8bd2 100644 --- a/src/libmagickwand.jl +++ b/src/libmagickwand.jl @@ -312,19 +312,37 @@ function getblob(wand::MagickWand, format::AbstractString) end function pingimage(wand::MagickWand, filename::AbstractString) + #Warning: filename is not UTF-8 on Windows status = ccall((:MagickPingImage, libwand), Cint, (Ptr{Cvoid}, Ptr{UInt8}), wand, filename) status == 0 && error(wand) nothing end -function readimage(wand::MagickWand, filename::AbstractString) - status = ccall((:MagickReadImage, libwand), Cint, (Ptr{Cvoid}, Ptr{UInt8}), wand, filename) +#MagickSetFilename for reading, MagickSetImageFilename for writing +function setfilename(wand::MagickWand, filename::AbstractString) + status = ccall((:MagickSetFilename, libwand), Cint, (Ptr{Cvoid}, Ptr{UInt8}), wand, filename) + status == 0 && error(wand) + nothing +end + +function setimagefilename(wand::MagickWand, filename::AbstractString) + status = ccall((:MagickSetImageFilename, libwand), Cint, (Ptr{Cvoid}, Ptr{UInt8}), wand, filename) status == 0 && error(wand) nothing end +function readimage(wand::MagickWand, filename::AbstractString) + open(filename, "r") do io + (_, ext) = splitext(filename) + setfilename(wand, "a"*ext) + readimage(wand, io) + end +end + function readimage(wand::MagickWand, stream::IO) - status = ccall((:MagickReadImageFile, libwand), Cint, (Ptr{Cvoid}, Ptr{Cvoid}), wand, Libc.FILE(stream).ptr) + file = Libc.FILE(stream) + status = ccall((:MagickReadImageFile, libwand), Cint, (Ptr{Cvoid}, Ptr{Cvoid}), wand, file.ptr) + close(file) status == 0 && error(wand) nothing end @@ -343,13 +361,18 @@ end readimage(wand::MagickWand, stream::IOBuffer) = readimage(wand, stream.data) function writeimage(wand::MagickWand, filename::AbstractString) - status = ccall((:MagickWriteImages, libwand), Cint, (Ptr{Cvoid}, Ptr{UInt8}, Cint), wand, filename, true) - status == 0 && error(wand) - nothing + open(filename, "w") do io + #Let IM infer the format when writing to a stream + (_, ext) = splitext(filename) + setimagefilename(wand, "a"*ext) + writeimage(wand, io) + end end function writeimage(wand::MagickWand, stream::IO) - status = ccall((:MagickWriteImagesFile, libwand), Cint, (Ptr{Cvoid}, Ptr{Cvoid}), wand, Libc.FILE(stream).ptr) + file = Libc.FILE(stream) + status = ccall((:MagickWriteImagesFile, libwand), Cint, (Ptr{Cvoid}, Ptr{Cvoid}), wand, file.ptr) + close(file) status == 0 && error(wand) nothing end diff --git a/test/unicode.jl b/test/unicode.jl new file mode 100644 index 0000000..57cebc8 --- /dev/null +++ b/test/unicode.jl @@ -0,0 +1,51 @@ +using ImageMagick, ColorTypes, FileIO +using Test + +cjkchars = "\xe4\xb8\xad\xe6\x96\x87\x20\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e\x20\xed\x95\x9c\xea\xb5\xad\xec\x96\xb4" + +workdir = joinpath(tempdir(), "Images") +cjkdir = joinpath(workdir, cjkchars) +if !isdir(workdir) + mkdir(workdir) +end +if !isdir(cjkdir) + mkdir(cjkdir) +end + +@testset "Unicode compatibility" begin + + @testset "Unicode path names" begin + img = [RGB(1, 0, 0) RGB(0, 1, 0);RGB(0, 0, 1) RGB(1, 1, 1)] + fn = joinpath(cjkdir, cjkchars * ".png") + x = nothing + open(fn, "w") do io + @test try + ImageMagick.save(Stream(format"PNG", io), img);true + catch + false + end + end + @test try + x = ImageMagick.load(fn);true + catch + false + end + @test x == img + @test try + ImageMagick.save(fn, img);true + catch + false + end + open(fn, "r") do io + @test try + x = ImageMagick.load(io);true + catch + false + end + @test x == img + end + end + +end + +nothing