Skip to content

Commit

Permalink
Remove write support for JPEG-based compression
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshineplan committed Jul 9, 2021
1 parent fdb89ce commit 54a6525
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 70 deletions.
Binary file added testdata/video-001-jpeg.tiff
Binary file not shown.
3 changes: 0 additions & 3 deletions tiff/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ const (
LZW
CCITTGroup3
CCITTGroup4
JPEG
)

// specValue returns the compression type constant from the TIFF spec that
Expand All @@ -150,8 +149,6 @@ func (c CompressionType) specValue() uint32 {
return cG3
case CCITTGroup4:
return cG4
case JPEG:
return cJPEG
}
return cNone
}
45 changes: 45 additions & 0 deletions tiff/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,51 @@ func TestDecodeCCITT(t *testing.T) {
}
}

func delta(u0, u1 uint32) int64 {
d := int64(u0) - int64(u1)
if d < 0 {
return -d
}
return d
}

// averageDelta returns the average delta in RGB space. The two images must
// have the same bounds.
func averageDelta(m0, m1 image.Image) int64 {
b := m0.Bounds()
var sum, n int64
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
c0 := m0.At(x, y)
c1 := m1.At(x, y)
r0, g0, b0, _ := c0.RGBA()
r1, g1, b1, _ := c1.RGBA()
sum += delta(r0, r1)
sum += delta(g0, g1)
sum += delta(b0, b1)
n += 3
}
}
return sum / n
}

// TestDecodeJPEG tests decoding an image use JPEG compression.
func TestDecodeJPEG(t *testing.T) {
img, err := openImage("video-001.tiff")
if err != nil {
t.Fatal(err)
}

img2, err := openImage("video-001-jpeg.tiff")
if err != nil {
t.Fatal(err)
}
want := int64(6 << 8)
if got := averageDelta(img, img2); got > want {
t.Errorf("average delta too high; got %d, want <= %d", got, want)
}
}

// TestDecodeTagOrder tests that a malformed image with unsorted IFD entries is
// correctly rejected.
func TestDecodeTagOrder(t *testing.T) {
Expand Down
16 changes: 0 additions & 16 deletions tiff/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"compress/zlib"
"encoding/binary"
"image"
"image/jpeg"
"io"
"sort"
)
Expand Down Expand Up @@ -286,15 +285,6 @@ type Options struct {
Predictor bool
}

type discard struct{}

func (discard) Write(p []byte) (int, error) {
return len(p), nil
}
func (discard) Close() error {
return nil
}

// Encode writes the image m to w. opt determines the options used for
// encoding, such as the compression type. If opt is nil, an uncompressed
// image is written.
Expand Down Expand Up @@ -346,12 +336,6 @@ func Encode(w io.Writer, m image.Image, opt *Options) error {
if err != nil {
return err
}
case cJPEG:
dst = discard{}
err = jpeg.Encode(&buf, m, nil)
if err != nil {
return err
}
case cDeflate:
dst = zlib.NewWriter(&buf)
}
Expand Down
51 changes: 0 additions & 51 deletions tiff/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,57 +75,6 @@ func TestRoundtrip2(t *testing.T) {
compare(t, m0, m1)
}

func delta(u0, u1 uint32) int64 {
d := int64(u0) - int64(u1)
if d < 0 {
return -d
}
return d
}

// averageDelta returns the average delta in RGB space. The two images must
// have the same bounds.
func averageDelta(m0, m1 image.Image) int64 {
b := m0.Bounds()
var sum, n int64
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
c0 := m0.At(x, y)
c1 := m1.At(x, y)
r0, g0, b0, _ := c0.RGBA()
r1, g1, b1, _ := c1.RGBA()
sum += delta(r0, r1)
sum += delta(g0, g1)
sum += delta(b0, b1)
n += 3
}
}
return sum / n
}

// TestRoundtrip3 tests that encoding and decoding an image use JPEG compression.
func TestRoundtrip3(t *testing.T) {
img, err := openImage("video-001.tiff")
if err != nil {
t.Fatal(err)
}

out := new(bytes.Buffer)
err = Encode(out, img, &Options{Compression: JPEG})
if err != nil {
t.Fatal(err)
}

img2, err := Decode(&buffer{buf: out.Bytes()})
if err != nil {
t.Fatal(err)
}
want := int64(6 << 8)
if got := averageDelta(img, img2); got > want {
t.Errorf("average delta too high; got %d, want <= %d", got, want)
}
}

func benchmarkEncode(b *testing.B, name string, pixelSize int) {
b.Helper()
img, err := openImage(name)
Expand Down

0 comments on commit 54a6525

Please sign in to comment.