Skip to content

Commit

Permalink
go.image/tiff: encoder fix non LZW Predictor and Paletted SamplesPerP…
Browse files Browse the repository at this point in the history
…ixel and non RGB ExtraSamples.

TIFF6.0 Spec:
Page 31: ExtraSamples can only included in RGB mode image.
Page 39, SamplesPerPixel is usually 1 for bilevel, grayscale, and palette-color images.
Page 64, Currently Predictor field is used only with LZW.

Fixes golang/go#6421.

R=bsiegert, nigeltao, gbulmer
CC=golang-dev
https://golang.org/cl/13779043
  • Loading branch information
chai2010 authored and nigeltao committed Sep 22, 2013
1 parent b2d744f commit 420d421
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions tiff/writer.go
Expand Up @@ -291,11 +291,12 @@ type Options struct {
func Encode(w io.Writer, m image.Image, opt *Options) error {
d := m.Bounds().Size()

predictor := false
compression := uint32(cNone)
predictor := false
if opt != nil {
predictor = opt.Predictor
compression = opt.Compression.specValue()
// The predictor field is only used with LZW. See page 64 of the spec.
predictor = opt.Predictor && compression == cLZW
}

_, err := io.WriteString(w, leHeader)
Expand Down Expand Up @@ -343,7 +344,7 @@ func Encode(w io.Writer, m image.Image, opt *Options) error {
photometricInterpretation := uint32(pRGB)
samplesPerPixel := uint32(4)
bitsPerSample := []uint32{8, 8, 8, 8}
extrasamples := uint32(1) // Associated alpha (default).
extraSamples := uint32(0)
colorMap := []uint32{}

if predictor {
Expand All @@ -352,7 +353,7 @@ func Encode(w io.Writer, m image.Image, opt *Options) error {
switch m := m.(type) {
case *image.Paletted:
photometricInterpretation = pPaletted
samplesPerPixel = 3
samplesPerPixel = 1
bitsPerSample = []uint32{8}
colorMap = make([]uint32, 256*3)
for i := 0; i < 256 && i < len(m.Palette); i++ {
Expand All @@ -373,18 +374,21 @@ func Encode(w io.Writer, m image.Image, opt *Options) error {
bitsPerSample = []uint32{16}
err = encodeGray16(dst, m.Pix, d.X, d.Y, m.Stride, predictor)
case *image.NRGBA:
extrasamples = 2 // Unassociated alpha.
extraSamples = 2 // Unassociated alpha.
err = encodeRGBA(dst, m.Pix, d.X, d.Y, m.Stride, predictor)
case *image.NRGBA64:
extrasamples = 2 // Unassociated alpha.
extraSamples = 2 // Unassociated alpha.
bitsPerSample = []uint32{16, 16, 16, 16}
err = encodeRGBA64(dst, m.Pix, d.X, d.Y, m.Stride, predictor)
case *image.RGBA:
extraSamples = 1 // Associated alpha.
err = encodeRGBA(dst, m.Pix, d.X, d.Y, m.Stride, predictor)
case *image.RGBA64:
extraSamples = 1 // Associated alpha.
bitsPerSample = []uint32{16, 16, 16, 16}
err = encodeRGBA64(dst, m.Pix, d.X, d.Y, m.Stride, predictor)
default:
extraSamples = 1 // Associated alpha.
err = encode(dst, m, predictor)
}
if err != nil {
Expand Down Expand Up @@ -419,12 +423,16 @@ func Encode(w io.Writer, m image.Image, opt *Options) error {
{tXResolution, dtRational, []uint32{72, 1}},
{tYResolution, dtRational, []uint32{72, 1}},
{tResolutionUnit, dtShort, []uint32{resPerInch}},
{tPredictor, dtShort, []uint32{pr}},
{tExtraSamples, dtShort, []uint32{extrasamples}},
}
if pr != prNone {
ifd = append(ifd, ifdEntry{tPredictor, dtShort, []uint32{pr}})
}
if len(colorMap) != 0 {
ifd = append(ifd, ifdEntry{tColorMap, dtShort, colorMap})
}
if extraSamples > 0 {
ifd = append(ifd, ifdEntry{tExtraSamples, dtShort, []uint32{extraSamples}})
}

return writeIFD(w, imageLen+8, ifd)
}

0 comments on commit 420d421

Please sign in to comment.