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.
|
|
|
|
|
2013-09-02 05:29:55 +02:00
|
|
|
// Package tiff implements a TIFF image decoder and encoder.
|
2012-01-31 01:32:50 +01:00
|
|
|
//
|
|
|
|
// The TIFF specification is at http://partners.adobe.com/public/developer/en/tiff/TIFF6.pdf
|
2018-12-06 17:03:52 +01:00
|
|
|
package tiff // import "git.gutmet.org/golang-image.git/tiff"
|
2012-01-31 01:32:50 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"compress/zlib"
|
|
|
|
"encoding/binary"
|
2014-05-12 08:41:44 +02:00
|
|
|
"fmt"
|
2012-01-31 01:32:50 +01:00
|
|
|
"image"
|
|
|
|
"image/color"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2015-04-28 20:02:45 +02:00
|
|
|
"math"
|
2014-06-02 06:51:51 +02:00
|
|
|
|
2018-12-06 17:03:52 +01:00
|
|
|
"git.gutmet.org/golang-image.git/tiff/lzw"
|
2012-01-31 01:32:50 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// A FormatError reports that the input is not a valid TIFF image.
|
|
|
|
type FormatError string
|
|
|
|
|
|
|
|
func (e FormatError) Error() string {
|
|
|
|
return "tiff: invalid format: " + string(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
// An UnsupportedError reports that the input uses a valid but
|
|
|
|
// unimplemented feature.
|
|
|
|
type UnsupportedError string
|
|
|
|
|
|
|
|
func (e UnsupportedError) Error() string {
|
|
|
|
return "tiff: unsupported feature: " + string(e)
|
|
|
|
}
|
|
|
|
|
2015-06-13 02:51:52 +02:00
|
|
|
var errNoPixels = FormatError("not enough pixel data")
|
|
|
|
|
2012-01-31 01:32:50 +01:00
|
|
|
type decoder struct {
|
|
|
|
r io.ReaderAt
|
|
|
|
byteOrder binary.ByteOrder
|
|
|
|
config image.Config
|
|
|
|
mode imageMode
|
2013-07-30 07:02:33 +02:00
|
|
|
bpp uint
|
2012-01-31 01:32:50 +01:00
|
|
|
features map[int][]uint
|
|
|
|
palette []color.Color
|
|
|
|
|
|
|
|
buf []byte
|
|
|
|
off int // Current offset in buf.
|
|
|
|
v uint32 // Buffer value for reading with arbitrary bit depths.
|
|
|
|
nbits uint // Remaining number of bits in v.
|
|
|
|
}
|
|
|
|
|
|
|
|
// firstVal returns the first uint of the features entry with the given tag,
|
|
|
|
// or 0 if the tag does not exist.
|
|
|
|
func (d *decoder) firstVal(tag int) uint {
|
|
|
|
f := d.features[tag]
|
|
|
|
if len(f) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return f[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
// ifdUint decodes the IFD entry in p, which must be of the Byte, Short
|
|
|
|
// or Long type, and returns the decoded uint values.
|
|
|
|
func (d *decoder) ifdUint(p []byte) (u []uint, err error) {
|
|
|
|
var raw []byte
|
2015-04-28 19:05:11 +02:00
|
|
|
if len(p) < ifdLen {
|
|
|
|
return nil, FormatError("bad IFD entry")
|
|
|
|
}
|
|
|
|
|
2012-01-31 01:32:50 +01:00
|
|
|
datatype := d.byteOrder.Uint16(p[2:4])
|
2015-04-28 19:05:11 +02:00
|
|
|
if dt := int(datatype); dt <= 0 || dt >= len(lengths) {
|
|
|
|
return nil, UnsupportedError("IFD entry datatype")
|
|
|
|
}
|
|
|
|
|
2012-01-31 01:32:50 +01:00
|
|
|
count := d.byteOrder.Uint32(p[4:8])
|
2015-04-28 20:02:45 +02:00
|
|
|
if count > math.MaxInt32/lengths[datatype] {
|
|
|
|
return nil, FormatError("IFD data too large")
|
|
|
|
}
|
2012-01-31 01:32:50 +01:00
|
|
|
if datalen := lengths[datatype] * count; datalen > 4 {
|
|
|
|
// The IFD contains a pointer to the real value.
|
|
|
|
raw = make([]byte, datalen)
|
|
|
|
_, err = d.r.ReadAt(raw, int64(d.byteOrder.Uint32(p[8:12])))
|
|
|
|
} else {
|
|
|
|
raw = p[8 : 8+datalen]
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
u = make([]uint, count)
|
|
|
|
switch datatype {
|
|
|
|
case dtByte:
|
|
|
|
for i := uint32(0); i < count; i++ {
|
|
|
|
u[i] = uint(raw[i])
|
|
|
|
}
|
|
|
|
case dtShort:
|
|
|
|
for i := uint32(0); i < count; i++ {
|
|
|
|
u[i] = uint(d.byteOrder.Uint16(raw[2*i : 2*(i+1)]))
|
|
|
|
}
|
|
|
|
case dtLong:
|
|
|
|
for i := uint32(0); i < count; i++ {
|
|
|
|
u[i] = uint(d.byteOrder.Uint32(raw[4*i : 4*(i+1)]))
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, UnsupportedError("data type")
|
|
|
|
}
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
2018-10-30 01:08:48 +01:00
|
|
|
// parseIFD decides whether the IFD entry in p is "interesting" and
|
2015-04-28 17:45:40 +02:00
|
|
|
// stows away the data in the decoder. It returns the tag number of the
|
|
|
|
// entry and an error, if any.
|
|
|
|
func (d *decoder) parseIFD(p []byte) (int, error) {
|
2012-01-31 01:32:50 +01:00
|
|
|
tag := d.byteOrder.Uint16(p[0:2])
|
|
|
|
switch tag {
|
|
|
|
case tBitsPerSample,
|
|
|
|
tExtraSamples,
|
|
|
|
tPhotometricInterpretation,
|
|
|
|
tCompression,
|
|
|
|
tPredictor,
|
|
|
|
tStripOffsets,
|
|
|
|
tStripByteCounts,
|
|
|
|
tRowsPerStrip,
|
2013-09-06 12:07:58 +02:00
|
|
|
tTileWidth,
|
|
|
|
tTileLength,
|
|
|
|
tTileOffsets,
|
|
|
|
tTileByteCounts,
|
2012-01-31 01:32:50 +01:00
|
|
|
tImageLength,
|
|
|
|
tImageWidth:
|
|
|
|
val, err := d.ifdUint(p)
|
|
|
|
if err != nil {
|
2015-04-28 17:45:40 +02:00
|
|
|
return 0, err
|
2012-01-31 01:32:50 +01:00
|
|
|
}
|
|
|
|
d.features[int(tag)] = val
|
|
|
|
case tColorMap:
|
|
|
|
val, err := d.ifdUint(p)
|
|
|
|
if err != nil {
|
2015-04-28 17:45:40 +02:00
|
|
|
return 0, err
|
2012-01-31 01:32:50 +01:00
|
|
|
}
|
|
|
|
numcolors := len(val) / 3
|
|
|
|
if len(val)%3 != 0 || numcolors <= 0 || numcolors > 256 {
|
2015-04-28 17:45:40 +02:00
|
|
|
return 0, FormatError("bad ColorMap length")
|
2012-01-31 01:32:50 +01:00
|
|
|
}
|
|
|
|
d.palette = make([]color.Color, numcolors)
|
|
|
|
for i := 0; i < numcolors; i++ {
|
|
|
|
d.palette[i] = color.RGBA64{
|
|
|
|
uint16(val[i]),
|
|
|
|
uint16(val[i+numcolors]),
|
|
|
|
uint16(val[i+2*numcolors]),
|
|
|
|
0xffff,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case tSampleFormat:
|
|
|
|
// Page 27 of the spec: If the SampleFormat is present and
|
|
|
|
// the value is not 1 [= unsigned integer data], a Baseline
|
|
|
|
// TIFF reader that cannot handle the SampleFormat value
|
|
|
|
// must terminate the import process gracefully.
|
|
|
|
val, err := d.ifdUint(p)
|
|
|
|
if err != nil {
|
2015-04-28 17:45:40 +02:00
|
|
|
return 0, err
|
2012-01-31 01:32:50 +01:00
|
|
|
}
|
|
|
|
for _, v := range val {
|
|
|
|
if v != 1 {
|
2015-04-28 17:45:40 +02:00
|
|
|
return 0, UnsupportedError("sample format")
|
2012-01-31 01:32:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-28 17:45:40 +02:00
|
|
|
return int(tag), nil
|
2012-01-31 01:32:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// readBits reads n bits from the internal buffer starting at the current offset.
|
2015-04-24 07:43:07 +02:00
|
|
|
func (d *decoder) readBits(n uint) (v uint32, ok bool) {
|
2012-01-31 01:32:50 +01:00
|
|
|
for d.nbits < n {
|
|
|
|
d.v <<= 8
|
2015-04-24 07:43:07 +02:00
|
|
|
if d.off >= len(d.buf) {
|
|
|
|
return 0, false
|
|
|
|
}
|
2012-01-31 01:32:50 +01:00
|
|
|
d.v |= uint32(d.buf[d.off])
|
|
|
|
d.off++
|
|
|
|
d.nbits += 8
|
|
|
|
}
|
|
|
|
d.nbits -= n
|
|
|
|
rv := d.v >> d.nbits
|
|
|
|
d.v &^= rv << d.nbits
|
2015-04-24 07:43:07 +02:00
|
|
|
return rv, true
|
2012-01-31 01:32:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// flushBits discards the unread bits in the buffer used by readBits.
|
|
|
|
// It is used at the end of a line.
|
|
|
|
func (d *decoder) flushBits() {
|
|
|
|
d.v = 0
|
|
|
|
d.nbits = 0
|
|
|
|
}
|
|
|
|
|
2013-09-06 12:07:58 +02:00
|
|
|
// minInt returns the smaller of x or y.
|
|
|
|
func minInt(a, b int) int {
|
|
|
|
if a <= b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2012-01-31 01:32:50 +01:00
|
|
|
// decode decodes the raw data of an image.
|
2013-09-06 12:07:58 +02:00
|
|
|
// It reads from d.buf and writes the strip or tile into dst.
|
|
|
|
func (d *decoder) decode(dst image.Image, xmin, ymin, xmax, ymax int) error {
|
2012-01-31 01:32:50 +01:00
|
|
|
d.off = 0
|
|
|
|
|
|
|
|
// Apply horizontal predictor if necessary.
|
|
|
|
// In this case, p contains the color difference to the preceding pixel.
|
|
|
|
// See page 64-65 of the spec.
|
2013-09-18 09:16:05 +02:00
|
|
|
if d.firstVal(tPredictor) == prHorizontal {
|
2015-06-17 01:10:39 +02:00
|
|
|
switch d.bpp {
|
|
|
|
case 16:
|
2013-09-18 09:16:05 +02:00
|
|
|
var off int
|
2015-04-24 07:43:07 +02:00
|
|
|
n := 2 * len(d.features[tBitsPerSample]) // bytes per sample times samples per pixel
|
2013-09-18 09:16:05 +02:00
|
|
|
for y := ymin; y < ymax; y++ {
|
2015-04-24 07:43:07 +02:00
|
|
|
off += n
|
|
|
|
for x := 0; x < (xmax-xmin-1)*n; x += 2 {
|
|
|
|
if off+2 > len(d.buf) {
|
2015-06-13 02:51:52 +02:00
|
|
|
return errNoPixels
|
2015-04-24 07:43:07 +02:00
|
|
|
}
|
|
|
|
v0 := d.byteOrder.Uint16(d.buf[off-n : off-n+2])
|
2013-09-18 09:16:05 +02:00
|
|
|
v1 := d.byteOrder.Uint16(d.buf[off : off+2])
|
|
|
|
d.byteOrder.PutUint16(d.buf[off:off+2], v1+v0)
|
|
|
|
off += 2
|
|
|
|
}
|
|
|
|
}
|
2015-06-17 01:10:39 +02:00
|
|
|
case 8:
|
2013-09-18 09:16:05 +02:00
|
|
|
var off int
|
2015-04-24 07:43:07 +02:00
|
|
|
n := 1 * len(d.features[tBitsPerSample]) // bytes per sample times samples per pixel
|
2013-09-18 09:16:05 +02:00
|
|
|
for y := ymin; y < ymax; y++ {
|
2015-04-24 07:43:07 +02:00
|
|
|
off += n
|
|
|
|
for x := 0; x < (xmax-xmin-1)*n; x++ {
|
|
|
|
if off >= len(d.buf) {
|
2015-06-13 02:51:52 +02:00
|
|
|
return errNoPixels
|
2015-04-24 07:43:07 +02:00
|
|
|
}
|
|
|
|
d.buf[off] += d.buf[off-n]
|
2013-09-18 09:16:05 +02:00
|
|
|
off++
|
|
|
|
}
|
2012-01-31 01:32:50 +01:00
|
|
|
}
|
2015-06-17 01:10:39 +02:00
|
|
|
case 1:
|
|
|
|
return UnsupportedError("horizontal predictor with 1 BitsPerSample")
|
2012-01-31 01:32:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-06 12:07:58 +02:00
|
|
|
rMaxX := minInt(xmax, dst.Bounds().Max.X)
|
|
|
|
rMaxY := minInt(ymax, dst.Bounds().Max.Y)
|
2012-01-31 01:32:50 +01:00
|
|
|
switch d.mode {
|
|
|
|
case mGray, mGrayInvert:
|
2013-07-30 07:02:33 +02:00
|
|
|
if d.bpp == 16 {
|
|
|
|
img := dst.(*image.Gray16)
|
2013-09-06 12:07:58 +02:00
|
|
|
for y := ymin; y < rMaxY; y++ {
|
|
|
|
for x := xmin; x < rMaxX; x++ {
|
2015-04-24 07:43:07 +02:00
|
|
|
if d.off+2 > len(d.buf) {
|
2015-06-13 02:51:52 +02:00
|
|
|
return errNoPixels
|
2015-04-24 07:43:07 +02:00
|
|
|
}
|
2013-07-30 07:02:33 +02:00
|
|
|
v := d.byteOrder.Uint16(d.buf[d.off : d.off+2])
|
|
|
|
d.off += 2
|
|
|
|
if d.mode == mGrayInvert {
|
|
|
|
v = 0xffff - v
|
|
|
|
}
|
|
|
|
img.SetGray16(x, y, color.Gray16{v})
|
2012-01-31 01:32:50 +01:00
|
|
|
}
|
2018-04-03 03:08:12 +02:00
|
|
|
if rMaxX == img.Bounds().Max.X {
|
|
|
|
d.off += 2 * (xmax - img.Bounds().Max.X)
|
|
|
|
}
|
2012-01-31 01:32:50 +01:00
|
|
|
}
|
2013-07-30 07:02:33 +02:00
|
|
|
} else {
|
|
|
|
img := dst.(*image.Gray)
|
|
|
|
max := uint32((1 << d.bpp) - 1)
|
2013-09-06 12:07:58 +02:00
|
|
|
for y := ymin; y < rMaxY; y++ {
|
|
|
|
for x := xmin; x < rMaxX; x++ {
|
2015-04-24 07:43:07 +02:00
|
|
|
v, ok := d.readBits(d.bpp)
|
|
|
|
if !ok {
|
2015-06-13 02:51:52 +02:00
|
|
|
return errNoPixels
|
2015-04-24 07:43:07 +02:00
|
|
|
}
|
|
|
|
v = v * 0xff / max
|
2013-07-30 07:02:33 +02:00
|
|
|
if d.mode == mGrayInvert {
|
|
|
|
v = 0xff - v
|
|
|
|
}
|
2015-04-24 07:43:07 +02:00
|
|
|
img.SetGray(x, y, color.Gray{uint8(v)})
|
2013-07-30 07:02:33 +02:00
|
|
|
}
|
|
|
|
d.flushBits()
|
|
|
|
}
|
2012-01-31 01:32:50 +01:00
|
|
|
}
|
|
|
|
case mPaletted:
|
|
|
|
img := dst.(*image.Paletted)
|
2013-09-06 12:07:58 +02:00
|
|
|
for y := ymin; y < rMaxY; y++ {
|
|
|
|
for x := xmin; x < rMaxX; x++ {
|
2015-04-24 07:43:07 +02:00
|
|
|
v, ok := d.readBits(d.bpp)
|
|
|
|
if !ok {
|
2015-06-13 02:51:52 +02:00
|
|
|
return errNoPixels
|
2015-04-24 07:43:07 +02:00
|
|
|
}
|
|
|
|
img.SetColorIndex(x, y, uint8(v))
|
2012-01-31 01:32:50 +01:00
|
|
|
}
|
|
|
|
d.flushBits()
|
|
|
|
}
|
|
|
|
case mRGB:
|
2013-09-18 09:16:05 +02:00
|
|
|
if d.bpp == 16 {
|
|
|
|
img := dst.(*image.RGBA64)
|
|
|
|
for y := ymin; y < rMaxY; y++ {
|
|
|
|
for x := xmin; x < rMaxX; x++ {
|
2015-04-24 07:43:07 +02:00
|
|
|
if d.off+6 > len(d.buf) {
|
2015-06-13 02:51:52 +02:00
|
|
|
return errNoPixels
|
2015-04-24 07:43:07 +02:00
|
|
|
}
|
2013-09-18 09:16:05 +02:00
|
|
|
r := d.byteOrder.Uint16(d.buf[d.off+0 : d.off+2])
|
|
|
|
g := d.byteOrder.Uint16(d.buf[d.off+2 : d.off+4])
|
|
|
|
b := d.byteOrder.Uint16(d.buf[d.off+4 : d.off+6])
|
|
|
|
d.off += 6
|
|
|
|
img.SetRGBA64(x, y, color.RGBA64{r, g, b, 0xffff})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
img := dst.(*image.RGBA)
|
|
|
|
for y := ymin; y < rMaxY; y++ {
|
|
|
|
min := img.PixOffset(xmin, y)
|
|
|
|
max := img.PixOffset(rMaxX, y)
|
|
|
|
off := (y - ymin) * (xmax - xmin) * 3
|
|
|
|
for i := min; i < max; i += 4 {
|
2015-06-13 02:45:13 +02:00
|
|
|
if off+3 > len(d.buf) {
|
2015-06-13 02:51:52 +02:00
|
|
|
return errNoPixels
|
2015-04-24 07:43:07 +02:00
|
|
|
}
|
2013-09-18 09:16:05 +02:00
|
|
|
img.Pix[i+0] = d.buf[off+0]
|
|
|
|
img.Pix[i+1] = d.buf[off+1]
|
|
|
|
img.Pix[i+2] = d.buf[off+2]
|
|
|
|
img.Pix[i+3] = 0xff
|
|
|
|
off += 3
|
|
|
|
}
|
2013-09-06 12:07:58 +02:00
|
|
|
}
|
2012-01-31 01:32:50 +01:00
|
|
|
}
|
|
|
|
case mNRGBA:
|
2013-09-18 09:16:05 +02:00
|
|
|
if d.bpp == 16 {
|
|
|
|
img := dst.(*image.NRGBA64)
|
|
|
|
for y := ymin; y < rMaxY; y++ {
|
|
|
|
for x := xmin; x < rMaxX; x++ {
|
2015-04-24 07:43:07 +02:00
|
|
|
if d.off+8 > len(d.buf) {
|
2015-06-13 02:51:52 +02:00
|
|
|
return errNoPixels
|
2015-04-24 07:43:07 +02:00
|
|
|
}
|
2013-09-18 09:16:05 +02:00
|
|
|
r := d.byteOrder.Uint16(d.buf[d.off+0 : d.off+2])
|
|
|
|
g := d.byteOrder.Uint16(d.buf[d.off+2 : d.off+4])
|
|
|
|
b := d.byteOrder.Uint16(d.buf[d.off+4 : d.off+6])
|
|
|
|
a := d.byteOrder.Uint16(d.buf[d.off+6 : d.off+8])
|
|
|
|
d.off += 8
|
|
|
|
img.SetNRGBA64(x, y, color.NRGBA64{r, g, b, a})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
img := dst.(*image.NRGBA)
|
|
|
|
for y := ymin; y < rMaxY; y++ {
|
|
|
|
min := img.PixOffset(xmin, y)
|
|
|
|
max := img.PixOffset(rMaxX, y)
|
2015-04-24 07:43:07 +02:00
|
|
|
i0, i1 := (y-ymin)*(xmax-xmin)*4, (y-ymin+1)*(xmax-xmin)*4
|
|
|
|
if i1 > len(d.buf) {
|
2015-06-13 02:51:52 +02:00
|
|
|
return errNoPixels
|
2015-04-24 07:43:07 +02:00
|
|
|
}
|
|
|
|
copy(img.Pix[min:max], d.buf[i0:i1])
|
2013-09-18 09:16:05 +02:00
|
|
|
}
|
2012-01-31 01:32:50 +01:00
|
|
|
}
|
|
|
|
case mRGBA:
|
2013-09-18 09:16:05 +02:00
|
|
|
if d.bpp == 16 {
|
|
|
|
img := dst.(*image.RGBA64)
|
|
|
|
for y := ymin; y < rMaxY; y++ {
|
|
|
|
for x := xmin; x < rMaxX; x++ {
|
2015-04-24 07:43:07 +02:00
|
|
|
if d.off+8 > len(d.buf) {
|
2015-06-13 02:51:52 +02:00
|
|
|
return errNoPixels
|
2015-04-24 07:43:07 +02:00
|
|
|
}
|
2013-09-18 09:16:05 +02:00
|
|
|
r := d.byteOrder.Uint16(d.buf[d.off+0 : d.off+2])
|
|
|
|
g := d.byteOrder.Uint16(d.buf[d.off+2 : d.off+4])
|
|
|
|
b := d.byteOrder.Uint16(d.buf[d.off+4 : d.off+6])
|
|
|
|
a := d.byteOrder.Uint16(d.buf[d.off+6 : d.off+8])
|
|
|
|
d.off += 8
|
|
|
|
img.SetRGBA64(x, y, color.RGBA64{r, g, b, a})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
img := dst.(*image.RGBA)
|
|
|
|
for y := ymin; y < rMaxY; y++ {
|
|
|
|
min := img.PixOffset(xmin, y)
|
|
|
|
max := img.PixOffset(rMaxX, y)
|
2015-04-24 07:43:07 +02:00
|
|
|
i0, i1 := (y-ymin)*(xmax-xmin)*4, (y-ymin+1)*(xmax-xmin)*4
|
|
|
|
if i1 > len(d.buf) {
|
2015-06-13 02:51:52 +02:00
|
|
|
return errNoPixels
|
2015-04-24 07:43:07 +02:00
|
|
|
}
|
|
|
|
copy(img.Pix[min:max], d.buf[i0:i1])
|
2013-09-18 09:16:05 +02:00
|
|
|
}
|
2012-01-31 01:32:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDecoder(r io.Reader) (*decoder, error) {
|
|
|
|
d := &decoder{
|
|
|
|
r: newReaderAt(r),
|
|
|
|
features: make(map[int][]uint),
|
|
|
|
}
|
|
|
|
|
|
|
|
p := make([]byte, 8)
|
|
|
|
if _, err := d.r.ReadAt(p, 0); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch string(p[0:4]) {
|
|
|
|
case leHeader:
|
|
|
|
d.byteOrder = binary.LittleEndian
|
|
|
|
case beHeader:
|
|
|
|
d.byteOrder = binary.BigEndian
|
|
|
|
default:
|
|
|
|
return nil, FormatError("malformed header")
|
|
|
|
}
|
|
|
|
|
|
|
|
ifdOffset := int64(d.byteOrder.Uint32(p[4:8]))
|
|
|
|
|
|
|
|
// The first two bytes contain the number of entries (12 bytes each).
|
|
|
|
if _, err := d.r.ReadAt(p[0:2], ifdOffset); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
numItems := int(d.byteOrder.Uint16(p[0:2]))
|
|
|
|
|
|
|
|
// All IFD entries are read in one chunk.
|
|
|
|
p = make([]byte, ifdLen*numItems)
|
|
|
|
if _, err := d.r.ReadAt(p, ifdOffset+2); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-04-28 17:45:40 +02:00
|
|
|
prevTag := -1
|
2012-01-31 01:32:50 +01:00
|
|
|
for i := 0; i < len(p); i += ifdLen {
|
2015-04-28 17:45:40 +02:00
|
|
|
tag, err := d.parseIFD(p[i : i+ifdLen])
|
|
|
|
if err != nil {
|
2012-01-31 01:32:50 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-28 17:45:40 +02:00
|
|
|
if tag <= prevTag {
|
|
|
|
return nil, FormatError("tags are not sorted in ascending order")
|
|
|
|
}
|
|
|
|
prevTag = tag
|
2012-01-31 01:32:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
d.config.Width = int(d.firstVal(tImageWidth))
|
|
|
|
d.config.Height = int(d.firstVal(tImageLength))
|
|
|
|
|
|
|
|
if _, ok := d.features[tBitsPerSample]; !ok {
|
|
|
|
return nil, FormatError("BitsPerSample tag missing")
|
|
|
|
}
|
2013-07-30 07:02:33 +02:00
|
|
|
d.bpp = d.firstVal(tBitsPerSample)
|
2015-06-13 01:13:12 +02:00
|
|
|
switch d.bpp {
|
|
|
|
case 0:
|
|
|
|
return nil, FormatError("BitsPerSample must not be 0")
|
|
|
|
case 1, 8, 16:
|
|
|
|
// Nothing to do, these are accepted by this implementation.
|
|
|
|
default:
|
|
|
|
return nil, UnsupportedError(fmt.Sprintf("BitsPerSample of %v", d.bpp))
|
|
|
|
}
|
2012-01-31 01:32:50 +01:00
|
|
|
|
|
|
|
// Determine the image mode.
|
|
|
|
switch d.firstVal(tPhotometricInterpretation) {
|
|
|
|
case pRGB:
|
2013-09-18 09:16:05 +02:00
|
|
|
if d.bpp == 16 {
|
|
|
|
for _, b := range d.features[tBitsPerSample] {
|
|
|
|
if b != 16 {
|
|
|
|
return nil, FormatError("wrong number of samples for 16bit RGB")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, b := range d.features[tBitsPerSample] {
|
|
|
|
if b != 8 {
|
|
|
|
return nil, FormatError("wrong number of samples for 8bit RGB")
|
|
|
|
}
|
2012-01-31 01:32:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// RGB images normally have 3 samples per pixel.
|
|
|
|
// If there are more, ExtraSamples (p. 31-32 of the spec)
|
|
|
|
// gives their meaning (usually an alpha channel).
|
|
|
|
//
|
|
|
|
// This implementation does not support extra samples
|
|
|
|
// of an unspecified type.
|
|
|
|
switch len(d.features[tBitsPerSample]) {
|
|
|
|
case 3:
|
|
|
|
d.mode = mRGB
|
2013-09-18 09:16:05 +02:00
|
|
|
if d.bpp == 16 {
|
|
|
|
d.config.ColorModel = color.RGBA64Model
|
|
|
|
} else {
|
|
|
|
d.config.ColorModel = color.RGBAModel
|
|
|
|
}
|
2012-01-31 01:32:50 +01:00
|
|
|
case 4:
|
|
|
|
switch d.firstVal(tExtraSamples) {
|
|
|
|
case 1:
|
|
|
|
d.mode = mRGBA
|
2013-09-18 09:16:05 +02:00
|
|
|
if d.bpp == 16 {
|
|
|
|
d.config.ColorModel = color.RGBA64Model
|
|
|
|
} else {
|
|
|
|
d.config.ColorModel = color.RGBAModel
|
|
|
|
}
|
2012-01-31 01:32:50 +01:00
|
|
|
case 2:
|
|
|
|
d.mode = mNRGBA
|
2013-09-18 09:16:05 +02:00
|
|
|
if d.bpp == 16 {
|
|
|
|
d.config.ColorModel = color.NRGBA64Model
|
|
|
|
} else {
|
|
|
|
d.config.ColorModel = color.NRGBAModel
|
|
|
|
}
|
2012-01-31 01:32:50 +01:00
|
|
|
default:
|
|
|
|
return nil, FormatError("wrong number of samples for RGB")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, FormatError("wrong number of samples for RGB")
|
|
|
|
}
|
|
|
|
case pPaletted:
|
|
|
|
d.mode = mPaletted
|
|
|
|
d.config.ColorModel = color.Palette(d.palette)
|
|
|
|
case pWhiteIsZero:
|
|
|
|
d.mode = mGrayInvert
|
2013-07-30 07:02:33 +02:00
|
|
|
if d.bpp == 16 {
|
|
|
|
d.config.ColorModel = color.Gray16Model
|
|
|
|
} else {
|
|
|
|
d.config.ColorModel = color.GrayModel
|
|
|
|
}
|
2012-01-31 01:32:50 +01:00
|
|
|
case pBlackIsZero:
|
|
|
|
d.mode = mGray
|
2013-07-30 07:02:33 +02:00
|
|
|
if d.bpp == 16 {
|
|
|
|
d.config.ColorModel = color.Gray16Model
|
|
|
|
} else {
|
|
|
|
d.config.ColorModel = color.GrayModel
|
|
|
|
}
|
2012-01-31 01:32:50 +01:00
|
|
|
default:
|
|
|
|
return nil, UnsupportedError("color model")
|
|
|
|
}
|
|
|
|
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeConfig returns the color model and dimensions of a TIFF image without
|
|
|
|
// decoding the entire image.
|
|
|
|
func DecodeConfig(r io.Reader) (image.Config, error) {
|
|
|
|
d, err := newDecoder(r)
|
|
|
|
if err != nil {
|
|
|
|
return image.Config{}, err
|
|
|
|
}
|
|
|
|
return d.config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode reads a TIFF image from r and returns it as an image.Image.
|
|
|
|
// The type of Image returned depends on the contents of the TIFF.
|
|
|
|
func Decode(r io.Reader) (img image.Image, err error) {
|
|
|
|
d, err := newDecoder(r)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-09-06 12:07:58 +02:00
|
|
|
blockPadding := false
|
|
|
|
blockWidth := d.config.Width
|
|
|
|
blockHeight := d.config.Height
|
|
|
|
blocksAcross := 1
|
|
|
|
blocksDown := 1
|
|
|
|
|
2015-04-21 00:05:38 +02:00
|
|
|
if d.config.Width == 0 {
|
|
|
|
blocksAcross = 0
|
|
|
|
}
|
|
|
|
if d.config.Height == 0 {
|
|
|
|
blocksDown = 0
|
|
|
|
}
|
|
|
|
|
2013-09-06 12:07:58 +02:00
|
|
|
var blockOffsets, blockCounts []uint
|
|
|
|
|
|
|
|
if int(d.firstVal(tTileWidth)) != 0 {
|
|
|
|
blockPadding = true
|
|
|
|
|
|
|
|
blockWidth = int(d.firstVal(tTileWidth))
|
|
|
|
blockHeight = int(d.firstVal(tTileLength))
|
|
|
|
|
2015-04-21 00:05:38 +02:00
|
|
|
if blockWidth != 0 {
|
|
|
|
blocksAcross = (d.config.Width + blockWidth - 1) / blockWidth
|
|
|
|
}
|
|
|
|
if blockHeight != 0 {
|
|
|
|
blocksDown = (d.config.Height + blockHeight - 1) / blockHeight
|
|
|
|
}
|
2013-09-06 12:07:58 +02:00
|
|
|
|
|
|
|
blockCounts = d.features[tTileByteCounts]
|
|
|
|
blockOffsets = d.features[tTileOffsets]
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if int(d.firstVal(tRowsPerStrip)) != 0 {
|
|
|
|
blockHeight = int(d.firstVal(tRowsPerStrip))
|
|
|
|
}
|
|
|
|
|
2015-04-21 00:05:38 +02:00
|
|
|
if blockHeight != 0 {
|
|
|
|
blocksDown = (d.config.Height + blockHeight - 1) / blockHeight
|
|
|
|
}
|
2013-09-06 12:07:58 +02:00
|
|
|
|
|
|
|
blockOffsets = d.features[tStripOffsets]
|
|
|
|
blockCounts = d.features[tStripByteCounts]
|
2012-01-31 01:32:50 +01:00
|
|
|
}
|
2013-09-06 12:07:58 +02:00
|
|
|
|
|
|
|
// Check if we have the right number of strips/tiles, offsets and counts.
|
|
|
|
if n := blocksAcross * blocksDown; len(blockOffsets) < n || len(blockCounts) < n {
|
2012-01-31 01:32:50 +01:00
|
|
|
return nil, FormatError("inconsistent header")
|
|
|
|
}
|
|
|
|
|
2013-07-30 07:02:33 +02:00
|
|
|
imgRect := image.Rect(0, 0, d.config.Width, d.config.Height)
|
2012-01-31 01:32:50 +01:00
|
|
|
switch d.mode {
|
|
|
|
case mGray, mGrayInvert:
|
2013-07-30 07:02:33 +02:00
|
|
|
if d.bpp == 16 {
|
|
|
|
img = image.NewGray16(imgRect)
|
|
|
|
} else {
|
|
|
|
img = image.NewGray(imgRect)
|
|
|
|
}
|
2012-01-31 01:32:50 +01:00
|
|
|
case mPaletted:
|
2013-07-30 07:02:33 +02:00
|
|
|
img = image.NewPaletted(imgRect, d.palette)
|
2012-01-31 01:32:50 +01:00
|
|
|
case mNRGBA:
|
2013-09-18 09:16:05 +02:00
|
|
|
if d.bpp == 16 {
|
|
|
|
img = image.NewNRGBA64(imgRect)
|
|
|
|
} else {
|
|
|
|
img = image.NewNRGBA(imgRect)
|
|
|
|
}
|
2012-01-31 01:32:50 +01:00
|
|
|
case mRGB, mRGBA:
|
2013-09-18 09:16:05 +02:00
|
|
|
if d.bpp == 16 {
|
|
|
|
img = image.NewRGBA64(imgRect)
|
|
|
|
} else {
|
|
|
|
img = image.NewRGBA(imgRect)
|
|
|
|
}
|
2012-01-31 01:32:50 +01:00
|
|
|
}
|
|
|
|
|
2013-09-06 12:07:58 +02:00
|
|
|
for i := 0; i < blocksAcross; i++ {
|
|
|
|
blkW := blockWidth
|
|
|
|
if !blockPadding && i == blocksAcross-1 && d.config.Width%blockWidth != 0 {
|
|
|
|
blkW = d.config.Width % blockWidth
|
2012-01-31 01:32:50 +01:00
|
|
|
}
|
2013-09-06 12:07:58 +02:00
|
|
|
for j := 0; j < blocksDown; j++ {
|
|
|
|
blkH := blockHeight
|
|
|
|
if !blockPadding && j == blocksDown-1 && d.config.Height%blockHeight != 0 {
|
|
|
|
blkH = d.config.Height % blockHeight
|
|
|
|
}
|
|
|
|
offset := int64(blockOffsets[j*blocksAcross+i])
|
|
|
|
n := int64(blockCounts[j*blocksAcross+i])
|
|
|
|
switch d.firstVal(tCompression) {
|
2014-05-12 08:41:44 +02:00
|
|
|
|
|
|
|
// According to the spec, Compression does not have a default value,
|
|
|
|
// but some tools interpret a missing Compression value as none so we do
|
|
|
|
// the same.
|
|
|
|
case cNone, 0:
|
2013-09-06 12:07:58 +02:00
|
|
|
if b, ok := d.r.(*buffer); ok {
|
|
|
|
d.buf, err = b.Slice(int(offset), int(n))
|
|
|
|
} else {
|
|
|
|
d.buf = make([]byte, n)
|
|
|
|
_, err = d.r.ReadAt(d.buf, offset)
|
|
|
|
}
|
|
|
|
case cLZW:
|
|
|
|
r := lzw.NewReader(io.NewSectionReader(d.r, offset, n), lzw.MSB, 8)
|
|
|
|
d.buf, err = ioutil.ReadAll(r)
|
|
|
|
r.Close()
|
|
|
|
case cDeflate, cDeflateOld:
|
2015-04-23 09:51:29 +02:00
|
|
|
var r io.ReadCloser
|
|
|
|
r, err = zlib.NewReader(io.NewSectionReader(d.r, offset, n))
|
2013-09-06 12:07:58 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
d.buf, err = ioutil.ReadAll(r)
|
|
|
|
r.Close()
|
|
|
|
case cPackBits:
|
|
|
|
d.buf, err = unpackBits(io.NewSectionReader(d.r, offset, n))
|
|
|
|
default:
|
2014-05-12 08:41:44 +02:00
|
|
|
err = UnsupportedError(fmt.Sprintf("compression value %d", d.firstVal(tCompression)))
|
2012-02-21 01:25:19 +01:00
|
|
|
}
|
2012-01-31 01:32:50 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-09-06 12:07:58 +02:00
|
|
|
|
|
|
|
xmin := i * blockWidth
|
|
|
|
ymin := j * blockHeight
|
|
|
|
xmax := xmin + blkW
|
|
|
|
ymax := ymin + blkH
|
|
|
|
err = d.decode(img, xmin, ymin, xmax, ymax)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2012-01-31 01:32:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
image.RegisterFormat("tiff", leHeader, Decode, DecodeConfig)
|
|
|
|
image.RegisterFormat("tiff", beHeader, Decode, DecodeConfig)
|
|
|
|
}
|