raw-buffer-api #1

Merged
sashakoshka merged 9 commits from raw-buffer-api into main 2023-01-14 19:04:35 -07:00
16 changed files with 260 additions and 325 deletions
Showing only changes of commit 34bf3038ac - Show all commits

View File

@ -1,2 +1,52 @@
package artist package artist
import "image"
import "image/color"
// Pattern is capable of generating a pattern pixel by pixel.
type Pattern interface {
// AtWhen returns the color of the pixel located at (x, y) relative to
// the origin point of the pattern (0, 0), when the pattern has the
// specified width and height. Patterns may ignore the width and height
// parameters, but it may be useful for some patterns such as gradients.
AtWhen (x, y, width, height int) (color.RGBA)
}
// Texture is a struct that allows an image to be converted into a tiling
// texture pattern.
type Texture struct {
data []color.RGBA
width, height int
}
// NewTexture converts an image into a texture.
func NewTexture (source image.Image) (texture Texture) {
bounds := source.Bounds()
texture.width = bounds.Dx()
texture.height = bounds.Dy()
texture.data = make([]color.RGBA, texture.width * texture.height)
index := 0
for y := bounds.Min.Y; y < bounds.Max.Y; y ++ {
for x := bounds.Min.X; x < bounds.Max.X; x ++ {
r, g, b, a := source.At(x, y).RGBA()
texture.data[index] = color.RGBA {
uint8(r >> 8),
uint8(g >> 8),
uint8(b >> 8),
uint8(a >> 8),
}
index ++
}}
return
}
// AtWhen returns the color at the specified x and y coordinates, wrapped to the
// image's width. the width and height are ignored.
func (texture Texture) AtWhen (x, y, width, height int) (pixel color.RGBA) {
x %= texture.width
y %= texture.height
if x < 0 { x += texture.width }
if y < 0 { y += texture.height }
return texture.data[x + y * texture.width]
}

View File

@ -7,10 +7,10 @@ import "git.tebibyte.media/sashakoshka/tomo"
// ShadingProfile contains shading information that can be used to draw chiseled // ShadingProfile contains shading information that can be used to draw chiseled
// objects. // objects.
type ShadingProfile struct { type ShadingProfile struct {
Highlight tomo.Image Highlight Pattern
Shadow tomo.Image Shadow Pattern
Stroke tomo.Image Stroke Pattern
Fill tomo.Image Fill Pattern
StrokeWeight int StrokeWeight int
ShadingWeight int ShadingWeight int
} }
@ -43,6 +43,7 @@ func ChiseledRectangle (
strokeWeight := profile.StrokeWeight strokeWeight := profile.StrokeWeight
shadingWeight := profile.ShadingWeight shadingWeight := profile.ShadingWeight
data, stride := destination.Buffer()
bounds = bounds.Canon() bounds = bounds.Canon()
updatedRegion = bounds updatedRegion = bounds