Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix path encoding problems on Windows #144

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions src/libmagickwand.jl
Original file line number Diff line number Diff line change
Expand Up @@ -264,19 +264,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
Expand All @@ -290,13 +308,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
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ using Base.CoreLogging: SimpleLogger, with_logger
include("constructed_images.jl")
include("readremote.jl")
include("badimages.jl")
include("unicode.jl")

workdir = joinpath(tempdir(), "Images")
try
Expand Down
51 changes: 51 additions & 0 deletions test/unicode.jl
Original file line number Diff line number Diff line change
@@ -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
Loading