2024-08-20 10:04:11 +04:00
|
|
|
package imageutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
|
|
|
"image/color"
|
|
|
|
"image/draw"
|
|
|
|
"image/png"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/potassium5703/texture"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Render(img image.Image, rect image.Rectangle) image.Image {
|
|
|
|
newimg := image.NewRGBA(rect)
|
|
|
|
draw.Draw(newimg, rect, image.White, image.ZP, draw.Src)
|
|
|
|
draw.Draw(newimg, rect, img, image.ZP, draw.Over)
|
|
|
|
return newimg
|
|
|
|
}
|
|
|
|
|
2024-09-03 11:01:55 +04:00
|
|
|
type SinglePixel struct{}
|
|
|
|
|
|
|
|
func (s SinglePixel) At(x, y int) color.Color {
|
|
|
|
return color.White
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s SinglePixel) ColorModel() color.Model {
|
|
|
|
return color.RGBAModel
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s SinglePixel) Bounds() image.Rectangle {
|
|
|
|
return image.Rect(0,0,1,1)
|
|
|
|
}
|
|
|
|
|
2024-08-20 10:04:11 +04:00
|
|
|
func BenchmarkScale(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
err := png.Encode(io.Discard,
|
|
|
|
Scale(Render(
|
|
|
|
texture.New(color.White, color.Black, 2),
|
2024-08-20 11:07:01 +04:00
|
|
|
image.Rect(0, 0, 64, 64),
|
|
|
|
), 64),
|
2024-08-20 10:04:11 +04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-03 11:01:55 +04:00
|
|
|
|
|
|
|
func BenchmarkSinglePixel(b *testing.B) {
|
|
|
|
instance := SinglePixel{}
|
|
|
|
err := png.Encode(io.Discard,
|
|
|
|
Scale(instance, b.N),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|