Compare commits

..

4 Commits

Author SHA1 Message Date
Charles Iliya Krempeaux 0d476688dd initial commits 2024-02-09 11:12:33 -08:00
Charles Iliya Krempeaux c63ac6dedd initial commits 2024-02-09 11:10:59 -08:00
Charles Iliya Krempeaux d49b848418 initial commits 2024-02-09 11:10:11 -08:00
Charles Iliya Krempeaux d95d73b49f initial commits 2024-02-09 11:10:01 -08:00
6 changed files with 1051 additions and 0 deletions

12
dividebyzero.go 100644
View File

@ -0,0 +1,12 @@
package imgrow
func divideByZero(n int) int {
switch {
case 0 < n:
return intMax
case n < 0:
return intMin
default: // 0 == n
return 0
}
}

61
image.go 100644
View File

@ -0,0 +1,61 @@
package imgrow
import (
"image"
"image/color"
)
// Image returns an image that is 'img' scalad by 'scalar'.
//
// So, for example:
//
// var newImage image.Image = imgrow.Image(2, oldImage)
//
// ... would return a new image that is just like 'oldImage' except that it is 2 times as big.
func Image(scalar int, img image.Image) image.Image {
return internalImage {
img:img,
scalar:scalar,
}
}
var _ image.Image = &internalImage{}
type internalImage struct {
img image.Image
scalar int
}
func (receiver internalImage) At(x, y int) color.Color {
var img image.Image = receiver.img
if nil == img {
return nil
}
var scalar int = receiver.scalar
x2, y2 := inverseScaleXY(scalar, x, y)
return img.At(x2, y2)
}
func (receiver internalImage) Bounds() image.Rectangle {
var img image.Image = receiver.img
if nil == img {
return image.Rectangle{}
}
return scaleRectangle(receiver.scalar, img.Bounds())
}
func (receiver internalImage) ColorModel() color.Model {
var img image.Image = receiver.img
if nil == img {
return nil
}
return img.ColorModel()
}

15
inversescale.go 100644
View File

@ -0,0 +1,15 @@
package imgrow
// inverseScale basically does:
//
// return n / scalar
//
// ... except, that it deals with the case where 0 == scalar in a special way.
func inverseScale(scalar int, n int) int {
switch {
case 0 == scalar:
return divideByZero(n)
default:
return n / scalar
}
}

View File

@ -0,0 +1,379 @@
package imgrow
import (
"testing"
)
func TestInverseScale(t *testing.T) {
tests := []struct{
Scalar int
N int
Expected int
}{
{
Scalar: 0,
N: -8,
Expected: intMin,
},
{
Scalar: 0,
N: -7,
Expected: intMin,
},
{
Scalar: 0,
N: -6,
Expected: intMin,
},
{
Scalar: 0,
N: -5,
Expected: intMin,
},
{
Scalar: 0,
N: -4,
Expected: intMin,
},
{
Scalar: 0,
N: -3,
Expected: intMin,
},
{
Scalar: 0,
N: -2,
Expected: intMin,
},
{
Scalar: 0,
N: -1,
Expected: intMin,
},
{
Scalar: 0,
N: 0,
Expected: 0,
},
{
Scalar: 0,
N: 1,
Expected: intMax,
},
{
Scalar: 0,
N: 2,
Expected: intMax,
},
{
Scalar: 0,
N: 3,
Expected: intMax,
},
{
Scalar: 0,
N: 4,
Expected: intMax,
},
{
Scalar: 0,
N: 5,
Expected: intMax,
},
{
Scalar: 0,
N: 6,
Expected: intMax,
},
{
Scalar: 0,
N: 7,
Expected: intMax,
},
{
Scalar: 0,
N: 8,
Expected: intMax,
},
{
Scalar: 1,
N: -8,
Expected: -8,
},
{
Scalar: 1,
N: -7,
Expected: -7,
},
{
Scalar: 1,
N: -6,
Expected: -6,
},
{
Scalar: 1,
N: -5,
Expected: -5,
},
{
Scalar: 1,
N: -4,
Expected: -4,
},
{
Scalar: 1,
N: -3,
Expected: -3,
},
{
Scalar: 1,
N: -2,
Expected: -2,
},
{
Scalar: 1,
N: -1,
Expected: -1,
},
{
Scalar: 1,
N: 0,
Expected: 0,
},
{
Scalar: 1,
N: 1,
Expected: 1,
},
{
Scalar: 1,
N: 2,
Expected: 2,
},
{
Scalar: 1,
N: 3,
Expected: 3,
},
{
Scalar: 1,
N: 4,
Expected: 4,
},
{
Scalar: 1,
N: 5,
Expected: 5,
},
{
Scalar: 1,
N: 6,
Expected: 6,
},
{
Scalar: 1,
N: 7,
Expected: 7,
},
{
Scalar: 1,
N: 8,
Expected: 8,
},
{
Scalar: 2,
N: -8,
Expected: -4,
},
{
Scalar: 2,
N: -7,
Expected: -3,
},
{
Scalar: 2,
N: -6,
Expected: -3,
},
{
Scalar: 2,
N: -5,
Expected: -2,
},
{
Scalar: 2,
N: -4,
Expected: -2,
},
{
Scalar: 2,
N: -3,
Expected: -1,
},
{
Scalar: 2,
N: -2,
Expected: -1,
},
{
Scalar: 2,
N: -1,
Expected: 0,
},
{
Scalar: 2,
N: 0,
Expected: 0,
},
{
Scalar: 2,
N: 1,
Expected: 0,
},
{
Scalar: 2,
N: 2,
Expected: 1,
},
{
Scalar: 2,
N: 3,
Expected: 1,
},
{
Scalar: 2,
N: 4,
Expected: 2,
},
{
Scalar: 2,
N: 5,
Expected: 2,
},
{
Scalar: 2,
N: 6,
Expected: 3,
},
{
Scalar: 2,
N: 7,
Expected: 3,
},
{
Scalar: 2,
N: 8,
Expected: 4,
},
{
Scalar: 3,
N: -8,
Expected: -2,
},
{
Scalar: 3,
N: -7,
Expected: -2,
},
{
Scalar: 3,
N: -6,
Expected: -2,
},
{
Scalar: 3,
N: -5,
Expected: -1,
},
{
Scalar: 3,
N: -4,
Expected: -1,
},
{
Scalar: 3,
N: -3,
Expected: -1,
},
{
Scalar: 3,
N: -2,
Expected: 0,
},
{
Scalar: 3,
N: -1,
Expected: 0,
},
{
Scalar: 3,
N: 0,
Expected: 0,
},
{
Scalar: 3,
N: 1,
Expected: 0,
},
{
Scalar: 3,
N: 2,
Expected: 0,
},
{
Scalar: 3,
N: 3,
Expected: 1,
},
{
Scalar: 3,
N: 4,
Expected: 1,
},
{
Scalar: 3,
N: 5,
Expected: 1,
},
{
Scalar: 3,
N: 6,
Expected: 2,
},
{
Scalar: 3,
N: 7,
Expected: 2,
},
{
Scalar: 3,
N: 8,
Expected: 2,
},
}
for testNumber, test := range tests {
actual := inverseScale(test.Scalar, test.N)
expected := test.Expected
if expected != actual {
t.Errorf("For test #%d, the actual value is not what was expected.", testNumber)
t.Logf("EXPECTED: %d", expected)
t.Logf("ACTUAL: %d", actual)
t.Logf("SCALAR: %d", test.Scalar)
t.Logf("N: %d", test.N)
continue
}
}
}

View File

@ -0,0 +1,5 @@
package imgrow
func inverseScaleXY(scalar int, x int, y int) (int, int) {
return inverseScale(scalar, x), inverseScale(scalar, y)
}

View File

@ -0,0 +1,579 @@
package imgrow
import (
"testing"
)
func TestInverseScaleXY(t *testing.T) {
tests := []struct{
Scalar int
X int
Y int
ExpectedX int
ExpectedY int
}{
{
Scalar: 0,
X: 0,
Y: 0,
ExpectedX: 0,
ExpectedY: 0,
},
{
Scalar: 0,
X: 1,
Y: 0,
ExpectedX: intMax,
ExpectedY: 0,
},
{
Scalar: 0,
X: 0,
Y: 1,
ExpectedX: 0,
ExpectedY: intMax,
},
{
Scalar: 0,
X: -1,
Y: 0,
ExpectedX: intMin,
ExpectedY: 0,
},
{
Scalar: 0,
X: 0,
Y: -1,
ExpectedX: 0,
ExpectedY: intMin,
},
{
Scalar: 0,
X: 1,
Y: 1,
ExpectedX: intMax,
ExpectedY: intMax,
},
{
Scalar: 0,
X: -1,
Y: -1,
ExpectedX: intMin,
ExpectedY: intMin,
},
{
Scalar: 0,
X: -1,
Y: 1,
ExpectedX: intMin,
ExpectedY: intMax,
},
{
Scalar: 0,
X: 1,
Y: -1,
ExpectedX: intMax,
ExpectedY: intMin,
},
{
Scalar: 0,
X: 5,
Y: -7,
ExpectedX: intMax,
ExpectedY: intMin,
},
{
Scalar: 1,
X: 0,
Y: 0,
ExpectedX: 0,
ExpectedY: 0,
},
{
Scalar: 1,
X: 1,
Y: 0,
ExpectedX: 1,
ExpectedY: 0,
},
{
Scalar: 1,
X: 2,
Y: 0,
ExpectedX: 2,
ExpectedY: 0,
},
{
Scalar: 1,
X: 3,
Y: 0,
ExpectedX: 3,
ExpectedY: 0,
},
{
Scalar: 1,
X: 4,
Y: 0,
ExpectedX: 4,
ExpectedY: 0,
},
{
Scalar: 1,
X: 5,
Y: 0,
ExpectedX: 5,
ExpectedY: 0,
},
{
Scalar: 1,
X: 6,
Y: 0,
ExpectedX: 6,
ExpectedY: 0,
},
{
Scalar: 1,
X: 7,
Y: 0,
ExpectedX: 7,
ExpectedY: 0,
},
{
Scalar: 1,
X: 8,
Y: 0,
ExpectedX: 8,
ExpectedY: 0,
},
{
Scalar: 1,
X: 9,
Y: 0,
ExpectedX: 9,
ExpectedY: 0,
},
{
Scalar: 1,
X: 10,
Y: 0,
ExpectedX: 10,
ExpectedY: 0,
},
{
Scalar: 1,
X: 0,
Y: 1,
ExpectedX: 0,
ExpectedY: 1,
},
{
Scalar: 1,
X: 1,
Y: 1,
ExpectedX: 1,
ExpectedY: 1,
},
{
Scalar: 1,
X: 2,
Y: 1,
ExpectedX: 2,
ExpectedY: 1,
},
{
Scalar: 1,
X: 3,
Y: 1,
ExpectedX: 3,
ExpectedY: 1,
},
{
Scalar: 1,
X: 4,
Y: 1,
ExpectedX: 4,
ExpectedY: 1,
},
{
Scalar: 1,
X: 5,
Y: 1,
ExpectedX: 5,
ExpectedY: 1,
},
{
Scalar: 1,
X: 6,
Y: 1,
ExpectedX: 6,
ExpectedY: 1,
},
{
Scalar: 1,
X: 7,
Y: 1,
ExpectedX: 7,
ExpectedY: 1,
},
{
Scalar: 1,
X: 8,
Y: 1,
ExpectedX: 8,
ExpectedY: 1,
},
{
Scalar: 1,
X: 9,
Y: 1,
ExpectedX: 9,
ExpectedY: 1,
},
{
Scalar: 1,
X: 10,
Y: 1,
ExpectedX: 10,
ExpectedY: 1,
},
{
Scalar: 1,
X: 0,
Y: 2,
ExpectedX: 0,
ExpectedY: 2,
},
{
Scalar: 1,
X: 1,
Y: 2,
ExpectedX: 1,
ExpectedY: 2,
},
{
Scalar: 1,
X: 2,
Y: 2,
ExpectedX: 2,
ExpectedY: 2,
},
{
Scalar: 1,
X: 3,
Y: 2,
ExpectedX: 3,
ExpectedY: 2,
},
{
Scalar: 1,
X: 4,
Y: 2,
ExpectedX: 4,
ExpectedY: 2,
},
{
Scalar: 1,
X: 5,
Y: 2,
ExpectedX: 5,
ExpectedY: 2,
},
{
Scalar: 1,
X: 6,
Y: 2,
ExpectedX: 6,
ExpectedY: 2,
},
{
Scalar: 1,
X: 7,
Y: 2,
ExpectedX: 7,
ExpectedY: 2,
},
{
Scalar: 1,
X: 8,
Y: 2,
ExpectedX: 8,
ExpectedY: 2,
},
{
Scalar: 1,
X: 9,
Y: 2,
ExpectedX: 9,
ExpectedY: 2,
},
{
Scalar: 1,
X: 10,
Y: 2,
ExpectedX: 10,
ExpectedY: 2,
},
{
Scalar: 1,
X: 0,
Y: 3,
ExpectedX: 0,
ExpectedY: 3,
},
{
Scalar: 1,
X: 1,
Y: 3,
ExpectedX: 1,
ExpectedY: 3,
},
{
Scalar: 1,
X: 2,
Y: 3,
ExpectedX: 2,
ExpectedY: 3,
},
{
Scalar: 1,
X: 3,
Y: 3,
ExpectedX: 3,
ExpectedY: 3,
},
{
Scalar: 1,
X: 4,
Y: 3,
ExpectedX: 4,
ExpectedY: 3,
},
{
Scalar: 1,
X: 5,
Y: 3,
ExpectedX: 5,
ExpectedY: 3,
},
{
Scalar: 1,
X: 6,
Y: 3,
ExpectedX: 6,
ExpectedY: 3,
},
{
Scalar: 1,
X: 7,
Y: 3,
ExpectedX: 7,
ExpectedY: 3,
},
{
Scalar: 1,
X: 8,
Y: 3,
ExpectedX: 8,
ExpectedY: 3,
},
{
Scalar: 1,
X: 9,
Y: 3,
ExpectedX: 9,
ExpectedY: 3,
},
{
Scalar: 1,
X: 10,
Y: 3,
ExpectedX: 10,
ExpectedY: 3,
},
{
Scalar: 1,
X: 0,
Y: 4,
ExpectedX: 0,
ExpectedY: 4,
},
{
Scalar: 1,
X: 1,
Y: 4,
ExpectedX: 1,
ExpectedY: 4,
},
{
Scalar: 1,
X: 2,
Y: 4,
ExpectedX: 2,
ExpectedY: 4,
},
{
Scalar: 1,
X: 3,
Y: 4,
ExpectedX: 3,
ExpectedY: 4,
},
{
Scalar: 1,
X: 4,
Y: 4,
ExpectedX: 4,
ExpectedY: 4,
},
{
Scalar: 1,
X: 5,
Y: 4,
ExpectedX: 5,
ExpectedY: 4,
},
{
Scalar: 1,
X: 6,
Y: 4,
ExpectedX: 6,
ExpectedY: 4,
},
{
Scalar: 1,
X: 7,
Y: 4,
ExpectedX: 7,
ExpectedY: 4,
},
{
Scalar: 1,
X: 8,
Y: 4,
ExpectedX: 8,
ExpectedY: 4,
},
{
Scalar: 1,
X: 9,
Y: 4,
ExpectedX: 9,
ExpectedY: 4,
},
{
Scalar: 1,
X: 10,
Y: 4,
ExpectedX: 10,
ExpectedY: 4,
},
{
Scalar: 1,
X: 0,
Y: 5,
ExpectedX: 0,
ExpectedY: 5,
},
{
Scalar: 1,
X: 1,
Y: 5,
ExpectedX: 1,
ExpectedY: 5,
},
{
Scalar: 1,
X: 2,
Y: 5,
ExpectedX: 2,
ExpectedY: 5,
},
{
Scalar: 1,
X: 3,
Y: 5,
ExpectedX: 3,
ExpectedY: 5,
},
{
Scalar: 1,
X: 4,
Y: 5,
ExpectedX: 4,
ExpectedY: 5,
},
{
Scalar: 1,
X: 5,
Y: 5,
ExpectedX: 5,
ExpectedY: 5,
},
{
Scalar: 1,
X: 6,
Y: 5,
ExpectedX: 6,
ExpectedY: 5,
},
{
Scalar: 1,
X: 7,
Y: 5,
ExpectedX: 7,
ExpectedY: 5,
},
{
Scalar: 1,
X: 8,
Y: 5,
ExpectedX: 8,
ExpectedY: 5,
},
{
Scalar: 1,
X: 9,
Y: 5,
ExpectedX: 9,
ExpectedY: 5,
},
{
Scalar: 1,
X: 10,
Y: 5,
ExpectedX: 10,
ExpectedY: 5,
},
}
for testNumber, test := range tests {
actualX, actualY := inverseScaleXY(test.Scalar, test.X, test.Y)
expectedX, expectedY := test.ExpectedX, test.ExpectedY
if expectedX != actualX || expectedY != actualY {
t.Errorf("For test #%d, the actual value is not what was expected.", testNumber)
t.Logf("EXPECTED (X,Y) = (%d, %d)", expectedX, expectedY)
t.Logf("ACTUAL (X,Y) = (%d, %d)", actualX, actualY)
t.Logf("SCALAR = %d", test.Scalar)
t.Logf("(X,Y) = (%d, %d)", test.X, test.Y)
continue
}
}
}