Skip to content

Commit

Permalink
Return a zero width or zero height image of the appropriate type.
Browse files Browse the repository at this point in the history
  • Loading branch information
chai2010 committed Apr 28, 2015
1 parent 5dec728 commit 2e6993a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
25 changes: 25 additions & 0 deletions decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,31 @@ func TestDecompress(t *testing.T) {
}
}

// Do not panic when image dimensions are zero, return zero-sized
// image instead.
// Issue golang/go#10393.
func TestZeroSizedImages(t *testing.T) {
testsizes := []struct {
w, h int
}{
{0, 0},
{1, 0},
{0, 1},
{1, 1},
}
for _, r := range testsizes {
img := image.NewRGBA(image.Rect(0, 0, r.w, r.h))
var buf bytes.Buffer
if err := Encode(&buf, img, nil); err != nil {
t.Errorf("encode w=%d h=%d: %v", r.w, r.h, err)
continue
}
if _, err := Decode(&buf); err != nil {
t.Errorf("decode w=%d h=%d: %v", r.w, r.h, err)
}
}
}

// benchmarkDecode benchmarks the decoding of an image.
func benchmarkDecode(b *testing.B, filename string) {
b.StopTimer()
Expand Down
33 changes: 18 additions & 15 deletions tiff_ifd_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,31 @@ import (
)

func (p *IFD) BlocksAcross() int {
if _, ok := p.TagGetter().GetTileWidth(); ok {
imageWidth, _ := p.TagGetter().GetImageWidth()
blockWidth, _ := p.TagGetter().GetTileWidth()
imageWidth, _ := p.TagGetter().GetImageWidth()
if imageWidth == 0 {
return 0
}
blockWidth, _ := p.TagGetter().GetTileWidth()
if blockWidth > 0 {
return int((imageWidth + blockWidth - 1) / blockWidth)
} else {
return 1
}
return 1
}

func (p *IFD) BlocksDown() int {
if _, ok := p.TagGetter().GetTileLength(); ok {
imageHeight, _ := p.TagGetter().GetImageLength()
blockHeight, _ := p.TagGetter().GetTileLength()
return int((imageHeight + blockHeight - 1) / blockHeight)
} else {
imageHeight, _ := p.TagGetter().GetImageLength()
blockHeight, ok := p.TagGetter().GetRowsPerStrip()
if !ok || blockHeight == 0 || blockHeight > imageHeight {
blockHeight = imageHeight
}
imageHeight, _ := p.TagGetter().GetImageLength()
if imageHeight == 0 {
return 0
}
blockHeight, _ := p.TagGetter().GetTileLength()
if blockHeight > 0 {
return int((imageHeight + blockHeight - 1) / blockHeight)
}
blockHeight, _ = p.TagGetter().GetRowsPerStrip()
if blockHeight == 0 || blockHeight > imageHeight {
blockHeight = imageHeight
}
return int((imageHeight + blockHeight - 1) / blockHeight)
}

func (p *IFD) BlockBounds(col, row int) image.Rectangle {
Expand Down
8 changes: 0 additions & 8 deletions tiff_ifd_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,6 @@ func (p *IFD) ImageConfig() (config image.Config, err error) {
bitsPerSample, _ = p.TagGetter().GetBitsPerSample()
extraSamples, _ = p.TagGetter().GetExtraSamples()
)
if imageWidth == 0 {
err = fmt.Errorf("tiff: IFD.ImageWidth, zero width")
return
}
if imageHeight == 0 {
err = fmt.Errorf("tiff: IFD.ImageLength, zero length")
return
}
if len(bitsPerSample) == 0 {
err = fmt.Errorf("tiff: IFD.ColorModel, bad bitsPerSample length")
return
Expand Down

0 comments on commit 2e6993a

Please sign in to comment.