Skip to content

Commit 5798d88

Browse files
Gnimucjohnnychen94
authored andcommitted
cleanup: a few Ref tweaks on jpeg_decode
1 parent deaa9a1 commit 5798d88

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

src/decode.jl

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,10 @@ function jpeg_decode(
5454
@assert infile.ptr != C_NULL
5555
out_CT, jpeg_cls = _jpeg_out_color_space(CT)
5656

57-
local cinfo, out
57+
cinfo_ref = Ref(LibJpeg.jpeg_decompress_struct())
58+
jerr = Ref{LibJpeg.jpeg_error_mgr}()
5859
try
59-
cinfo = LibJpeg.jpeg_decompress_struct()
60-
cinfo_ref = Ref(cinfo)
61-
jerr = Ref{LibJpeg.jpeg_error_mgr}()
60+
cinfo = cinfo_ref[]
6261
cinfo.err = LibJpeg.jpeg_std_error(jerr)
6362
LibJpeg.jpeg_create_decompress(cinfo_ref)
6463
LibJpeg.jpeg_stdio_src(cinfo_ref, infile)
@@ -79,60 +78,61 @@ function jpeg_decode(
7978
@assert out_ndims == length(out_CT) "Suspicous output color space: $cinfo.out_color_space"
8079

8180
out = Matrix{out_CT}(undef, out_size)
82-
_jpeg_decode!(out, cinfo)
81+
_jpeg_decode!(out, cinfo_ref)
82+
83+
if out_CT <: CT
84+
return transpose ? out : permutedims(out, (2, 1))
85+
else
86+
return transpose ? CT.(out) : CT.(PermutedDimsArray(out, (2, 1)))
87+
end
8388
finally
84-
LibJpeg.jpeg_destroy_decompress(Ref(cinfo))
89+
LibJpeg.jpeg_destroy_decompress(cinfo_ref)
8590
ccall(:fclose, Cint, (Ptr{Libc.FILE},), infile)
8691
end
87-
88-
if out_CT <: CT
89-
return transpose ? out : permutedims(out, (2, 1))
90-
else
91-
return transpose ? CT.(out) : CT.(PermutedDimsArray(out, (2, 1)))
92-
end
9392
end
9493
function jpeg_decode(filename::AbstractString; kwargs...)
9594
return jpeg_decode(_default_out_color_space(filename), filename; kwargs...)
9695
end
9796

98-
function _jpeg_decode!(out::Matrix{<:Colorant}, cinfo::LibJpeg.jpeg_decompress_struct)
99-
cinfo_ref = Ref(cinfo)
100-
97+
function _jpeg_decode!(out::Matrix{<:Colorant}, cinfo_ref::Ref{LibJpeg.jpeg_decompress_struct})
10198
row_stride = size(out, 1) * length(eltype(out))
10299
buf = Vector{UInt8}(undef, row_stride)
100+
buf_ref = Ref(pointer(buf))
103101
out_uint8 = reinterpret(UInt8, out)
104102

103+
cinfo = cinfo_ref[]
105104
LibJpeg.jpeg_start_decompress(cinfo_ref)
106105
while cinfo.output_scanline < cinfo.output_height
107106
# TODO(johnnychen94): try if we can directly write to `out` without using `buf`
108-
GC.@preserve buf LibJpeg.jpeg_read_scanlines(cinfo_ref, Ref(pointer(buf)), 1)
107+
GC.@preserve buf LibJpeg.jpeg_read_scanlines(cinfo_ref, buf_ref, 1)
109108
copyto!(out_uint8, (cinfo.output_scanline-1) * row_stride + 1, buf, 1, row_stride)
110109
end
111110
LibJpeg.jpeg_finish_decompress(cinfo_ref)
112111

113112
return out
114113
end
115114

115+
116+
_jpeg_decode!(out::Matrix{<:Colorant}, cinfo::LibJpeg.jpeg_decompress_struct) = _jpeg_decode!(out, Ref(cinfo))
117+
116118
# libjpeg-turbo only supports ratio M/8 with M from 1 to 16
117119
const _allowed_scale_ratios = ntuple(i->i//8, 16)
118120
_cal_scale_ratio(r::Real) = _allowed_scale_ratios[findmin(x->abs(x-r), _allowed_scale_ratios)[2]]
119121

120122
function _default_out_color_space(filename::AbstractString)
121123
infile = ccall(:fopen, Libc.FILE, (Cstring, Cstring), filename, "rb")
122124
@assert infile.ptr != C_NULL
123-
local cinfo
125+
cinfo_ref = Ref(LibJpeg.jpeg_decompress_struct())
126+
jerr = Ref{LibJpeg.jpeg_error_mgr}()
124127
try
125-
cinfo = LibJpeg.jpeg_decompress_struct()
126-
cinfo_ref = Ref(cinfo)
127-
jerr = Ref{LibJpeg.jpeg_error_mgr}()
128-
cinfo.err = LibJpeg.jpeg_std_error(jerr)
128+
cinfo_ref[].err = LibJpeg.jpeg_std_error(jerr)
129129
LibJpeg.jpeg_create_decompress(cinfo_ref)
130130
LibJpeg.jpeg_stdio_src(cinfo_ref, infile)
131131
LibJpeg.jpeg_read_header(cinfo_ref, true)
132132
LibJpeg.jpeg_calc_output_dimensions(cinfo_ref)
133-
return jpeg_color_space(cinfo.out_color_space)
133+
return jpeg_color_space(cinfo_ref[].out_color_space)
134134
finally
135-
LibJpeg.jpeg_destroy_decompress(Ref(cinfo))
135+
LibJpeg.jpeg_destroy_decompress(cinfo_ref)
136136
ccall(:fclose, Cint, (Ptr{Libc.FILE},), infile)
137137
end
138138
end

0 commit comments

Comments
 (0)