Imports #
"bufio"
"compress/lzw"
"errors"
"fmt"
"image"
"image/color"
"io"
"bufio"
"bytes"
"compress/lzw"
"errors"
"image"
"image/color"
"image/color/palette"
"image/draw"
"internal/byteorder"
"io"
"bufio"
"compress/lzw"
"errors"
"fmt"
"image"
"image/color"
"io"
"bufio"
"bytes"
"compress/lzw"
"errors"
"image"
"image/color"
"image/color/palette"
"image/draw"
"internal/byteorder"
"io"
Disposal Methods.
const DisposalBackground = 0x02Disposal Methods.
const DisposalNone = 0x01Disposal Methods.
const DisposalPrevious = 0x03Extensions.
const eApplication = 0xFFExtensions.
const eComment = 0xFEExtensions.
const eGraphicControl = 0xF9Extensions.
const eText = 0x01var errBadPixel = *ast.CallExprvar errNotEnough = *ast.CallExprvar errTooMuch = *ast.CallExprFields.
const fColorTable = *ast.BinaryExprMasks etc.
const fColorTableBitsMask = 7Masks etc.
const fInterlace = *ast.BinaryExprGraphic control extension fields.
const gcBlockSize = 0x04Masks etc.
const gcDisposalMethodMask = *ast.BinaryExprGraphic control extension fields.
const gcLabel = 0xF9Graphic control flags.
const gcTransparentColorSet = *ast.BinaryExprinterlacing represents the set of scans in an interlaced GIF image.
var interlacing = []interlaceScan{...}var log2Lookup = [8]int{...}Section indicators.
const sExtension = 0x21Section indicators.
const sImageDescriptor = 0x2CSection indicators.
const sTrailer = 0x3BIf the io.Reader does not also have ReadByte, then decode will introduce its own buffering.
type reader interface {
io.Reader
io.ByteReader
}writer is a buffered writer.
type writer interface {
Flush() error
io.Writer
io.ByteWriter
}GIF represents the possibly multiple images stored in a GIF file.
type GIF struct {
Image []*image.Paletted
Delay []int
LoopCount int
Disposal []byte
Config image.Config
BackgroundIndex byte
}Options are the encoding parameters.
type Options struct {
NumColors int
Quantizer draw.Quantizer
Drawer draw.Drawer
}blockReader parses the block structure of GIF image data, which comprises (n, (n bytes)) blocks, with 1 <= n <= 255. It is the reader given to the LZW decoder, which is thus immune to the blocking. After the LZW decoder completes, there will be a 0-byte block remaining (0, ()), which is consumed when checking that the blockReader is exhausted. To avoid the allocation of a bufio.Reader for the lzw Reader, blockReader implements io.ByteReader and buffers blocks into the decoder's "tmp" buffer.
type blockReader struct {
d *decoder
i uint8
j uint8
err error
}blockWriter writes the block structure of GIF image data, which comprises (n, (n bytes)) blocks, with 1 <= n <= 255. It is the writer given to the LZW encoder, which is thus immune to the blocking.
type blockWriter struct {
e *encoder
}decoder is the type used to decode a GIF file.
type decoder struct {
r reader
vers string
width int
height int
loopCount int
delayTime int
backgroundIndex byte
disposalMethod byte
imageFields byte
transparentIndex byte
hasTransparentIndex bool
globalColorTable color.Palette
delay []int
disposal []byte
image []*image.Paletted
tmp [1024]byte
}encoder encodes an image to the GIF format.
type encoder struct {
w writer
err error
g GIF
globalCT int
buf [256]byte
globalColorTable [*ast.BinaryExpr]byte
localColorTable [*ast.BinaryExpr]byte
}interlaceScan defines the ordering for a pass of the interlace algorithm.
type interlaceScan struct {
skip int
start int
}Decode reads a GIF image from r and returns the first embedded image as an [image.Image].
func Decode(r io.Reader) (image.Image, error)DecodeAll reads a GIF image from r and returns the sequential frames and timing information.
func DecodeAll(r io.Reader) (*GIF, error)DecodeConfig returns the global color model and dimensions of a GIF image without decoding the entire image.
func DecodeConfig(r io.Reader) (image.Config, error)Encode writes the Image m to w in GIF format.
func Encode(w io.Writer, m image.Image, o *Options) errorEncodeAll writes the images in g to w in GIF format with the given loop count and delay between frames.
func EncodeAll(w io.Writer, g *GIF) errorfunc (b blockWriter) Flush() errorblockReader must implement io.Reader, but its Read shouldn't ever actually be called in practice. The compress/lzw package will only call [blockReader.ReadByte].
func (b *blockReader) Read(p []byte) (int, error)func (b *blockReader) ReadByte() (byte, error)blockWriter must be an io.Writer for lzw.NewWriter, but this is never actually called.
func (b blockWriter) Write(data []byte) (int, error)func (b blockWriter) WriteByte(c byte) errorfunc (b blockWriter) close()close primarily detects whether or not a block terminator was encountered after reading a sequence of data sub-blocks. It allows at most one trailing sub-block worth of data. I.e., if some number of bytes exist in one sub-block following the end of LZW data, the very next sub-block must be the block terminator. If the very end of LZW data happened to fill one sub-block, at most one more sub-block of length 1 may exist before the block-terminator. These accommodations allow us to support GIFs created by less strict encoders. See https://golang.org/issue/16146.
func (b *blockReader) close() errorfunc (e *encoder) colorTablesMatch(localLen int, transparentIndex int) booldecode reads a GIF image from r and stores the result in d.
func (d *decoder) decode(r io.Reader, configOnly bool, keepAllFrames bool) errorfunc encodeColorTable(dst []byte, p color.Palette, size int) (int, error)func (b *blockReader) fill()func (e *encoder) flush()func init()func log2(x int) intfunc (d *decoder) newImageFromDescriptor() (*image.Paletted, error)func (d *decoder) readBlock() (int, error)func readByte(r io.ByteReader) (byte, error)func (d *decoder) readColorTable(fields byte) (color.Palette, error)func (d *decoder) readExtension() errorfunc readFull(r io.Reader, b []byte) errorfunc (d *decoder) readGraphicControl() errorfunc (d *decoder) readHeaderAndScreenDescriptor() errorfunc (d *decoder) readImageDescriptor(keepAllFrames bool) errorfunc (b blockWriter) setup()uninterlace rearranges the pixels in m to account for interlaced input.
func uninterlace(m *image.Paletted)func (e *encoder) write(p []byte)func (e *encoder) writeByte(b byte)func (e *encoder) writeHeader()func (e *encoder) writeImageBlock(pm *image.Paletted, delay int, disposal byte)Generated with Arrow