aboutsummaryrefslogtreecommitdiff
path: root/src/image
diff options
context:
space:
mode:
authorNigel Tao <nigeltao@golang.org>2015-05-05 17:39:09 +1000
committerNigel Tao <nigeltao@golang.org>2015-05-06 01:00:58 +0000
commit8ae44af2bb94963c626b2ec637fc580fdc7d7934 (patch)
treed382fd26b85996b318549a5e83f001c4a9e81815 /src/image
parent96dad7ff3ea15327b386c6497e171b119db33ee3 (diff)
downloadgo-8ae44af2bb94963c626b2ec637fc580fdc7d7934.tar.xz
image/gif: allow encoding a single-frame image whose top-left corner
isn't (0, 0). Also fix a s/b.Min.X/b.Max.X/ typo in bounds checking. Fixes #10676 Change-Id: Ie5ff7ec20ca30367a8e65d32061959a2d8e089e9 Reviewed-on: https://go-review.googlesource.com/9712 Reviewed-by: Rob Pike <r@golang.org>
Diffstat (limited to 'src/image')
-rw-r--r--src/image/gif/writer.go11
-rw-r--r--src/image/gif/writer_test.go28
2 files changed, 37 insertions, 2 deletions
diff --git a/src/image/gif/writer.go b/src/image/gif/writer.go
index e14d034602..dd317901d4 100644
--- a/src/image/gif/writer.go
+++ b/src/image/gif/writer.go
@@ -190,7 +190,7 @@ func (e *encoder) writeImageBlock(pm *image.Paletted, delay int, disposal byte)
}
b := pm.Bounds()
- if b.Dx() >= 1<<16 || b.Dy() >= 1<<16 || b.Min.X < 0 || b.Min.X >= 1<<16 || b.Min.Y < 0 || b.Min.Y >= 1<<16 {
+ if b.Min.X < 0 || b.Max.X >= 1<<16 || b.Min.Y < 0 || b.Max.Y >= 1<<16 {
e.err = errors.New("gif: image block is too large to encode")
return
}
@@ -365,6 +365,15 @@ func Encode(w io.Writer, m image.Image, o *Options) error {
opts.Drawer.Draw(pm, b, m, image.ZP)
}
+ // When calling Encode instead of EncodeAll, the single-frame image is
+ // translated such that its top-left corner is (0, 0), so that the single
+ // frame completely fills the overall GIF's bounds.
+ if pm.Rect.Min != (image.Point{}) {
+ dup := *pm
+ dup.Rect = dup.Rect.Sub(dup.Rect.Min)
+ pm = &dup
+ }
+
return EncodeAll(w, &GIF{
Image: []*image.Paletted{pm},
Delay: []int{0},
diff --git a/src/image/gif/writer_test.go b/src/image/gif/writer_test.go
index d33976e415..db61a5c3c2 100644
--- a/src/image/gif/writer_test.go
+++ b/src/image/gif/writer_test.go
@@ -297,7 +297,7 @@ func TestEncodeZeroGIF(t *testing.T) {
}
}
-func TestEncodeFrameOutOfBounds(t *testing.T) {
+func TestEncodeAllFramesOutOfBounds(t *testing.T) {
images := []*image.Paletted{
image.NewPaletted(image.Rect(0, 0, 5, 5), palette.Plan9),
image.NewPaletted(image.Rect(2, 2, 8, 8), palette.Plan9),
@@ -326,6 +326,32 @@ func TestEncodeFrameOutOfBounds(t *testing.T) {
}
}
+func TestEncodeNonZeroMinPoint(t *testing.T) {
+ points := []image.Point{
+ image.Point{-8, -9},
+ image.Point{-4, -4},
+ image.Point{-3, +3},
+ image.Point{+0, +0},
+ image.Point{+2, +2},
+ }
+ for _, p := range points {
+ src := image.NewPaletted(image.Rectangle{Min: p, Max: p.Add(image.Point{6, 6})}, palette.Plan9)
+ var buf bytes.Buffer
+ if err := Encode(&buf, src, nil); err != nil {
+ t.Errorf("p=%v: Encode: %v", p, err)
+ continue
+ }
+ m, err := Decode(&buf)
+ if err != nil {
+ t.Errorf("p=%v: Decode: %v", p, err)
+ continue
+ }
+ if got, want := m.Bounds(), image.Rect(0, 0, 6, 6); got != want {
+ t.Errorf("p=%v: got %v, want %v", p, got, want)
+ }
+ }
+}
+
func TestEncodeImplicitConfigSize(t *testing.T) {
// For backwards compatibility for Go 1.4 and earlier code, the Config
// field is optional, and if zero, the width and height is implied by the