reader.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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. }
  246. } else {
  247. img := dst.(*image.Gray)
  248. max := uint32((1 << d.bpp) - 1)
  249. for y := ymin; y < rMaxY; y++ {
  250. for x := xmin; x < rMaxX; x++ {
  251. v, ok := d.readBits(d.bpp)
  252. if !ok {
  253. return errNoPixels
  254. }
  255. v = v * 0xff / max
  256. if d.mode == mGrayInvert {
  257. v = 0xff - v
  258. }
  259. img.SetGray(x, y, color.Gray{uint8(v)})
  260. }
  261. d.flushBits()
  262. }
  263. }
  264. case mPaletted:
  265. img := dst.(*image.Paletted)
  266. for y := ymin; y < rMaxY; y++ {
  267. for x := xmin; x < rMaxX; x++ {
  268. v, ok := d.readBits(d.bpp)
  269. if !ok {
  270. return errNoPixels
  271. }
  272. img.SetColorIndex(x, y, uint8(v))
  273. }
  274. d.flushBits()
  275. }
  276. case mRGB:
  277. if d.bpp == 16 {
  278. img := dst.(*image.RGBA64)
  279. for y := ymin; y < rMaxY; y++ {
  280. for x := xmin; x < rMaxX; x++ {
  281. if d.off+6 > len(d.buf) {
  282. return errNoPixels
  283. }
  284. r := d.byteOrder.Uint16(d.buf[d.off+0 : d.off+2])
  285. g := d.byteOrder.Uint16(d.buf[d.off+2 : d.off+4])
  286. b := d.byteOrder.Uint16(d.buf[d.off+4 : d.off+6])
  287. d.off += 6
  288. img.SetRGBA64(x, y, color.RGBA64{r, g, b, 0xffff})
  289. }
  290. }
  291. } else {
  292. img := dst.(*image.RGBA)
  293. for y := ymin; y < rMaxY; y++ {
  294. min := img.PixOffset(xmin, y)
  295. max := img.PixOffset(rMaxX, y)
  296. off := (y - ymin) * (xmax - xmin) * 3
  297. for i := min; i < max; i += 4 {
  298. if off+3 > len(d.buf) {
  299. return errNoPixels
  300. }
  301. img.Pix[i+0] = d.buf[off+0]
  302. img.Pix[i+1] = d.buf[off+1]
  303. img.Pix[i+2] = d.buf[off+2]
  304. img.Pix[i+3] = 0xff
  305. off += 3
  306. }
  307. }
  308. }
  309. case mNRGBA:
  310. if d.bpp == 16 {
  311. img := dst.(*image.NRGBA64)
  312. for y := ymin; y < rMaxY; y++ {
  313. for x := xmin; x < rMaxX; x++ {
  314. if d.off+8 > len(d.buf) {
  315. return errNoPixels
  316. }
  317. r := d.byteOrder.Uint16(d.buf[d.off+0 : d.off+2])
  318. g := d.byteOrder.Uint16(d.buf[d.off+2 : d.off+4])
  319. b := d.byteOrder.Uint16(d.buf[d.off+4 : d.off+6])
  320. a := d.byteOrder.Uint16(d.buf[d.off+6 : d.off+8])
  321. d.off += 8
  322. img.SetNRGBA64(x, y, color.NRGBA64{r, g, b, a})
  323. }
  324. }
  325. } else {
  326. img := dst.(*image.NRGBA)
  327. for y := ymin; y < rMaxY; y++ {
  328. min := img.PixOffset(xmin, y)
  329. max := img.PixOffset(rMaxX, y)
  330. i0, i1 := (y-ymin)*(xmax-xmin)*4, (y-ymin+1)*(xmax-xmin)*4
  331. if i1 > len(d.buf) {
  332. return errNoPixels
  333. }
  334. copy(img.Pix[min:max], d.buf[i0:i1])
  335. }
  336. }
  337. case mRGBA:
  338. if d.bpp == 16 {
  339. img := dst.(*image.RGBA64)
  340. for y := ymin; y < rMaxY; y++ {
  341. for x := xmin; x < rMaxX; x++ {
  342. if d.off+8 > len(d.buf) {
  343. return errNoPixels
  344. }
  345. r := d.byteOrder.Uint16(d.buf[d.off+0 : d.off+2])
  346. g := d.byteOrder.Uint16(d.buf[d.off+2 : d.off+4])
  347. b := d.byteOrder.Uint16(d.buf[d.off+4 : d.off+6])
  348. a := d.byteOrder.Uint16(d.buf[d.off+6 : d.off+8])
  349. d.off += 8
  350. img.SetRGBA64(x, y, color.RGBA64{r, g, b, a})
  351. }
  352. }
  353. } else {
  354. img := dst.(*image.RGBA)
  355. for y := ymin; y < rMaxY; y++ {
  356. min := img.PixOffset(xmin, y)
  357. max := img.PixOffset(rMaxX, y)
  358. i0, i1 := (y-ymin)*(xmax-xmin)*4, (y-ymin+1)*(xmax-xmin)*4
  359. if i1 > len(d.buf) {
  360. return errNoPixels
  361. }
  362. copy(img.Pix[min:max], d.buf[i0:i1])
  363. }
  364. }
  365. }
  366. return nil
  367. }
  368. func newDecoder(r io.Reader) (*decoder, error) {
  369. d := &decoder{
  370. r: newReaderAt(r),
  371. features: make(map[int][]uint),
  372. }
  373. p := make([]byte, 8)
  374. if _, err := d.r.ReadAt(p, 0); err != nil {
  375. return nil, err
  376. }
  377. switch string(p[0:4]) {
  378. case leHeader:
  379. d.byteOrder = binary.LittleEndian
  380. case beHeader:
  381. d.byteOrder = binary.BigEndian
  382. default:
  383. return nil, FormatError("malformed header")
  384. }
  385. ifdOffset := int64(d.byteOrder.Uint32(p[4:8]))
  386. // The first two bytes contain the number of entries (12 bytes each).
  387. if _, err := d.r.ReadAt(p[0:2], ifdOffset); err != nil {
  388. return nil, err
  389. }
  390. numItems := int(d.byteOrder.Uint16(p[0:2]))
  391. // All IFD entries are read in one chunk.
  392. p = make([]byte, ifdLen*numItems)
  393. if _, err := d.r.ReadAt(p, ifdOffset+2); err != nil {
  394. return nil, err
  395. }
  396. prevTag := -1
  397. for i := 0; i < len(p); i += ifdLen {
  398. tag, err := d.parseIFD(p[i : i+ifdLen])
  399. if err != nil {
  400. return nil, err
  401. }
  402. if tag <= prevTag {
  403. return nil, FormatError("tags are not sorted in ascending order")
  404. }
  405. prevTag = tag
  406. }
  407. d.config.Width = int(d.firstVal(tImageWidth))
  408. d.config.Height = int(d.firstVal(tImageLength))
  409. if _, ok := d.features[tBitsPerSample]; !ok {
  410. return nil, FormatError("BitsPerSample tag missing")
  411. }
  412. d.bpp = d.firstVal(tBitsPerSample)
  413. switch d.bpp {
  414. case 0:
  415. return nil, FormatError("BitsPerSample must not be 0")
  416. case 1, 8, 16:
  417. // Nothing to do, these are accepted by this implementation.
  418. default:
  419. return nil, UnsupportedError(fmt.Sprintf("BitsPerSample of %v", d.bpp))
  420. }
  421. // Determine the image mode.
  422. switch d.firstVal(tPhotometricInterpretation) {
  423. case pRGB:
  424. if d.bpp == 16 {
  425. for _, b := range d.features[tBitsPerSample] {
  426. if b != 16 {
  427. return nil, FormatError("wrong number of samples for 16bit RGB")
  428. }
  429. }
  430. } else {
  431. for _, b := range d.features[tBitsPerSample] {
  432. if b != 8 {
  433. return nil, FormatError("wrong number of samples for 8bit RGB")
  434. }
  435. }
  436. }
  437. // RGB images normally have 3 samples per pixel.
  438. // If there are more, ExtraSamples (p. 31-32 of the spec)
  439. // gives their meaning (usually an alpha channel).
  440. //
  441. // This implementation does not support extra samples
  442. // of an unspecified type.
  443. switch len(d.features[tBitsPerSample]) {
  444. case 3:
  445. d.mode = mRGB
  446. if d.bpp == 16 {
  447. d.config.ColorModel = color.RGBA64Model
  448. } else {
  449. d.config.ColorModel = color.RGBAModel
  450. }
  451. case 4:
  452. switch d.firstVal(tExtraSamples) {
  453. case 1:
  454. d.mode = mRGBA
  455. if d.bpp == 16 {
  456. d.config.ColorModel = color.RGBA64Model
  457. } else {
  458. d.config.ColorModel = color.RGBAModel
  459. }
  460. case 2:
  461. d.mode = mNRGBA
  462. if d.bpp == 16 {
  463. d.config.ColorModel = color.NRGBA64Model
  464. } else {
  465. d.config.ColorModel = color.NRGBAModel
  466. }
  467. default:
  468. return nil, FormatError("wrong number of samples for RGB")
  469. }
  470. default:
  471. return nil, FormatError("wrong number of samples for RGB")
  472. }
  473. case pPaletted:
  474. d.mode = mPaletted
  475. d.config.ColorModel = color.Palette(d.palette)
  476. case pWhiteIsZero:
  477. d.mode = mGrayInvert
  478. if d.bpp == 16 {
  479. d.config.ColorModel = color.Gray16Model
  480. } else {
  481. d.config.ColorModel = color.GrayModel
  482. }
  483. case pBlackIsZero:
  484. d.mode = mGray
  485. if d.bpp == 16 {
  486. d.config.ColorModel = color.Gray16Model
  487. } else {
  488. d.config.ColorModel = color.GrayModel
  489. }
  490. default:
  491. return nil, UnsupportedError("color model")
  492. }
  493. return d, nil
  494. }
  495. // DecodeConfig returns the color model and dimensions of a TIFF image without
  496. // decoding the entire image.
  497. func DecodeConfig(r io.Reader) (image.Config, error) {
  498. d, err := newDecoder(r)
  499. if err != nil {
  500. return image.Config{}, err
  501. }
  502. return d.config, nil
  503. }
  504. // Decode reads a TIFF image from r and returns it as an image.Image.
  505. // The type of Image returned depends on the contents of the TIFF.
  506. func Decode(r io.Reader) (img image.Image, err error) {
  507. d, err := newDecoder(r)
  508. if err != nil {
  509. return
  510. }
  511. blockPadding := false
  512. blockWidth := d.config.Width
  513. blockHeight := d.config.Height
  514. blocksAcross := 1
  515. blocksDown := 1
  516. if d.config.Width == 0 {
  517. blocksAcross = 0
  518. }
  519. if d.config.Height == 0 {
  520. blocksDown = 0
  521. }
  522. var blockOffsets, blockCounts []uint
  523. if int(d.firstVal(tTileWidth)) != 0 {
  524. blockPadding = true
  525. blockWidth = int(d.firstVal(tTileWidth))
  526. blockHeight = int(d.firstVal(tTileLength))
  527. if blockWidth != 0 {
  528. blocksAcross = (d.config.Width + blockWidth - 1) / blockWidth
  529. }
  530. if blockHeight != 0 {
  531. blocksDown = (d.config.Height + blockHeight - 1) / blockHeight
  532. }
  533. blockCounts = d.features[tTileByteCounts]
  534. blockOffsets = d.features[tTileOffsets]
  535. } else {
  536. if int(d.firstVal(tRowsPerStrip)) != 0 {
  537. blockHeight = int(d.firstVal(tRowsPerStrip))
  538. }
  539. if blockHeight != 0 {
  540. blocksDown = (d.config.Height + blockHeight - 1) / blockHeight
  541. }
  542. blockOffsets = d.features[tStripOffsets]
  543. blockCounts = d.features[tStripByteCounts]
  544. }
  545. // Check if we have the right number of strips/tiles, offsets and counts.
  546. if n := blocksAcross * blocksDown; len(blockOffsets) < n || len(blockCounts) < n {
  547. return nil, FormatError("inconsistent header")
  548. }
  549. imgRect := image.Rect(0, 0, d.config.Width, d.config.Height)
  550. switch d.mode {
  551. case mGray, mGrayInvert:
  552. if d.bpp == 16 {
  553. img = image.NewGray16(imgRect)
  554. } else {
  555. img = image.NewGray(imgRect)
  556. }
  557. case mPaletted:
  558. img = image.NewPaletted(imgRect, d.palette)
  559. case mNRGBA:
  560. if d.bpp == 16 {
  561. img = image.NewNRGBA64(imgRect)
  562. } else {
  563. img = image.NewNRGBA(imgRect)
  564. }
  565. case mRGB, mRGBA:
  566. if d.bpp == 16 {
  567. img = image.NewRGBA64(imgRect)
  568. } else {
  569. img = image.NewRGBA(imgRect)
  570. }
  571. }
  572. for i := 0; i < blocksAcross; i++ {
  573. blkW := blockWidth
  574. if !blockPadding && i == blocksAcross-1 && d.config.Width%blockWidth != 0 {
  575. blkW = d.config.Width % blockWidth
  576. }
  577. for j := 0; j < blocksDown; j++ {
  578. blkH := blockHeight
  579. if !blockPadding && j == blocksDown-1 && d.config.Height%blockHeight != 0 {
  580. blkH = d.config.Height % blockHeight
  581. }
  582. offset := int64(blockOffsets[j*blocksAcross+i])
  583. n := int64(blockCounts[j*blocksAcross+i])
  584. switch d.firstVal(tCompression) {
  585. // According to the spec, Compression does not have a default value,
  586. // but some tools interpret a missing Compression value as none so we do
  587. // the same.
  588. case cNone, 0:
  589. if b, ok := d.r.(*buffer); ok {
  590. d.buf, err = b.Slice(int(offset), int(n))
  591. } else {
  592. d.buf = make([]byte, n)
  593. _, err = d.r.ReadAt(d.buf, offset)
  594. }
  595. case cLZW:
  596. r := lzw.NewReader(io.NewSectionReader(d.r, offset, n), lzw.MSB, 8)
  597. d.buf, err = ioutil.ReadAll(r)
  598. r.Close()
  599. case cDeflate, cDeflateOld:
  600. var r io.ReadCloser
  601. r, err = zlib.NewReader(io.NewSectionReader(d.r, offset, n))
  602. if err != nil {
  603. return nil, err
  604. }
  605. d.buf, err = ioutil.ReadAll(r)
  606. r.Close()
  607. case cPackBits:
  608. d.buf, err = unpackBits(io.NewSectionReader(d.r, offset, n))
  609. default:
  610. err = UnsupportedError(fmt.Sprintf("compression value %d", d.firstVal(tCompression)))
  611. }
  612. if err != nil {
  613. return nil, err
  614. }
  615. xmin := i * blockWidth
  616. ymin := j * blockHeight
  617. xmax := xmin + blkW
  618. ymax := ymin + blkH
  619. err = d.decode(img, xmin, ymin, xmax, ymax)
  620. if err != nil {
  621. return nil, err
  622. }
  623. }
  624. }
  625. return
  626. }
  627. func init() {
  628. image.RegisterFormat("tiff", leHeader, Decode, DecodeConfig)
  629. image.RegisterFormat("tiff", beHeader, Decode, DecodeConfig)
  630. }