reader.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package tiff implements a TIFF image decoder and encoder.
  5. //
  6. // The TIFF specification is at http://partners.adobe.com/public/developer/en/tiff/TIFF6.pdf
  7. package tiff // import "golang.org/x/image/tiff"
  8. import (
  9. "compress/zlib"
  10. "encoding/binary"
  11. "fmt"
  12. "image"
  13. "image/color"
  14. "io"
  15. "io/ioutil"
  16. "math"
  17. "golang.org/x/image/tiff/lzw"
  18. )
  19. // A FormatError reports that the input is not a valid TIFF image.
  20. type FormatError string
  21. func (e FormatError) Error() string {
  22. return "tiff: invalid format: " + string(e)
  23. }
  24. // An UnsupportedError reports that the input uses a valid but
  25. // unimplemented feature.
  26. type UnsupportedError string
  27. func (e UnsupportedError) Error() string {
  28. return "tiff: unsupported feature: " + string(e)
  29. }
  30. var errNoPixels = FormatError("not enough pixel data")
  31. type decoder struct {
  32. r io.ReaderAt
  33. byteOrder binary.ByteOrder
  34. config image.Config
  35. mode imageMode
  36. bpp uint
  37. features map[int][]uint
  38. palette []color.Color
  39. buf []byte
  40. off int // Current offset in buf.
  41. v uint32 // Buffer value for reading with arbitrary bit depths.
  42. nbits uint // Remaining number of bits in v.
  43. }
  44. // firstVal returns the first uint of the features entry with the given tag,
  45. // or 0 if the tag does not exist.
  46. func (d *decoder) firstVal(tag int) uint {
  47. f := d.features[tag]
  48. if len(f) == 0 {
  49. return 0
  50. }
  51. return f[0]
  52. }
  53. // ifdUint decodes the IFD entry in p, which must be of the Byte, Short
  54. // or Long type, and returns the decoded uint values.
  55. func (d *decoder) ifdUint(p []byte) (u []uint, err error) {
  56. var raw []byte
  57. if len(p) < ifdLen {
  58. return nil, FormatError("bad IFD entry")
  59. }
  60. datatype := d.byteOrder.Uint16(p[2:4])
  61. if dt := int(datatype); dt <= 0 || dt >= len(lengths) {
  62. return nil, UnsupportedError("IFD entry datatype")
  63. }
  64. count := d.byteOrder.Uint32(p[4:8])
  65. if count > math.MaxInt32/lengths[datatype] {
  66. return nil, FormatError("IFD data too large")
  67. }
  68. if datalen := lengths[datatype] * count; datalen > 4 {
  69. // The IFD contains a pointer to the real value.
  70. raw = make([]byte, datalen)
  71. _, err = d.r.ReadAt(raw, int64(d.byteOrder.Uint32(p[8:12])))
  72. } else {
  73. raw = p[8 : 8+datalen]
  74. }
  75. if err != nil {
  76. return nil, err
  77. }
  78. u = make([]uint, count)
  79. switch datatype {
  80. case dtByte:
  81. for i := uint32(0); i < count; i++ {
  82. u[i] = uint(raw[i])
  83. }
  84. case dtShort:
  85. for i := uint32(0); i < count; i++ {
  86. u[i] = uint(d.byteOrder.Uint16(raw[2*i : 2*(i+1)]))
  87. }
  88. case dtLong:
  89. for i := uint32(0); i < count; i++ {
  90. u[i] = uint(d.byteOrder.Uint32(raw[4*i : 4*(i+1)]))
  91. }
  92. default:
  93. return nil, UnsupportedError("data type")
  94. }
  95. return u, nil
  96. }
  97. // parseIFD decides whether the the IFD entry in p is "interesting" and
  98. // stows away the data in the decoder. It returns the tag number of the
  99. // entry and an error, if any.
  100. func (d *decoder) parseIFD(p []byte) (int, error) {
  101. tag := d.byteOrder.Uint16(p[0:2])
  102. switch tag {
  103. case tBitsPerSample,
  104. tExtraSamples,
  105. tPhotometricInterpretation,
  106. tCompression,
  107. tPredictor,
  108. tStripOffsets,
  109. tStripByteCounts,
  110. tRowsPerStrip,
  111. tTileWidth,
  112. tTileLength,
  113. tTileOffsets,
  114. tTileByteCounts,
  115. tImageLength,
  116. tImageWidth:
  117. val, err := d.ifdUint(p)
  118. if err != nil {
  119. return 0, err
  120. }
  121. d.features[int(tag)] = val
  122. case tColorMap:
  123. val, err := d.ifdUint(p)
  124. if err != nil {
  125. return 0, err
  126. }
  127. numcolors := len(val) / 3
  128. if len(val)%3 != 0 || numcolors <= 0 || numcolors > 256 {
  129. return 0, FormatError("bad ColorMap length")
  130. }
  131. d.palette = make([]color.Color, numcolors)
  132. for i := 0; i < numcolors; i++ {
  133. d.palette[i] = color.RGBA64{
  134. uint16(val[i]),
  135. uint16(val[i+numcolors]),
  136. uint16(val[i+2*numcolors]),
  137. 0xffff,
  138. }
  139. }
  140. case tSampleFormat:
  141. // Page 27 of the spec: If the SampleFormat is present and
  142. // the value is not 1 [= unsigned integer data], a Baseline
  143. // TIFF reader that cannot handle the SampleFormat value
  144. // must terminate the import process gracefully.
  145. val, err := d.ifdUint(p)
  146. if err != nil {
  147. return 0, err
  148. }
  149. for _, v := range val {
  150. if v != 1 {
  151. return 0, UnsupportedError("sample format")
  152. }
  153. }
  154. }
  155. return int(tag), nil
  156. }
  157. // readBits reads n bits from the internal buffer starting at the current offset.
  158. func (d *decoder) readBits(n uint) (v uint32, ok bool) {
  159. for d.nbits < n {
  160. d.v <<= 8
  161. if d.off >= len(d.buf) {
  162. return 0, false
  163. }
  164. d.v |= uint32(d.buf[d.off])
  165. d.off++
  166. d.nbits += 8
  167. }
  168. d.nbits -= n
  169. rv := d.v >> d.nbits
  170. d.v &^= rv << d.nbits
  171. return rv, true
  172. }
  173. // flushBits discards the unread bits in the buffer used by readBits.
  174. // It is used at the end of a line.
  175. func (d *decoder) flushBits() {
  176. d.v = 0
  177. d.nbits = 0
  178. }
  179. // minInt returns the smaller of x or y.
  180. func minInt(a, b int) int {
  181. if a <= b {
  182. return a
  183. }
  184. return b
  185. }
  186. // decode decodes the raw data of an image.
  187. // It reads from d.buf and writes the strip or tile into dst.
  188. func (d *decoder) decode(dst image.Image, xmin, ymin, xmax, ymax int) error {
  189. d.off = 0
  190. // Apply horizontal predictor if necessary.
  191. // In this case, p contains the color difference to the preceding pixel.
  192. // See page 64-65 of the spec.
  193. if d.firstVal(tPredictor) == prHorizontal {
  194. switch d.bpp {
  195. case 16:
  196. var off int
  197. n := 2 * len(d.features[tBitsPerSample]) // bytes per sample times samples per pixel
  198. for y := ymin; y < ymax; y++ {
  199. off += n
  200. for x := 0; x < (xmax-xmin-1)*n; x += 2 {
  201. if off+2 > len(d.buf) {
  202. return errNoPixels
  203. }
  204. v0 := d.byteOrder.Uint16(d.buf[off-n : off-n+2])
  205. v1 := d.byteOrder.Uint16(d.buf[off : off+2])
  206. d.byteOrder.PutUint16(d.buf[off:off+2], v1+v0)
  207. off += 2
  208. }
  209. }
  210. case 8:
  211. var off int
  212. n := 1 * len(d.features[tBitsPerSample]) // bytes per sample times samples per pixel
  213. for y := ymin; y < ymax; y++ {
  214. off += n
  215. for x := 0; x < (xmax-xmin-1)*n; x++ {
  216. if off >= len(d.buf) {
  217. return errNoPixels
  218. }
  219. d.buf[off] += d.buf[off-n]
  220. off++
  221. }
  222. }
  223. case 1:
  224. return UnsupportedError("horizontal predictor with 1 BitsPerSample")
  225. }
  226. }
  227. rMaxX := minInt(xmax, dst.Bounds().Max.X)
  228. rMaxY := minInt(ymax, dst.Bounds().Max.Y)
  229. switch d.mode {
  230. case mGray, mGrayInvert:
  231. if d.bpp == 16 {
  232. img := dst.(*image.Gray16)
  233. for y := ymin; y < rMaxY; y++ {
  234. for x := xmin; x < rMaxX; x++ {
  235. if d.off+2 > len(d.buf) {
  236. return errNoPixels
  237. }
  238. v := d.byteOrder.Uint16(d.buf[d.off : d.off+2])
  239. d.off += 2
  240. if d.mode == mGrayInvert {
  241. v = 0xffff - v
  242. }
  243. img.SetGray16(x, y, color.Gray16{v})
  244. }
  245. if rMaxX == img.Bounds().Max.X {
  246. d.off += 2 * (xmax - img.Bounds().Max.X)
  247. }
  248. }
  249. } else {
  250. img := dst.(*image.Gray)
  251. max := uint32((1 << d.bpp) - 1)
  252. for y := ymin; y < rMaxY; y++ {
  253. for x := xmin; x < rMaxX; x++ {
  254. v, ok := d.readBits(d.bpp)
  255. if !ok {
  256. return errNoPixels
  257. }
  258. v = v * 0xff / max
  259. if d.mode == mGrayInvert {
  260. v = 0xff - v
  261. }
  262. img.SetGray(x, y, color.Gray{uint8(v)})
  263. }
  264. d.flushBits()
  265. }
  266. }
  267. case mPaletted:
  268. img := dst.(*image.Paletted)
  269. for y := ymin; y < rMaxY; y++ {
  270. for x := xmin; x < rMaxX; x++ {
  271. v, ok := d.readBits(d.bpp)
  272. if !ok {
  273. return errNoPixels
  274. }
  275. img.SetColorIndex(x, y, uint8(v))
  276. }
  277. d.flushBits()
  278. }
  279. case mRGB:
  280. if d.bpp == 16 {
  281. img := dst.(*image.RGBA64)
  282. for y := ymin; y < rMaxY; y++ {
  283. for x := xmin; x < rMaxX; x++ {
  284. if d.off+6 > len(d.buf) {
  285. return errNoPixels
  286. }
  287. r := d.byteOrder.Uint16(d.buf[d.off+0 : d.off+2])
  288. g := d.byteOrder.Uint16(d.buf[d.off+2 : d.off+4])
  289. b := d.byteOrder.Uint16(d.buf[d.off+4 : d.off+6])
  290. d.off += 6
  291. img.SetRGBA64(x, y, color.RGBA64{r, g, b, 0xffff})
  292. }
  293. }
  294. } else {
  295. img := dst.(*image.RGBA)
  296. for y := ymin; y < rMaxY; y++ {
  297. min := img.PixOffset(xmin, y)
  298. max := img.PixOffset(rMaxX, y)
  299. off := (y - ymin) * (xmax - xmin) * 3
  300. for i := min; i < max; i += 4 {
  301. if off+3 > len(d.buf) {
  302. return errNoPixels
  303. }
  304. img.Pix[i+0] = d.buf[off+0]
  305. img.Pix[i+1] = d.buf[off+1]
  306. img.Pix[i+2] = d.buf[off+2]
  307. img.Pix[i+3] = 0xff
  308. off += 3
  309. }
  310. }
  311. }
  312. case mNRGBA:
  313. if d.bpp == 16 {
  314. img := dst.(*image.NRGBA64)
  315. for y := ymin; y < rMaxY; y++ {
  316. for x := xmin; x < rMaxX; x++ {
  317. if d.off+8 > len(d.buf) {
  318. return errNoPixels
  319. }
  320. r := d.byteOrder.Uint16(d.buf[d.off+0 : d.off+2])
  321. g := d.byteOrder.Uint16(d.buf[d.off+2 : d.off+4])
  322. b := d.byteOrder.Uint16(d.buf[d.off+4 : d.off+6])
  323. a := d.byteOrder.Uint16(d.buf[d.off+6 : d.off+8])
  324. d.off += 8
  325. img.SetNRGBA64(x, y, color.NRGBA64{r, g, b, a})
  326. }
  327. }
  328. } else {
  329. img := dst.(*image.NRGBA)
  330. for y := ymin; y < rMaxY; y++ {
  331. min := img.PixOffset(xmin, y)
  332. max := img.PixOffset(rMaxX, y)
  333. i0, i1 := (y-ymin)*(xmax-xmin)*4, (y-ymin+1)*(xmax-xmin)*4
  334. if i1 > len(d.buf) {
  335. return errNoPixels
  336. }
  337. copy(img.Pix[min:max], d.buf[i0:i1])
  338. }
  339. }
  340. case mRGBA:
  341. if d.bpp == 16 {
  342. img := dst.(*image.RGBA64)
  343. for y := ymin; y < rMaxY; y++ {
  344. for x := xmin; x < rMaxX; x++ {
  345. if d.off+8 > len(d.buf) {
  346. return errNoPixels
  347. }
  348. r := d.byteOrder.Uint16(d.buf[d.off+0 : d.off+2])
  349. g := d.byteOrder.Uint16(d.buf[d.off+2 : d.off+4])
  350. b := d.byteOrder.Uint16(d.buf[d.off+4 : d.off+6])
  351. a := d.byteOrder.Uint16(d.buf[d.off+6 : d.off+8])
  352. d.off += 8
  353. img.SetRGBA64(x, y, color.RGBA64{r, g, b, a})
  354. }
  355. }
  356. } else {
  357. img := dst.(*image.RGBA)
  358. for y := ymin; y < rMaxY; y++ {
  359. min := img.PixOffset(xmin, y)
  360. max := img.PixOffset(rMaxX, y)
  361. i0, i1 := (y-ymin)*(xmax-xmin)*4, (y-ymin+1)*(xmax-xmin)*4
  362. if i1 > len(d.buf) {
  363. return errNoPixels
  364. }
  365. copy(img.Pix[min:max], d.buf[i0:i1])
  366. }
  367. }
  368. }
  369. return nil
  370. }
  371. func newDecoder(r io.Reader) (*decoder, error) {
  372. d := &decoder{
  373. r: newReaderAt(r),
  374. features: make(map[int][]uint),
  375. }
  376. p := make([]byte, 8)
  377. if _, err := d.r.ReadAt(p, 0); err != nil {
  378. return nil, err
  379. }
  380. switch string(p[0:4]) {
  381. case leHeader:
  382. d.byteOrder = binary.LittleEndian
  383. case beHeader:
  384. d.byteOrder = binary.BigEndian
  385. default:
  386. return nil, FormatError("malformed header")
  387. }
  388. ifdOffset := int64(d.byteOrder.Uint32(p[4:8]))
  389. // The first two bytes contain the number of entries (12 bytes each).
  390. if _, err := d.r.ReadAt(p[0:2], ifdOffset); err != nil {
  391. return nil, err
  392. }
  393. numItems := int(d.byteOrder.Uint16(p[0:2]))
  394. // All IFD entries are read in one chunk.
  395. p = make([]byte, ifdLen*numItems)
  396. if _, err := d.r.ReadAt(p, ifdOffset+2); err != nil {
  397. return nil, err
  398. }
  399. prevTag := -1
  400. for i := 0; i < len(p); i += ifdLen {
  401. tag, err := d.parseIFD(p[i : i+ifdLen])
  402. if err != nil {
  403. return nil, err
  404. }
  405. if tag <= prevTag {
  406. return nil, FormatError("tags are not sorted in ascending order")
  407. }
  408. prevTag = tag
  409. }
  410. d.config.Width = int(d.firstVal(tImageWidth))
  411. d.config.Height = int(d.firstVal(tImageLength))
  412. if _, ok := d.features[tBitsPerSample]; !ok {
  413. return nil, FormatError("BitsPerSample tag missing")
  414. }
  415. d.bpp = d.firstVal(tBitsPerSample)
  416. switch d.bpp {
  417. case 0:
  418. return nil, FormatError("BitsPerSample must not be 0")
  419. case 1, 8, 16:
  420. // Nothing to do, these are accepted by this implementation.
  421. default:
  422. return nil, UnsupportedError(fmt.Sprintf("BitsPerSample of %v", d.bpp))
  423. }
  424. // Determine the image mode.
  425. switch d.firstVal(tPhotometricInterpretation) {
  426. case pRGB:
  427. if d.bpp == 16 {
  428. for _, b := range d.features[tBitsPerSample] {
  429. if b != 16 {
  430. return nil, FormatError("wrong number of samples for 16bit RGB")
  431. }
  432. }
  433. } else {
  434. for _, b := range d.features[tBitsPerSample] {
  435. if b != 8 {
  436. return nil, FormatError("wrong number of samples for 8bit RGB")
  437. }
  438. }
  439. }
  440. // RGB images normally have 3 samples per pixel.
  441. // If there are more, ExtraSamples (p. 31-32 of the spec)
  442. // gives their meaning (usually an alpha channel).
  443. //
  444. // This implementation does not support extra samples
  445. // of an unspecified type.
  446. switch len(d.features[tBitsPerSample]) {
  447. case 3:
  448. d.mode = mRGB
  449. if d.bpp == 16 {
  450. d.config.ColorModel = color.RGBA64Model
  451. } else {
  452. d.config.ColorModel = color.RGBAModel
  453. }
  454. case 4:
  455. switch d.firstVal(tExtraSamples) {
  456. case 1:
  457. d.mode = mRGBA
  458. if d.bpp == 16 {
  459. d.config.ColorModel = color.RGBA64Model
  460. } else {
  461. d.config.ColorModel = color.RGBAModel
  462. }
  463. case 2:
  464. d.mode = mNRGBA
  465. if d.bpp == 16 {
  466. d.config.ColorModel = color.NRGBA64Model
  467. } else {
  468. d.config.ColorModel = color.NRGBAModel
  469. }
  470. default:
  471. return nil, FormatError("wrong number of samples for RGB")
  472. }
  473. default:
  474. return nil, FormatError("wrong number of samples for RGB")
  475. }
  476. case pPaletted:
  477. d.mode = mPaletted
  478. d.config.ColorModel = color.Palette(d.palette)
  479. case pWhiteIsZero:
  480. d.mode = mGrayInvert
  481. if d.bpp == 16 {
  482. d.config.ColorModel = color.Gray16Model
  483. } else {
  484. d.config.ColorModel = color.GrayModel
  485. }
  486. case pBlackIsZero:
  487. d.mode = mGray
  488. if d.bpp == 16 {
  489. d.config.ColorModel = color.Gray16Model
  490. } else {
  491. d.config.ColorModel = color.GrayModel
  492. }
  493. default:
  494. return nil, UnsupportedError("color model")
  495. }
  496. return d, nil
  497. }
  498. // DecodeConfig returns the color model and dimensions of a TIFF image without
  499. // decoding the entire image.
  500. func DecodeConfig(r io.Reader) (image.Config, error) {
  501. d, err := newDecoder(r)
  502. if err != nil {
  503. return image.Config{}, err
  504. }
  505. return d.config, nil
  506. }
  507. // Decode reads a TIFF image from r and returns it as an image.Image.
  508. // The type of Image returned depends on the contents of the TIFF.
  509. func Decode(r io.Reader) (img image.Image, err error) {
  510. d, err := newDecoder(r)
  511. if err != nil {
  512. return
  513. }
  514. blockPadding := false
  515. blockWidth := d.config.Width
  516. blockHeight := d.config.Height
  517. blocksAcross := 1
  518. blocksDown := 1
  519. if d.config.Width == 0 {
  520. blocksAcross = 0
  521. }
  522. if d.config.Height == 0 {
  523. blocksDown = 0
  524. }
  525. var blockOffsets, blockCounts []uint
  526. if int(d.firstVal(tTileWidth)) != 0 {
  527. blockPadding = true
  528. blockWidth = int(d.firstVal(tTileWidth))
  529. blockHeight = int(d.firstVal(tTileLength))
  530. if blockWidth != 0 {
  531. blocksAcross = (d.config.Width + blockWidth - 1) / blockWidth
  532. }
  533. if blockHeight != 0 {
  534. blocksDown = (d.config.Height + blockHeight - 1) / blockHeight
  535. }
  536. blockCounts = d.features[tTileByteCounts]
  537. blockOffsets = d.features[tTileOffsets]
  538. } else {
  539. if int(d.firstVal(tRowsPerStrip)) != 0 {
  540. blockHeight = int(d.firstVal(tRowsPerStrip))
  541. }
  542. if blockHeight != 0 {
  543. blocksDown = (d.config.Height + blockHeight - 1) / blockHeight
  544. }
  545. blockOffsets = d.features[tStripOffsets]
  546. blockCounts = d.features[tStripByteCounts]
  547. }
  548. // Check if we have the right number of strips/tiles, offsets and counts.
  549. if n := blocksAcross * blocksDown; len(blockOffsets) < n || len(blockCounts) < n {
  550. return nil, FormatError("inconsistent header")
  551. }
  552. imgRect := image.Rect(0, 0, d.config.Width, d.config.Height)
  553. switch d.mode {
  554. case mGray, mGrayInvert:
  555. if d.bpp == 16 {
  556. img = image.NewGray16(imgRect)
  557. } else {
  558. img = image.NewGray(imgRect)
  559. }
  560. case mPaletted:
  561. img = image.NewPaletted(imgRect, d.palette)
  562. case mNRGBA:
  563. if d.bpp == 16 {
  564. img = image.NewNRGBA64(imgRect)
  565. } else {
  566. img = image.NewNRGBA(imgRect)
  567. }
  568. case mRGB, mRGBA:
  569. if d.bpp == 16 {
  570. img = image.NewRGBA64(imgRect)
  571. } else {
  572. img = image.NewRGBA(imgRect)
  573. }
  574. }
  575. for i := 0; i < blocksAcross; i++ {
  576. blkW := blockWidth
  577. if !blockPadding && i == blocksAcross-1 && d.config.Width%blockWidth != 0 {
  578. blkW = d.config.Width % blockWidth
  579. }
  580. for j := 0; j < blocksDown; j++ {
  581. blkH := blockHeight
  582. if !blockPadding && j == blocksDown-1 && d.config.Height%blockHeight != 0 {
  583. blkH = d.config.Height % blockHeight
  584. }
  585. offset := int64(blockOffsets[j*blocksAcross+i])
  586. n := int64(blockCounts[j*blocksAcross+i])
  587. switch d.firstVal(tCompression) {
  588. // According to the spec, Compression does not have a default value,
  589. // but some tools interpret a missing Compression value as none so we do
  590. // the same.
  591. case cNone, 0:
  592. if b, ok := d.r.(*buffer); ok {
  593. d.buf, err = b.Slice(int(offset), int(n))
  594. } else {
  595. d.buf = make([]byte, n)
  596. _, err = d.r.ReadAt(d.buf, offset)
  597. }
  598. case cLZW:
  599. r := lzw.NewReader(io.NewSectionReader(d.r, offset, n), lzw.MSB, 8)
  600. d.buf, err = ioutil.ReadAll(r)
  601. r.Close()
  602. case cDeflate, cDeflateOld:
  603. var r io.ReadCloser
  604. r, err = zlib.NewReader(io.NewSectionReader(d.r, offset, n))
  605. if err != nil {
  606. return nil, err
  607. }
  608. d.buf, err = ioutil.ReadAll(r)
  609. r.Close()
  610. case cPackBits:
  611. d.buf, err = unpackBits(io.NewSectionReader(d.r, offset, n))
  612. default:
  613. err = UnsupportedError(fmt.Sprintf("compression value %d", d.firstVal(tCompression)))
  614. }
  615. if err != nil {
  616. return nil, err
  617. }
  618. xmin := i * blockWidth
  619. ymin := j * blockHeight
  620. xmax := xmin + blkW
  621. ymax := ymin + blkH
  622. err = d.decode(img, xmin, ymin, xmax, ymax)
  623. if err != nil {
  624. return nil, err
  625. }
  626. }
  627. }
  628. return
  629. }
  630. func init() {
  631. image.RegisterFormat("tiff", leHeader, Decode, DecodeConfig)
  632. image.RegisterFormat("tiff", beHeader, Decode, DecodeConfig)
  633. }