just for test, and after then rewrite concat's At function

This commit is contained in:
potassium 2024-05-01 15:50:36 +03:00
parent 479bfc4e86
commit f4985ae53c

View File

@ -1,4 +1,5 @@
//go:build todo //go:build todo
package imageutils package imageutils
import ( import (
@ -7,10 +8,21 @@ import (
"image/draw" "image/draw"
) )
type Side int
const (
Left Side = iota
Right
Up
Down
)
type pair struct { type pair struct {
first, second image.Image first, second image.Image
side Side
} }
// Takes color.Model of the first Image.
func (p pair) ColorModel() color.Model { return p.first.ColorModel() } func (p pair) ColorModel() color.Model { return p.first.ColorModel() }
func (p pair) Bounds() image.Rectangle { func (p pair) Bounds() image.Rectangle {
@ -19,27 +31,59 @@ func (p pair) Bounds() image.Rectangle {
b2 = p.second.Bounds() b2 = p.second.Bounds()
) )
return image.Rectangle{ rect := image.Rectangle{}
switch p.side {
case Left:
fallthrough
case Right:
rect = image.Rectangle{
image.ZP, image.ZP,
image.Point{ image.Point{
X: b1.Dx() + b2.Dx(), X: b1.Dx() + b2.Dx(),
Y: max(b1.Dy(), b2.Dy()), Y: max(b1.Dy(), b2.Dy()),
}, },
} }
case Up:
fallthrough
case Down:
rect = image.Rectangle{
image.ZP,
image.Point{
X: max(b1.Dx(), b2.Dx()),
Y: b1.Dy() + b2.Dy(),
},
}
}
return rect
} }
func (p pair) At(x, y int) color.Color { func (p pair) At(x, y int) color.Color {
rgbaImg := image.NewRGBA(p.Bounds()) rgbaImg := image.NewRGBA(p.Bounds())
switch p.side {
case Left:
draw.Draw( draw.Draw(
rgbaImg, rgbaImg, rgbaImg.Bounds(),
rgbaImg.Bounds(), p.second,
p.first, image.ZP, draw.Src,
image.ZP,
draw.Src,
) )
draw.Draw( draw.Draw(
rgbaImg, rgbaImg, p.Bounds(),
p.Bounds(), p.first,
image.Point{
p.second.Bounds().Dx(),
0,
},
draw.Src,
)
case Right:
draw.Draw(
rgbaImg, rgbaImg.Bounds(),
p.first,
image.ZP, draw.Src,
)
draw.Draw(
rgbaImg, p.Bounds(),
p.second, p.second,
image.Point{ image.Point{
p.first.Bounds().Dx(), p.first.Bounds().Dx(),
@ -47,10 +91,41 @@ func (p pair) At(x, y int) color.Color {
}, },
draw.Src, draw.Src,
) )
return rgbaImg case Up:
draw.Draw(
rgbaImg, rgbaImg.Bounds()
p.first,
image.ZP, draw.Src
)
draw.Draw(
rgbaImg, p.Bounds(),
p.second,
image.Point{
0,
p.first.Bounds().Dy()
},
draw.Src,
)
case Down:
draw.Draw(
rgbaImg, rgbaImg.Bounds()
p.second,
image.ZP, draw.Src
)
draw.Draw(
rgbaImg, p.Bounds(),
p.first,
image.Point{
0,
p.second.Bounds().Dy()
},
draw.Src,
)
}
return rgbaImg.At(x, y)
} }
// Concat concatenates second image to the right side of first. // Concat concatenates second image on a given side.
func Concat(i1, i2 image.Image) image.Image { func Concat(i1, i2 image.Image, s Side) image.Image {
return pair{i1, i2} return pair{i1, i2, s}
} }