2023-02-25 23:41:16 +00:00
|
|
|
package patterns
|
2023-01-09 06:03:19 +00:00
|
|
|
|
2023-01-14 06:54:57 +00:00
|
|
|
import "image"
|
2023-04-30 17:45:21 +00:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
2023-01-14 06:54:57 +00:00
|
|
|
|
2023-02-25 23:41:16 +00:00
|
|
|
// Texture is a pattern that tiles the content of a canvas both horizontally and
|
|
|
|
// vertically.
|
2023-01-14 06:54:57 +00:00
|
|
|
type Texture struct {
|
2023-04-30 17:45:21 +00:00
|
|
|
artist.Canvas
|
2023-01-14 06:54:57 +00:00
|
|
|
}
|
|
|
|
|
2023-03-12 06:04:06 +00:00
|
|
|
// Draw tiles the pattern's canvas within the given bounds. The minimum
|
2023-04-03 02:37:38 +00:00
|
|
|
// point of the pattern's canvas will be lined up with the minimum point of the
|
|
|
|
// bounding rectangle.
|
2023-04-30 17:45:21 +00:00
|
|
|
func (pattern Texture) Draw (destination artist.Canvas, bounds image.Rectangle) {
|
2023-04-03 02:37:38 +00:00
|
|
|
dstBounds := bounds.Canon().Intersect(destination.Bounds())
|
|
|
|
if dstBounds.Empty() { return }
|
|
|
|
|
2023-02-25 23:41:16 +00:00
|
|
|
dstData, dstStride := destination.Buffer()
|
|
|
|
srcData, srcStride := pattern.Buffer()
|
|
|
|
srcBounds := pattern.Bounds()
|
2023-02-27 21:38:33 +00:00
|
|
|
|
2023-04-03 02:37:38 +00:00
|
|
|
// offset is a vector that is added to points in destination space to
|
|
|
|
// convert them to points in source space
|
|
|
|
offset := srcBounds.Min.Sub(bounds.Min)
|
|
|
|
|
|
|
|
// calculate the starting position in source space
|
|
|
|
srcPoint := dstBounds.Min.Add(offset)
|
2023-03-07 02:34:14 +00:00
|
|
|
srcPoint.X = wrap(srcPoint.X, srcBounds.Min.X, srcBounds.Max.X)
|
|
|
|
srcPoint.Y = wrap(srcPoint.Y, srcBounds.Min.Y, srcBounds.Max.Y)
|
2023-04-03 02:37:38 +00:00
|
|
|
srcStartPoint := srcPoint
|
|
|
|
|
|
|
|
// for each row
|
|
|
|
dstPoint := image.Point { }
|
|
|
|
for dstPoint.Y = dstBounds.Min.Y; dstPoint.Y < dstBounds.Max.Y; dstPoint.Y ++ {
|
|
|
|
srcPoint.X = srcStartPoint.X
|
|
|
|
dstPoint.X = dstBounds.Min.X
|
2023-03-07 17:48:29 +00:00
|
|
|
dstYComponent := dstPoint.Y * dstStride
|
|
|
|
srcYComponent := srcPoint.Y * srcStride
|
2023-04-03 02:37:38 +00:00
|
|
|
|
|
|
|
// for each pixel in the row
|
2023-03-07 17:48:29 +00:00
|
|
|
for {
|
2023-04-03 02:37:38 +00:00
|
|
|
// draw pixel
|
2023-03-07 17:48:29 +00:00
|
|
|
dstIndex := dstYComponent + dstPoint.X
|
|
|
|
srcIndex := srcYComponent + srcPoint.X
|
2023-03-07 02:34:14 +00:00
|
|
|
dstData[dstIndex] = srcData[srcIndex]
|
|
|
|
|
2023-04-03 02:37:38 +00:00
|
|
|
// increment X in source space. wrap to start if out of
|
|
|
|
// bounds.
|
2023-03-07 02:34:14 +00:00
|
|
|
srcPoint.X ++
|
|
|
|
if srcPoint.X >= srcBounds.Max.X {
|
|
|
|
srcPoint.X = srcBounds.Min.X
|
|
|
|
}
|
2023-03-07 17:48:29 +00:00
|
|
|
|
2023-04-03 02:37:38 +00:00
|
|
|
// increment X in destination space. stop drawing this
|
|
|
|
// row if out of bounds.
|
2023-03-07 17:48:29 +00:00
|
|
|
dstPoint.X ++
|
2023-04-03 02:37:38 +00:00
|
|
|
if dstPoint.X >= dstBounds.Max.X {
|
2023-03-07 17:48:29 +00:00
|
|
|
break
|
|
|
|
}
|
2023-03-07 02:34:14 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 02:37:38 +00:00
|
|
|
// increment row in source space. wrap to start if out of
|
|
|
|
// bounds.
|
2023-03-07 02:34:14 +00:00
|
|
|
srcPoint.Y ++
|
|
|
|
if srcPoint.Y >= srcBounds.Max.Y {
|
|
|
|
srcPoint.Y = srcBounds.Min.Y
|
|
|
|
}
|
|
|
|
}
|
2023-01-14 06:54:57 +00:00
|
|
|
}
|
|
|
|
|
2023-02-25 23:41:16 +00:00
|
|
|
func wrap (value, min, max int) int {
|
|
|
|
difference := max - min
|
|
|
|
value = (value - min) % difference
|
|
|
|
if value < 0 { value += difference }
|
|
|
|
return value + min
|
2023-01-14 06:54:57 +00:00
|
|
|
}
|