pixiv / go-libjpeg

An implementation of Go binding for libjpeg (or libjpeg-turbo).
BSD 3-Clause "New" or "Revised" License
156 stars 52 forks source link

Streaming image decoding via go channels #50

Open gmp216 opened 6 years ago

gmp216 commented 6 years ago

For interactive programs, it is useful to be able to decode large images in pieces in order to improve responsiveness. I have implemented a draft version of a "streaming" API which returns Subimages on a go channel if a channel is passed to the jpeg.Decode function. See here:

https://github.com/gmp216/go-libjpeg/tree/streaming

If no channel is provided, the Decode function should perform as before. This is useful but not fully implemented yet since rgb.Image does not implement the Subimage function that is available for most of the built-in image types (DecodeRGB and DecodeIntoRGB return Subimages with the wrong bounds).

Example:

fd,err := os.Open(filename)
ch := make(chan image.Image)
go func() {
    for im := range(ch) {
        ...
    }
}()
i,err := jpeg.Decode(fd,&jpeg.DecoderOptions{},ch)
...

Any thoughts on this implementation are appreciated. Currently it is not possible to specify the number of lines (or max pixels) to return in each Subimage. This could possibly be added as an option in jpeg.DecoderOptions.