2012-03-05 01:45:47 +01:00
|
|
|
// Copyright 2012 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 tiff
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2012-06-07 01:41:24 +02:00
|
|
|
"image"
|
|
|
|
"io/ioutil"
|
2012-03-05 01:45:47 +01:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2012-10-03 02:11:21 +02:00
|
|
|
var roundtripTests = []struct {
|
|
|
|
filename string
|
|
|
|
opts *Options
|
|
|
|
}{
|
|
|
|
{"video-001.tiff", nil},
|
2013-09-18 09:16:05 +02:00
|
|
|
{"video-001-16bit.tiff", nil},
|
2013-09-13 09:42:53 +02:00
|
|
|
{"video-001-gray.tiff", nil},
|
|
|
|
{"video-001-gray-16bit.tiff", nil},
|
|
|
|
{"video-001-paletted.tiff", nil},
|
2012-10-03 02:11:21 +02:00
|
|
|
{"bw-packbits.tiff", nil},
|
|
|
|
{"video-001.tiff", &Options{Predictor: true}},
|
2012-12-12 03:24:12 +01:00
|
|
|
{"video-001.tiff", &Options{Compression: Deflate}},
|
|
|
|
{"video-001.tiff", &Options{Predictor: true, Compression: Deflate}},
|
2012-03-05 01:45:47 +01:00
|
|
|
}
|
|
|
|
|
2012-06-07 01:41:24 +02:00
|
|
|
func openImage(filename string) (image.Image, error) {
|
|
|
|
f, err := os.Open(testdataDir + filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
return Decode(f)
|
|
|
|
}
|
|
|
|
|
2012-03-05 01:45:47 +01:00
|
|
|
func TestRoundtrip(t *testing.T) {
|
2012-10-03 02:11:21 +02:00
|
|
|
for _, rt := range roundtripTests {
|
|
|
|
img, err := openImage(rt.filename)
|
2012-03-05 01:45:47 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
out := new(bytes.Buffer)
|
2012-10-03 02:11:21 +02:00
|
|
|
err = Encode(out, img, rt.opts)
|
2012-03-05 01:45:47 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
img2, err := Decode(&buffer{buf: out.Bytes()})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
compare(t, img, img2)
|
|
|
|
}
|
|
|
|
}
|
2012-06-07 01:41:24 +02:00
|
|
|
|
2012-06-13 02:40:38 +02:00
|
|
|
// TestRoundtrip2 tests that encoding and decoding an image whose
|
|
|
|
// origin is not (0, 0) gives the same thing.
|
|
|
|
func TestRoundtrip2(t *testing.T) {
|
|
|
|
m0 := image.NewRGBA(image.Rect(3, 4, 9, 8))
|
|
|
|
for i := range m0.Pix {
|
|
|
|
m0.Pix[i] = byte(i)
|
|
|
|
}
|
|
|
|
out := new(bytes.Buffer)
|
2012-08-23 12:16:03 +02:00
|
|
|
if err := Encode(out, m0, nil); err != nil {
|
2012-06-13 02:40:38 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
m1, err := Decode(&buffer{buf: out.Bytes()})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
compare(t, m0, m1)
|
|
|
|
}
|
|
|
|
|
2013-09-13 09:42:53 +02:00
|
|
|
func benchmarkEncode(b *testing.B, name string, pixelSize int) {
|
|
|
|
img, err := openImage(name)
|
2012-06-07 01:41:24 +02:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
s := img.Bounds().Size()
|
2013-09-13 09:42:53 +02:00
|
|
|
b.SetBytes(int64(s.X * s.Y * pixelSize))
|
2012-06-07 01:41:24 +02:00
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2012-08-23 12:16:03 +02:00
|
|
|
Encode(ioutil.Discard, img, nil)
|
2012-06-07 01:41:24 +02:00
|
|
|
}
|
|
|
|
}
|
2013-09-13 09:42:53 +02:00
|
|
|
|
|
|
|
func BenchmarkEncode(b *testing.B) { benchmarkEncode(b, "video-001.tiff", 4) }
|
|
|
|
func BenchmarkEncodePaletted(b *testing.B) { benchmarkEncode(b, "video-001-paletted.tiff", 1) }
|
|
|
|
func BenchmarkEncodeGray(b *testing.B) { benchmarkEncode(b, "video-001-gray.tiff", 1) }
|
|
|
|
func BenchmarkEncodeGray16(b *testing.B) { benchmarkEncode(b, "video-001-gray-16bit.tiff", 2) }
|
2013-09-18 09:16:05 +02:00
|
|
|
func BenchmarkEncodeRGBA(b *testing.B) { benchmarkEncode(b, "video-001.tiff", 4) }
|
|
|
|
func BenchmarkEncodeRGBA64(b *testing.B) { benchmarkEncode(b, "video-001-16bit.tiff", 8) }
|