avif

package module
v0.0.0-...-f5d1245 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2026 License: MIT Imports: 14 Imported by: 0

README

go-avif

実行に必要なファイル

このライブラリは purego を使って libavif の共有ライブラリを動的に読み込みます。事前に以下のファイルをインストールしてください(libavif 1.1.0 以上が必要です)。

OS 必要なファイル
Windows libavif.dll
Linux libavif.so
macOS libavif.dylib

使い方

インポート
import "github.com/f0reth/go-avif"
デコード(AVIF → image.Image)
f, _ := os.Open("input.avif")
defer f.Close()

img, err := avif.Decode(f)
アニメーション AVIF のデコード
f, _ := os.Open("anim.avifs")
defer f.Close()

ret, err := avif.DecodeAll(f)
// ret.Image: 各フレームの画像スライス
// ret.Delay: 各フレームの表示時間(秒)
エンコード(image.Image → AVIF)
f, _ := os.Create("output.avif")
defer f.Close()

err := avif.Encode(f, img)
エンコードオプション
err := avif.Encode(f, img, avif.Options{
    Quality:           80,  // 画質 0〜100(100 で可逆、デフォルト: 60)
    QualityAlpha:      80,  // アルファの画質 0〜100
    Speed:             8,   // エンコード速度 0(高品質)〜10(速い、デフォルト: 10)
    ChromaSubsampling: image.YCbCrSubsampleRatio420, // クロマサブサンプリング 444|422|420
})
ライブラリの読み込み確認
if err := avif.Dynamic(); err != nil {
    log.Fatal("共有ライブラリが見つかりません:", err)
}

Documentation

Overview

Package avif implements an AVIF image decoder/encoder based on libavif shared library used via purego (CGo-free).

Index

Constants

View Source
const DefaultQuality = 60

DefaultQuality is the default quality encoding parameter.

View Source
const DefaultSpeed = 10

DefaultSpeed is the default speed encoding parameter.

Variables

View Source
var (
	ErrMemRead  = errors.New("avif: mem read failed")
	ErrMemWrite = errors.New("avif: mem write failed")
	ErrDecode   = errors.New("avif: decode failed")
	ErrEncode   = errors.New("avif: encode failed")
)

Errors .

Functions

func Decode

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

Decode reads a AVIF 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 AVIF image without decoding the entire image.

func Dynamic

func Dynamic() error

Dynamic returns error (if there was any) during opening dynamic/shared library.

func Encode

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

Encode writes the image m to w with the given options.

func InitDecoder

func InitDecoder()

InitDecoder is kept for backward compatibility and does nothing. The dynamic/shared library is loaded during package initialization; use Dynamic to check whether loading succeeded.

func InitEncoder

func InitEncoder()

InitEncoder is kept for backward compatibility and does nothing. The dynamic/shared library is loaded during package initialization; use Dynamic to check whether loading succeeded.

Types

type AVIF

type AVIF struct {
	// Decoded images, NRGBA or NRGBA64.
	Image []image.Image
	// Delay times, one per frame, in seconds.
	Delay []float64
}

AVIF represents the possibly multiple images stored in a AVIF file.

func DecodeAll

func DecodeAll(r io.Reader) (*AVIF, error)

DecodeAll reads a AVIF image from r and returns the sequential frames and timing information.

type Options

type Options struct {
	// Quality in the range [0,100]. Quality of 100 implies lossless. Default is 60.
	Quality int
	// Quality in the range [0,100].
	QualityAlpha int
	// Speed in the range [0,10]. Slower should make for a better quality image in less bytes.
	Speed int
	// Chroma subsampling, 444|422|420.
	ChromaSubsampling image.YCbCrSubsampleRatio
}

Options are the encoding parameters.

Jump to

Keyboard shortcuts

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