diff options
Diffstat (limited to 'src/pkg/image')
-rw-r--r-- | src/pkg/image/color/palette/gen.go | 4 | ||||
-rw-r--r-- | src/pkg/image/color/palette/palette.go | 4 | ||||
-rw-r--r-- | src/pkg/image/gif/reader.go | 22 | ||||
-rw-r--r-- | src/pkg/image/gif/reader_test.go | 4 | ||||
-rw-r--r-- | src/pkg/image/jpeg/huffman.go | 6 | ||||
-rw-r--r-- | src/pkg/image/jpeg/reader_test.go | 27 | ||||
-rw-r--r-- | src/pkg/image/jpeg/scan.go | 21 | ||||
-rw-r--r-- | src/pkg/image/png/reader.go | 10 | ||||
-rw-r--r-- | src/pkg/image/testdata/video-001.separate.dc.progression.jpeg | bin | 0 -> 14288 bytes | |||
-rw-r--r-- | src/pkg/image/testdata/video-001.separate.dc.progression.progressive.jpeg | bin | 0 -> 14312 bytes |
10 files changed, 66 insertions, 32 deletions
diff --git a/src/pkg/image/color/palette/gen.go b/src/pkg/image/color/palette/gen.go index f20c021de..4f4d88345 100644 --- a/src/pkg/image/color/palette/gen.go +++ b/src/pkg/image/color/palette/gen.go @@ -14,6 +14,10 @@ import ( ) func main() { + fmt.Println(`// Copyright 2013 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file.`) + fmt.Println() fmt.Println("// generated by go run gen.go; DO NOT EDIT") fmt.Println() fmt.Println("// Package palette provides standard color palettes.") diff --git a/src/pkg/image/color/palette/palette.go b/src/pkg/image/color/palette/palette.go index 3aba7401d..f761e5368 100644 --- a/src/pkg/image/color/palette/palette.go +++ b/src/pkg/image/color/palette/palette.go @@ -1,3 +1,7 @@ +// Copyright 2013 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + // generated by go run gen.go; DO NOT EDIT // Package palette provides standard color palettes. diff --git a/src/pkg/image/gif/reader.go b/src/pkg/image/gif/reader.go index 8b0298a29..926710a45 100644 --- a/src/pkg/image/gif/reader.go +++ b/src/pkg/image/gif/reader.go @@ -79,7 +79,8 @@ type decoder struct { imageFields byte // From graphics control. - transparentIndex byte + transparentIndex byte + hasTransparentIndex bool // Computed. pixelSize uint @@ -175,11 +176,12 @@ func (d *decoder) decode(r io.Reader, configOnly bool) error { if err != nil { return err } - // TODO: do we set transparency in this map too? That would be - // d.setTransparency(m.Palette) } else { m.Palette = d.globalColorMap } + if d.hasTransparentIndex && int(d.transparentIndex) < len(m.Palette) { + m.Palette[d.transparentIndex] = color.RGBA{} + } litWidth, err := d.r.ReadByte() if err != nil { return err @@ -228,7 +230,11 @@ func (d *decoder) decode(r io.Reader, configOnly bool) error { d.image = append(d.image, m) d.delay = append(d.delay, d.delayTime) - d.delayTime = 0 // TODO: is this correct, or should we hold on to the value? + // The GIF89a spec, Section 23 (Graphic Control Extension) says: + // "The scope of this extension is the first graphic rendering block + // to follow." We therefore reset the GCE fields to zero. + d.delayTime = 0 + d.hasTransparentIndex = false case sTrailer: if len(d.image) == 0 { @@ -339,17 +345,11 @@ func (d *decoder) readGraphicControl() error { d.delayTime = int(d.tmp[2]) | int(d.tmp[3])<<8 if d.flags&gcTransparentColorSet != 0 { d.transparentIndex = d.tmp[4] - d.setTransparency(d.globalColorMap) + d.hasTransparentIndex = true } return nil } -func (d *decoder) setTransparency(colorMap color.Palette) { - if int(d.transparentIndex) < len(colorMap) { - colorMap[d.transparentIndex] = color.RGBA{} - } -} - func (d *decoder) newImageFromDescriptor() (*image.Paletted, error) { if _, err := io.ReadFull(d.r, d.tmp[0:9]); err != nil { return nil, fmt.Errorf("gif: can't read image descriptor: %s", err) diff --git a/src/pkg/image/gif/reader_test.go b/src/pkg/image/gif/reader_test.go index 09867132d..fc2041e99 100644 --- a/src/pkg/image/gif/reader_test.go +++ b/src/pkg/image/gif/reader_test.go @@ -1,3 +1,7 @@ +// Copyright 2013 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package gif import ( diff --git a/src/pkg/image/jpeg/huffman.go b/src/pkg/image/jpeg/huffman.go index 9b731fdc4..f53d873a5 100644 --- a/src/pkg/image/jpeg/huffman.go +++ b/src/pkg/image/jpeg/huffman.go @@ -37,6 +37,9 @@ func (d *decoder) ensureNBits(n int) error { for d.b.n < n { c, err := d.r.ReadByte() if err != nil { + if err == io.EOF { + return FormatError("short Huffman data") + } return err } d.b.a = d.b.a<<8 | uint32(c) @@ -50,6 +53,9 @@ func (d *decoder) ensureNBits(n int) error { if c == 0xff { c, err = d.r.ReadByte() if err != nil { + if err == io.EOF { + return FormatError("short Huffman data") + } return err } if c != 0x00 { diff --git a/src/pkg/image/jpeg/reader_test.go b/src/pkg/image/jpeg/reader_test.go index e951e038c..926bb0434 100644 --- a/src/pkg/image/jpeg/reader_test.go +++ b/src/pkg/image/jpeg/reader_test.go @@ -28,6 +28,7 @@ func TestDecodeProgressive(t *testing.T) { "../testdata/video-001.q50.444", "../testdata/video-005.gray.q50", "../testdata/video-005.gray.q50.2x2", + "../testdata/video-001.separate.dc.progression", } for _, tc := range testCases { m0, err := decodeFile(tc + ".jpeg") @@ -44,6 +45,12 @@ func TestDecodeProgressive(t *testing.T) { t.Errorf("%s: bounds differ: %v and %v", tc, m0.Bounds(), m1.Bounds()) continue } + // All of the video-*.jpeg files are 150x103. + if m0.Bounds() != image.Rect(0, 0, 150, 103) { + t.Errorf("%s: bad bounds: %v", tc, m0.Bounds()) + continue + } + switch m0 := m0.(type) { case *image.YCbCr: m1 := m1.(*image.YCbCr) @@ -84,18 +91,15 @@ func decodeFile(filename string) (image.Image, error) { // check checks that the two pix data are equal, within the given bounds. func check(bounds image.Rectangle, pix0, pix1 []byte, stride0, stride1 int) error { - if len(pix0) != len(pix1) { - return fmt.Errorf("len(pix) %d and %d differ", len(pix0), len(pix1)) - } - if stride0 != stride1 { - return fmt.Errorf("strides %d and %d differ", stride0, stride1) + if stride0 <= 0 || stride0%8 != 0 { + return fmt.Errorf("bad stride %d", stride0) } - if stride0%8 != 0 { - return fmt.Errorf("stride %d is not a multiple of 8", stride0) + if stride1 <= 0 || stride1%8 != 0 { + return fmt.Errorf("bad stride %d", stride1) } // Compare the two pix data, one 8x8 block at a time. - for y := 0; y < len(pix0)/stride0; y += 8 { - for x := 0; x < stride0; x += 8 { + for y := 0; y < len(pix0)/stride0 && y < len(pix1)/stride1; y += 8 { + for x := 0; x < stride0 && x < stride1; x += 8 { if x >= bounds.Max.X || y >= bounds.Max.Y { // We don't care if the two pix data differ if the 8x8 block is // entirely outside of the image's bounds. For example, this can @@ -108,8 +112,9 @@ func check(bounds image.Rectangle, pix0, pix1 []byte, stride0, stride1 int) erro for j := 0; j < 8; j++ { for i := 0; i < 8; i++ { - index := (y+j)*stride0 + (x + i) - if pix0[index] != pix1[index] { + index0 := (y+j)*stride0 + (x + i) + index1 := (y+j)*stride1 + (x + i) + if pix0[index0] != pix1[index1] { return fmt.Errorf("blocks at (%d, %d) differ:\n%sand\n%s", x, y, pixString(pix0, stride0, x, y), pixString(pix1, stride1, x, y), diff --git a/src/pkg/image/jpeg/scan.go b/src/pkg/image/jpeg/scan.go index a69ed1748..559235d51 100644 --- a/src/pkg/image/jpeg/scan.go +++ b/src/pkg/image/jpeg/scan.go @@ -141,25 +141,30 @@ func (d *decoder) processSOS(n int) error { for j := 0; j < d.comp[compIndex].h*d.comp[compIndex].v; j++ { // The blocks are traversed one MCU at a time. For 4:2:0 chroma // subsampling, there are four Y 8x8 blocks in every 16x16 MCU. + // // For a baseline 32x16 pixel image, the Y blocks visiting order is: // 0 1 4 5 // 2 3 6 7 // - // For progressive images, the DC data blocks (zigStart == 0) are traversed - // as above, but AC data blocks are traversed left to right, top to bottom: + // For progressive images, the interleaved scans (those with nComp > 1) + // are traversed as above, but non-interleaved scans are traversed left + // to right, top to bottom: // 0 1 2 3 // 4 5 6 7 + // Only DC scans (zigStart == 0) can be interleaved. AC scans must have + // only one component. // - // To further complicate matters, there is no AC data for any blocks that - // are inside the image at the MCU level but outside the image at the pixel - // level. For example, a 24x16 pixel 4:2:0 progressive image consists of - // two 16x16 MCUs. The earlier scans will process 8 Y blocks: + // To further complicate matters, for non-interleaved scans, there is no + // data for any blocks that are inside the image at the MCU level but + // outside the image at the pixel level. For example, a 24x16 pixel 4:2:0 + // progressive image consists of two 16x16 MCUs. The interleaved scans + // will process 8 Y blocks: // 0 1 4 5 // 2 3 6 7 - // The later scans will process only 6 Y blocks: + // The non-interleaved scans will process only 6 Y blocks: // 0 1 2 // 3 4 5 - if zigStart == 0 { + if nComp != 1 { mx0, my0 = d.comp[compIndex].h*mx, d.comp[compIndex].v*my if h0 == 1 { my0 += j diff --git a/src/pkg/image/png/reader.go b/src/pkg/image/png/reader.go index a6bf86ede..dfe299102 100644 --- a/src/pkg/image/png/reader.go +++ b/src/pkg/image/png/reader.go @@ -505,8 +505,14 @@ func (d *decoder) decode() (image.Image, error) { } // Check for EOF, to verify the zlib checksum. - n, err := r.Read(pr[:1]) - if err != io.EOF { + n := 0 + for i := 0; n == 0 && err == nil; i++ { + if i == 100 { + return nil, io.ErrNoProgress + } + n, err = r.Read(pr[:1]) + } + if err != nil && err != io.EOF { return nil, FormatError(err.Error()) } if n != 0 || d.idatLength != 0 { diff --git a/src/pkg/image/testdata/video-001.separate.dc.progression.jpeg b/src/pkg/image/testdata/video-001.separate.dc.progression.jpeg Binary files differnew file mode 100644 index 000000000..107f0fa0c --- /dev/null +++ b/src/pkg/image/testdata/video-001.separate.dc.progression.jpeg diff --git a/src/pkg/image/testdata/video-001.separate.dc.progression.progressive.jpeg b/src/pkg/image/testdata/video-001.separate.dc.progression.progressive.jpeg Binary files differnew file mode 100644 index 000000000..a1d493ef8 --- /dev/null +++ b/src/pkg/image/testdata/video-001.separate.dc.progression.progressive.jpeg |