png

Imports

Imports #

"compress/zlib"
"encoding/binary"
"fmt"
"hash"
"hash/crc32"
"image"
"image/color"
"io"
"bufio"
"compress/zlib"
"encoding/binary"
"hash/crc32"
"image"
"image/color"
"io"
"strconv"

Constants & Variables

BestCompression const #

const BestCompression CompressionLevel = *ast.UnaryExpr

BestSpeed const #

const BestSpeed CompressionLevel = *ast.UnaryExpr

DefaultCompression const #

const DefaultCompression CompressionLevel = 0

NoCompression const #

const NoCompression CompressionLevel = *ast.UnaryExpr

cbG1 const #

A cb is a combination of color type and bit depth.

const cbG1

cbG16 const #

A cb is a combination of color type and bit depth.

const cbG16

cbG2 const #

A cb is a combination of color type and bit depth.

const cbG2

cbG4 const #

A cb is a combination of color type and bit depth.

const cbG4

cbG8 const #

A cb is a combination of color type and bit depth.

const cbG8

cbGA16 const #

A cb is a combination of color type and bit depth.

const cbGA16

cbGA8 const #

A cb is a combination of color type and bit depth.

const cbGA8

cbInvalid const #

A cb is a combination of color type and bit depth.

const cbInvalid = iota

cbP1 const #

A cb is a combination of color type and bit depth.

const cbP1

cbP2 const #

A cb is a combination of color type and bit depth.

const cbP2

cbP4 const #

A cb is a combination of color type and bit depth.

const cbP4

cbP8 const #

A cb is a combination of color type and bit depth.

const cbP8

cbTC16 const #

A cb is a combination of color type and bit depth.

const cbTC16

cbTC8 const #

A cb is a combination of color type and bit depth.

const cbTC8

cbTCA16 const #

A cb is a combination of color type and bit depth.

const cbTCA16

cbTCA8 const #

A cb is a combination of color type and bit depth.

const cbTCA8

chunkOrderError var #

var chunkOrderError = *ast.CallExpr

ctGrayscale const #

Color type, as per the PNG spec.

const ctGrayscale = 0

ctGrayscaleAlpha const #

Color type, as per the PNG spec.

const ctGrayscaleAlpha = 4

ctPaletted const #

Color type, as per the PNG spec.

const ctPaletted = 3

ctTrueColor const #

Color type, as per the PNG spec.

const ctTrueColor = 2

ctTrueColorAlpha const #

Color type, as per the PNG spec.

const ctTrueColorAlpha = 6

dsSeenIDAT const #

Decoding stage. The PNG specification says that the IHDR, PLTE (if present), tRNS (if present), IDAT and IEND chunks must appear in that order. There may be multiple IDAT chunks, and IDAT chunks must be sequential (i.e. they may not have any other chunks between them). https://www.w3.org/TR/PNG/#5ChunkOrdering

const dsSeenIDAT

dsSeenIEND const #

Decoding stage. The PNG specification says that the IHDR, PLTE (if present), tRNS (if present), IDAT and IEND chunks must appear in that order. There may be multiple IDAT chunks, and IDAT chunks must be sequential (i.e. they may not have any other chunks between them). https://www.w3.org/TR/PNG/#5ChunkOrdering

const dsSeenIEND

dsSeenIHDR const #

Decoding stage. The PNG specification says that the IHDR, PLTE (if present), tRNS (if present), IDAT and IEND chunks must appear in that order. There may be multiple IDAT chunks, and IDAT chunks must be sequential (i.e. they may not have any other chunks between them). https://www.w3.org/TR/PNG/#5ChunkOrdering

const dsSeenIHDR

dsSeenPLTE const #

Decoding stage. The PNG specification says that the IHDR, PLTE (if present), tRNS (if present), IDAT and IEND chunks must appear in that order. There may be multiple IDAT chunks, and IDAT chunks must be sequential (i.e. they may not have any other chunks between them). https://www.w3.org/TR/PNG/#5ChunkOrdering

const dsSeenPLTE

dsSeentRNS const #

Decoding stage. The PNG specification says that the IHDR, PLTE (if present), tRNS (if present), IDAT and IEND chunks must appear in that order. There may be multiple IDAT chunks, and IDAT chunks must be sequential (i.e. they may not have any other chunks between them). https://www.w3.org/TR/PNG/#5ChunkOrdering

const dsSeentRNS

dsStart const #

Decoding stage. The PNG specification says that the IHDR, PLTE (if present), tRNS (if present), IDAT and IEND chunks must appear in that order. There may be multiple IDAT chunks, and IDAT chunks must be sequential (i.e. they may not have any other chunks between them). https://www.w3.org/TR/PNG/#5ChunkOrdering

const dsStart = iota

ftAverage const #

Filter type, as per the PNG spec.

const ftAverage = 3

ftNone const #

Filter type, as per the PNG spec.

const ftNone = 0

ftPaeth const #

Filter type, as per the PNG spec.

const ftPaeth = 4

ftSub const #

Filter type, as per the PNG spec.

const ftSub = 1

ftUp const #

Filter type, as per the PNG spec.

const ftUp = 2

intSize const #

intSize is either 32 or 64.

const intSize = *ast.BinaryExpr

interlacing var #

interlacing defines Adam7 interlacing, with 7 passes of reduced images. See https://www.w3.org/TR/PNG/#8Interlace

var interlacing = []interlaceScan{...}

itAdam7 const #

Interlace type.

const itAdam7 = 1

itNone const #

Interlace type.

const itNone = 0

nFilter const #

Filter type, as per the PNG spec.

const nFilter = 5

pngHeader const #

const pngHeader = "\x89PNG\r\n\x1a\n"

Type Aliases

CompressionLevel type #

CompressionLevel indicates the compression level.

type CompressionLevel int

EncoderBuffer type #

EncoderBuffer holds the buffers used for encoding PNG images.

type EncoderBuffer encoder

FormatError type #

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

type FormatError string

UnsupportedError type #

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

type UnsupportedError string

Interfaces

EncoderBufferPool interface #

EncoderBufferPool is an interface for getting and returning temporary instances of the [EncoderBuffer] struct. This can be used to reuse buffers when encoding multiple images.

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

opaquer interface #

type opaquer interface {
Opaque() bool
}

Structs

Encoder struct #

Encoder configures encoding PNG images.

type Encoder struct {
CompressionLevel CompressionLevel
BufferPool EncoderBufferPool
}

decoder struct #

type decoder struct {
r io.Reader
img image.Image
crc hash.Hash32
width int
height int
depth int
palette color.Palette
cb int
stage int
idatLength uint32
tmp [*ast.BinaryExpr]byte
interlace int
useTransparent bool
transparent [6]byte
}

encoder struct #

type encoder struct {
enc *Encoder
w io.Writer
m image.Image
cb int
err error
header [8]byte
footer [4]byte
tmp [*ast.BinaryExpr]byte
cr [nFilter][]uint8
pr []uint8
zw *zlib.Writer
zwLevel int
bw *bufio.Writer
}

interlaceScan struct #

interlaceScan defines the placement and size of a pass for Adam7 interlacing.

type interlaceScan struct {
xFactor int
yFactor int
xOffset int
yOffset int
}

Functions

Decode function #

Decode reads a PNG image from r and returns it as an [image.Image]. The type of Image returned depends on the PNG contents.

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

DecodeConfig function #

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

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

Encode method #

Encode writes the Image m to w in PNG format.

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

Encode function #

Encode writes the Image m to w in PNG format. Any Image may be encoded, but images that are not [image.NRGBA] might be encoded lossily.

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

Error method #

func (e FormatError) Error() string

Error method #

func (e UnsupportedError) Error() string

Read method #

Read presents one or more IDAT chunks as one continuous stream (minus the intermediate chunk headers and footers). If the PNG data looked like: ... len0 IDAT xxx crc0 len1 IDAT yy crc1 len2 IEND crc2 then this reader presents xxxyy. For well-formed PNG data, the decoder state immediately before the first Read call is that d.r is positioned between the first IDAT and xxx, and the decoder state immediately after the last Read call is that d.r is positioned between yy and crc1.

func (d *decoder) Read(p []byte) (int, error)

Write method #

An encoder is an io.Writer that satisfies writes by writing PNG IDAT chunks, including an 8-byte header and 4-byte CRC checksum per Write call. Such calls should be relatively infrequent, since writeIDATs uses a [bufio.Writer]. This method should only be called from writeIDATs (via writeImage). No other code should treat an encoder as an io.Writer.

func (e *encoder) Write(b []byte) (int, error)

abs function #

func abs(x int) int

abs8 function #

The absolute value of a byte interpreted as a signed int8.

func abs8(d uint8) int

cbPaletted function #

func cbPaletted(cb int) bool

cbTrueColor function #

func cbTrueColor(cb int) bool

checkHeader method #

func (d *decoder) checkHeader() error

decode method #

decode decodes the IDAT data into an image.

func (d *decoder) decode() (image.Image, error)

filter function #

Chooses the filter to use for encoding the current row, and applies it. The return value is the index of the filter and also of the row in cr that has had it applied.

func filter(cr *[nFilter][]byte, pr []byte, bpp int) int

filterPaeth function #

filterPaeth applies the Paeth filter to the cdat slice. cdat is the current row's data, pdat is the previous row's data.

func filterPaeth(cdat []byte, pdat []byte, bytesPerPixel int)

init function #

func init()

levelToZlib function #

This function is required because we want the zero value of Encoder.CompressionLevel to map to zlib.DefaultCompression.

func levelToZlib(l CompressionLevel) int

mergePassInto method #

mergePassInto merges a single pass into a full sized image.

func (d *decoder) mergePassInto(dst image.Image, src image.Image, pass int)

opaque function #

Returns whether or not the image is fully opaque.

func opaque(m image.Image) bool

paeth function #

paeth implements the Paeth filter function, as per the PNG specification.

func paeth(a uint8, b uint8, c uint8) uint8

parseChunk method #

func (d *decoder) parseChunk(configOnly bool) error

parseIDAT method #

func (d *decoder) parseIDAT(length uint32) (err error)

parseIEND method #

func (d *decoder) parseIEND(length uint32) error

parseIHDR method #

func (d *decoder) parseIHDR(length uint32) error

parsePLTE method #

func (d *decoder) parsePLTE(length uint32) error

parsetRNS method #

func (d *decoder) parsetRNS(length uint32) error

readImagePass method #

readImagePass reads a single image pass, sized according to the pass number.

func (d *decoder) readImagePass(r io.Reader, pass int, allocateOnly bool) (image.Image, error)

verifyChecksum method #

func (d *decoder) verifyChecksum() error

writeChunk method #

func (e *encoder) writeChunk(b []byte, name string)

writeIDATs method #

Write the actual image data to one or more IDAT chunks.

func (e *encoder) writeIDATs()

writeIEND method #

func (e *encoder) writeIEND()

writeIHDR method #

func (e *encoder) writeIHDR()

writeImage method #

func (e *encoder) writeImage(w io.Writer, m image.Image, cb int, level int) error

writePLTEAndTRNS method #

func (e *encoder) writePLTEAndTRNS(p color.Palette)

Generated with Arrow