JuliaIO / JpegTurbo.jl

Julia interface to libjpeg-turbo
MIT License
15 stars 7 forks source link

faster `jpeg_encode` for filename and IO #7

Open johnnychen94 opened 2 years ago

johnnychen94 commented 2 years ago

The current pipeline consists of two stages:

1) first allocate a C-side buffer in memory and stores the encoded bytes, and 2) unsafe_wrap the buffer in Julia side and use Julia’s IO operation to write to the output file

function jpeg_encode(filename::AbstractString, img; kwargs...)
    open(filename, "w") do io
        jpeg_encode(io, img; kwargs...)
    end
end
jpeg_encode(io::IO, img; kwargs...) = write(io, jpeg_encode(img; kwargs...))

If we can fuse two stages, then we can get a faster version.

For jpeg_encode(filename, img) specifically, it's also possible to wrap the C stdio Libc.FILE version.