Skip to content

Commit

Permalink
Add option to make most renders square
Browse files Browse the repository at this point in the history
  • Loading branch information
PassTheMayo committed Jul 13, 2024
1 parent 5915c69 commit f79bc6b
Show file tree
Hide file tree
Showing 16 changed files with 756 additions and 1 deletion.
4 changes: 4 additions & 0 deletions backbody.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,9 @@ func RenderBackBody(img image.Image, opts Options) *image.NRGBA {
// Right Leg
composite(output, backRightLeg, 8, 20)

if opts.Square {
output = squareAndCenter(output)
}

return scale(output, opts.Scale)
}
116 changes: 116 additions & 0 deletions backbody_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestBackBodySteve(t *testing.T) {
Scale: scale,
Overlay: true,
Slim: false,
Square: false,
})

if output.Bounds().Dx() < 1 {
Expand Down Expand Up @@ -55,6 +56,7 @@ func BenchmarkBackBodySteve(b *testing.B) {
Scale: defaultBenchmarkRenderScale,
Overlay: true,
Slim: false,
Square: false,
})
}
}
Expand All @@ -69,6 +71,7 @@ func TestBackBodyAlex(t *testing.T) {
Scale: scale,
Overlay: true,
Slim: true,
Square: false,
})

if output.Bounds().Dx() < 1 {
Expand Down Expand Up @@ -105,6 +108,119 @@ func BenchmarkBackBodyAlex(b *testing.B) {
Scale: defaultBenchmarkRenderScale,
Overlay: true,
Slim: true,
Square: false,
})
}
}

func TestBackBodySteveSquare(t *testing.T) {
rawSkin := skin.GetDefaultSkin(false)

for i := 0; i <= 8; i++ {
scale := 1 << i

output := skin.RenderBackBody(rawSkin, skin.Options{
Scale: scale,
Overlay: true,
Slim: false,
Square: true,
})

if output.Bounds().Dx() < 1 {
t.Fatalf("result image width is %d pixels\n", output.Bounds().Dx())
}

if output.Bounds().Dy() < 1 {
t.Fatalf("result image height is %d pixels\n", output.Bounds().Dy())
}

if output.Bounds().Size().X != output.Bounds().Size().Y {
t.Fatalf("result image is not square (%s)\n", output.Bounds().Size())
}

if writeRenders {
f, err := os.OpenFile(fmt.Sprintf("backbody_steve_test_%d_square.png", scale), os.O_CREATE|os.O_RDWR, 0777)

if err != nil {
t.Fatal(err)
}

if err = png.Encode(f, output); err != nil {
t.Fatal(err)
}

if err = f.Close(); err != nil {
t.Fatal(err)
}
}
}
}

func BenchmarkBackBodySteveSquare(b *testing.B) {
rawSkin := skin.GetDefaultSkin(false)

for n := 0; n <= b.N; n++ {
skin.RenderBackBody(rawSkin, skin.Options{
Scale: defaultBenchmarkRenderScale,
Overlay: true,
Slim: false,
Square: true,
})
}
}

func TestBackBodyAlexSquare(t *testing.T) {
rawSkin := skin.GetDefaultSkin(true)

for i := 0; i <= 8; i++ {
scale := 1 << i

output := skin.RenderBackBody(rawSkin, skin.Options{
Scale: scale,
Overlay: true,
Slim: true,
Square: true,
})

if output.Bounds().Dx() < 1 {
t.Fatalf("result image width is %d pixels\n", output.Bounds().Dx())
}

if output.Bounds().Dy() < 1 {
t.Fatalf("result image height is %d pixels\n", output.Bounds().Dy())
}

if output.Bounds().Size().X != output.Bounds().Size().Y {
t.Fatalf("result image is not square (%s)\n", output.Bounds().Size())
}

if writeRenders {
f, err := os.OpenFile(fmt.Sprintf("backbody_alex_test_%d_square.png", scale), os.O_CREATE|os.O_RDWR, 0777)

if err != nil {
t.Fatal(err)
}

if err = png.Encode(f, output); err != nil {
t.Fatal(err)
}

if err = f.Close(); err != nil {
t.Fatal(err)
}
}
}
}

func BenchmarkBackBodyAlexSquare(b *testing.B) {
rawSkin := skin.GetDefaultSkin(true)

for n := 0; n <= b.N; n++ {
skin.RenderBackBody(rawSkin, skin.Options{
Scale: defaultBenchmarkRenderScale,
Overlay: true,
Slim: true,
Square: true,
})
}
}
4 changes: 4 additions & 0 deletions body.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,9 @@ func RenderBody(img *image.NRGBA, opts Options) *image.NRGBA {
// Right Side of Head
compositeTransform(output, scale(rightHead, opts.Scale), sideMatrix, 2*scaleDouble, 3*scaleDouble)

if opts.Square {
return squareAndCenter(output)
}

return output
}
116 changes: 116 additions & 0 deletions body_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestFullBodySteve(t *testing.T) {
Scale: scale,
Overlay: true,
Slim: false,
Square: false,
})

if output.Bounds().Dx() < 1 {
Expand Down Expand Up @@ -55,6 +56,7 @@ func BenchmarkFullBodySteve(b *testing.B) {
Scale: defaultBenchmarkRenderScale,
Overlay: true,
Slim: false,
Square: false,
})
}
}
Expand All @@ -69,6 +71,7 @@ func TestFullBodyAlex(t *testing.T) {
Scale: scale,
Overlay: true,
Slim: true,
Square: false,
})

if output.Bounds().Dx() < 1 {
Expand Down Expand Up @@ -105,6 +108,119 @@ func BenchmarkFullBodyAlex(b *testing.B) {
Scale: defaultBenchmarkRenderScale,
Overlay: true,
Slim: true,
Square: false,
})
}
}

func TestFullBodySteveSquare(t *testing.T) {
rawSkin := skin.GetDefaultSkin(false)

for i := 0; i <= 8; i++ {
scale := 1 << i

output := skin.RenderBody(rawSkin, skin.Options{
Scale: scale,
Overlay: true,
Slim: false,
Square: true,
})

if output.Bounds().Dx() < 1 {
t.Fatalf("result image width is %d pixels\n", output.Bounds().Dx())
}

if output.Bounds().Dy() < 1 {
t.Fatalf("result image height is %d pixels\n", output.Bounds().Dy())
}

if output.Bounds().Size().X != output.Bounds().Size().Y {
t.Fatalf("result image is not square (%s)\n", output.Bounds().Size())
}

if writeRenders {
f, err := os.OpenFile(fmt.Sprintf("fullbody_steve_test_%d_square.png", scale), os.O_CREATE|os.O_RDWR, 0777)

if err != nil {
t.Fatal(err)
}

if err = png.Encode(f, output); err != nil {
t.Fatal(err)
}

if err = f.Close(); err != nil {
t.Fatal(err)
}
}
}
}

func BenchmarkFullBodySteveSquare(b *testing.B) {
rawSkin := skin.GetDefaultSkin(false)

for n := 0; n <= b.N; n++ {
skin.RenderBody(rawSkin, skin.Options{
Scale: defaultBenchmarkRenderScale,
Overlay: true,
Slim: false,
Square: true,
})
}
}

func TestFullBodyAlexSquare(t *testing.T) {
rawSkin := skin.GetDefaultSkin(true)

for i := 0; i <= 8; i++ {
scale := 1 << i

output := skin.RenderBody(rawSkin, skin.Options{
Scale: scale,
Overlay: true,
Slim: true,
Square: true,
})

if output.Bounds().Dx() < 1 {
t.Fatalf("result image width is %d pixels\n", output.Bounds().Dx())
}

if output.Bounds().Dy() < 1 {
t.Fatalf("result image height is %d pixels\n", output.Bounds().Dy())
}

if output.Bounds().Size().X != output.Bounds().Size().Y {
t.Fatalf("result image is not square (%s)\n", output.Bounds().Size())
}

if writeRenders {
f, err := os.OpenFile(fmt.Sprintf("fullbody_alex_test_%d_square.png", scale), os.O_CREATE|os.O_RDWR, 0777)

if err != nil {
t.Fatal(err)
}

if err = png.Encode(f, output); err != nil {
t.Fatal(err)
}

if err = f.Close(); err != nil {
t.Fatal(err)
}
}
}
}

func BenchmarkFullBodyAlexSquare(b *testing.B) {
rawSkin := skin.GetDefaultSkin(true)

for n := 0; n <= b.N; n++ {
skin.RenderBody(rawSkin, skin.Options{
Scale: defaultBenchmarkRenderScale,
Overlay: true,
Slim: true,
Square: true,
})
}
}
4 changes: 3 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Options struct {
Scale int `short:"s" long:"scale" description:"The scale of the rendered output image" default:"16"`
NoOverlay bool `short:"O" long:"no-overlay" description:"Disables the overlay layer of the resulting image"`
Slim bool `short:"S" long:"slim" description:"Enable this option if the input skin image is slim"`
Square bool `long:"square" description:"Forces the output image to be square"`
Output string `short:"o" long:"output" description:"The file to write the output image to" default:"output.png"`
Verbose bool `short:"V" long:"verbose" description:"Prints extra debug information"`
}
Expand Down Expand Up @@ -110,11 +111,12 @@ func main() {
}

var (
result *image.NRGBA
result *image.NRGBA = nil
options skin.Options = skin.Options{
Scale: opts.Scale,
Overlay: !opts.NoOverlay,
Slim: opts.Slim,
Square: opts.Square,
}
)

Expand Down
4 changes: 4 additions & 0 deletions face_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestFaceSteve(t *testing.T) {
Scale: scale,
Overlay: true,
Slim: false,
Square: false,
})

if output.Bounds().Dx() < 1 {
Expand Down Expand Up @@ -73,6 +74,7 @@ func BenchmarkFaceSteve(b *testing.B) {
Scale: defaultBenchmarkRenderScale,
Overlay: true,
Slim: false,
Square: false,
})
}
}
Expand All @@ -87,6 +89,7 @@ func TestFaceAlex(t *testing.T) {
Scale: scale,
Overlay: true,
Slim: true,
Square: false,
})

if output.Bounds().Dx() < 1 {
Expand Down Expand Up @@ -123,6 +126,7 @@ func BenchmarkFaceAlex(b *testing.B) {
Scale: defaultBenchmarkRenderScale,
Overlay: true,
Slim: true,
Square: false,
})
}
}
4 changes: 4 additions & 0 deletions frontbody.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,9 @@ func RenderFrontBody(img *image.NRGBA, opts Options) *image.NRGBA {
// Right Leg
composite(output, rightLeg, 4, 20)

if opts.Square {
output = squareAndCenter(output)
}

return scale(output, opts.Scale)
}
Loading

0 comments on commit f79bc6b

Please sign in to comment.