]> Cypherpunks repositories - gostls13.git/commitdiff
image/jpeg: fix bounds calculation for grayscale JPEG images.
authorNigel Tao <nigeltao@golang.org>
Tue, 17 May 2011 22:47:14 +0000 (15:47 -0700)
committerNigel Tao <nigeltao@golang.org>
Tue, 17 May 2011 22:47:14 +0000 (15:47 -0700)
Also add grayscale test cases for image/decode_test.

R=r
CC=golang-dev
https://golang.org/cl/4526063

src/pkg/image/decode_test.go
src/pkg/image/jpeg/reader.go
src/pkg/image/testdata/video-005.gray.jpeg [new file with mode: 0644]
src/pkg/image/testdata/video-005.gray.png [new file with mode: 0644]

index c957c8209e96761de6c53fdd33d70cf74761cfb8..540d5eda5c2164a98be3d2a4f31e235cda150e5d 100644 (file)
@@ -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
                                }
                        }
index f3a473b3515913c5878c3ba08bcd1f1ecbcdef3f..ef8383a35ef010dcf964af8a96c37c7088ed28ee 100644 (file)
@@ -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 (file)
index 0000000..f9d6e5c
Binary files /dev/null and b/src/pkg/image/testdata/video-005.gray.jpeg 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 (file)
index 0000000..0b0ee75
Binary files /dev/null and b/src/pkg/image/testdata/video-005.gray.png differ