Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added Bounds as own struct

  • Loading branch information...
commit 11ce6af8a2a4bdef83e846a689acd29a0befa0bd 1 parent 8c4321f
@tobi authored
View
40 bounds.go
@@ -0,0 +1,40 @@
+package mogrify
+
+import (
+ "math"
+ "fmt"
+)
+
+type Bounds struct {
+ Width, Height int
+}
+
+func (b Bounds) String() string {
+ return fmt.Sprintf("%dx%d", b.Width, b.Height)
+}
+
+func (b Bounds) ScaleProportionally(targetWidth, targetHeight int) Bounds {
+ scalex := (float64)(targetWidth) / (float64)(b.Width)
+ scaley := (float64)(targetHeight) / (float64)(b.Height)
+ scale := math.Min(scalex, scaley)
+
+ return Bounds{ (int)(math.Floor((float64)(b.Width) * scale)), (int)(math.Floor((float64)(b.Height) * scale)) }
+}
+
+func (b Bounds) ShrinkProportionally(targetWidth, targetHeight int) Bounds {
+ // Make sure there is work to be done
+ if b.Width < targetWidth || b.Height < targetHeight {
+ return b
+ }
+
+ return b.ScaleProportionally(targetWidth, targetHeight)
+}
+
+func (b Bounds) GrowProportionally(targetWidth, targetHeight int) Bounds {
+ // Make sure there is work to be done
+ if b.Width > targetWidth || b.Height > targetHeight {
+ return b
+ }
+
+ return b.ScaleProportionally(targetWidth, targetHeight)
+}
View
65 bounds_test.go
@@ -0,0 +1,65 @@
+package mogrify
+
+import (
+ "testing"
+)
+
+
+func TestBounds(t *testing.T) {
+
+ bounds := Bounds{100, 50}
+ if bounds.Width != 100 {
+ t.FailNow()
+ }
+
+ if bounds.Height != 50 {
+ t.FailNow()
+ }
+
+}
+
+func TestProportionalOperation(t *testing.T) {
+ // half
+ bounds := Bounds{100, 50}.ScaleProportionally(50, 50)
+ if bounds.Width != 50 { t.FailNow() }
+ if bounds.Height != 25 { t.FailNow() }
+
+ // no changes
+ bounds = Bounds{100, 50}.ScaleProportionally(100, 100)
+ if bounds.Width != 100 { t.FailNow() }
+ if bounds.Height != 50 { t.FailNow() }
+
+ // no changes
+ bounds = Bounds{100, 50}.ScaleProportionally(100, 100000000)
+ if bounds.Width != 100 { t.FailNow() }
+ if bounds.Height != 50 { t.FailNow() }
+
+ // scale up
+ bounds = Bounds{100, 50}.ScaleProportionally(1000, 1000)
+ if bounds.Width != 1000 { t.FailNow() }
+ if bounds.Height != 500 { t.FailNow() }
+}
+
+func TestShrink(t *testing.T) {
+ // half
+ bounds := Bounds{100, 50}.ShrinkProportionally(50, 50)
+ if bounds.Width != 50 { t.Errorf("Width is wrong: %d", bounds.Width) }
+ if bounds.Height != 25 { t.Errorf("Height is wrong: %d", bounds.Height) }
+
+ // no changes
+ bounds = Bounds{100, 50}.ShrinkProportionally(100000, 100000)
+ if bounds.Width != 100 { t.Errorf("Width is wrong: %d", bounds.Width) }
+ if bounds.Height != 50 { t.Errorf("Height is wrong: %d", bounds.Height) }
+}
+
+func TestGrow(t *testing.T) {
+ // no changes
+ bounds := Bounds{100, 50}.GrowProportionally(50, 50)
+ if bounds.Width != 100 { t.Errorf("Width is wrong: %d", bounds.Width) }
+ if bounds.Height != 50 { t.Errorf("Height is wrong: %d", bounds.Height) }
+
+ // no changes
+ bounds = Bounds{100, 50}.GrowProportionally(100000, 100000)
+ if bounds.Width != 100000 { t.Errorf("Width is wrong: %d", bounds.Width) }
+ if bounds.Height != 50000 { t.Errorf("Height is wrong: %d", bounds.Height) }
+}
View
4 gd_image.go
@@ -34,8 +34,8 @@ func (img *GdImage) NewResampled(width, height int) (*GdImage, error) {
return &GdImage{resized}, nil
}
-func (img *GdImage) Width() int {
- return img.image().width()
+func (img *GdImage) Bounds() Bounds {
+ return Bounds{img.image().width(), img.image().height()}
}
func (img *GdImage) Height() int {
View
10 image.go
@@ -2,7 +2,6 @@ package mogrify
import (
"errors"
- "fmt"
)
var (
@@ -11,9 +10,9 @@ var (
loadError = errors.New("Image cannot be loaded")
)
-type Image interface {
- Width() int
- Height() int
+type Image interface {
+ Bounds() Bounds
+
Destroy()
NewResampled(width, height int) (*GdImage, error)
@@ -22,6 +21,7 @@ type Image interface {
image() *gdImage
}
+
func Dimensions(img Image) string {
- return fmt.Sprintf("%dx%d", img.Width(), img.Height())
+ return img.Bounds().String()
}
View
8 image_test.go
@@ -36,12 +36,12 @@ func TestHeightWidth(t *testing.T) {
img := asset("./assets/image.jpg")
defer img.Destroy()
- if img.Width() != 600 {
- t.Fatalf("%d", img.Width())
+ if img.Bounds().Width != 600 {
+ t.Fatalf("%d", img.Bounds().Width)
}
- if img.Height() != 399 {
- t.Fatalf("%d", img.Height())
+ if img.Bounds().Height != 399 {
+ t.Fatalf("%d", img.Bounds().Height)
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.