| 1 |
package gfx |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"image" |
| 6 |
"image/color" |
| 7 |
"image/draw" |
| 8 |
"image/png" |
| 9 |
"io" |
| 10 |
) |
| 11 |
|
| 12 |
// ZP is the zero image.Point. |
| 13 |
var ZP = image.Point{} |
| 14 |
|
| 15 |
// NewImage creates an image of the given size (optionally filled with a color) |
| 16 |
func NewImage(w, h int, colors ...color.Color) *image.RGBA { |
| 17 |
m := NewRGBA(IR(0, 0, w, h)) |
| 18 |
|
| 19 |
if len(colors) > 0 { |
| 20 |
DrawColor(m, m.Bounds(), colors[0]) |
| 21 |
} |
| 22 |
|
| 23 |
return m |
| 24 |
} |
| 25 |
|
| 26 |
// NewNRGBA returns a new NRGBA image with the given bounds. |
| 27 |
func NewNRGBA(r image.Rectangle) *image.NRGBA { |
| 28 |
return image.NewNRGBA(r) |
| 29 |
} |
| 30 |
|
| 31 |
// NewRGBA returns a new RGBA image with the given bounds. |
| 32 |
func NewRGBA(r image.Rectangle) *image.RGBA { |
| 33 |
return image.NewRGBA(r) |
| 34 |
} |
| 35 |
|
| 36 |
// NewGray returns a new Gray image with the given bounds. |
| 37 |
func NewGray(r image.Rectangle) *image.Gray { |
| 38 |
return image.NewGray(r) |
| 39 |
} |
| 40 |
|
| 41 |
// NewGray16 returns a new Gray16 image with the given bounds. |
| 42 |
// (For example useful for height maps) |
| 43 |
func NewGray16(r image.Rectangle) *image.Gray16 { |
| 44 |
return image.NewGray16(r) |
| 45 |
} |
| 46 |
|
| 47 |
// NewUniform creates a new uniform image of the given color. |
| 48 |
func NewUniform(c color.Color) *image.Uniform { |
| 49 |
return image.NewUniform(c) |
| 50 |
} |
| 51 |
|
| 52 |
// Pt returns an image.Point for the given x and y. |
| 53 |
func Pt(x, y int) image.Point { |
| 54 |
return image.Pt(x, y) |
| 55 |
} |
| 56 |
|
| 57 |
// IR returns an image.Rectangle for the given input. |
| 58 |
func IR(x0, y0, x1, y1 int) image.Rectangle { |
| 59 |
return image.Rect(x0, y0, x1, y1) |
| 60 |
} |
| 61 |
|
| 62 |
// Mix the current pixel color at x and y with the given color. |
| 63 |
func Mix(m draw.Image, x, y int, c color.Color) { |
| 64 |
_, _, _, a := c.RGBA() |
| 65 |
|
| 66 |
switch a { |
| 67 |
case 0xFFFF: |
| 68 |
m.Set(x, y, c) |
| 69 |
default: |
| 70 |
DrawColorOver(m, IR(x, y, x+1, y+1), c) |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
// MixPoint the current pixel color at the image.Point with the given color. |
| 75 |
func MixPoint(dst draw.Image, p image.Point, c color.Color) { |
| 76 |
Mix(dst, p.X, p.Y, c) |
| 77 |
} |
| 78 |
|
| 79 |
// Set x and y to the given color. |
| 80 |
func Set(dst draw.Image, x, y int, c color.Color) { |
| 81 |
dst.Set(x, y, c) |
| 82 |
} |
| 83 |
|
| 84 |
// SetPoint to the given color. |
| 85 |
func SetPoint(dst draw.Image, p image.Point, c color.Color) { |
| 86 |
dst.Set(p.X, p.Y, c) |
| 87 |
} |
| 88 |
|
| 89 |
// SetVec to the given color. |
| 90 |
func SetVec(dst draw.Image, u Vec, c color.Color) { |
| 91 |
pt := u.Pt() |
| 92 |
|
| 93 |
dst.Set(pt.X, pt.Y, c) |
| 94 |
} |
| 95 |
|
| 96 |
// EachImageVec calls the provided function for each Vec |
| 97 |
// in the provided image in the given direction. |
| 98 |
// |
| 99 |
// gfx.V(1,1) to call the function on each pixel starting from the top left. |
| 100 |
func EachImageVec(src image.Image, dir Vec, fn func(u Vec)) { |
| 101 |
BoundsToRect(src.Bounds()).EachVec(dir, fn) |
| 102 |
} |
| 103 |
|
| 104 |
// EachPixel calls the provided function for each pixel in the provided rectangle. |
| 105 |
func EachPixel(r image.Rectangle, fn func(x, y int)) { |
| 106 |
for x := r.Min.X; x < r.Max.X; x++ { |
| 107 |
for y := r.Min.Y; y < r.Max.Y; y++ { |
| 108 |
fn(x, y) |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
// EncodePNG encodes an image as PNG to the provided io.Writer. |
| 114 |
func EncodePNG(w io.Writer, src image.Image) error { |
| 115 |
return png.Encode(w, src) |
| 116 |
} |
| 117 |
|
| 118 |
// DecodePNG decodes a PNG from the provided io.Reader. |
| 119 |
func DecodePNG(r io.Reader) (image.Image, error) { |
| 120 |
return png.Decode(r) |
| 121 |
} |
| 122 |
|
| 123 |
// DecodePNGBytes decodes a PNG from the provided []byte. |
| 124 |
func DecodePNGBytes(b []byte) (image.Image, error) { |
| 125 |
return DecodePNG(bytes.NewReader(b)) |
| 126 |
} |
| 127 |
|
| 128 |
// DecodeImage decodes an image from the provided io.Reader. |
| 129 |
func DecodeImage(r io.Reader) (image.Image, error) { |
| 130 |
m, _, err := image.Decode(r) |
| 131 |
|
| 132 |
return m, err |
| 133 |
} |
| 134 |
|
| 135 |
// DecodeImageBytes decodes an image from the provided []byte. |
| 136 |
func DecodeImageBytes(b []byte) (image.Image, error) { |
| 137 |
return DecodeImage(bytes.NewReader(b)) |
| 138 |
} |