sharpyuv

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2026 License: BSD-3-Clause, MIT Imports: 5 Imported by: 0

Documentation

Overview

Package sharpyuv implements sharp RGB to YUV420 conversion that minimizes chroma subsampling artifacts using iterative error diffusion.

This is a pure Go port of libwebp's sharpyuv library.

Index

Constants

This section is empty.

Variables

View Source
var (
	BT601 = ColorSpace{Kr: 0.2990, Kb: 0.1140, BitDepth: 8, Range: RangeLimited}
	BT709 = ColorSpace{Kr: 0.2126, Kb: 0.0722, BitDepth: 8, Range: RangeLimited}

	BT601Full = ColorSpace{Kr: 0.2990, Kb: 0.1140, BitDepth: 8, Range: RangeFull}
	BT709Full = ColorSpace{Kr: 0.2126, Kb: 0.0722, BitDepth: 8, Range: RangeFull}
)

Predefined color spaces.

Functions

func Convert

func Convert(rgb []byte, width, height, rgbStride int, yuv *image.YCbCr, opts *Options) error

Convert performs RGB to YUV420 conversion. When opts.SharpEnabled is true, it uses the sharp iterative algorithm that minimizes chroma subsampling artifacts. Otherwise it uses simple averaging.

rgb must be packed RGB (3 bytes per pixel, row-major). The result is written to the provided YCbCr image which must be allocated with 4:2:0 subsampling and matching dimensions.

func GammaToLinear

func GammaToLinear(v uint16, bitDepth int, tf TransferFunc) uint32

GammaToLinear converts a gamma-encoded value (in the range of bitDepth bits) to a 16-bit linear value using the specified transfer function.

func LinearToGamma

func LinearToGamma(v uint32, bitDepth int, tf TransferFunc) uint16

LinearToGamma converts a 16-bit linear value to a gamma-encoded value in the range of bitDepth bits using the specified transfer function.

Types

type ColorSpace

type ColorSpace struct {
	Kr       float64 // Luma coefficient for red
	Kb       float64 // Luma coefficient for blue
	BitDepth int     // 8, 10, or 12
	Range    Range
}

ColorSpace defines the color primaries for YUV conversion.

type ConversionMatrix

type ConversionMatrix struct {
	RGBToY [4]int32
	RGBToU [4]int32
	RGBToV [4]int32
}

ConversionMatrix holds the RGB to YUV conversion coefficients in 16-bit fixed point.

The conversion is:

y = (RGBToY[0]*r + RGBToY[1]*g + RGBToY[2]*b + RGBToY[3] + (1<<15)) >> 16
u = (RGBToU[0]*r + RGBToU[1]*g + RGBToU[2]*b + RGBToU[3] + (1<<15)) >> 16
v = (RGBToV[0]*r + RGBToV[1]*g + RGBToV[2]*b + RGBToV[3] + (1<<15)) >> 16

func ComputeConversionMatrix

func ComputeConversionMatrix(cs *ColorSpace) *ConversionMatrix

ComputeConversionMatrix fills a ConversionMatrix from a ColorSpace definition.

func GetConversionMatrix

func GetConversionMatrix(mt MatrixType) *ConversionMatrix

GetConversionMatrix returns a predefined conversion matrix.

type MatrixType

type MatrixType int

MatrixType identifies a predefined conversion matrix.

const (
	MatrixWebP MatrixType = iota
	MatrixRec601Limited
	MatrixRec601Full
	MatrixRec709Limited
	MatrixRec709Full
)

type Options

type Options struct {
	Matrix       *ConversionMatrix
	TransferType TransferFunc
	SharpEnabled bool // When false, use standard (averaging) downsampling
}

Options controls the SharpYUV conversion.

func DefaultOptions

func DefaultOptions() *Options

DefaultOptions returns default options using the WebP matrix and sRGB transfer.

type Range

type Range int

Range specifies the range of YUV values.

const (
	RangeFull    Range = iota // YUV values between [0, 255] for 8-bit
	RangeLimited              // Y in [16, 235], UV in [16, 240] for 8-bit
)

type TransferFunc

type TransferFunc int

TransferFunc identifies a transfer function (OETF/EOTF) as defined in H.273.

const (
	TransferBT709      TransferFunc = 1
	TransferBT470M     TransferFunc = 4
	TransferBT470BG    TransferFunc = 5
	TransferBT601      TransferFunc = 6
	TransferSMPTE240   TransferFunc = 7
	TransferLinear     TransferFunc = 8
	TransferLog100     TransferFunc = 9
	TransferLog100Sqrt TransferFunc = 10
	TransferIEC61966   TransferFunc = 11
	TransferBT1361     TransferFunc = 12
	TransferSRGB       TransferFunc = 13
	TransferBT2020_10  TransferFunc = 14
	TransferBT2020_12  TransferFunc = 15
	TransferPQ         TransferFunc = 16 // SMPTE 2084
	TransferSMPTE428   TransferFunc = 17
	TransferHLG        TransferFunc = 18
)

Jump to

Keyboard shortcuts

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