Skip to content

Commit

Permalink
added some improvements from Images.jl master
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonDanisch committed Sep 24, 2015
1 parent 5f41b46 commit 0d5a9e4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 26 deletions.
23 changes: 14 additions & 9 deletions deps/build.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using BinDeps
using BinDeps, Compat

@BinDeps.setup

Expand All @@ -10,15 +10,17 @@ end
libnames = ["libMagickWand", "CORE_RL_wand_"]
suffixes = ["", "-Q16", "-6.Q16", "-Q8"]
options = ["", "HDRI"]
extensions = ["", ".so.4", ".so.5"]
extensions = ["", ".so.2", ".so.4", ".so.5"]
aliases = vec(libnames.*transpose(suffixes).*reshape(options,(1,1,length(options))).*reshape(extensions,(1,1,1,length(extensions))))
libwand = library_dependency("libwand", aliases = aliases)

@linux_only begin
provides(AptGet, "libmagickwand4", libwand)
provides(AptGet, "libmagickwand5", libwand)
provides(Pacman, "imagemagick", libwand)
provides(Yum, "ImageMagick", libwand)
kwargs = Any[(:onload, "ccall((:MagickWandGenesis,libwand), Void, ())")]
provides(AptGet, "libmagickwand4", libwand; kwargs...)
provides(AptGet, "libmagickwand5", libwand; kwargs...)
provides(AptGet, "libmagickwand-6.q16-2", libwand; kwargs...)
provides(Pacman, "imagemagick", libwand; kwargs...)
provides(Yum, "ImageMagick", libwand; kwargs...)
end

# TODO: remove me when upstream is fixed
Expand All @@ -32,7 +34,7 @@ end
magick_base = "http://www.imagemagick.org/download/binaries"
binariesfn = download(magick_base)
str = readall(binariesfn)
pattern = "ImageMagick-6.9.*-Q16-$(OS_ARCH)-dll.exe"
pattern = "ImageMagick-6.9.*?-Q16-$(OS_ARCH)-dll.exe"
m = match(Regex(pattern), str)
magick_exe = convert(ASCIIString, m.match)

Expand Down Expand Up @@ -61,7 +63,8 @@ end
"""
ENV["MAGICK_CONFIGURE_PATH"] = \"$(escape_string(magick_libdir))\"
ENV["MAGICK_CODER_MODULE_PATH"] = \"$(escape_string(magick_libdir))\"
""")
""",
onload = "ccall((:MagickWandGenesis,libwand), Void, ())")
end

@osx_only begin
Expand All @@ -75,6 +78,7 @@ end
ENV["MAGICK_CONFIGURE_PATH"] = joinpath("$(Homebrew.prefix("imagemagick"))","lib","ImageMagick","config-Q16")
ENV["MAGICK_CODER_MODULE_PATH"] = joinpath("$(Homebrew.prefix("imagemagick"))", "lib","ImageMagick","modules-Q16","coders")
ENV["PATH"] = joinpath("$(Homebrew.prefix("imagemagick"))", "bin") * ":" * ENV["PATH"]
ccall((:MagickWandGenesis,libwand), Void, ())
end
""" )
end
Expand All @@ -84,6 +88,7 @@ end
# Save the library version; by checking this now, we avoid a runtime dependency on libwand
# See https://github.com/timholy/Images.jl/issues/184#issuecomment-55643225
module CheckVersion
using Compat
include("deps.jl")
if isdefined(:__init__)
__init__()
Expand All @@ -93,4 +98,4 @@ vstr = string("v\"", join(split(bytestring(p), ',')[1:3], '.'), "\"")
open(joinpath(dirname(@__FILE__),"versioninfo.jl"), "w") do file
write(file, "const libversion = $vstr\n")
end
end
end
4 changes: 2 additions & 2 deletions src/ImageMagick.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ImageMagick

using FixedPointNumbers, ColorTypes, FileIO, Compat, Images
using FixedPointNumbers, ColorTypes, FileIO, Compat, Images, ColorVectorSpace

export MagickWand
export constituteimage
Expand Down Expand Up @@ -68,7 +68,7 @@ end
const ufixedtype = @compat Dict(10=>Ufixed10, 12=>Ufixed12, 14=>Ufixed14, 16=>Ufixed16)


function load_(file::Union{AbstractString,IO}, ImageType=Image)
function load_(file::@compat(Union{AbstractString,IO}), ImageType=Image)
wand = MagickWand()
readimage(wand, file)
resetiterator(wand)
Expand Down
44 changes: 29 additions & 15 deletions src/libmagickwand.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@

import Base: error, size

export MagickWand,
constituteimage,
exportimagepixels!,
getblob,
getimagealphachannel,
getimagecolorspace,
getimagedepth,
getnumberimages,
importimagepixels,
readimage,
resetiterator,
setimagecolorspace,
setimagecompression,
setimagecompressionquality,
setimageformat,
writeimage


# Find the library
depsfile = joinpath(dirname(@__FILE__),"..","deps","deps.jl")
Expand All @@ -12,14 +30,12 @@ end
if isfile(versionfile)
include(versionfile)
end

const have_imagemagick = isdefined(:libwand)

# Initialize the library
function init()
global libwand
if have_imagemagick
eval(:(ccall((:MagickWandGenesis, $libwand), Void, ())))
else
function __init__()
if !have_imagemagick
warn("ImageMagick utilities not found. Install for more file format support.")
end
end
Expand All @@ -32,7 +48,7 @@ const DOUBLEPIXEL = 2
const FLOATPIXEL = 3
const INTEGERPIXEL = 4
const SHORTPIXEL = 7
IMStorageTypes = Union{UInt8, UInt16, UInt32, Float32, Float64}
@compat IMStorageTypes = Union{UInt8, UInt16, UInt32, Float32, Float64}
storagetype(::Type{UInt8}) = CHARPIXEL
storagetype(::Type{UInt16}) = SHORTPIXEL
storagetype(::Type{UInt32}) = INTEGERPIXEL
Expand Down Expand Up @@ -71,7 +87,7 @@ const DefaultChannels = ChannelType( (AllChannels.value | SyncChannels.value) &~
const IMType = ["BilevelType", "GrayscaleType", "GrayscaleMatteType", "PaletteType", "PaletteMatteType", "TrueColorType", "TrueColorMatteType", "ColorSeparationType", "ColorSeparationMatteType", "OptimizeType", "PaletteBilevelMatteType"]
const IMTypedict = Dict([(IMType[i], i) for i = 1:length(IMType)])

const CStoIMTypedict = @compat Dict("Gray" => "GrayscaleType", "GrayAlpha" => "GrayscaleMatteType", "RGB" => "TrueColorType", "ARGB" => "TrueColorMatteType", "CMYK" => "ColorSeparationType")
const CStoIMTypedict = @compat Dict("Gray" => "GrayscaleType", "GrayA" => "GrayscaleMatteType", "RGB" => "TrueColorType", "ARGB" => "TrueColorMatteType", "CMYK" => "ColorSeparationType")

# Colorspace
const IMColorspace = ["RGB", "Gray", "Transparent", "OHTA", "Lab", "XYZ", "YCbCr", "YCC", "YIQ", "YPbPr", "YUV", "CMYK", "sRGB"]
Expand All @@ -81,7 +97,7 @@ function nchannels(imtype::AbstractString, cs::AbstractString, havealpha = false
n = 3
if startswith(imtype, "Grayscale") || startswith(imtype, "Bilevel")
n = 1
cs = havealpha ? "GrayAlpha" : "Gray"
cs = havealpha ? "GrayA" : "Gray"
elseif cs == "CMYK"
n = 4
else
Expand All @@ -90,7 +106,7 @@ function nchannels(imtype::AbstractString, cs::AbstractString, havealpha = false
n + havealpha, cs
end

# channelorder = ["Gray" => "I", "GrayAlpha" => "IA", "RGB" => "RGB", "ARGB" => "ARGB", "RGBA" => "RGBA", "CMYK" => "CMYK"]
# channelorder = ["Gray" => "I", "GrayA" => "IA", "RGB" => "RGB", "ARGB" => "ARGB", "RGBA" => "RGBA", "CMYK" => "CMYK"]

# Compression
const NoCompression = 1
Expand Down Expand Up @@ -140,25 +156,23 @@ function getsize(buffer, channelorder)
return size(buffer, 2), size(buffer, 3), size(buffer, 4)
end
end
getsize{C <: Colorant}(buffer::AbstractArray{C}, channelorder) = size(buffer, 1), size(buffer, 2), size(buffer, 3)
getsize{C<:Colorant}(buffer::AbstractArray{C}, channelorder) = size(buffer, 1), size(buffer, 2), size(buffer, 3)

colorsize(buffer, channelorder) = channelorder == "I" ? 1 : size(buffer, 1)
colorsize{C<:Colorant}(buffer::AbstractArray{C}, channelorder) = 1

bitdepth{C<:Colorant}(buffer::AbstractArray{C}) = 8*eltype(C)
bitdepth{T}(buffer::AbstractArray{T}) = 8*sizeof(T)

MagickExportImagePixels(wand, x, y, cols, rows, channelorder, T, p) = ccall((:MagickExportImagePixels, libwand), Cint, (Ptr{Void}, Cssize_t, Cssize_t, Csize_t, Csize_t, Ptr{UInt8}, Cint, Ptr{Void}), wand.ptr, x, y, cols, rows, channelorder, storagetype(T), p)

# colorspace is included for consistency with constituteimage, but it is not used
function exportimagepixels!{T}(buffer::AbstractArray{T}, wand::MagickWand, colorspace::ASCIIString, channelorder::ASCIIString; x = 0, y = 0)
cols, rows, nimages = getsize(buffer, channelorder)
ncolors = colorsize(buffer, channelorder)
p = pointer(buffer)
for i = 1:nimages
status = MagickExportImagePixels(wand, x, y, cols, rows, channelorder, T, p)
status == 0 && error(wand)
nextimage(wand)
status = ccall((:MagickExportImagePixels, libwand), Cint, (Ptr{Void}, Cssize_t, Cssize_t, Csize_t, Csize_t, Ptr{UInt8}, Cint, Ptr{Void}), wand.ptr, x, y, cols, rows, channelorder, storagetype(T), p)
status == 0 && error(wand)
p += sizeof(T)*cols*rows*ncolors
end
buffer
Expand Down Expand Up @@ -256,7 +270,7 @@ end

function getimageproperty(wand::MagickWand,prop::AbstractString)
p = ccall((:MagickGetImageProperty, libwand),Ptr{UInt8},(Ptr{Void},Ptr{UInt8}),wand.ptr,prop)
if p == C_NULL
if p == convert(Ptr{UInt8}, C_NULL)
possib = getimageproperties(wand,"*")
warn("Undefined property, possible names are \"$(join(possib,"\",\""))\"")
nothing
Expand Down

0 comments on commit 0d5a9e4

Please sign in to comment.