2017-02-28 11:24:25 -06:00
|
|
|
package pixel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"image"
|
|
|
|
"image/color"
|
|
|
|
"image/draw"
|
|
|
|
"math"
|
|
|
|
)
|
|
|
|
|
2017-03-15 13:40:39 -05:00
|
|
|
// TrianglesData specifies a list of Triangles vertices with three common properties:
|
|
|
|
// TrianglesPosition, TrianglesColor and TrianglesPicture.
|
2017-02-28 11:24:25 -06:00
|
|
|
type TrianglesData []struct {
|
2017-03-05 05:17:07 -06:00
|
|
|
Position Vec
|
2017-04-09 15:00:26 -05:00
|
|
|
Color RGBA
|
2017-03-05 05:17:07 -06:00
|
|
|
Picture Vec
|
|
|
|
Intensity float64
|
2017-02-28 11:24:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// MakeTrianglesData creates TrianglesData of length len initialized with default property values.
|
|
|
|
//
|
|
|
|
// Prefer this function to make(TrianglesData, len), because make zeros them, while this function
|
2017-03-15 13:40:39 -05:00
|
|
|
// does the correct intialization.
|
2017-03-05 17:44:16 -06:00
|
|
|
func MakeTrianglesData(len int) *TrianglesData {
|
|
|
|
td := &TrianglesData{}
|
2017-02-28 11:24:25 -06:00
|
|
|
td.SetLen(len)
|
|
|
|
return td
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the number of vertices in TrianglesData.
|
|
|
|
func (td *TrianglesData) Len() int {
|
|
|
|
return len(*td)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLen resizes TrianglesData to len, while keeping the original content.
|
|
|
|
//
|
|
|
|
// If len is greater than TrianglesData's current length, the new data is filled with default
|
2017-03-15 13:40:39 -05:00
|
|
|
// values ((0, 0), white, (0, 0), 0).
|
2017-02-28 11:24:25 -06:00
|
|
|
func (td *TrianglesData) SetLen(len int) {
|
|
|
|
if len > td.Len() {
|
|
|
|
needAppend := len - td.Len()
|
|
|
|
for i := 0; i < needAppend; i++ {
|
|
|
|
*td = append(*td, struct {
|
2017-03-05 05:17:07 -06:00
|
|
|
Position Vec
|
2017-04-09 15:00:26 -05:00
|
|
|
Color RGBA
|
2017-03-05 05:17:07 -06:00
|
|
|
Picture Vec
|
|
|
|
Intensity float64
|
2017-04-09 15:00:26 -05:00
|
|
|
}{V(0, 0), RGBA{1, 1, 1, 1}, V(0, 0), 0})
|
2017-02-28 11:24:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len < td.Len() {
|
|
|
|
*td = (*td)[:len]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Slice returns a sub-Triangles of this TrianglesData.
|
|
|
|
func (td *TrianglesData) Slice(i, j int) Triangles {
|
|
|
|
s := TrianglesData((*td)[i:j])
|
|
|
|
return &s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (td *TrianglesData) updateData(t Triangles) {
|
|
|
|
// fast path optimization
|
|
|
|
if t, ok := t.(*TrianglesData); ok {
|
|
|
|
copy(*td, *t)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// slow path manual copy
|
|
|
|
if t, ok := t.(TrianglesPosition); ok {
|
|
|
|
for i := range *td {
|
|
|
|
(*td)[i].Position = t.Position(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if t, ok := t.(TrianglesColor); ok {
|
|
|
|
for i := range *td {
|
|
|
|
(*td)[i].Color = t.Color(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if t, ok := t.(TrianglesPicture); ok {
|
|
|
|
for i := range *td {
|
2017-03-05 05:17:07 -06:00
|
|
|
(*td)[i].Picture, (*td)[i].Intensity = t.Picture(i)
|
2017-02-28 11:24:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update copies vertex properties from the supplied Triangles into this TrianglesData.
|
|
|
|
//
|
|
|
|
// TrianglesPosition, TrianglesColor and TrianglesTexture are supported.
|
|
|
|
func (td *TrianglesData) Update(t Triangles) {
|
|
|
|
if td.Len() != t.Len() {
|
2017-03-08 16:51:53 -06:00
|
|
|
panic(fmt.Errorf("(%T).Update: invalid triangles length", td))
|
2017-02-28 11:24:25 -06:00
|
|
|
}
|
|
|
|
td.updateData(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy returns an exact independent copy of this TrianglesData.
|
|
|
|
func (td *TrianglesData) Copy() Triangles {
|
|
|
|
copyTd := TrianglesData{}
|
|
|
|
copyTd.SetLen(td.Len())
|
|
|
|
copyTd.Update(td)
|
|
|
|
return ©Td
|
|
|
|
}
|
|
|
|
|
|
|
|
// Position returns the position property of i-th vertex.
|
|
|
|
func (td *TrianglesData) Position(i int) Vec {
|
|
|
|
return (*td)[i].Position
|
|
|
|
}
|
|
|
|
|
|
|
|
// Color returns the color property of i-th vertex.
|
2017-04-09 15:00:26 -05:00
|
|
|
func (td *TrianglesData) Color(i int) RGBA {
|
2017-02-28 11:24:25 -06:00
|
|
|
return (*td)[i].Color
|
|
|
|
}
|
|
|
|
|
|
|
|
// Picture returns the picture property of i-th vertex.
|
2017-03-05 05:17:07 -06:00
|
|
|
func (td *TrianglesData) Picture(i int) (pic Vec, intensity float64) {
|
|
|
|
return (*td)[i].Picture, (*td)[i].Intensity
|
2017-02-28 11:24:25 -06:00
|
|
|
}
|
|
|
|
|
2017-03-19 14:20:07 -05:00
|
|
|
// PictureData specifies an in-memory rectangular area of pixels and implements Picture and
|
2017-02-28 11:24:25 -06:00
|
|
|
// PictureColor.
|
|
|
|
//
|
|
|
|
// Pixels are small rectangles of unit size of form (x, y, x+1, y+1), where x and y are integers.
|
|
|
|
// PictureData contains and assigns a color to all pixels that are at least partially contained
|
|
|
|
// within it's Bounds (Rect).
|
|
|
|
//
|
|
|
|
// The struct's innards are exposed for convenience, manual modification is at your own risk.
|
2017-03-19 14:20:07 -05:00
|
|
|
//
|
2017-04-09 15:00:26 -05:00
|
|
|
// The format of the pixels is color.RGBA and not pixel.RGBA for a very serious reason:
|
|
|
|
// pixel.RGBA takes up 8x more memory than color.RGBA.
|
2017-02-28 11:24:25 -06:00
|
|
|
type PictureData struct {
|
2017-04-09 15:00:26 -05:00
|
|
|
Pix []color.RGBA
|
2017-02-28 11:24:25 -06:00
|
|
|
Stride int
|
|
|
|
Rect Rect
|
|
|
|
}
|
|
|
|
|
|
|
|
// MakePictureData creates a zero-initialized PictureData covering the given rectangle.
|
2017-03-05 17:44:16 -06:00
|
|
|
func MakePictureData(rect Rect) *PictureData {
|
2017-03-13 17:52:34 -05:00
|
|
|
w := int(math.Ceil(rect.Max.X())) - int(math.Floor(rect.Min.X()))
|
|
|
|
h := int(math.Ceil(rect.Max.Y())) - int(math.Floor(rect.Min.Y()))
|
2017-03-05 17:44:16 -06:00
|
|
|
pd := &PictureData{
|
2017-02-28 11:24:25 -06:00
|
|
|
Stride: w,
|
|
|
|
Rect: rect,
|
|
|
|
}
|
2017-04-09 15:00:26 -05:00
|
|
|
pd.Pix = make([]color.RGBA, w*h)
|
2017-02-28 11:24:25 -06:00
|
|
|
return pd
|
|
|
|
}
|
|
|
|
|
2017-04-09 15:00:26 -05:00
|
|
|
func verticalFlip(rgba *image.RGBA) {
|
|
|
|
bounds := rgba.Bounds()
|
2017-02-28 11:24:25 -06:00
|
|
|
width := bounds.Dx()
|
|
|
|
|
|
|
|
tmpRow := make([]uint8, width*4)
|
|
|
|
for i, j := 0, bounds.Dy()-1; i < j; i, j = i+1, j-1 {
|
2017-04-09 15:00:26 -05:00
|
|
|
iRow := rgba.Pix[i*rgba.Stride : i*rgba.Stride+width*4]
|
|
|
|
jRow := rgba.Pix[j*rgba.Stride : j*rgba.Stride+width*4]
|
2017-02-28 11:24:25 -06:00
|
|
|
|
|
|
|
copy(tmpRow, iRow)
|
|
|
|
copy(iRow, jRow)
|
|
|
|
copy(jRow, tmpRow)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PictureDataFromImage converts an image.Image into PictureData.
|
|
|
|
//
|
|
|
|
// The resulting PictureData's Bounds will be the equivalent of the supplied image.Image's Bounds.
|
2017-03-05 17:44:16 -06:00
|
|
|
func PictureDataFromImage(img image.Image) *PictureData {
|
2017-04-09 15:00:26 -05:00
|
|
|
var rgba *image.RGBA
|
|
|
|
if rgbaImg, ok := img.(*image.RGBA); ok {
|
|
|
|
rgba = rgbaImg
|
2017-03-01 05:34:30 -06:00
|
|
|
} else {
|
2017-04-09 15:00:26 -05:00
|
|
|
rgba = image.NewRGBA(img.Bounds())
|
|
|
|
draw.Draw(rgba, rgba.Bounds(), img, img.Bounds().Min, draw.Src)
|
2017-03-01 05:34:30 -06:00
|
|
|
}
|
2017-02-28 11:24:25 -06:00
|
|
|
|
2017-04-09 15:00:26 -05:00
|
|
|
verticalFlip(rgba)
|
2017-02-28 11:24:25 -06:00
|
|
|
|
|
|
|
pd := MakePictureData(R(
|
2017-04-09 15:00:26 -05:00
|
|
|
float64(rgba.Bounds().Min.X),
|
|
|
|
float64(rgba.Bounds().Min.Y),
|
|
|
|
float64(rgba.Bounds().Dx()),
|
|
|
|
float64(rgba.Bounds().Dy()),
|
2017-02-28 11:24:25 -06:00
|
|
|
))
|
|
|
|
|
|
|
|
for i := range pd.Pix {
|
2017-04-09 15:00:26 -05:00
|
|
|
pd.Pix[i].R = rgba.Pix[i*4+0]
|
|
|
|
pd.Pix[i].G = rgba.Pix[i*4+1]
|
|
|
|
pd.Pix[i].B = rgba.Pix[i*4+2]
|
|
|
|
pd.Pix[i].A = rgba.Pix[i*4+3]
|
2017-02-28 11:24:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return pd
|
|
|
|
}
|
|
|
|
|
|
|
|
// PictureDataFromPicture converts an arbitrary Picture into PictureData (the conversion may be
|
|
|
|
// lossy, because PictureData works with unit-sized pixels).
|
|
|
|
//
|
|
|
|
// Bounds are preserved.
|
2017-03-05 17:44:16 -06:00
|
|
|
func PictureDataFromPicture(pic Picture) *PictureData {
|
2017-03-05 16:16:22 -06:00
|
|
|
if pd, ok := pic.(*PictureData); ok {
|
2017-03-05 17:44:16 -06:00
|
|
|
return pd
|
2017-02-28 11:24:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
bounds := pic.Bounds()
|
|
|
|
pd := MakePictureData(bounds)
|
|
|
|
|
|
|
|
if pic, ok := pic.(PictureColor); ok {
|
2017-03-13 17:52:34 -05:00
|
|
|
for y := math.Floor(bounds.Min.Y()); y < bounds.Max.Y(); y++ {
|
|
|
|
for x := math.Floor(bounds.Min.X()); x < bounds.Max.X(); x++ {
|
2017-02-28 11:36:43 -06:00
|
|
|
// this together with the Floor is a trick to get all of the pixels
|
2017-02-28 11:34:02 -06:00
|
|
|
at := V(
|
2017-03-13 17:52:34 -05:00
|
|
|
math.Max(x, bounds.Min.X()),
|
|
|
|
math.Max(y, bounds.Min.Y()),
|
2017-02-28 11:34:02 -06:00
|
|
|
)
|
2017-02-28 11:24:25 -06:00
|
|
|
pd.SetColor(at, pic.Color(at))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pd
|
|
|
|
}
|
|
|
|
|
2017-04-09 15:00:26 -05:00
|
|
|
// Image converts PictureData into an image.RGBA.
|
2017-02-28 11:24:25 -06:00
|
|
|
//
|
2017-04-09 15:00:26 -05:00
|
|
|
// The resulting image.RGBA's Bounds will be equivalent of the PictureData's Bounds.
|
|
|
|
func (pd *PictureData) Image() *image.RGBA {
|
2017-02-28 11:24:25 -06:00
|
|
|
bounds := image.Rect(
|
2017-03-13 17:52:34 -05:00
|
|
|
int(math.Floor(pd.Rect.Min.X())),
|
|
|
|
int(math.Floor(pd.Rect.Min.Y())),
|
|
|
|
int(math.Ceil(pd.Rect.Max.X())),
|
|
|
|
int(math.Ceil(pd.Rect.Max.Y())),
|
2017-02-28 11:24:25 -06:00
|
|
|
)
|
2017-04-09 15:00:26 -05:00
|
|
|
rgba := image.NewRGBA(bounds)
|
2017-02-28 11:24:25 -06:00
|
|
|
|
|
|
|
i := 0
|
|
|
|
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
|
|
|
|
for x := bounds.Min.X; x < bounds.Max.X; x++ {
|
2017-03-20 17:13:18 -05:00
|
|
|
off := pd.Index(V(float64(x), float64(y)))
|
2017-04-09 15:00:26 -05:00
|
|
|
rgba.Pix[i*4+0] = pd.Pix[off].R
|
|
|
|
rgba.Pix[i*4+1] = pd.Pix[off].G
|
|
|
|
rgba.Pix[i*4+2] = pd.Pix[off].B
|
|
|
|
rgba.Pix[i*4+3] = pd.Pix[off].A
|
2017-02-28 11:24:25 -06:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-09 15:00:26 -05:00
|
|
|
verticalFlip(rgba)
|
2017-02-28 11:24:25 -06:00
|
|
|
|
2017-04-09 15:00:26 -05:00
|
|
|
return rgba
|
2017-02-28 11:24:25 -06:00
|
|
|
}
|
|
|
|
|
2017-03-20 17:13:18 -05:00
|
|
|
// Index returns the index of the pixel at the specified position inside the Pix slice.
|
|
|
|
func (pd *PictureData) Index(at Vec) int {
|
2017-03-13 17:52:34 -05:00
|
|
|
at -= pd.Rect.Min.Map(math.Floor)
|
2017-02-28 11:24:25 -06:00
|
|
|
x, y := int(at.X()), int(at.Y())
|
|
|
|
return y*pd.Stride + x
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bounds returns the bounds of this PictureData.
|
2017-03-05 16:16:22 -06:00
|
|
|
func (pd *PictureData) Bounds() Rect {
|
2017-02-28 11:24:25 -06:00
|
|
|
return pd.Rect
|
|
|
|
}
|
|
|
|
|
|
|
|
// Color returns the color located at the given position.
|
2017-04-09 15:00:26 -05:00
|
|
|
func (pd *PictureData) Color(at Vec) RGBA {
|
2017-02-28 11:24:25 -06:00
|
|
|
if !pd.Rect.Contains(at) {
|
2017-04-09 15:00:26 -05:00
|
|
|
return RGBA{0, 0, 0, 0}
|
2017-02-28 11:24:25 -06:00
|
|
|
}
|
2017-04-09 15:00:26 -05:00
|
|
|
return ToRGBA(pd.Pix[pd.Index(at)])
|
2017-02-28 11:24:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetColor changes the color located at the given position.
|
2017-03-19 14:20:07 -05:00
|
|
|
func (pd *PictureData) SetColor(at Vec, col color.Color) {
|
2017-02-28 11:24:25 -06:00
|
|
|
if !pd.Rect.Contains(at) {
|
|
|
|
return
|
|
|
|
}
|
2017-04-09 15:00:26 -05:00
|
|
|
rgba := ToRGBA(col)
|
|
|
|
pd.Pix[pd.Index(at)] = color.RGBA{
|
|
|
|
R: uint8(rgba.R * 255),
|
|
|
|
G: uint8(rgba.G * 255),
|
|
|
|
B: uint8(rgba.B * 255),
|
|
|
|
A: uint8(rgba.A * 255),
|
2017-03-23 17:07:49 -05:00
|
|
|
}
|
2017-02-28 11:24:25 -06:00
|
|
|
}
|