The image/png package decodes PNG files through an io.Reader and returns an image.Image. A maze loader can then type-assert that result to *image.RGBA for pixel access and modification. section 9.2.2
A practical PNG-loading workflow is:
- Open the input path and defer closing the file. section 9.2.2
- Call png.Decode with the file as its io.Reader. section 9.2.2
- Handle missing files, invalid PNG data, and unexpected image types explicitly. section 9.2.2
- Use RGBAAt to read pixels and Set to write pixels when working with image.RGBA. section 9.7
Loading a maze image
func openMaze(imagePath string) (*image.RGBA, error) { f, err := os.Open(imagePath) if err != nil { return nil, err } defer f.Close() img, err := png.Decode(f) if err != nil { return nil, err } rgba, ok := img.(*image.RGBA) if !ok { return nil, fmt.Errorf("expected RGBA image, got %T", img) } return rgba, nil } Running the loader on a paletted PNG reports: "expected RGBA image, got *image.Paletted".
