jpeg

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2021 License: BSD-3-Clause Imports: 9 Imported by: 1

README

go-libjpeg

Build Status

An implementation of Go binding for LibJpeg (preferably libjpeg-turbo).

The core codes are picked from go-thumber and rewritten to compatible with image.Image interface.

Usage

import "github.com/pixiv/go-libjpeg/jpeg"

func main() {
    // Decoding JPEG into image.Image
    io, err := os.Open("in.jpg")
    if err != nil {
        log.Fatal(err)
    }
    img, err := jpeg.Decode(io, &jpeg.DecoderOptions{})
    if err != nil {
        log.Fatalf("Decode returns error: %v\n", err)
    }

    // Encode JPEG
    f, err := os.Create("out.jpg")
    if err != nil {
        panic(err)
    }
    w := bufio.NewWriter(f)
    if err := jpeg.Encode(w, img, &jpeg.EncoderOptions{Quality: 90}); err != nil {
        log.Printf("Encode returns error: %v\n", err)
        return
    }
    w.Flush()
    f.Close()
}

See test code to read full features.

Features

  • Raw JPEG decoding in YCbCr color.
  • Decoding with color conversion into RGB/RGBA (RGBA conversion is only supported with libjpeg-turbo).
  • Scaled decoding.
  • Encoding from some color models (YCbCr, RGB and RGBA).

Benchmark

$ go test -bench . -benchtime 10s
...
BenchmarkDecode                     1000          26345730 ns/op
BenchmarkDecodeIntoRGB               500          30886383 ns/op
BenchmarkDecodeWithNativeJPEG        300          49815928 ns/op
...

With libjpeg-turbo:

BenchmarkDecode                     2000           9557646 ns/op
BenchmarkDecodeIntoRGB              1000          12676414 ns/op
BenchmarkDecodeWithNativeJPEG        300          45836153 ns/op

go-libjpeg is about 1.9x faster than image/jpeg. With libjpeg-turbo, it can make more faster (about 4.8x faster than image/jpeg).

Dependencies
  • Go 1.6 or later.

  • libjpeg (preferably libjpeg-turbo)

    DecodeIntoRGBA can only work if go-libjpeg is built with libjpeg-turbo. Because DecdeIntoRGBA uses JCS_ALPHA_EXTENSIONS. You can use DecodeIntoRGB and convert to image.RGBA if can not use libjpeg-turbo.

License

Copyright (c) 2014 pixiv Inc. All rights reserved.

See LICENSE.

Documentation

Overview

Package jpeg decodes JPEG image to image.YCbCr using libjpeg (or libjpeg-turbo).

Index

Constants

View Source
const (
	Y  = 0
	Cb = 1
	Cr = 2
)

Y/Cb/Cr Planes

Variables

This section is empty.

Functions

func Decode

func Decode(r io.Reader, options *DecoderOptions) (dest image.Image, err error)

Decode reads a JPEG data stream from r and returns decoded image as an image.Image. Output image has YCbCr colors or 8bit Grayscale.

func DecodeConfig

func DecodeConfig(r io.Reader) (config image.Config, err error)

DecodeConfig returns the color model and dimensions of a JPEG image without decoding the entire image.

func DecodeIntoRGB

func DecodeIntoRGB(r io.Reader, options *DecoderOptions) (dest *rgb.Image, err error)

DecodeIntoRGB reads a JPEG data stream from r and returns decoded image as an rgb.Image with RGB colors.

func DecodeIntoRGBA

func DecodeIntoRGBA(r io.Reader, options *DecoderOptions) (dest *image.RGBA, err error)

DecodeIntoRGBA reads a JPEG data stream from r and returns decoded image as an image.RGBA with RGBA colors. This function only works with libjpeg-trubo, not libjpeg.

func DestinationManagerMapLen

func DestinationManagerMapLen() int

DestinationManagerMapLen returns the number of globally working destinationManagers for debug.

func Encode

func Encode(w io.Writer, src image.Image, opt *EncoderOptions) (err error)

Encode encodes src image and writes into w as JPEG format data.

func NewGrayAligned

func NewGrayAligned(r image.Rectangle) *image.Gray

NewGrayAligned Allocates Grey image with padding. This func add an extra padding to cover overflow from decoding image.

func NewYCbCrAligned

func NewYCbCrAligned(r image.Rectangle, subsampleRatio image.YCbCrSubsampleRatio) *image.YCbCr

NewYCbCrAligned Allocates YCbCr image with padding. Because LibJPEG needs extra padding to decoding buffer, This func add an extra alignSize (16) padding to cover overflow from any such modes.

func SourceManagerMapLen

func SourceManagerMapLen() int

SourceManagerMapLen returns the number of globally working sourceManagers for debug.

func SupportRGBA

func SupportRGBA() bool

SupportRGBA returns whether RGBA decoding is supported.

Types

type DCTMethod

type DCTMethod C.J_DCT_METHOD

DCTMethod is the DCT/IDCT method type.

const (
	// DCTISlow is slow but accurate integer algorithm
	DCTISlow DCTMethod = C.JDCT_ISLOW
	// DCTIFast is faster, less accurate integer method
	DCTIFast DCTMethod = C.JDCT_IFAST
	// DCTFloat is floating-point: accurate, fast on fast HW
	DCTFloat DCTMethod = C.JDCT_FLOAT
)

type DecoderOptions

type DecoderOptions struct {
	ScaleTarget            image.Rectangle // ScaleTarget is the target size to scale image.
	DCTMethod              DCTMethod       // DCTMethod is DCT Algorithm method.
	DisableFancyUpsampling bool            // If true, disable fancy upsampling
	DisableBlockSmoothing  bool            // If true, disable block smoothing
}

DecoderOptions specifies JPEG decoding parameters.

type EncoderOptions

type EncoderOptions struct {
	Quality         int
	OptimizeCoding  bool
	ProgressiveMode bool
	DCTMethod       DCTMethod
}

EncoderOptions specifies which settings to use during Compression.

Directories

Path Synopsis
example
Package rgb provides RGB image which implements image.Image interface.
Package rgb provides RGB image which implements image.Image interface.
test
util
Package util contains utility code for demosntration of go-libjpeg.
Package util contains utility code for demosntration of go-libjpeg.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL