Go’s image package represents, decodes, encodes, and manipulates raster images: two-dimensional grids whose pixels carry colors or other numerical information. It fits projects such as a maze solver, where exact pixel values encode walls, paths, an entrance, and treasure. section 9.1.1 section 9.7
Use the package to work through an image:
- Decode a file with the format-specific package, such as image/png, into an image.Image. section 9.7
- Read pixels through Image.At; use RGBAAt when the concrete image is image.RGBA and direct color comparisons are useful. section 9.1.1 section 9.7
- Write pixels with RGBA.Set, scanning rows outside columns so the nested loops follow the image’s scanlines. section 9.7
- Encode with the package for the chosen format; PNG preserves exact values, while JPEG may alter pixel colors through lossy compression. section 9.1.1 section 9.7
Encoding a maze grid
Represent the maze as a raster image, assign colors to walls, paths, the entrance, and treasure, then write the result as PNG so decoding preserves those exact cell values. section 9.1.1
> go doc image
package image // import "image"
[...]
type Image interface{ ... }
func Decode(r io.Reader) (Image, string, error)
type Point struct{ ... }
type RGBA struct{ ... }
func NewRGBA(r Rectangle) *RGBA
> go doc image.Point
type Point struct {
X, Y int
}
A Point is an X, Y coordinate pair. The axes increase right and down.
The Go documentation listing provides quick access to Image.Decode, Point coordinates, RGBA, and NewRGBA; Point axes increase rightward and downward. section 9.1.1
