From 38c1f4ffe81ebcd2355e1997f495897576787098 Mon Sep 17 00:00:00 2001 From: Patrick Riley Date: Mon, 12 May 2014 16:41:44 +1000 Subject: [PATCH] go.image/tiff: Support missing Compression tag no_compress.tiff is no_rps.tiff that was read in by go.image/tiff and written out again (with a hack to avoid creating the Compression tag on the way out). $ tiffdump no_compress.tiff no_compress.tiff: Magic: 0x4949 Version: 0x2a Directory 0: offset 968 (0x3c8) next 0 (0) ImageWidth (256) SHORT (3) 1<16> ImageLength (257) SHORT (3) 1<15> BitsPerSample (258) SHORT (3) 4<8 8 8 8> Photometric (262) SHORT (3) 1<2> StripOffsets (273) LONG (4) 1<8> SamplesPerPixel (277) SHORT (3) 1<4> RowsPerStrip (278) SHORT (3) 1<15> StripByteCounts (279) LONG (4) 1<960> XResolution (282) RATIONAL (5) 1<72> YResolution (283) RATIONAL (5) 1<72> ResolutionUnit (296) SHORT (3) 1<2> ExtraSamples (338) SHORT (3) 1<2> LGTM=bsiegert, nigeltao R=bsiegert, nigeltao CC=golang-codereviews https://golang.org/cl/95930044 --- testdata/no_compress.tiff | Bin 0 -> 1142 bytes tiff/reader.go | 9 +++++++-- tiff/reader_test.go | 9 +++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 testdata/no_compress.tiff diff --git a/testdata/no_compress.tiff b/testdata/no_compress.tiff new file mode 100644 index 0000000000000000000000000000000000000000..3f72b29ae12590119d5d8a2b6bcdc5601a859f75 GIT binary patch literal 1142 zcmaKsZAg<*6vyxOXh@k(!$lUUEZ35pX@dDiTf|T#22*;O)8^)iURLv^Wm>Zj;znX( zOUt*^8KLc4q;oA(E1@V!EGyrV`cQ_0^g|zn;B@ZT;F-PLhx45ObIJya8AADj0G(;rYm87`@*B zWA^JH$xN?MtBZ(ArSj{U%jHNT`C+}L^98W7`~zOT8i(|hBsiG58~Uu3@WIs%pPt%4 zohO?K-xR@>NF;uINbvf~{VC#aeby>)^;*GYZ-LqiMNn=y3;kVYc;{?|x6U?jbk;yx z>OTE3rP7bb^9n>uI;A?awD7DOCLOmhzZC{-b@2K@lSk7Uv%{TGe!37ghVsn5y;JWS z5#gcM@rTVYIoJvl1Giy{u8Vr4uK^rbi}Ge;F$Mh2(Z{3bX%A;D%no;Xcu$Q-ZO~qK z5gN@^@ZQx19oNbzzw2-7(>ehRbHr)0czUKhwKv$)M1M2#9dNJl66KTs#bYe|vOfRk z4EE~deV+2&^+w9?`47(&AI)Bz8}0sspTT`ujM`twZ(;KSfq+npjg2K-CXhL)%vqdY z!_#w|!=8+K4QvPw(Xn~A+fAsguC9`j(z1}W#GNaj$L_)up54cxJ1~!Thwh<7o3|7% z7S~Co2XffFP$(qSL?RL4@p!@5pSI#m!Lp;L9y%-rFlZF+=xzM2sHmuDlgUJ;r>EE6 zA4L4e-sI#bGMTJjE|*I&5=NsjF)^gNy4sJ&#W)P%V3dc(@#;|2T+{*R3-PtiJI}@M zFiwCcKL~Y%cb?|)S?p$S&ItY?A~}Jc{1ce8)vM2;7O`q7SQ<-_An%ifn)c@NzX67Q B2$TQ- literal 0 HcmV?d00001 diff --git a/tiff/reader.go b/tiff/reader.go index fcb2b56..ea4f1f2 100644 --- a/tiff/reader.go +++ b/tiff/reader.go @@ -11,6 +11,7 @@ import ( "compress/lzw" "compress/zlib" "encoding/binary" + "fmt" "image" "image/color" "io" @@ -553,7 +554,11 @@ func Decode(r io.Reader) (img image.Image, err error) { offset := int64(blockOffsets[j*blocksAcross+i]) n := int64(blockCounts[j*blocksAcross+i]) switch d.firstVal(tCompression) { - case cNone: + + // 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: if b, ok := d.r.(*buffer); ok { d.buf, err = b.Slice(int(offset), int(n)) } else { @@ -574,7 +579,7 @@ func Decode(r io.Reader) (img image.Image, err error) { case cPackBits: d.buf, err = unpackBits(io.NewSectionReader(d.r, offset, n)) default: - err = UnsupportedError("compression") + err = UnsupportedError(fmt.Sprintf("compression value %d", d.firstVal(tCompression))) } if err != nil { return nil, err diff --git a/tiff/reader_test.go b/tiff/reader_test.go index 8539e9c..d62e277 100644 --- a/tiff/reader_test.go +++ b/tiff/reader_test.go @@ -44,6 +44,15 @@ func TestNoRPS(t *testing.T) { } } +// TestNoCompression tries to decode an images that has no Compression tag. +// This tag is mandatory, but most tools interpret a missing value as no compression. +func TestNoCompression(t *testing.T) { + _, err := load("no_compress.tiff") + if err != nil { + t.Fatal(err) + } +} + // TestUnpackBits tests the decoding of PackBits-encoded data. func TestUnpackBits(t *testing.T) { var unpackBitsTests = []struct {