imgrow.DrawImage()

master
Charles Iliya Krempeaux 2024-02-21 07:14:28 -08:00
parent 0d476688dd
commit b5bd2da416
1 changed files with 78 additions and 0 deletions

78
drawimage.go 100644
View File

@ -0,0 +1,78 @@
package imgrow
import (
"image"
"image/color"
"image/draw"
)
// DrawImage returns an image that is 'img' where (only) drawing is scalad by 'scalar'.
// The actual image stays the same size.
// Only the drawing is scaled.
//
// So, for example:
//
// var newImage draw.Image = imgrow.Image(2, oldImage)
//
// ... would return a new image that is just like 'oldImage' except any drawing on it is 2 times as big.
func DrawImage(scalar int, img draw.Image) draw.Image {
return internalDrawImage {
img:img,
scalar:scalar,
}
}
var _ draw.Image = &internalDrawImage{}
type internalDrawImage struct {
img draw.Image
scalar int
}
func (receiver internalDrawImage) At(x, y int) color.Color {
var img image.Image = receiver.img
if nil == img {
return nil
}
return img.At(x, y)
}
func (receiver internalDrawImage) Bounds() image.Rectangle {
var img image.Image = receiver.img
if nil == img {
return image.Rectangle{}
}
return img.Bounds()
}
func (receiver internalDrawImage) ColorModel() color.Model {
var img image.Image = receiver.img
if nil == img {
return nil
}
return img.ColorModel()
}
func (receiver internalDrawImage) Set(x int, y int, c color.Color) {
var img draw.Image = receiver.img
if nil == img {
return
}
var scalar int = receiver.scalar
xScaled, yScaled := inverseScaleXY(scalar, x, y)
for yi := yScaled; yi < (yScaled + scalar); yi++ {
for xi := xScaled; xi < (xScaled + scalar); xi++ {
img.Set(xi, yi, c)
}
}
}