This repository has been archived by the owner on Aug 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
194 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,5 @@ | |
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
*.png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,5 @@ | ||
module github.com/dusk125/rectpack | ||
|
||
go 1.16 | ||
go 1.17 | ||
|
||
require ( | ||
github.com/dusk125/pixelutils v1.0.0 | ||
github.com/faiface/pixel v0.10.0 | ||
) | ||
require golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,4 @@ | ||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/dusk125/pixelutils v1.0.0 h1:oaJW0Bl6I+WQb4DU8gzMKxNW5+ZwBBQXQLriXsiaKEU= | ||
github.com/dusk125/pixelutils v1.0.0/go.mod h1:V17sw4+q2QWactw9hamp1XgGQQqKBJ6MAO1jEu+fPp8= | ||
github.com/faiface/glhf v0.0.0-20181018222622-82a6317ac380 h1:FvZ0mIGh6b3kOITxUnxS3tLZMh7yEoHo75v3/AgUqg0= | ||
github.com/faiface/glhf v0.0.0-20181018222622-82a6317ac380/go.mod h1:zqnPFFIuYFFxl7uH2gYByJwIVKG7fRqlqQCbzAnHs9g= | ||
github.com/faiface/mainthread v0.0.0-20171120011319-8b78f0a41ae3 h1:baVdMKlASEHrj19iqjARrPbaRisD7EuZEVJj6ZMLl1Q= | ||
github.com/faiface/mainthread v0.0.0-20171120011319-8b78f0a41ae3/go.mod h1:VEPNJUlxl5KdWjDvz6Q1l+rJlxF2i6xqDeGuGAxa87M= | ||
github.com/faiface/pixel v0.10.0 h1:EHm3ZdQw2Ck4y51cZqFfqQpwLqNHOoXwbNEc9Dijql0= | ||
github.com/faiface/pixel v0.10.0/go.mod h1:lU0YYcW77vL0F1CG8oX51GXurymL45MXd57otHNLK7A= | ||
github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7 h1:SCYMcCJ89LjRGwEa0tRluNRiMjZHalQZrVrvTbPh+qw= | ||
github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7/go.mod h1:482civXOzJJCPzJ4ZOX/pwvXBWSnzD4OKMdH4ClKGbk= | ||
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72 h1:b+9H1GAsx5RsjvDFLoS5zkNBzIQMuVKUYQDmxU3N5XE= | ||
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= | ||
github.com/go-gl/mathgl v0.0.0-20190416160123-c4601bc793c7 h1:THttjeRn1iiz69E875U6gAik8KTWk/JYAHoSVpUxBBI= | ||
github.com/go-gl/mathgl v0.0.0-20190416160123-c4601bc793c7/go.mod h1:yhpkQzEiH9yPyxDUGzkmgScbaBVlhC06qodikEM0ZwQ= | ||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= | ||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= | ||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/pzsz/voronoi v0.0.0-20130609164533-4314be88c79f h1:YeUeWAPTrycaikVXiVoAu1dxIR4+tSsn9IbTkNfRsss= | ||
github.com/pzsz/voronoi v0.0.0-20130609164533-4314be88c79f/go.mod h1:T6EcHUIQ7r0Xr1jjOJ7hmpUaqVyqb9WimeNzS9nz4Zo= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= | ||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= | ||
golang.org/x/image v0.0.0-20190321063152-3fc05d484e9f/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= | ||
golang.org/x/image v0.0.0-20190523035834-f03afa92d3ff/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= | ||
golang.org/x/image v0.0.0-20210607152325-775e3b0c77b9 h1:D0iM1dTCbD5Dg1CbuvLC/v/agLc79efSj/L35Q3Vqhs= | ||
golang.org/x/image v0.0.0-20210607152325-775e3b0c77b9/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d h1:RNPAfi2nHY7C2srAV8A49jpsYr0ADedCk1wq6fTMTvs= | ||
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM= | ||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | ||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package rectpack_test | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"image" | ||
"image/color" | ||
"image/jpeg" | ||
"image/png" | ||
"math/rand" | ||
"os" | ||
"path" | ||
"testing" | ||
|
||
"github.com/dusk125/rectpack" | ||
"golang.org/x/image/colornames" | ||
) | ||
|
||
func fill(w, h int, c color.Color) (img *image.RGBA) { | ||
img = image.NewRGBA(image.Rect(0, 0, w, h)) | ||
for x := 0; x < w; x++ { | ||
for y := 0; y < h; y++ { | ||
img.Set(x, y, c) | ||
} | ||
} | ||
return | ||
} | ||
|
||
func colorEq(i2 image.Image, w, h int, c color.Color) (err error) { | ||
var i1 = fill(w, h, c) | ||
|
||
if !i1.Bounds().Size().Eq(i2.Bounds().Size()) { | ||
return fmt.Errorf("Image sizes are not the same: Expected: %s, Got: %s", i1.Bounds().Size(), i2.Bounds().Size()) | ||
} | ||
|
||
for x := 0; x < i1.Bounds().Dx(); x++ { | ||
for y := 0; y < i1.Bounds().Dy(); y++ { | ||
r1, g1, b1, a1 := i1.At(x, y).RGBA() | ||
r2, g2, b2, a2 := i2.At(x, y).RGBA() | ||
if r1 != r2 || g1 != g2 || b1 != b2 || a1 != a2 { | ||
return fmt.Errorf("At: (%d, %d), Expected: (%v, %v, %v, %v), Got: (%v, %v, %v, %v)", x, y, r1, g1, b1, a1, r2, b2, g2, a2) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func TestNewPacker(t *testing.T) { | ||
t.Run("Test", func(t *testing.T) { | ||
pack := rectpack.NewPacker(rectpack.PackerCfg{}) | ||
colors := []struct { | ||
col color.Color | ||
w, h int | ||
}{ | ||
{ | ||
col: colornames.Black, | ||
w: rand.Intn(1024), | ||
h: rand.Intn(1024), | ||
}, | ||
{ | ||
col: colornames.Aliceblue, | ||
w: rand.Intn(1024), | ||
h: rand.Intn(1024), | ||
}, | ||
{ | ||
col: colornames.Navy, | ||
w: rand.Intn(1024), | ||
h: rand.Intn(1024), | ||
}, | ||
{ | ||
col: colornames.Salmon, | ||
w: rand.Intn(1024), | ||
h: rand.Intn(1024), | ||
}, | ||
{ | ||
col: colornames.Orchid, | ||
w: rand.Intn(1024), | ||
h: rand.Intn(1024), | ||
}, | ||
{ | ||
col: colornames.Olive, | ||
w: rand.Intn(1024), | ||
h: rand.Intn(1024), | ||
}, | ||
{ | ||
col: colornames.Oldlace, | ||
w: rand.Intn(1024), | ||
h: rand.Intn(1024), | ||
}, | ||
} | ||
for i, c := range colors { | ||
pack.Insert(i, fill(c.w, c.h, c.col)) | ||
} | ||
if err := pack.Pack(); err != nil { | ||
t.Error(err) | ||
} | ||
if err := pack.Save("test.png"); err != nil { | ||
t.Error(err) | ||
} | ||
for i, c := range colors { | ||
img := pack.SubImage(i) | ||
if err := colorEq(img, c.w, c.h, c.col); err != nil { | ||
t.Errorf("%d is not expected: %s", i, err.Error()) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
func Save(filename string, img image.Image) (err error) { | ||
var ( | ||
file *os.File | ||
) | ||
|
||
if err = os.Remove(filename); err != nil && !errors.Is(err, os.ErrNotExist) { | ||
return | ||
} | ||
|
||
if file, err = os.Create(filename); err != nil { | ||
return | ||
} | ||
defer file.Close() | ||
|
||
switch path.Ext(filename) { | ||
case ".png": | ||
err = png.Encode(file, img) | ||
case ".jpeg", ".jpg": | ||
err = jpeg.Encode(file, img, nil) | ||
default: | ||
err = errors.New("Bad extension") | ||
} | ||
if err != nil { | ||
return | ||
} | ||
|
||
return | ||
} |
Oops, something went wrong.