|
| 1 | +""" |
| 2 | + jpeg_decode([T,] filename::AbstractString; kwargs...) -> Matrix{T} |
| 3 | +
|
| 4 | +Decode the JPEG image from given I/O stream as colorant matrix. |
| 5 | +
|
| 6 | +# parameters |
| 7 | +
|
| 8 | +- `transpose::Bool`: whether we need to permute the image's width and height dimension |
| 9 | + before encoding. The default value is `false`. |
| 10 | +- `scale_ratio::Real`: scale the image by ratio `scale_ratio` in `M/8` with `M ∈ 1:16`. The |
| 11 | + default value is `1`. For values are not in the range, they will be mapped to the nearest |
| 12 | + value, e.g., `0.3 => 2/8` and `0.35 => 3/8`. |
| 13 | +
|
| 14 | +# Examples |
| 15 | +
|
| 16 | +```jldoctest |
| 17 | +julia> using JpegTurbo, TestImages, ImageCore |
| 18 | +
|
| 19 | +julia> filename = testimage("earth", download_only=true); |
| 20 | +
|
| 21 | +julia> img = jpeg_decode(filename); summary(img) |
| 22 | +"3002×3000 Array{RGB{N0f8},2} with eltype RGB{N0f8}" |
| 23 | +
|
| 24 | +julia> img = jpeg_decode(Gray, filename; scale_ratio=0.25); summary(img) |
| 25 | +"751×750 Array{Gray{N0f8},2} with eltype Gray{N0f8}" |
| 26 | +``` |
| 27 | +
|
| 28 | +For image preview and similar purposes, `T` and `scale_ratio` are useful parameters to |
| 29 | +accelerate the JPEG decoding process. For color JPEG image, `jpeg_decode(Gray, filename)` is |
| 30 | +faster than `jpeg_decode(filename)` since the color components need not be processed. |
| 31 | +Smaller `scale_ratio` permits significantly faster decoding since fewer pixels need be |
| 32 | +processed and a simpler IDCT method can be used. |
| 33 | +
|
| 34 | +```julia |
| 35 | +using BenchmarkTools, TestImages, JpegTurbo |
| 36 | +filename = testimage("earth", download_only=true) |
| 37 | +# full decompression |
| 38 | +@btime jpeg_decode(filename); # 224.760 ms (7 allocations: 51.54 MiB) |
| 39 | +# only decompress luminance component |
| 40 | +@btime jpeg_decode(Gray, filename); # 91.157 ms (6 allocations: 17.18 MiB) |
| 41 | +# process only a few pixels |
| 42 | +@btime jpeg_decode(filename; scale_ratio=0.25); # 77.254 ms (8 allocations: 3.23 MiB) |
| 43 | +# process only a few pixels for luminance component |
| 44 | +@btime jpeg_decode(Gray, filename; scale_ratio=0.25); # 63.119 ms (6 allocations: 1.08 MiB) |
| 45 | +``` |
| 46 | +
|
| 47 | +""" |
| 48 | +function jpeg_decode( |
| 49 | + ::Type{CT}, |
| 50 | + filename::AbstractString; |
| 51 | + transpose=false, |
| 52 | + scale_ratio=1) where CT<:Colorant |
| 53 | + infile = ccall(:fopen, Libc.FILE, (Cstring, Cstring), filename, "rb") |
| 54 | + @assert infile.ptr != C_NULL |
| 55 | + out_CT, jpeg_cls = _jpeg_out_color_space(CT) |
| 56 | + |
| 57 | + local cinfo, out |
| 58 | + try |
| 59 | + cinfo = LibJpeg.jpeg_decompress_struct() |
| 60 | + cinfo_ref = Ref(cinfo) |
| 61 | + jerr = Ref{LibJpeg.jpeg_error_mgr}() |
| 62 | + cinfo.err = LibJpeg.jpeg_std_error(jerr) |
| 63 | + LibJpeg.jpeg_create_decompress(cinfo_ref) |
| 64 | + LibJpeg.jpeg_stdio_src(cinfo_ref, infile) |
| 65 | + LibJpeg.jpeg_read_header(cinfo_ref, true) |
| 66 | + |
| 67 | + # set decompression parameters, if given |
| 68 | + r = _cal_scale_ratio(scale_ratio) |
| 69 | + cinfo.scale_num, cinfo.scale_denom = r.num, r.den |
| 70 | + cinfo.out_color_space = jpeg_cls |
| 71 | + |
| 72 | + # eagerly calculate dimension information so that `output_XXX` fields are valid. |
| 73 | + LibJpeg.jpeg_calc_output_dimensions(cinfo_ref) |
| 74 | + out_size = (Int(cinfo.output_width), Int(cinfo.output_height)) |
| 75 | + if !all(x -> x<=65535, out_size) |
| 76 | + error("Suspicious inferred image size $out_size: each dimension is expected to have at most 65535 pixels.") |
| 77 | + end |
| 78 | + out_ndims = Int(cinfo.output_components) |
| 79 | + @assert out_ndims == length(out_CT) "Suspicous output color space: $cinfo.out_color_space" |
| 80 | + |
| 81 | + out = Matrix{out_CT}(undef, out_size) |
| 82 | + _jpeg_decode!(out, cinfo) |
| 83 | + finally |
| 84 | + LibJpeg.jpeg_destroy_decompress(Ref(cinfo)) |
| 85 | + ccall(:fclose, Cint, (Ptr{Libc.FILE},), infile) |
| 86 | + 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 |
| 93 | +end |
| 94 | +function jpeg_decode(filename::AbstractString; kwargs...) |
| 95 | + return jpeg_decode(_default_out_color_space(filename), filename; kwargs...) |
| 96 | +end |
| 97 | + |
| 98 | +function _jpeg_decode!(out::Matrix{<:Colorant}, cinfo::LibJpeg.jpeg_decompress_struct) |
| 99 | + cinfo_ref = Ref(cinfo) |
| 100 | + |
| 101 | + row_stride = size(out, 1) * length(eltype(out)) |
| 102 | + buf = Vector{UInt8}(undef, row_stride) |
| 103 | + out_uint8 = reinterpret(UInt8, out) |
| 104 | + |
| 105 | + LibJpeg.jpeg_start_decompress(cinfo_ref) |
| 106 | + while cinfo.output_scanline < cinfo.output_height |
| 107 | + # 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) |
| 109 | + copyto!(out_uint8, (cinfo.output_scanline-1) * row_stride + 1, buf, 1, row_stride) |
| 110 | + end |
| 111 | + LibJpeg.jpeg_finish_decompress(cinfo_ref) |
| 112 | + |
| 113 | + return out |
| 114 | +end |
| 115 | + |
| 116 | +# libjpeg-turbo only supports ratio M/8 with M from 1 to 16 |
| 117 | +const _allowed_scale_ratios = ntuple(i->i//8, 16) |
| 118 | +_cal_scale_ratio(r::Real) = _allowed_scale_ratios[findmin(x->abs(x-r), _allowed_scale_ratios)[2]] |
| 119 | + |
| 120 | +function _default_out_color_space(filename::AbstractString) |
| 121 | + infile = ccall(:fopen, Libc.FILE, (Cstring, Cstring), filename, "rb") |
| 122 | + @assert infile.ptr != C_NULL |
| 123 | + local cinfo |
| 124 | + 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) |
| 129 | + LibJpeg.jpeg_create_decompress(cinfo_ref) |
| 130 | + LibJpeg.jpeg_stdio_src(cinfo_ref, infile) |
| 131 | + LibJpeg.jpeg_read_header(cinfo_ref, true) |
| 132 | + LibJpeg.jpeg_calc_output_dimensions(cinfo_ref) |
| 133 | + return jpeg_color_space(cinfo.out_color_space) |
| 134 | + finally |
| 135 | + LibJpeg.jpeg_destroy_decompress(Ref(cinfo)) |
| 136 | + ccall(:fclose, Cint, (Ptr{Libc.FILE},), infile) |
| 137 | + end |
| 138 | +end |
| 139 | + |
| 140 | +function _jpeg_out_color_space(::Type{CT}) where CT |
| 141 | + try |
| 142 | + n0f8(CT), jpeg_color_space(n0f8(CT)) |
| 143 | + catch e |
| 144 | + @debug "Unsupported libjpeg-turbo color space, fallback to RGB{N0f8}" e |
| 145 | + RGB{N0f8}, jpeg_color_space(RGB{N0f8}) |
| 146 | + end |
| 147 | +end |
0 commit comments