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
This commit is contained in:
parent
c35a1ecb6c
commit
5198c64fc9
|
@ -108,3 +108,10 @@ const (
|
|||
mRGBA
|
||||
mNRGBA
|
||||
)
|
||||
|
||||
// Compression describes the type of compression used in Options.
|
||||
type CompressionType int
|
||||
|
||||
const (
|
||||
Uncompressed CompressionType = iota
|
||||
)
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user