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

use generic interface instead of accessing fields #19

Merged
merged 2 commits into from
Aug 20, 2020
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ Manifest.toml
/test/temp
/test/_debugfile.jl
*.png
test/_debugfile.jmd
36 changes: 20 additions & 16 deletions src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,24 @@ function load(fpath::String; gamma::Union{Nothing,Float64}=nothing, expand_palet
close_png(fp)
return out
end

maybe_lock(f, io::IO) = lock(f, io)
# IOStream doesn't support locking...
maybe_lock(f, io::IOStream) = f()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the report in JuliaImages/Images.jl#912 (comment)

It looks like IOBuffer should also be included as a special case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably? I guess you could add that to #21


function load(s::IO; gamma::Union{Nothing,Float64}=nothing, expand_paletted::Bool=false)
isreadable(s) || throw(ArgumentError("read failed, IOStream is not readable"))
Base.eof(s) && throw(EOFError())

png_ptr = create_read_struct()
info_ptr = create_info_struct(png_ptr)

lock(s.lock)
png_set_read_fn(png_ptr, s.handle, readcallback_c[])
# https://stackoverflow.com/questions/22564718/libpng-error-png-unsigned-integer-out-of-range
png_set_sig_bytes(png_ptr, 0)
out = _load(png_ptr, info_ptr, gamma=gamma, expand_paletted=expand_paletted)
unlock(s.lock)
return out

maybe_lock(s) do
png_set_read_fn(png_ptr, s.handle, readcallback_c[])
# https://stackoverflow.com/questions/22564718/libpng-error-png-unsigned-integer-out-of-range
png_set_sig_bytes(png_ptr, 0)
return _load(png_ptr, info_ptr, gamma=gamma, expand_paletted=expand_paletted)
end
end

function _readcallback(png_ptr::png_structp, data::png_bytep, length::png_size_t)::Cvoid
Expand Down Expand Up @@ -290,16 +294,16 @@ function save(

png_ptr = create_write_struct()
info_ptr = create_info_struct(png_ptr)
lock(s.lock)
png_set_write_fn(png_ptr, s.handle, writecallback_c[], C_NULL)
maybe_lock(s) do
png_set_write_fn(png_ptr, s.handle, writecallback_c[], C_NULL)

_save(png_ptr, info_ptr, image,
compression_level=compression_level,
compression_strategy=compression_strategy,
filters=filters,
palette=palette)
_save(png_ptr, info_ptr, image,
compression_level=compression_level,
compression_strategy=compression_strategy,
filters=filters,
palette=palette)

unlock(s.lock)
end
end

function _save(png_ptr, info_ptr, image::S;
Expand Down