jpeg

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2026 License: BSD-3-Clause Imports: 5 Imported by: 2

README

jpeg

A fork of Go's image/jpeg, under the same BSD-3-Clause licence. The import path is github.com/go-images/jpeg. Every change is stated in NOTICE, and Go's own test suite passes here unmodified in substance.

What differs: a four-component JPEG's chroma is upsampled the way libjpeg does — reading between the samples — rather than by repeating each one.

image/jpeg merges the four planes itself, through image/internal/imageutil.DrawYCbCr, which takes the chroma sample whose index is the output coordinate divided down. A caller receives an *image.CMYK whose chroma is a staircase and cannot do better, because the planes are gone. A three-component picture is unaffected: it still comes back as an *image.YCbCr with its planes intact.

Measured against poppler, which is libjpeg, on a 258×258 YCCK picture with 2×2 chroma — 266 256 samples:

C M Y K within one level
image/jpeg 36 18 21 1 91.16%
this 2 2 3 1 99.90%

The black plate is the control: it is carried at full resolution, is never upsampled, and is 1 both before and after. A decoder that simply differed would have moved it too.

Why a fork

The alternative is to reconstruct the chroma from what image/jpeg hands back. That was measured too: the per-block chroma can be recovered exactly where nothing clipped — 14 215 of 14 244 blocks at zero spread — but 7.8% of blocks have every pixel clipped on some channel and their chroma is gone, so the reconstruction stops at 26 levels where this reaches 3.

Everything else

Identical to the standard library, including Encode. Use it exactly as you would use image/jpeg.

Documentation

Overview

Package jpeg implements a JPEG image decoder and encoder.

JPEG is defined in ITU-T T.81: https://www.w3.org/Graphics/JPEG/itu-t81.pdf.

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 DecodeConfig

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

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

func Encode

func Encode(w io.Writer, m image.Image, o *Options) 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 FormatError

type FormatError string

A FormatError reports that the input is not a valid JPEG.

func (FormatError) Error

func (e FormatError) Error() string

type Options

type Options struct {
	Quality int
}

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

type Reader deprecated

type Reader interface {
	io.ByteReader
	io.Reader
}

Deprecated: Reader is not used by the image/jpeg package and should not be used by others. It is kept for compatibility.

type UnsupportedError

type UnsupportedError string

An UnsupportedError reports that the input uses a valid but unimplemented JPEG feature.

func (UnsupportedError) Error

func (e UnsupportedError) Error() string

Jump to

Keyboard shortcuts

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