jpegn

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2025 License: MIT Imports: 8 Imported by: 0

README

jpegn

Status Go Reference

JPEG decoder with SIMD optimizations.

Benchmark

Compared to the standard library:

BenchmarkDecodeBaseline420-8             	    3546	   1011092 ns/op	  426775 B/op	       9 allocs/op
BenchmarkDecodeBaseline420StdLib-8       	    1942	   1874464 ns/op	  407088 B/op	       5 allocs/op

BenchmarkDecodeProgressive420-8          	    2221	   2741827 ns/op	 2004426 B/op	      17 allocs/op
BenchmarkDecodeProgressive420StdLib-8    	     950	   3645997 ns/op	 1980096 B/op	      17 allocs/op

BenchmarkDecodeConfig-8                  	11457307	       322.3 ns/op	      52 B/op	       1 allocs/op
BenchmarkDecodeConfigStdLib-8            	 2974725	      1199 ns/op	   13616 B/op	       2 allocs/op

BenchmarkDecodeToRGBANearestNeighbor-8   	    2683	   1405335 ns/op	 2002868 B/op	      13 allocs/op
BenchmarkDecodeToRGBACatmullRom-8        	    2307	   1568539 ns/op	 2271535 B/op	      15 allocs/op
BenchmarkDecodeToRGBAStdLib-8            	    1304	   2837424 ns/op	 1455730 B/op	       7 allocs/op

Difference with assembly optimizations (noasm vs asm):

benchmark                                  old ns/op     new ns/op     delta
BenchmarkDecodeBaseline420-8               1984046       1011092       -49.04%
BenchmarkDecodeProgressive420-8            3645160       2741827       -24.79%
BenchmarkDecodeConfig-8                    344           322           -6.44%
BenchmarkDecodeToRGBANearestNeighbor-8     3368435       1405335       -58.28%
BenchmarkDecodeToRGBACatmullRom-8          5253731       1568539       -70.14%
BenchmarkIdct-8                            147           37.7          -74.30%
BenchmarkUpsampleNearestNeighbor-8         349901        126524        -63.84%
BenchmarkUpsampleCatmullRom-8              7418931       378112        -94.90%

Build tags

  • noasm - do not use SIMD optimizations

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoJPEG             = errors.New("not a JPEG file")
	ErrNoEXIF             = errors.New("no EXIF data found")
	ErrUnsupported        = errors.New("unsupported format")
	ErrOutOfMemory        = errors.New("out of memory")
	ErrInternal           = errors.New("internal error")
	ErrSyntax             = errors.New("syntax error")
	ErrMissingHuffmanCode = errors.New("missing Huffman code")
)

Standard error types for JPEG decoding.

Functions

func Decode

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

Decode reads a JPEG image from r and returns it as an image.Image. It accepts an optional Options struct to control decoding parameters.

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 data. The dimensions returned are as stored in the file (SOF marker), ignoring any EXIF orientation tags.

Types

type Exif added in v0.3.0

type Exif struct {
	// Basic image info
	Orientation int // EXIF orientation (1-8). 1 = normal, values 2-8 indicate rotation/flip.
	Width       int // Image width in pixels (may differ from actual JPEG dimensions if rotated).
	Height      int // Image height in pixels.

	// Camera info
	Make     string // Camera manufacturer (e.g., "Canon").
	Model    string // Camera model (e.g., "Canon EOS 5D Mark III").
	Software string // Software used to process/create the image.

	// Date/Time (format: "YYYY:MM:DD HH:MM:SS")
	DateTime         string // File modification date/time.
	DateTimeOriginal string // Original capture date/time (when photo was taken).

	// Exposure settings
	ExposureTime float64 // Shutter speed in seconds (e.g., 0.004 = 1/250s).
	FNumber      float64 // Aperture f-number (e.g., 5.6 = f/5.6).
	ISOSpeed     int     // ISO speed rating (e.g., 800).
	FocalLength  float64 // Lens focal length in millimeters.
	Flash        int     // Flash mode/status (0 = no flash, non-zero = flash fired).

	// GPS location
	GPSLatitude  float64 // Latitude in decimal degrees (positive = North, negative = South).
	GPSLongitude float64 // Longitude in decimal degrees (positive = East, negative = West).
	GPSAltitude  float64 // Altitude in meters above sea level.

	// Copyright/Author
	Copyright string // Copyright notice.
	Artist    string // Creator/photographer name.
}

Exif contains metadata extracted from a JPEG image's EXIF data.

func DecodeExif added in v0.3.0

func DecodeExif(r io.Reader) (*Exif, error)

DecodeExif reads EXIF metadata from a JPEG image without decoding the entire image. It returns an Exif struct containing common EXIF tags like camera make/model, exposure settings, GPS location, and date/time information.

Returns an error if:

  • The input is not a valid JPEG
  • No EXIF data is present in the image
  • EXIF data is corrupted or cannot be parsed

Individual fields in the returned Exif struct may be zero/empty if those specific tags are not present in the EXIF data.

type Options

type Options struct {
	// ToRGBA forces the output image to be in the RGBA color space.
	// If false, the image will be returned in its native color space,
	// which is typically YCbCr for color images or Grayscale for monochrome images.
	ToRGBA bool
	// UpsampleMethod defines the algorithm used for chroma upsampling when
	// converting a subsampled YCbCr image to a full-color format.
	// This option is only used when the output is RGBA (either because
	// ToRGBA is true or the source JPEG is in the RGB format).
	UpsampleMethod UpsampleMethod
	// AutoRotate enables automatic image rotation based on the EXIF orientation tag.
	// If true, the decoded image will be rotated/flipped to match the intended viewing orientation.
	// This process forces the output to RGBA if a transformation is applied.
	AutoRotate bool
	// ScaleDenom specifies the IDCT scaling denominator for efficient downscaling.
	// Valid values are 1 (no scaling), 2 (1/2 size), 4 (1/4 size), or 8 (1/8 size).
	// Invalid values will be clamped to the nearest valid value.
	// This produces higher quality and faster decoding than decoding full size and then downsampling.
	ScaleDenom int
}

Options specifies decoding parameters.

type UpsampleMethod

type UpsampleMethod int

UpsampleMethod defines the algorithm used for chroma upsampling.

const (
	// NearestNeighbor is a fast but low-quality upsampling method.
	NearestNeighbor UpsampleMethod = iota
	// CatmullRom is a higher-quality bicubic upsampling method.
	CatmullRom
)

Jump to

Keyboard shortcuts

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