git.lucas.co / go_mono
git clone https://git.lucas.co/go_mono.git

vp8l/decode.go (14.6K)

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