Function references
JpegTurbo.jpeg_encode
— Functionjpeg_encode(filename::AbstractString, img; kwargs...) -> Int
jpeg_encode(io::IO, img; kwargs...) -> Int
jpeg_encode(img; kwargs...) -> Vector{UInt8}
Encode 2D image img
as JPEG byte sequences and write to given I/O stream or file. The return value is number of bytes. If output is not specified, the encoded result is stored in memory as return value.
Parameters
transpose::Bool
: whether we need to permute the image's width and height dimension before encoding. The default value isfalse
.quality::Int
: Constructs JPEG quantization tables appropriate for the indicated quality setting. The quality value is expressed on the 0..100 scale recommended by IJG. The default value is92
. Passquality=nothing
to let libjpeg-turbo dynamicly guess a value.
JPEG has a large number of compression parameters that determine how the image is encoded. Most applications don't need or want to know about all these parameters. For more detailed information and explaination, please refer to the "Compression parameter selection" in [1]. Unsupported custom parameters might cause Julia segmentation fault.
Examples
julia> using JpegTurbo, TestImages
julia> img = testimage("cameraman");
julia> jpeg_encode("out.jpg", img) # write to file
51396
julia> buf = jpeg_encode(img); length(buf) # directly write to memory
51396
References
JpegTurbo.jpeg_decode
— Functionjpeg_decode([T,] filename::AbstractString; kwargs...) -> Matrix{T}
jpeg_decode([T,] io::IO; kwargs...) -> Matrix{T}
jpeg_decode([T,] data::Vector{UInt8}; kwargs...) -> Matrix{T}
Decode the JPEG image as colorant matrix. The source data can be either a filename, an IO , or an in-memory byte sequence.
parameters
transpose::Bool
: whether we need to permute the image's width and height dimension before encoding. The default value isfalse
.scale_ratio::Real
: scale the image by ratioscale_ratio
inM/8
withM ∈ 1:16
. The default value is1
. For values are not in the range, they will be mapped to the nearest value, e.g.,0.3 => 2/8
and0.35 => 3/8
.scale_ratio
andpreferred_size
may not be used together.preferred_size::Tuple
: infer the minimalscale_ratio
thatall(size(out) .>= preferred_size))
holds. It can optionally be(op, preferred_size)
format, with compare operationop
be one of>
,>=
,<
or<=
. Ifop in (>=, >)
then it gets the minimalscale_ratio
, otherwise it gets the maximumscale_ratio
forop in (<=, <)
.scale_ratio
andpreferred_size
may not be used together. Thepreferred_size
dimensions are not affected by keywordtranspose
.
Examples
julia> using JpegTurbo, TestImages, ImageCore
julia> filename = testimage("earth", download_only=true);
julia> img = jpeg_decode(filename); summary(img)
"3002×3000 Array{RGB{N0f8},2} with eltype RGB{N0f8}"
julia> img = jpeg_decode(Gray, filename; scale_ratio=0.25); summary(img)
"751×750 Array{Gray{N0f8},2} with eltype Gray{N0f8}"
For image preview and similar purposes, T
and scale_ratio
are useful parameters to accelerate the JPEG decoding process. For color JPEG image, jpeg_decode(Gray, filename)
is faster than jpeg_decode(filename)
since the color components need not be processed. Smaller scale_ratio
permits significantly faster decoding since fewer pixels need be processed and a simpler IDCT method can be used.
using BenchmarkTools, TestImages, JpegTurbo
filename = testimage("earth", download_only=true)
# full decompression
@btime jpeg_decode(filename); # 224.760 ms (7 allocations: 51.54 MiB)
# only decompress luminance component
@btime jpeg_decode(Gray, filename); # 91.157 ms (6 allocations: 17.18 MiB)
# process only a few pixels
@btime jpeg_decode(filename; scale_ratio=0.25); # 77.254 ms (8 allocations: 3.23 MiB)
# process only a few pixels for luminance component
@btime jpeg_decode(Gray, filename; scale_ratio=0.25); # 63.119 ms (6 allocations: 1.08 MiB)
Utilities
JpegTurbo.versioninfo
— Functionversioninfo(io=stdout)
Print information about the libjpeg-turbo in use.