decode.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. // Copyright 2014 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 vp8l implements a decoder for the VP8L lossless image format.
  5. //
  6. // The VP8L specification is at:
  7. // https://developers.google.com/speed/webp/docs/riff_container
  8. package vp8l // import "golang.org/x/image/vp8l"
  9. import (
  10. "bufio"
  11. "errors"
  12. "image"
  13. "image/color"
  14. "io"
  15. )
  16. var (
  17. errInvalidCodeLengths = errors.New("vp8l: invalid code lengths")
  18. errInvalidHuffmanTree = errors.New("vp8l: invalid Huffman tree")
  19. )
  20. // colorCacheMultiplier is the multiplier used for the color cache hash
  21. // function, specified in section 4.2.3.
  22. const colorCacheMultiplier = 0x1e35a7bd
  23. // distanceMapTable is the look-up table for distanceMap.
  24. var distanceMapTable = [120]uint8{
  25. 0x18, 0x07, 0x17, 0x19, 0x28, 0x06, 0x27, 0x29, 0x16, 0x1a,
  26. 0x26, 0x2a, 0x38, 0x05, 0x37, 0x39, 0x15, 0x1b, 0x36, 0x3a,
  27. 0x25, 0x2b, 0x48, 0x04, 0x47, 0x49, 0x14, 0x1c, 0x35, 0x3b,
  28. 0x46, 0x4a, 0x24, 0x2c, 0x58, 0x45, 0x4b, 0x34, 0x3c, 0x03,
  29. 0x57, 0x59, 0x13, 0x1d, 0x56, 0x5a, 0x23, 0x2d, 0x44, 0x4c,
  30. 0x55, 0x5b, 0x33, 0x3d, 0x68, 0x02, 0x67, 0x69, 0x12, 0x1e,
  31. 0x66, 0x6a, 0x22, 0x2e, 0x54, 0x5c, 0x43, 0x4d, 0x65, 0x6b,
  32. 0x32, 0x3e, 0x78, 0x01, 0x77, 0x79, 0x53, 0x5d, 0x11, 0x1f,
  33. 0x64, 0x6c, 0x42, 0x4e, 0x76, 0x7a, 0x21, 0x2f, 0x75, 0x7b,
  34. 0x31, 0x3f, 0x63, 0x6d, 0x52, 0x5e, 0x00, 0x74, 0x7c, 0x41,
  35. 0x4f, 0x10, 0x20, 0x62, 0x6e, 0x30, 0x73, 0x7d, 0x51, 0x5f,
  36. 0x40, 0x72, 0x7e, 0x61, 0x6f, 0x50, 0x71, 0x7f, 0x60, 0x70,
  37. }
  38. // distanceMap maps a LZ77 backwards reference distance to a two-dimensional
  39. // pixel offset, specified in section 4.2.2.
  40. func distanceMap(w int32, code uint32) int32 {
  41. if int32(code) > int32(len(distanceMapTable)) {
  42. return int32(code) - int32(len(distanceMapTable))
  43. }
  44. distCode := int32(distanceMapTable[code-1])
  45. yOffset := distCode >> 4
  46. xOffset := 8 - distCode&0xf
  47. if d := yOffset*w + xOffset; d >= 1 {
  48. return d
  49. }
  50. return 1
  51. }
  52. // decoder holds the bit-stream for a VP8L image.
  53. type decoder struct {
  54. r io.ByteReader
  55. bits uint32
  56. nBits uint32
  57. }
  58. // read reads the next n bits from the decoder's bit-stream.
  59. func (d *decoder) read(n uint32) (uint32, error) {
  60. for d.nBits < n {
  61. c, err := d.r.ReadByte()
  62. if err != nil {
  63. if err == io.EOF {
  64. err = io.ErrUnexpectedEOF
  65. }
  66. return 0, err
  67. }
  68. d.bits |= uint32(c) << d.nBits
  69. d.nBits += 8
  70. }
  71. u := d.bits & (1<<n - 1)
  72. d.bits >>= n
  73. d.nBits -= n
  74. return u, nil
  75. }
  76. // decodeTransform decodes the next transform and the width of the image after
  77. // transformation (or equivalently, before inverse transformation), specified
  78. // in section 3.
  79. func (d *decoder) decodeTransform(w int32, h int32) (t transform, newWidth int32, err error) {
  80. t.oldWidth = w
  81. t.transformType, err = d.read(2)
  82. if err != nil {
  83. return transform{}, 0, err
  84. }
  85. switch t.transformType {
  86. case transformTypePredictor, transformTypeCrossColor:
  87. t.bits, err = d.read(3)
  88. if err != nil {
  89. return transform{}, 0, err
  90. }
  91. t.bits += 2
  92. t.pix, err = d.decodePix(nTiles(w, t.bits), nTiles(h, t.bits), 0, false)
  93. if err != nil {
  94. return transform{}, 0, err
  95. }
  96. case transformTypeSubtractGreen:
  97. // No-op.
  98. case transformTypeColorIndexing:
  99. nColors, err := d.read(8)
  100. if err != nil {
  101. return transform{}, 0, err
  102. }
  103. nColors++
  104. t.bits = 0
  105. switch {
  106. case nColors <= 2:
  107. t.bits = 3
  108. case nColors <= 4:
  109. t.bits = 2
  110. case nColors <= 16:
  111. t.bits = 1
  112. }
  113. w = nTiles(w, t.bits)
  114. pix, err := d.decodePix(int32(nColors), 1, 4*256, false)
  115. if err != nil {
  116. return transform{}, 0, err
  117. }
  118. for p := 4; p < len(pix); p += 4 {
  119. pix[p+0] += pix[p-4]
  120. pix[p+1] += pix[p-3]
  121. pix[p+2] += pix[p-2]
  122. pix[p+3] += pix[p-1]
  123. }
  124. // The spec says that "if the index is equal or larger than color_table_size,
  125. // the argb color value should be set to 0x00000000 (transparent black)."
  126. // We re-slice up to 256 4-byte pixels.
  127. t.pix = pix[:4*256]
  128. }
  129. return t, w, nil
  130. }
  131. // repeatsCodeLength is the minimum code length for repeated codes.
  132. const repeatsCodeLength = 16
  133. // These magic numbers are specified at the end of section 5.2.2.
  134. // The 3-length arrays apply to code lengths >= repeatsCodeLength.
  135. var (
  136. codeLengthCodeOrder = [19]uint8{
  137. 17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  138. }
  139. repeatBits = [3]uint8{2, 3, 7}
  140. repeatOffsets = [3]uint8{3, 3, 11}
  141. )
  142. // decodeCodeLengths decodes a Huffman tree's code lengths which are themselves
  143. // encoded via a Huffman tree, specified in section 5.2.2.
  144. func (d *decoder) decodeCodeLengths(dst []uint32, codeLengthCodeLengths []uint32) error {
  145. h := hTree{}
  146. if err := h.build(codeLengthCodeLengths); err != nil {
  147. return err
  148. }
  149. maxSymbol := len(dst)
  150. useLength, err := d.read(1)
  151. if err != nil {
  152. return err
  153. }
  154. if useLength != 0 {
  155. n, err := d.read(3)
  156. if err != nil {
  157. return err
  158. }
  159. n = 2 + 2*n
  160. ms, err := d.read(n)
  161. if err != nil {
  162. return err
  163. }
  164. maxSymbol = int(ms) + 2
  165. if maxSymbol > len(dst) {
  166. return errInvalidCodeLengths
  167. }
  168. }
  169. // The spec says that "if code 16 [meaning repeat] is used before
  170. // a non-zero value has been emitted, a value of 8 is repeated."
  171. prevCodeLength := uint32(8)
  172. for symbol := 0; symbol < len(dst); {
  173. if maxSymbol == 0 {
  174. break
  175. }
  176. maxSymbol--
  177. codeLength, err := h.next(d)
  178. if err != nil {
  179. return err
  180. }
  181. if codeLength < repeatsCodeLength {
  182. dst[symbol] = codeLength
  183. symbol++
  184. if codeLength != 0 {
  185. prevCodeLength = codeLength
  186. }
  187. continue
  188. }
  189. repeat, err := d.read(uint32(repeatBits[codeLength-repeatsCodeLength]))
  190. if err != nil {
  191. return err
  192. }
  193. repeat += uint32(repeatOffsets[codeLength-repeatsCodeLength])
  194. if symbol+int(repeat) > len(dst) {
  195. return errInvalidCodeLengths
  196. }
  197. // A code length of 16 repeats the previous non-zero code.
  198. // A code length of 17 or 18 repeats zeroes.
  199. cl := uint32(0)
  200. if codeLength == 16 {
  201. cl = prevCodeLength
  202. }
  203. for ; repeat > 0; repeat-- {
  204. dst[symbol] = cl
  205. symbol++
  206. }
  207. }
  208. return nil
  209. }
  210. // decodeHuffmanTree decodes a Huffman tree into h.
  211. func (d *decoder) decodeHuffmanTree(h *hTree, alphabetSize uint32) error {
  212. useSimple, err := d.read(1)
  213. if err != nil {
  214. return err
  215. }
  216. if useSimple != 0 {
  217. nSymbols, err := d.read(1)
  218. if err != nil {
  219. return err
  220. }
  221. nSymbols++
  222. firstSymbolLengthCode, err := d.read(1)
  223. if err != nil {
  224. return err
  225. }
  226. firstSymbolLengthCode = 7*firstSymbolLengthCode + 1
  227. var symbols [2]uint32
  228. symbols[0], err = d.read(firstSymbolLengthCode)
  229. if err != nil {
  230. return err
  231. }
  232. if nSymbols == 2 {
  233. symbols[1], err = d.read(8)
  234. if err != nil {
  235. return err
  236. }
  237. }
  238. return h.buildSimple(nSymbols, symbols, alphabetSize)
  239. }
  240. nCodes, err := d.read(4)
  241. if err != nil {
  242. return err
  243. }
  244. nCodes += 4
  245. if int(nCodes) > len(codeLengthCodeOrder) {
  246. return errInvalidHuffmanTree
  247. }
  248. codeLengthCodeLengths := [len(codeLengthCodeOrder)]uint32{}
  249. for i := uint32(0); i < nCodes; i++ {
  250. codeLengthCodeLengths[codeLengthCodeOrder[i]], err = d.read(3)
  251. if err != nil {
  252. return err
  253. }
  254. }
  255. codeLengths := make([]uint32, alphabetSize)
  256. if err = d.decodeCodeLengths(codeLengths, codeLengthCodeLengths[:]); err != nil {
  257. return err
  258. }
  259. return h.build(codeLengths)
  260. }
  261. const (
  262. huffGreen = 0
  263. huffRed = 1
  264. huffBlue = 2
  265. huffAlpha = 3
  266. huffDistance = 4
  267. nHuff = 5
  268. )
  269. // hGroup is an array of 5 Huffman trees.
  270. type hGroup [nHuff]hTree
  271. // decodeHuffmanGroups decodes the one or more hGroups used to decode the pixel
  272. // data. If one hGroup is used for the entire image, then hPix and hBits will
  273. // be zero. If more than one hGroup is used, then hPix contains the meta-image
  274. // that maps tiles to hGroup index, and hBits contains the log-2 tile size.
  275. func (d *decoder) decodeHuffmanGroups(w int32, h int32, topLevel bool, ccBits uint32) (
  276. hGroups []hGroup, hPix []byte, hBits uint32, err error) {
  277. maxHGroupIndex := 0
  278. if topLevel {
  279. useMeta, err := d.read(1)
  280. if err != nil {
  281. return nil, nil, 0, err
  282. }
  283. if useMeta != 0 {
  284. hBits, err = d.read(3)
  285. if err != nil {
  286. return nil, nil, 0, err
  287. }
  288. hBits += 2
  289. hPix, err = d.decodePix(nTiles(w, hBits), nTiles(h, hBits), 0, false)
  290. if err != nil {
  291. return nil, nil, 0, err
  292. }
  293. for p := 0; p < len(hPix); p += 4 {
  294. i := int(hPix[p])<<8 | int(hPix[p+1])
  295. if maxHGroupIndex < i {
  296. maxHGroupIndex = i
  297. }
  298. }
  299. }
  300. }
  301. hGroups = make([]hGroup, maxHGroupIndex+1)
  302. for i := range hGroups {
  303. for j, alphabetSize := range alphabetSizes {
  304. if j == 0 && ccBits > 0 {
  305. alphabetSize += 1 << ccBits
  306. }
  307. if err := d.decodeHuffmanTree(&hGroups[i][j], alphabetSize); err != nil {
  308. return nil, nil, 0, err
  309. }
  310. }
  311. }
  312. return hGroups, hPix, hBits, nil
  313. }
  314. const (
  315. nLiteralCodes = 256
  316. nLengthCodes = 24
  317. nDistanceCodes = 40
  318. )
  319. var alphabetSizes = [nHuff]uint32{
  320. nLiteralCodes + nLengthCodes,
  321. nLiteralCodes,
  322. nLiteralCodes,
  323. nLiteralCodes,
  324. nDistanceCodes,
  325. }
  326. // decodePix decodes pixel data, specified in section 5.2.2.
  327. func (d *decoder) decodePix(w int32, h int32, minCap int32, topLevel bool) ([]byte, error) {
  328. // Decode the color cache parameters.
  329. ccBits, ccShift, ccEntries := uint32(0), uint32(0), ([]uint32)(nil)
  330. useColorCache, err := d.read(1)
  331. if err != nil {
  332. return nil, err
  333. }
  334. if useColorCache != 0 {
  335. ccBits, err = d.read(4)
  336. if err != nil {
  337. return nil, err
  338. }
  339. if ccBits < 1 || 11 < ccBits {
  340. return nil, errors.New("vp8l: invalid color cache parameters")
  341. }
  342. ccShift = 32 - ccBits
  343. ccEntries = make([]uint32, 1<<ccBits)
  344. }
  345. // Decode the Huffman groups.
  346. hGroups, hPix, hBits, err := d.decodeHuffmanGroups(w, h, topLevel, ccBits)
  347. if err != nil {
  348. return nil, err
  349. }
  350. hMask, tilesPerRow := int32(0), int32(0)
  351. if hBits != 0 {
  352. hMask, tilesPerRow = 1<<hBits-1, nTiles(w, hBits)
  353. }
  354. // Decode the pixels.
  355. if minCap < 4*w*h {
  356. minCap = 4 * w * h
  357. }
  358. pix := make([]byte, 4*w*h, minCap)
  359. p, cachedP := 0, 0
  360. x, y := int32(0), int32(0)
  361. hg, lookupHG := &hGroups[0], hMask != 0
  362. for p < len(pix) {
  363. if lookupHG {
  364. i := 4 * (tilesPerRow*(y>>hBits) + (x >> hBits))
  365. hg = &hGroups[uint32(hPix[i])<<8|uint32(hPix[i+1])]
  366. }
  367. green, err := hg[huffGreen].next(d)
  368. if err != nil {
  369. return nil, err
  370. }
  371. switch {
  372. case green < nLiteralCodes:
  373. // We have a literal pixel.
  374. red, err := hg[huffRed].next(d)
  375. if err != nil {
  376. return nil, err
  377. }
  378. blue, err := hg[huffBlue].next(d)
  379. if err != nil {
  380. return nil, err
  381. }
  382. alpha, err := hg[huffAlpha].next(d)
  383. if err != nil {
  384. return nil, err
  385. }
  386. pix[p+0] = uint8(red)
  387. pix[p+1] = uint8(green)
  388. pix[p+2] = uint8(blue)
  389. pix[p+3] = uint8(alpha)
  390. p += 4
  391. x++
  392. if x == w {
  393. x, y = 0, y+1
  394. }
  395. lookupHG = hMask != 0 && x&hMask == 0
  396. case green < nLiteralCodes+nLengthCodes:
  397. // We have a LZ77 backwards reference.
  398. length, err := d.lz77Param(green - nLiteralCodes)
  399. if err != nil {
  400. return nil, err
  401. }
  402. distSym, err := hg[huffDistance].next(d)
  403. if err != nil {
  404. return nil, err
  405. }
  406. distCode, err := d.lz77Param(distSym)
  407. if err != nil {
  408. return nil, err
  409. }
  410. dist := distanceMap(w, distCode)
  411. pEnd := p + 4*int(length)
  412. q := p - 4*int(dist)
  413. qEnd := pEnd - 4*int(dist)
  414. if p < 0 || len(pix) < pEnd || q < 0 || len(pix) < qEnd {
  415. return nil, errors.New("vp8l: invalid LZ77 parameters")
  416. }
  417. for ; p < pEnd; p, q = p+1, q+1 {
  418. pix[p] = pix[q]
  419. }
  420. x += int32(length)
  421. for x >= w {
  422. x, y = x-w, y+1
  423. }
  424. lookupHG = hMask != 0
  425. default:
  426. // We have a color cache lookup. First, insert previous pixels
  427. // into the cache. Note that VP8L assumes ARGB order, but the
  428. // Go image.RGBA type is in RGBA order.
  429. for ; cachedP < p; cachedP += 4 {
  430. argb := uint32(pix[cachedP+0])<<16 |
  431. uint32(pix[cachedP+1])<<8 |
  432. uint32(pix[cachedP+2])<<0 |
  433. uint32(pix[cachedP+3])<<24
  434. ccEntries[(argb*colorCacheMultiplier)>>ccShift] = argb
  435. }
  436. green -= nLiteralCodes + nLengthCodes
  437. if int(green) >= len(ccEntries) {
  438. return nil, errors.New("vp8l: invalid color cache index")
  439. }
  440. argb := ccEntries[green]
  441. pix[p+0] = uint8(argb >> 16)
  442. pix[p+1] = uint8(argb >> 8)
  443. pix[p+2] = uint8(argb >> 0)
  444. pix[p+3] = uint8(argb >> 24)
  445. p += 4
  446. x++
  447. if x == w {
  448. x, y = 0, y+1
  449. }
  450. lookupHG = hMask != 0 && x&hMask == 0
  451. }
  452. }
  453. return pix, nil
  454. }
  455. // lz77Param returns the next LZ77 parameter: a length or a distance, specified
  456. // in section 4.2.2.
  457. func (d *decoder) lz77Param(symbol uint32) (uint32, error) {
  458. if symbol < 4 {
  459. return symbol + 1, nil
  460. }
  461. extraBits := (symbol - 2) >> 1
  462. offset := (2 + symbol&1) << extraBits
  463. n, err := d.read(extraBits)
  464. if err != nil {
  465. return 0, err
  466. }
  467. return offset + n + 1, nil
  468. }
  469. // decodeHeader decodes the VP8L header from r.
  470. func decodeHeader(r io.Reader) (d *decoder, w int32, h int32, err error) {
  471. rr, ok := r.(io.ByteReader)
  472. if !ok {
  473. rr = bufio.NewReader(r)
  474. }
  475. d = &decoder{r: rr}
  476. magic, err := d.read(8)
  477. if err != nil {
  478. return nil, 0, 0, err
  479. }
  480. if magic != 0x2f {
  481. return nil, 0, 0, errors.New("vp8l: invalid header")
  482. }
  483. width, err := d.read(14)
  484. if err != nil {
  485. return nil, 0, 0, err
  486. }
  487. width++
  488. height, err := d.read(14)
  489. if err != nil {
  490. return nil, 0, 0, err
  491. }
  492. height++
  493. _, err = d.read(1) // Read and ignore the hasAlpha hint.
  494. if err != nil {
  495. return nil, 0, 0, err
  496. }
  497. version, err := d.read(3)
  498. if err != nil {
  499. return nil, 0, 0, err
  500. }
  501. if version != 0 {
  502. return nil, 0, 0, errors.New("vp8l: invalid version")
  503. }
  504. return d, int32(width), int32(height), nil
  505. }
  506. // DecodeConfig decodes the color model and dimensions of a VP8L image from r.
  507. func DecodeConfig(r io.Reader) (image.Config, error) {
  508. _, w, h, err := decodeHeader(r)
  509. if err != nil {
  510. return image.Config{}, err
  511. }
  512. return image.Config{
  513. ColorModel: color.NRGBAModel,
  514. Width: int(w),
  515. Height: int(h),
  516. }, nil
  517. }
  518. // Decode decodes a VP8L image from r.
  519. func Decode(r io.Reader) (image.Image, error) {
  520. d, w, h, err := decodeHeader(r)
  521. if err != nil {
  522. return nil, err
  523. }
  524. // Decode the transforms.
  525. var (
  526. nTransforms int
  527. transforms [nTransformTypes]transform
  528. transformsSeen [nTransformTypes]bool
  529. originalW = w
  530. )
  531. for {
  532. more, err := d.read(1)
  533. if err != nil {
  534. return nil, err
  535. }
  536. if more == 0 {
  537. break
  538. }
  539. var t transform
  540. t, w, err = d.decodeTransform(w, h)
  541. if err != nil {
  542. return nil, err
  543. }
  544. if transformsSeen[t.transformType] {
  545. return nil, errors.New("vp8l: repeated transform")
  546. }
  547. transformsSeen[t.transformType] = true
  548. transforms[nTransforms] = t
  549. nTransforms++
  550. }
  551. // Decode the transformed pixels.
  552. pix, err := d.decodePix(w, h, 0, true)
  553. if err != nil {
  554. return nil, err
  555. }
  556. // Apply the inverse transformations.
  557. for i := nTransforms - 1; i >= 0; i-- {
  558. t := &transforms[i]
  559. pix = inverseTransforms[t.transformType](t, pix, h)
  560. }
  561. return &image.NRGBA{
  562. Pix: pix,
  563. Stride: 4 * int(originalW),
  564. Rect: image.Rect(0, 0, int(originalW), int(h)),
  565. }, nil
  566. }