Documentation
¶
Overview ¶
Package tilemask answers which cells of an image, or which tiles of a Tiled map, are used.
DecodePNG keeps one bit per pixel (alpha not zero) under a pixel cap checked before any buffer is allocated, and Mask.Cells projects that onto a grid of any cell size. DecodeMap lists each top-level tile layer's occupied tiles under one tile cap for the whole map. Every decoder bounds what its input claims, not the input's byte size: the caller limits the reader.
Index ¶
Constants ¶
const ( // DefaultMaxPixels is the image size cap when Options.MaxPixels is zero. DefaultMaxPixels int64 = 100_000_000 // DefaultMaxTiles is the map tile cap when Options.MaxTiles is zero. DefaultMaxTiles int64 = 4_000_000 )
Variables ¶
var ( // ErrTooLarge means a declared or decoded size exceeds its cap. ErrTooLarge = errors.New("tilemask: size exceeds the cap") // ErrUnsupported means a well-formed input uses an encoding or compression this package does not read. ErrUnsupported = errors.New("tilemask: unsupported encoding or compression") // ErrMalformed means the input is not the format it claims, or contradicts its own header. ErrMalformed = errors.New("tilemask: malformed input") )
Functions ¶
Types ¶
type Layer ¶
type Layer struct {
// Name is verbatim: no case folding or trimming.
Name string
// Tiles are the occupied tiles (GID not zero, flip bits included), in the map's coordinates: an infinite map's
// chunk offsets are applied.
Tiles []image.Point
}
Layer is one tile layer.
type Map ¶
type Map struct {
// Width and Height are the header's size in tiles, grown to cover every occupied tile.
Width, Height int
// Layers are the top-level tile layers in document order. Group layers are not entered.
Layers []Layer
}
Map is the occupied tiles of a Tiled map.
func DecodeMap ¶
DecodeMap reads r until the document ends and returns its occupied tiles.
The declared sizes of every layer and chunk together must be within opts.MaxTiles, checked before any data inflates (ErrTooLarge). A layer or chunk whose data holds more values than its declared width times height, or whose compressed data inflates past it, is ErrMalformed, so decode memory does not depend on the size of r. The parsed document is held whole, so memory also grows with the bytes of r (the caller bounds those), and element nesting deeper than 64 levels is ErrMalformed. An encoding or compression other than CSV, tile lists and base64 with none, zlib or gzip is ErrUnsupported, and fails the whole map.
type Mask ¶
type Mask struct {
Width, Height int
// contains filtered or unexported fields
}
Mask is one bit per pixel, set when the pixel's alpha is not zero. It is ordinary garbage-collected memory and safe for concurrent reads.
func DecodePNG ¶
DecodePNG reads r to the end and returns the mask of pixels whose alpha is not zero.
The size the IHDR claims is checked against opts.MaxPixels before any pixel buffer exists. Eight-bit, non-interlaced images are read by a fast path that skips what a mask does not need: it checks no chunk CRC and no Adler-32 trailer, needs no PLTE for colour type 3, needs no IDAT for an 8-bit grey or RGB image without tRNS (every pixel is set), and ignores data after the rows it needs. Each of those errs toward more coverage, never less. Everything else, and an image the fast path cannot inflate, goes through image/png.
func ReadMask ¶
ReadMask reads a mask WriteTo wrote. The size in the header is held to opts.MaxPixels before the bits are allocated, so a crafted header cannot claim more than a decode would have allowed, and every mask written under that cap reads back. The gzip trailer is verified, so a torn write is refused as ErrMalformed.
func (*Mask) Cells ¶
Cells yields each cell that holds a set pixel of src, row-major and each once. A cell is cell by cell pixels, src is clipped to the mask, and src's top-left lands at dst. Cell coordinates use floor division, so pixels left of or above the origin fall in cell -1, not cell 0. It yields nothing when cell <= 0, when src is empty after clipping, or when a coordinate of src or dst is beyond a quarter of the int range.
Cells holds one bool per cell of the covered range while it runs, so cell = 1 costs a byte per pixel.
type Options ¶
type Options struct {
// MaxPixels caps an image's width times height, checked before any pixel buffer is allocated.
MaxPixels int64
// MaxTiles caps a map's declared tiles, summed over every layer and chunk before any data inflates. No
// layer or chunk may then hold more values than it declared, so it also caps the tiles decoded.
MaxTiles int64
}
Options holds the caps. A zero or negative field means its default.