aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNigel Tao <nigeltao@golang.org>2011-05-17 15:47:14 -0700
committerNigel Tao <nigeltao@golang.org>2011-05-17 15:47:14 -0700
commit5f7b0bc36693c3790e4370c2892e10365a622aff (patch)
tree28a8ecb651fe7a669adcc617fbf943e05b3126b5
parent9fa6cb2f2bb77c23043dc38dbb26a3ab1aad51f2 (diff)
downloadgo-5f7b0bc36693c3790e4370c2892e10365a622aff.tar.gz
go-5f7b0bc36693c3790e4370c2892e10365a622aff.zip
image/jpeg: fix bounds calculation for grayscale JPEG images.
Also add grayscale test cases for image/decode_test. R=r CC=golang-dev https://golang.org/cl/4526063
-rw-r--r--src/pkg/image/decode_test.go46
-rw-r--r--src/pkg/image/jpeg/reader.go1
-rw-r--r--src/pkg/image/testdata/video-005.gray.jpegbin0 -> 5618 bytes
-rw-r--r--src/pkg/image/testdata/video-005.gray.pngbin0 -> 14974 bytes
4 files changed, 29 insertions, 18 deletions
diff --git a/src/pkg/image/decode_test.go b/src/pkg/image/decode_test.go
index c957c8209e..540d5eda5c 100644
--- a/src/pkg/image/decode_test.go
+++ b/src/pkg/image/decode_test.go
@@ -17,24 +17,27 @@ import (
_ "image/tiff"
)
-const goldenFile = "testdata/video-001.png"
-
type imageTest struct {
- filename string
- tolerance int
+ goldenFilename string
+ filename string
+ tolerance int
}
var imageTests = []imageTest{
- {"testdata/video-001.bmp", 0},
+ {"testdata/video-001.png", "testdata/video-001.bmp", 0},
// GIF images are restricted to a 256-color palette and the conversion
// to GIF loses significant image quality.
- {"testdata/video-001.gif", 64 << 8},
- {"testdata/video-001.interlaced.gif", 64 << 8},
- {"testdata/video-001.5bpp.gif", 128 << 8},
+ {"testdata/video-001.png", "testdata/video-001.gif", 64 << 8},
+ {"testdata/video-001.png", "testdata/video-001.interlaced.gif", 64 << 8},
+ {"testdata/video-001.png", "testdata/video-001.5bpp.gif", 128 << 8},
// JPEG is a lossy format and hence needs a non-zero tolerance.
- {"testdata/video-001.jpeg", 8 << 8},
- {"testdata/video-001.png", 0},
- {"testdata/video-001.tiff", 0},
+ {"testdata/video-001.png", "testdata/video-001.jpeg", 8 << 8},
+ {"testdata/video-001.png", "testdata/video-001.png", 0},
+ {"testdata/video-001.png", "testdata/video-001.tiff", 0},
+
+ // Test grayscale images.
+ {"testdata/video-005.gray.png", "testdata/video-005.gray.jpeg", 8 << 8},
+ {"testdata/video-005.gray.png", "testdata/video-005.gray.png", 0},
}
func decode(filename string) (image.Image, string, os.Error) {
@@ -74,26 +77,33 @@ func withinTolerance(c0, c1 image.Color, tolerance int) bool {
}
func TestDecode(t *testing.T) {
- golden, _, err := decode(goldenFile)
- if err != nil {
- t.Errorf("%s: %v", goldenFile, err)
- }
+ golden := make(map[string]image.Image)
loop:
for _, it := range imageTests {
+ g := golden[it.goldenFilename]
+ if g == nil {
+ var err os.Error
+ g, _, err = decode(it.goldenFilename)
+ if err != nil {
+ t.Errorf("%s: %v", it.goldenFilename, err)
+ continue loop
+ }
+ golden[it.goldenFilename] = g
+ }
m, imageFormat, err := decode(it.filename)
if err != nil {
t.Errorf("%s: %v", it.filename, err)
continue loop
}
- b := golden.Bounds()
+ b := g.Bounds()
if !b.Eq(m.Bounds()) {
t.Errorf("%s: want bounds %v got %v", it.filename, b, m.Bounds())
continue loop
}
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
- if !withinTolerance(golden.At(x, y), m.At(x, y), it.tolerance) {
- t.Errorf("%s: at (%d, %d), want %v got %v", it.filename, x, y, golden.At(x, y), m.At(x, y))
+ if !withinTolerance(g.At(x, y), m.At(x, y), it.tolerance) {
+ t.Errorf("%s: at (%d, %d), want %v got %v", it.filename, x, y, g.At(x, y), m.At(x, y))
continue loop
}
}
diff --git a/src/pkg/image/jpeg/reader.go b/src/pkg/image/jpeg/reader.go
index f3a473b351..ef8383a35e 100644
--- a/src/pkg/image/jpeg/reader.go
+++ b/src/pkg/image/jpeg/reader.go
@@ -200,6 +200,7 @@ func (d *decoder) processDQT(n int) os.Error {
func (d *decoder) makeImg(h0, v0, mxx, myy int) {
if d.nComp == nGrayComponent {
d.img1 = image.NewGray(8*mxx, 8*myy)
+ d.img1.Rect = image.Rect(0, 0, d.width, d.height)
return
}
var subsampleRatio ycbcr.SubsampleRatio
diff --git a/src/pkg/image/testdata/video-005.gray.jpeg b/src/pkg/image/testdata/video-005.gray.jpeg
new file mode 100644
index 0000000000..f9d6e5cdb4
--- /dev/null
+++ b/src/pkg/image/testdata/video-005.gray.jpeg
Binary files differ
diff --git a/src/pkg/image/testdata/video-005.gray.png b/src/pkg/image/testdata/video-005.gray.png
new file mode 100644
index 0000000000..0b0ee75384
--- /dev/null
+++ b/src/pkg/image/testdata/video-005.gray.png
Binary files differ