Commit d97b8a81 authored by Benny Siegert's avatar Benny Siegert Committed by Nigel Tao

image/tiff: implement a decoder.

The current iteration can decode 8-bit images in
grayscale, paletted, RGB, RGBA and NRGBA mode. LZW compression
is implemented but does not work on my test images.
Deflate (i.e. zlib) compression with or without a horizontal
predictor is supported.

R=nigeltao, nigeltao_gnome
CC=golang-dev, mpl
https://golang.org/cl/4240051
parent 737e96c7
......@@ -107,6 +107,7 @@ DIRS=\
image\
image/jpeg\
image/png\
image/tiff\
image/ycbcr\
index/suffixarray\
io\
......
......@@ -10,9 +10,10 @@ import (
"os"
"testing"
// TODO(nigeltao): implement bmp, gif and tiff decoders.
// TODO(nigeltao): implement bmp and gif decoders.
_ "image/jpeg"
_ "image/png"
_ "image/tiff"
)
const goldenFile = "testdata/video-001.png"
......@@ -30,7 +31,7 @@ var imageTests = []imageTest{
// JPEG is a lossy format and hence needs a non-zero tolerance.
{"testdata/video-001.jpeg", 8 << 8},
{"testdata/video-001.png", 0},
//{"testdata/video-001.tiff", 0},
{"testdata/video-001.tiff", 0},
}
func decode(filename string) (image.Image, string, os.Error) {
......
# 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.
include ../../../Make.inc
TARG=image/tiff
GOFILES=\
buffer.go\
consts.go\
reader.go\
include ../../../Make.pkg
// 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 (
"io"
"os"
)
// buffer buffers an io.Reader to satisfy io.ReaderAt.
type buffer struct {
r io.Reader
buf []byte
}
func (b *buffer) ReadAt(p []byte, off int64) (int, os.Error) {
o := int(off)
end := o + len(p)
if int64(end) != off+int64(len(p)) {
return 0, os.EINVAL
}
m := len(b.buf)
if end > m {
if end > cap(b.buf) {
newcap := 1024
for newcap < end {
newcap *= 2
}
newbuf := make([]byte, end, newcap)
copy(newbuf, b.buf)
b.buf = newbuf
} else {
b.buf = b.buf[:end]
}
if n, err := io.ReadFull(b.r, b.buf[m:end]); err != nil {
end = m + n
b.buf = b.buf[:end]
return copy(p, b.buf[o:end]), err
}
}
return copy(p, b.buf[o:end]), nil
}
// newReaderAt converts an io.Reader into an io.ReaderAt.
func newReaderAt(r io.Reader) io.ReaderAt {
if ra, ok := r.(io.ReaderAt); ok {
return ra
}
return &buffer{
r: r,
buf: make([]byte, 0, 1024),
}
}
// 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 (
"os"
"strings"
"testing"
)
var readAtTests = []struct {
n int
off int64
s string
err os.Error
}{
{2, 0, "ab", nil},
{6, 0, "abcdef", nil},
{3, 3, "def", nil},
{3, 5, "f", os.EOF},
{3, 6, "", os.EOF},
}
func TestReadAt(t *testing.T) {
r := newReaderAt(strings.NewReader("abcdef"))
b := make([]byte, 10)
for _, test := range readAtTests {
n, err := r.ReadAt(b[:test.n], test.off)
s := string(b[:n])
if s != test.s || err != test.err {
t.Errorf("buffer.ReadAt(<%v bytes>, %v): got %v, %q; want %v, %q", test.n, test.off, err, s, test.err, test.s)
}
}
}
// 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
// A tiff image file contains one or more images. The metadata
// of each image is contained in an Image File Directory (IFD),
// which contains entries of 12 bytes each and is described
// on page 14-16 of the specification. An IFD entry consists of
//
// - a tag, which describes the signification of the entry,
// - the data type and length of the entry,
// - the data itself or a pointer to it if it is more than 4 bytes.
//
// The presence of a length means that each IFD is effectively an array.
const (
leHeader = "II\x2A\x00" // Header for little-endian files.
beHeader = "MM\x00\x2A" // Header for big-endian files.
ifdLen = 12 // Length of an IFD entry in bytes.
)
// Data types (p. 14-16 of the spec).
const (
dtByte = 1
dtASCII = 2
dtShort = 3
dtLong = 4
dtRational = 5
)
// The length of one instance of each data type in bytes.
var lengths = [...]uint32{0, 1, 1, 2, 4, 8}
// Tags (see p. 28-41 of the spec).
const (
tImageWidth = 256
tImageLength = 257
tBitsPerSample = 258
tCompression = 259
tPhotometricInterpretation = 262
tStripOffsets = 273
tSamplesPerPixel = 277
tRowsPerStrip = 278
tStripByteCounts = 279
tXResolution = 282
tYResolution = 283
tResolutionUnit = 296
tPredictor = 317
tColorMap = 320
tExtraSamples = 338
)
// Compression types (defined in various places in the spec and supplements).
const (
cNone = 1
cCCITT = 2
cG3 = 3 // Group 3 Fax.
cG4 = 4 // Group 4 Fax.
cLZW = 5
cJPEGOld = 6 // Superseded by cJPEG.
cJPEG = 7
cDeflate = 8 // zlib compression.
cPackBits = 32773
cDeflateOld = 32946 // Superseded by cDeflate.
)
// Photometric interpretation values (see p. 37 of the spec).
const (
pWhiteIsZero = 0
pBlackIsZero = 1
pRGB = 2
pPaletted = 3
pTransMask = 4 // transparency mask
pCMYK = 5
pYCbCr = 6
pCIELab = 8
)
// Values for the tPredictor tag (page 64-65 of the spec).
const (
prNone = 1
prHorizontal = 2
)
// imageMode represents the mode of the image.
type imageMode int
const (
mBilevel imageMode = iota
mPaletted
mGray
mGrayInvert
mRGB
mRGBA
mNRGBA
)
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment