png

package
v1.25.0 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

pngパッケージは、PNG画像のデコーダとエンコーダを実装します。

PNGの仕様は https://www.w3.org/TR/PNG/ にあります。

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

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

Decodeは、rからPNG画像を読み取り、それを image.Image として返します。 返されるImageの型は、PNGの内容に依存します。

Example
// この例では、PNG画像のみをデコードできるpng.Decodeを使用しています。
// 任意の登録済み画像フォーマットをスニフし、デコードできる一般的なimage.Decodeの使用を検討してください。
img, err := png.Decode(gopherPNG())
if err != nil {
	log.Fatal(err)
}

levels := []string{" ", "░", "▒", "▓", "█"}

for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ {
	for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ {
		c := color.GrayModel.Convert(img.At(x, y)).(color.Gray)
		level := c.Y / 51 // 51 * 5 = 255
		if level == 5 {
			level--
		}
		fmt.Print(levels[level])
	}
	fmt.Print("\n")
}

func DecodeConfig

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

DecodeConfigは、画像全体をデコードすることなく、PNG画像のカラーモデルと寸法を返します。

func Encode

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

Encodeは、画像mをPNG形式でwに書き込みます。任意の画像をエンコードできますが、 image.NRGBA でない画像は、損失を伴ってエンコードされる可能性があります。

Example
package main

import (
	"github.com/shogo82148/std/image"
	"github.com/shogo82148/std/image/color"
	"github.com/shogo82148/std/image/png"
	"github.com/shogo82148/std/log"
	"github.com/shogo82148/std/os"
)

func main() {
	const width, height = 256, 256

	// 与えられた幅と高さのカラー画像を作成します。
	img := image.NewNRGBA(image.Rect(0, 0, width, height))

	for y := 0; y < height; y++ {
		for x := 0; x < width; x++ {
			img.Set(x, y, color.NRGBA{
				R: uint8((x + y) & 255),
				G: uint8((x + y) << 1 & 255),
				B: uint8((x + y) << 2 & 255),
				A: 255,
			})
		}
	}

	f, err := os.Create("image.png")
	if err != nil {
		log.Fatal(err)
	}

	if err := png.Encode(f, img); err != nil {
		f.Close()
		log.Fatal(err)
	}

	if err := f.Close(); err != nil {
		log.Fatal(err)
	}
}

Types

type CompressionLevel added in v1.4.0

type CompressionLevel int

CompressionLevelは、圧縮レベルを示します。

const (
	DefaultCompression CompressionLevel = 0
	NoCompression      CompressionLevel = -1
	BestSpeed          CompressionLevel = -2
	BestCompression    CompressionLevel = -3
)

type Encoder added in v1.4.0

type Encoder struct {
	CompressionLevel CompressionLevel

	// BufferPoolは、画像をエンコードする際に一時的な
	// EncoderBuffersを取得するためのバッファプールをオプションで指定します。
	BufferPool EncoderBufferPool
}

Encoderは、PNG画像のエンコーディングを設定します。

func (*Encoder) Encode added in v1.4.0

func (enc *Encoder) Encode(w io.Writer, m image.Image) error

Encodeは、画像mをPNG形式でwに書き込みます。

type EncoderBuffer added in v1.9.0

type EncoderBuffer encoder

EncoderBufferは、PNG画像のエンコーディングに使用されるバッファを保持します。

type EncoderBufferPool added in v1.9.0

type EncoderBufferPool interface {
	Get() *EncoderBuffer
	Put(*EncoderBuffer)
}

EncoderBufferPoolは、一時的な EncoderBuffer 構造体のインスタンスを取得し、 返すためのインターフェースです。これは、複数の画像をエンコードする際にバッファを再利用するために使用できます。

type FormatError

type FormatError string

FormatErrorは、入力が有効なPNGではないことを報告します。

func (FormatError) Error

func (e FormatError) Error() string

type UnsupportedError

type UnsupportedError string

UnsupportedErrorは、入力が有効だが未実装のPNG機能を使用していることを報告します。

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