Skip to content

Commit

Permalink
Merge branch 'master' into fix_path_encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
ViralBShah authored Oct 16, 2024
2 parents 0f95f31 + 26747c6 commit 213015d
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 16 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ jobs:
version:
- '1.6'
- '1'
# - 'nightly'
os:
- ubuntu-latest
- macOS-latest
- windows-latest
arch:
- x64
include:
- os: macOS-latest
arch: aarch64
version: '1'
steps:
- uses: actions/checkout@v4
- uses: julia-actions/setup-julia@v1
- uses: julia-actions/setup-julia@v2
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/CompatHelper.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
run: which julia
continue-on-error: true
- name: Install Julia, but only if it is not already available in the PATH
uses: julia-actions/setup-julia@v1
uses: julia-actions/setup-julia@v2
with:
version: '1'
arch: ${{ runner.arch }}
Expand Down
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ImageMagick"
uuid = "6218d12a-5da1-5696-b52f-db25d2ecc6d1"
version = "1.3.0"
version = "1.4.0"

[deps]
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
Expand All @@ -11,7 +11,7 @@ InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
[compat]
FileIO = "1"
ImageCore = "0.9, 0.10"
ImageMagick_jll = "= 6.9.11"
ImageMagick_jll = "= 7.1.1"
julia = "1.6"

[extras]
Expand Down
7 changes: 4 additions & 3 deletions src/ImageMagick.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ const image_formats = [
format"TIFF",
format"WMF",
format"WPG",
format"TGA"
format"TGA",
format"DCM"
]

"""
Expand Down Expand Up @@ -81,11 +82,11 @@ function _metadata(wand)
evendepth = ((depth+1)>>1)<<1
if depth <= 8
if cs == "Gray"
cdepth = getimagechanneldepth(wand, GrayChannel)
cdepth = getimagedepth(wand)
if n > 1
for k = 1:n # while it might seem that this should be 2:n, that doesn't work...
nextimage(wand)
cdepth = max(cdepth, getimagechanneldepth(wand, GrayChannel))
cdepth = max(cdepth, getimagedepth(wand))
end
resetiterator(wand)
end
Expand Down
97 changes: 91 additions & 6 deletions src/libmagickwand.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,87 @@ const IMTypedict = Dict([(IMType[i], i) for i = 1:length(IMType)])
const CStoIMTypedict = Dict("Gray" => "GrayscaleType", "GrayA" => "GrayscaleMatteType", "AGray" => "GrayscaleMatteType", "RGB" => "TrueColorType", "ARGB" => "TrueColorMatteType", "RGBA" => "TrueColorMatteType", "CMYK" => "ColorSeparationType", "I"=>"GrayscaleType", "IA"=>"GrayscaleMatteType", "AI"=>"GrayscaleMatteType", "BGRA"=>"TrueColorMatteType", "ABGR"=>"TrueColorMatteType")

# Colorspace
const IMColorspace = ["RGB", "Gray", "Transparent", "OHTA", "Lab", "XYZ", "YCbCr", "YCC", "YIQ", "YPbPr", "YUV", "CMYK", "sRGB"]
# The colorspace order comes from the C struct definition
# https://github.com/ImageMagick/ImageMagick/blob/e96022d5f377f2a4c0780be9f60ed44535dc5488/MagickCore/colorspace.h#L67
# It starts at 0 with UndefinedColorspace, which is not used in the Julia interface
const IMColorspace = [
"CMY"
"CMYK"
"Gray"
"HCL"
"HCLp"
"HSB"
"HSI"
"HSL"
"HSV"
"HWB"
"Lab"
"LCH"
"LCHab"
"LCHuv"
"Log"
"LMS"
"Luv"
"OHTA"
"Rec601YCbCr"
"Rec709YCbCr"
"RGB"
"scRGB"
"sRGB"
"Transparent"
"xyY"
"XYZ"
"YCbCr"
"YCC"
"YDbDr"
"YIQ"
"YPbPr"
"YUV"
"LinearGRAY"
"Jzazbz"
"DisplayP3"
"Adobe98"
"ProPhoto"
"Oklab"
"Oklch"
]
const IMColordict = Dict([(IMColorspace[i], i) for i = 1:length(IMColorspace)])
for AC in vcat(subtypes(AlphaColor), subtypes(ColorAlpha))

# See the discussion of https://github.com/JuliaIO/ImageMagick.jl/issues/235
# This can be removed once the `isbindingresolved` function is checked in InteractiveUtils.jl: subtypes
# So if it starts to cause problems in future version of Julia, it can be
# removed without any issues
# This was copied from the Julia source code, and is used to avoid deprecation warnings
# caused by deprecations on RGB1 and RGB4 in ColorTypes.jl
function _subtypes_and_avoid_deprecation(x::Type)
mods = Base.loaded_modules_array()
xt = Base.unwrap_unionall(x)
if !isabstracttype(x) || !isa(xt, DataType)
# Fast path
return Type[]
end
sts = Vector{Any}()
while !isempty(mods)
m = pop!(mods)
xt = xt::DataType
for s in names(m, all = true)
if Base.isbindingresolved(m, s) && !Base.isdeprecated(m, s) && isdefined(m, s)
t = getfield(m, s)
dt = isa(t, UnionAll) ? Base.unwrap_unionall(t) : t
if isa(dt, DataType)
if dt.name.name === s && dt.name.module == m && supertype(dt).name == xt.name
ti = typeintersect(t, x)
ti != Base.Bottom && push!(sts, ti)
end
elseif isa(t, Module) && nameof(t) === s && parentmodule(t) === m && t !== m
t === Base || push!(mods, t) # exclude Base, since it also parented by Main
end
end
end
end
return permute!(sts, sortperm(map(string, sts)))
end
for AC in vcat(_subtypes_and_avoid_deprecation(AlphaColor), _subtypes_and_avoid_deprecation(ColorAlpha))
Cstr = ColorTypes.colorant_string(color_type(AC))
if haskey(IMColordict, Cstr)
IMColordict[ColorTypes.colorant_string(AC)] = IMColordict[Cstr]
Expand Down Expand Up @@ -132,6 +210,11 @@ mutable struct MagickWand
ptr = ccall((:NewMagickWand, libwand), Ptr{Cvoid}, ())
ptr == C_NULL && throw(OutOfMemoryError())
obj = new(ptr)

# Set the list of TIFF tags to ignore
ccall((:MagickSetOption, libwand), Cint, (Ptr{Cvoid}, Ptr{UInt8}, Ptr{UInt8}), obj, "tiff:ignore-tags",
"32934,34016,34017,34018,34019,34020,34021,34022,34023,34024,34025,34026,34027,34028,34029,34030,34031")

finalizer(free, obj)
obj
end
Expand Down Expand Up @@ -299,7 +382,12 @@ function readimage(wand::MagickWand, stream::IO)
nothing
end

function readimage(wand::MagickWand, stream::Vector{UInt8})
if VERSION >= v"1.11"
StreamTypes = Union{Vector{UInt8}, Memory{UInt8}}
else
StreamTypes = Vector{UInt8}
end
function readimage(wand::MagickWand, stream::StreamTypes)
status = ccall((:MagickReadImageBlob, libwand), Cint, (Ptr{Cvoid}, Ptr{Cvoid}, Cint), wand, stream, length(stream)*sizeof(eltype(stream)))
status == 0 && error(wand)
nothing
Expand Down Expand Up @@ -449,9 +537,6 @@ end
# get the pixel depth
getimagedepth(wand::MagickWand) = convert(Int, ccall((:MagickGetImageDepth, libwand), Csize_t, (Ptr{Cvoid},), wand))

# pixel depth for given channel type
getimagechanneldepth(wand::MagickWand, channelType::ChannelType) = convert(Int, ccall((:MagickGetImageChannelDepth, libwand), Csize_t, (Ptr{Cvoid}, UInt32), wand, channelType.value))

pixelsetcolor(wand::PixelWand, colorstr::String) = ccall((:PixelSetColor, libwand), Csize_t, (Ptr{Cvoid}, Ptr{UInt8}), wand, colorstr) == 0 && error(wand)

relinquishmemory(p) = ccall((:MagickRelinquishMemory, libwand), Ptr{UInt8}, (Ptr{UInt8},), p)
Expand Down
11 changes: 10 additions & 1 deletion test/constructed_images.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ mutable struct TestType end
ImageMagick.save(fn, a)
b = ImageMagick.load(fn)
@test eltype(b) == Gray{N0f8}
@test round.(b) == a # JPG is lossy
end

@testset "Gray png" begin
Expand Down Expand Up @@ -356,9 +357,17 @@ mutable struct TestType end
@test buf2view == Ar
end

@testset "issue #206" begin
@testset "issue #206 - TIFFFile" begin
filepath = testimage("camera", download_only=true)
img = ImageMagick.load(filepath)
@test size(img) == (512, 512)
end

@testset "Issue #206-PNG File" begin
A = Gray{N0f8}.(reshape(collect(0.0:0.01:0.49), 5, 10))
fn = joinpath(workdir, "temp.png")
ImageMagick.save(fn, A)
B = ImageMagick.load(fn)
@test A == B
end
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ include("constructed_images.jl")
include("readremote.jl")
include("badimages.jl")
include("unicode.jl")
include("utilities.jl")

workdir = joinpath(tempdir(), "Images")
try
Expand Down
16 changes: 16 additions & 0 deletions test/utilities.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using InteractiveUtils: subtypes
@testset "Utilities" begin
@testset "subtypes" begin
println()
println("---------------------------------------------------------------------------")
println(" This next test may cause deprecation warnings.")
println(" Remove this note if it stops doing that and remove the function.")
println(" _subtypes_and_avoid_deprecation ")
println(" function from libmagickwand.jl.")
println("---------------------------------------------------------------------------")
println()
@test subtypes(Integer) == ImageMagick._subtypes_and_avoid_deprecation(Integer)
@test subtypes(ImageMagick.ColorAlpha) == ImageMagick._subtypes_and_avoid_deprecation(ImageMagick.ColorAlpha)
@test subtypes(ImageMagick.AlphaColor) == ImageMagick._subtypes_and_avoid_deprecation(ImageMagick.AlphaColor)
end
end

0 comments on commit 213015d

Please sign in to comment.