| 1 | ## Read JPEG images in Go, quickly |
| 2 | |
| 3 | This library is the fastest possible way to decode and encode JPEG images in Go. |
| 4 | |
| 5 | We achieve this via cgo bindings to [libjpeg-turbo](http://libjpeg-turbo.virtualgl.org) |
| 6 | library. |
| 7 | |
| 8 | The exact speed depends on the image and CPU. On Mac Book Pro, compared |
| 9 | to `image/jpeg` standard library, golibjpegturbo is: |
| 10 | * 6x faster when decoding |
| 11 | * 1.7x faster when encoding at quality level of 90% |
| 12 | |
| 13 | You can rerun the benchmark on your machine with `go test -bench=.` |
| 14 | |
| 15 | Additionally, unlike `image/jpeg`, this library can read JPEG images in CMYK format. |
| 16 | |
| 17 | ## Setup |
| 18 | |
| 19 | Before you import library, you need to install libjpeg-turbo. |
| 20 | |
| 21 | On Ubuntu: `sudo apt-get install libjpeg-turbo8-dev`. |
| 22 | |
| 23 | On Mac OS X: `brew install libjpeg-turbo` |
| 24 | |
| 25 | ## Usage |
| 26 | |
| 27 | `go get github.com/kjk/golibjpegturbo` |
| 28 | |
| 29 | The API is the same as `image/libjpeg`: |
| 30 | |
| 31 | ```go |
| 32 | import "golibjpegturbo" |
| 33 | |
| 34 | func decode(r io.Reader) (image.Image, error) { |
| 35 | return golibjpegturbo.Decode(r) |
| 36 | } |
| 37 | |
| 38 | func encode(img image.Image) ([]byte, error) { |
| 39 | options := &golibjpegturbo.Options{Quality: 90} |
| 40 | var buf bytes.Buffer |
| 41 | err = golibjpegturbo.Encode(&buf, decodedImg, options) |
| 42 | if err != nil { |
| 43 | return nil, err |
| 44 | } |
| 45 | return buf.Bytes(), nil |
| 46 | } |
| 47 | |
| 48 | ``` |
| 49 | |
| 50 | More docs: http://godoc.org/github.com/kjk/golibjpegturbo |
| 51 | |
| 52 | |
| 53 | ## Running a stress test |
| 54 | |
| 55 | There's a stress test. If you have a directory with images, you can |
| 56 | do `go run stress_test/main.go -dir=$directory`. |
| 57 | |
| 58 | The test loops infinitely and decodes/encodes the images to verify there |
| 59 | are no problems caused by incorrect `cgo` usage. |
| 60 | |
| 61 | ## License |
| 62 | |
| 63 | MIT. |
| 64 | |
| 65 | Written by [Krzysztof Kowalczyk](http://blog.kowalczyk.info/). |
| 66 | Inspired by [lye/libjpeg](https://github.com/lye/libjpeg) and [go-thumber](https://github.com/pixiv/go-thumber/tree/master/jpeg). |