Skip to content

Commit 658e4b6

Browse files
Remove ImageMagick bug workaround (#70)
* Remove ImageMagick/FileIO load-png-from-buffer bug workaround, following [ImageMagick#180](JuliaIO/ImageMagick.jl#180) Test with old version of FilesIO add ImageIO Update Project.toml Co-authored-by: Johnny Chen <[email protected]> * Fix deprecation warnings * add workaround for FileIO 1.6 deprecations suggeted by @johnnychen94 Co-authored-by: Johnny Chen <[email protected]>
1 parent 3e1041b commit 658e4b6

File tree

8 files changed

+283
-293
lines changed

8 files changed

+283
-293
lines changed

Project.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
1515

1616
[compat]
1717
ColorTypes = "0.7, 0.8, 0.9, 0.10"
18-
FileIO = "1"
18+
FileIO = "1.2.3"
1919
FixedPointNumbers = "0.6, 0.7, 0.8"
2020
ImageCore = "0.7, 0.8, 0.9"
2121
ProtoBuf = "0.10, 0.11"
@@ -24,6 +24,7 @@ StatsBase = "0.27, 0.28, 0.29, 0.30, 0.31, 0.32, 0.33"
2424
julia = "1.3"
2525

2626
[extras]
27+
ImageIO = "82e4d734-157c-48bb-816b-45c225c6df19"
2728
ImageMagick = "6218d12a-5da1-5696-b52f-db25d2ecc6d1"
2829
LightGraphs = "093fc24a-ae57-5d10-9952-331d41423f4d"
2930
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
@@ -38,4 +39,4 @@ ValueHistories = "98cad3c8-aec3-5f06-8e41-884608649ab7"
3839
WAV = "8149f6b0-98f6-5db9-b78f-408fbbb8ef88"
3940

4041
[targets]
41-
test = ["Test", "MLDatasets", "TestImages", "ImageMagick", "Logging", "LightGraphs", "Plots", "PyPlot", "WAV", "Tracker", "ValueHistories", "QuartzImageIO"]
42+
test = ["Test", "MLDatasets", "TestImages", "ImageMagick", "Logging", "LightGraphs", "Plots", "PyPlot", "WAV", "Tracker", "ValueHistories", "QuartzImageIO", "ImageIO"]

src/Deserialization/audio.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ function deserialize_audio_summary(summary)
22
audio = summary.audio
33

44
if audio.content_type == "audio/wav"
5-
value = load(Stream(format"WAV", PipeBuffer(audio.encoded_audio_string)))
5+
value = load(_format_stream(format"WAV", PipeBuffer(audio.encoded_audio_string)))
66
else
77
value = nothing
88
@warn "unknown audio format $(audio.content_type)"

src/Deserialization/images.jl

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function deserialize_image_summary(summary)
22
img = summary.image
33

4-
value = _deserialize_png(img.encoded_image_string)
4+
value = load(_format_stream(format"PNG", IOBuffer(img.encoded_image_string)))
55

66
return value
77
end
@@ -37,34 +37,3 @@ function lookahead_deserialize_image_summary(old_tag, old_val, evs::Summary,
3737

3838
return result
3939
end
40-
41-
42-
# Ugly-ugly hack to work around
43-
# https://github.com/JuliaIO/FileIO.jl/issues/174
44-
# Essentially, ImageMagick.load() cannot handle an in-memory stream
45-
# but only files. Instead, ImageMagick.readblob() should be used.
46-
# On MacOs it's not a problem thanks to QuartzImageIO
47-
if !Sys.isapple()
48-
did_patch_imagemagick = false
49-
50-
function _deserialize_png(data)
51-
if !did_patch_imagemagick
52-
global did_patch_imagemagick = true
53-
54-
# First load ImageMagick
55-
FileIO.checked_import(:ImageMagick)
56-
end
57-
# attempt to mimick ImageMagick type reconstruction
58-
attributes = PNGImage.read_info(data)
59-
T = PNGImage.png_color_T(attributes)
60-
61-
# This errors becase we are loading ImageMagick after defining func.
62-
# data = FileIO.ImageMagick.readblob(data)
63-
# ERROR: MethodError: no method matching readblob(::Array{UInt8,1})
64-
# The applicable method may be too new
65-
_data = Base.invokelatest(FileIO.ImageMagick.readblob, data)
66-
return reinterpret(T, _data)
67-
end
68-
else
69-
_deserialize_png(data) = load(Stream(format"PNG", IOBuffer(data)))
70-
end

src/FileIO_workaround.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using FileIO: FileIO
2+
3+
# Required for compatibility with pre 1.6 and post 1.6
4+
# drop in the future if the minimum requirement is bumped
5+
if isdefined(FileIO, :action)
6+
# FileIO >= 1.6
7+
_format_stream(format, io) = FileIO.Stream{format}(io)
8+
else
9+
_format_stream(format, io) = FileIO.Stream(format, io)
10+
end

src/Loggers/LogAudio.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function audio_summary(name::AbstractString, samples::AbstractArray, samplerate:
3030
samples = samples./max(maximum(samples), 1)
3131
samples = Int16.(floor.(samples.*32767))
3232
io = IOBuffer()
33-
save(Stream(format"WAV", io), samples)
33+
save(_format_stream(format"WAV",io), samples)
3434
eas = io.data
3535
audio = Summary_Audio(sample_rate = samplerate, num_channels = ndims(samples), length_frames = size(samples, 1), encoded_audio_string = eas, content_type = "audio/wav")
3636
Summary_Value(tag=name, audio=audio)

src/PNG.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ using FixedPointNumbers: Normed
77

88
export PngImage, png_color_T
99

10+
# workaround for FileIO pre 1.6
11+
include("FileIO_workaround.jl")
12+
1013
"""
1114
PngImage
1215
@@ -83,7 +86,7 @@ end
8386

8487
function Base.convert(::Type{PngImage}, img::AbstractArray{<:Colorant})
8588
pb = PipeBuffer()
86-
save(Stream{format"PNG"}(pb), img)
89+
save(_format_stream(format"PNG", pb), img)
8790
return PngImage(pb)
8891
end
8992

src/TensorBoardLogger.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ export tb_multiline, tb_margin
3232
# Wrapper types
3333
export TBText, TBVector, TBHistogram, TBImage, TBImages, TBAudio, TBAudios
3434

35+
# workaround for FileIO pre 1.6
36+
include("FileIO_workaround.jl")
37+
3538
# Protobuffer definitions for tensorboard
3639
include("protojl/tensorboard/tensorboard.jl")
3740
using .tensorboard: Summary_Value, GraphDef, Summary, Event, SessionLog_SessionStatus, SessionLog

0 commit comments

Comments
 (0)