Source File
huffman.go
Belonging Package
image/jpeg
package jpeg
import (
)
const maxCodeLength = 16
const maxNCodes = 256
const lutSize = 8
var errShortHuffmanData = FormatError("short Huffman data")
func ( *decoder) ( int) error {
for > 0 {
if < 17 {
return FormatError("DHT has wrong length")
}
if := .readFull(.tmp[:17]); != nil {
return
}
:= .tmp[0] >> 4
if > maxTc {
return FormatError("bad Tc value")
}
if > maxTh || (.baseline && > 1) {
return FormatError("bad Th value")
}
:= &.huff[][]
.nCodes = 0
var [maxCodeLength]int32
for := range {
[] = int32(.tmp[+1])
.nCodes += []
}
if .nCodes == 0 {
return FormatError("Huffman table has zero length")
}
if .nCodes > maxNCodes {
return FormatError("Huffman table has excessive length")
}
-= int(.nCodes) + 17
if < 0 {
return FormatError("DHT has wrong length")
}
if := .readFull(.vals[:.nCodes]); != nil {
return
}
var , int32
for , := range {
if == 0 {
.minCodes[] = -1
.maxCodes[] = -1
.valsIndices[] = -1
} else {
.minCodes[] =
.maxCodes[] = + - 1
.valsIndices[] =
+=
+=
}
<<= 1
}
}
return nil
}
func ( *decoder) ( *huffman) (uint8, error) {
if .nCodes == 0 {
return 0, FormatError("uninitialized Huffman table")
}
if .bits.n < 8 {
if := .ensureNBits(8); != nil {
if != errMissingFF00 && != errShortHuffmanData {
return 0,
if .bytes.nUnreadable != 0 {
.unreadByteStuffedByte()
}
goto
}
}
if := .lut[(.bits.a>>uint32(.bits.n-lutSize))&0xff]; != 0 {
:= ( & 0xff) - 1
.bits.n -= int32()
.bits.m >>=
return uint8( >> 8), nil
}
:
for , := 0, int32(0); < maxCodeLength; ++ {
if .bits.n == 0 {
if := .ensureNBits(1); != nil {
return 0,
}
}
if .bits.a&.bits.m != 0 {
|= 1
}
.bits.n--
.bits.m >>= 1
if <= .maxCodes[] {
return .vals[.valsIndices[]+-.minCodes[]], nil
}
<<= 1
}
return 0, FormatError("bad Huffman code")
}
func ( *decoder) () (bool, error) {
if .bits.n == 0 {
if := .ensureNBits(1); != nil {
return false,
}
}
:= .bits.a&.bits.m != 0
.bits.n--
.bits.m >>= 1
return , nil
}
func ( *decoder) ( int32) (uint32, error) {
if .bits.n < {
if := .ensureNBits(); != nil {
return 0,
}
}
:= .bits.a >> uint32(.bits.n-)
&= (1 << uint32()) - 1
.bits.n -=
.bits.m >>= uint32()
return , nil
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |