golibjpegturbo

package module
Version: v0.0.0-...-c03a2fa Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 8, 2014 License: MIT Imports: 7 Imported by: 3

README

Read JPEG images in Go, quickly

This library is the fastest possible way to decode and encode JPEG images in Go.

We achieve this via cgo bindings to libjpeg-turbo library.

The exact speed depends on the image and CPU. On Mac Book Pro, compared to image/jpeg standard library, golibjpegturbo is:

  • 6x faster when decoding
  • 1.7x faster when encoding at quality level of 90%

You can rerun the benchmark on your machine with go test -bench=.

Additionally, unlike image/jpeg, this library can read JPEG images in CMYK format.

Setup

Before you import library, you need to install libjpeg-turbo.

On Ubuntu: sudo apt-get install libjpeg-turbo8-dev.

On Mac OS X: brew install libjpeg-turbo

Usage

go get github.com/kjk/golibjpegturbo

The API is the same as image/libjpeg:

import "golibjpegturbo"

func decode(r io.Reader) (image.Image, error) {
    return golibjpegturbo.Decode(r)
}

func encode(img image.Image) ([]byte, error) {
    options := &golibjpegturbo.Options{Quality: 90}
    var buf bytes.Buffer
    err = golibjpegturbo.Encode(&buf, decodedImg, options)
    if err != nil {
        return nil, err
    }
    return buf.Bytes(), nil
}

More docs: http://godoc.org/github.com/kjk/golibjpegturbo

Running a stress test

There's a stress test. If you have a directory with images, you can do go run stress_test/main.go -dir=$directory.

The test loops infinitely and decodes/encodes the images to verify there are no problems caused by incorrect cgo usage.

License

MIT.

Written by Krzysztof Kowalczyk. Inspired by lye/libjpeg and go-thumber.

Documentation

Overview

Package golibjpegturbo is the fastest way to decode and encode JPEG images in Go.

We achieve this via cgo bindings to http://libjpeg-turbo.virtualgl.org library.

The exact speed depends on the image and CPU. On Mac Book Pro, compared to image/jpeg standard library, golibjpegturbo is:

* 6x faster when decoding

* 1.7x faster when encoding at quality level of 90%

Before you import library, you need to install libjpeg-turbo.

On Ubuntu: sudo apt-get install libjpeg-turbo8-dev.

On Mac OS X: brew install libjpeg-turbo

Index

Constants

View Source
const DefaultQuality = 75

DefaultQuality is the default quality encoding parameter.

Variables

This section is empty.

Functions

func Decode

func Decode(r io.Reader) (image.Image, error)

Decode reads a JPEG image from r and returns it as an image.Image.

func DecodeData

func DecodeData(d []byte) (img image.Image, err error)

DecodeData reads JPEG image from d and returns it as an image.Image.

func Encode

func Encode(w io.Writer, m image.Image, o *Options) (err error)

Encode writes the Image m to w in JPEG 4:2:0 baseline format with the given options. Default parameters are used if a nil *Options is passed.

Types

type JpegInfo

type JpegInfo struct {
	Components       int
	ColorSpace       int
	Width            int
	Height           int
	ColorSpaceString string
}

JpegInfo contains information about JPEG image.

func GetJpegInfo

func GetJpegInfo(d []byte) (info *JpegInfo, err error)

GetJpegInfo returns information about a JPEG image.

type Options

type Options struct {
	Quality int
}

Options are the encoding parameters. Quality ranges from 1 to 100 inclusive, higher is better.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL