2012-01-31 01:32:50 +01:00
|
|
|
// Copyright 2011 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 (
|
|
|
|
"image"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2012-01-31 04:02:27 +01:00
|
|
|
|
|
|
|
_ "image/png"
|
2012-01-31 01:32:50 +01:00
|
|
|
)
|
|
|
|
|
2012-01-31 04:02:27 +01:00
|
|
|
const testdataDir = "../testdata/"
|
|
|
|
|
2012-01-31 01:32:50 +01:00
|
|
|
// Read makes *buffer implements io.Reader, so that we can pass one to Decode.
|
|
|
|
func (*buffer) Read([]byte) (int, error) {
|
|
|
|
panic("unimplemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestNoRPS tries to decode an image that has no RowsPerStrip tag.
|
|
|
|
// The tag is mandatory according to the spec but some software omits
|
|
|
|
// it in the case of a single strip.
|
|
|
|
func TestNoRPS(t *testing.T) {
|
2012-01-31 04:02:27 +01:00
|
|
|
f, err := os.Open(testdataDir + "no_rps.tiff")
|
2012-01-31 01:32:50 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
_, err = Decode(f)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestUnpackBits tests the decoding of PackBits-encoded data.
|
|
|
|
func TestUnpackBits(t *testing.T) {
|
|
|
|
var unpackBitsTests = []struct {
|
|
|
|
compressed string
|
|
|
|
uncompressed string
|
|
|
|
}{{
|
|
|
|
// Example data from Wikipedia.
|
|
|
|
"\xfe\xaa\x02\x80\x00\x2a\xfd\xaa\x03\x80\x00\x2a\x22\xf7\xaa",
|
|
|
|
"\xaa\xaa\xaa\x80\x00\x2a\xaa\xaa\xaa\xaa\x80\x00\x2a\x22\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
|
|
|
|
}}
|
|
|
|
for _, u := range unpackBitsTests {
|
|
|
|
buf, err := unpackBits(strings.NewReader(u.compressed))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if string(buf) != u.uncompressed {
|
|
|
|
t.Fatalf("unpackBits: want %x, got %x", u.uncompressed, buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-31 04:02:27 +01:00
|
|
|
func compare(t *testing.T, img0, img1 image.Image) {
|
|
|
|
b := img1.Bounds()
|
|
|
|
if !b.Eq(img0.Bounds()) {
|
|
|
|
t.Fatalf("wrong image size: want %s, got %s", img0.Bounds(), b)
|
|
|
|
}
|
|
|
|
for y := b.Min.Y; y < b.Max.Y; y++ {
|
|
|
|
for x := b.Min.X; x < b.Max.X; x++ {
|
|
|
|
c0 := img0.At(x, y)
|
|
|
|
c1 := img1.At(x, y)
|
|
|
|
r0, g0, b0, a0 := c0.RGBA()
|
|
|
|
r1, g1, b1, a1 := c1.RGBA()
|
|
|
|
if r0 != r1 || g0 != g1 || b0 != b1 || a0 != a1 {
|
|
|
|
t.Fatalf("pixel at (%d, %d) has wrong color: want %v, got %v", x, y, c0, c1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestDecode tests that decoding a PNG image and a TIFF image result in the
|
|
|
|
// same pixel data.
|
|
|
|
func TestDecode(t *testing.T) {
|
|
|
|
f0, err := os.Open(testdataDir + "video-001.png")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer f0.Close()
|
|
|
|
img0, _, err := image.Decode(f0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f1, err := os.Open(testdataDir + "video-001.tiff")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer f1.Close()
|
|
|
|
img1, _, err := image.Decode(f1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
compare(t, img0, img1)
|
|
|
|
}
|
|
|
|
|
2012-01-31 01:32:50 +01:00
|
|
|
// TestDecompress tests that decoding some TIFF images that use different
|
|
|
|
// compression formats result in the same pixel data.
|
|
|
|
func TestDecompress(t *testing.T) {
|
|
|
|
var decompressTests = []string{
|
|
|
|
"bw-uncompressed.tiff",
|
|
|
|
"bw-deflate.tiff",
|
|
|
|
"bw-packbits.tiff",
|
|
|
|
}
|
|
|
|
var img0 image.Image
|
|
|
|
for _, name := range decompressTests {
|
2012-01-31 04:02:27 +01:00
|
|
|
f, err := os.Open(testdataDir + name)
|
2012-01-31 01:32:50 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
if img0 == nil {
|
|
|
|
img0, err = Decode(f)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("decoding %s: %v", name, err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
img1, err := Decode(f)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("decoding %s: %v", name, err)
|
|
|
|
}
|
2012-01-31 04:02:27 +01:00
|
|
|
compare(t, img0, img1)
|
2012-01-31 01:32:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-31 04:02:27 +01:00
|
|
|
// benchmarkDecode benchmarks the decoding of an image.
|
|
|
|
func benchmarkDecode(b *testing.B, filename string) {
|
2012-01-31 01:32:50 +01:00
|
|
|
b.StopTimer()
|
2012-01-31 04:02:27 +01:00
|
|
|
contents, err := ioutil.ReadFile(testdataDir + filename)
|
2012-01-31 01:32:50 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
r := &buffer{buf: contents}
|
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_, err := Decode(r)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal("Decode:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-31 04:02:27 +01:00
|
|
|
|
|
|
|
func BenchmarkDecodeCompressed(b *testing.B) { benchmarkDecode(b, "video-001.tiff") }
|
|
|
|
func BenchmarkDecodeUncompressed(b *testing.B) { benchmarkDecode(b, "video-001-uncompressed.tiff") }
|