git.lucas.co / go_mono
git clone https://git.lucas.co/go_mono.git

commit5198c64fc92b9ec79a470ad82dc1d792064848e1
parentc35a1ecb6c
authorBenny Siegert <bsiegert@gmail.com>
date2012-08-23 20:16
tiff: new Options parameter for Encode.

No functional change at the moment but it will allow choosing
the compression type.

This is an incompatible, user-visible change. To get the old
behavior, callers of tiff.Encode need to add ", nil" as the
last parameter.

R=nigeltao
CC=golang-dev
https://golang.org/cl/6479044

 tiff/consts.go      |  7 +++++++
 tiff/writer.go      | 28 ++++++++++++++++++++++++++--
 tiff/writer_test.go |  6 +++---
 3 files changed, 36 insertions(+), 5 deletions(-)

diff --git a/tiff/consts.go b/tiff/consts.go
index 7c458c8..3b0a865 100644
--- a/tiff/consts.go
+++ b/tiff/consts.go
@@ -108,3 +108,10 @@ const (
 	mRGBA
 	mNRGBA
 )
+
+// Compression describes the type of compression used in Options.
+type CompressionType int
+
+const (
+	Uncompressed CompressionType = iota
+)
diff --git a/tiff/writer.go b/tiff/writer.go
index 1012368..37ba47f 100644
--- a/tiff/writer.go
+++ b/tiff/writer.go
@@ -140,8 +140,32 @@ func writeIFD(w io.Writer, ifdOffset int, d []ifdEntry) error {
 	return err
 }
 
-// Encode writes the image m to w in uncompressed 8-bit RGBA or NRGBA format.
-func Encode(w io.Writer, m image.Image) error {
+// Options are the encoding parameters.
+type Options struct {
+	// Compression is the type of compression used.
+	Compression CompressionType
+	// Predictor determines whether a differencing predictor is used;
+	// if true, instead of each pixel's color, the color difference to the
+	// preceding one is saved.  This improves the compression for certain
+	// types of images and compressors. For example, it works well for
+	// photos with Deflate compression.
+	Predictor bool
+}
+
+// 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.
+func Encode(w io.Writer, m image.Image, opt *Options) error {
+	predictor := false
+	compression := Uncompressed
+	if opt != nil {
+		predictor = opt.Predictor
+		compression = opt.Compression
+	}
+	if compression != Uncompressed || predictor {
+		return UnsupportedError("compression type")
+	}
+
 	var extrasamples uint32
 	_, err := io.WriteString(w, leHeader)
 	if err != nil {
diff --git a/tiff/writer_test.go b/tiff/writer_test.go
index b511755..f8d809e 100644
--- a/tiff/writer_test.go
+++ b/tiff/writer_test.go
@@ -33,7 +33,7 @@ func TestRoundtrip(t *testing.T) {
 			t.Fatal(err)
 		}
 		out := new(bytes.Buffer)
-		err = Encode(out, img)
+		err = Encode(out, img, nil)
 		if err != nil {
 			t.Fatal(err)
 		}
@@ -54,7 +54,7 @@ func TestRoundtrip2(t *testing.T) {
 		m0.Pix[i] = byte(i)
 	}
 	out := new(bytes.Buffer)
-	if err := Encode(out, m0); err != nil {
+	if err := Encode(out, m0, nil); err != nil {
 		t.Fatal(err)
 	}
 	m1, err := Decode(&buffer{buf: out.Bytes()})
@@ -74,6 +74,6 @@ func BenchmarkEncode(b *testing.B) {
 	b.SetBytes(int64(s.X * s.Y * 4))
 	b.ResetTimer()
 	for i := 0; i < b.N; i++ {
-		Encode(ioutil.Discard, img)
+		Encode(ioutil.Discard, img, nil)
 	}
 }