github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/src/pkg/image/jpeg/huffman.go (about) 1 // Copyright 2009 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 jpeg 6 7 import "io" 8 9 // Each code is at most 16 bits long. 10 const maxCodeLength = 16 11 12 // Each decoded value is a uint8, so there are at most 256 such values. 13 const maxNumValues = 256 14 15 // Bit stream for the Huffman decoder. 16 // The n least significant bits of a form the unread bits, to be read in MSB to LSB order. 17 type bits struct { 18 a uint32 // accumulator. 19 m uint32 // mask. m==1<<(n-1) when n>0, with m==0 when n==0. 20 n int // the number of unread bits in a. 21 } 22 23 // Huffman table decoder, specified in section C. 24 type huffman struct { 25 l [maxCodeLength]int 26 length int // sum of l[i]. 27 val [maxNumValues]uint8 // the decoded values, as sorted by their encoding. 28 size [maxNumValues]int // size[i] is the number of bits to encode val[i]. 29 code [maxNumValues]int // code[i] is the encoding of val[i]. 30 minCode [maxCodeLength]int // min codes of length i, or -1 if no codes of that length. 31 maxCode [maxCodeLength]int // max codes of length i, or -1 if no codes of that length. 32 valIndex [maxCodeLength]int // index into val of minCode[i]. 33 } 34 35 // Reads bytes from the io.Reader to ensure that bits.n is at least n. 36 func (d *decoder) ensureNBits(n int) error { 37 for d.b.n < n { 38 c, err := d.r.ReadByte() 39 if err != nil { 40 if err == io.EOF { 41 return FormatError("short Huffman data") 42 } 43 return err 44 } 45 d.b.a = d.b.a<<8 | uint32(c) 46 d.b.n += 8 47 if d.b.m == 0 { 48 d.b.m = 1 << 7 49 } else { 50 d.b.m <<= 8 51 } 52 // Byte stuffing, specified in section F.1.2.3. 53 if c == 0xff { 54 c, err = d.r.ReadByte() 55 if err != nil { 56 if err == io.EOF { 57 return FormatError("short Huffman data") 58 } 59 return err 60 } 61 if c != 0x00 { 62 return FormatError("missing 0xff00 sequence") 63 } 64 } 65 } 66 return nil 67 } 68 69 // The composition of RECEIVE and EXTEND, specified in section F.2.2.1. 70 func (d *decoder) receiveExtend(t uint8) (int32, error) { 71 if d.b.n < int(t) { 72 if err := d.ensureNBits(int(t)); err != nil { 73 return 0, err 74 } 75 } 76 d.b.n -= int(t) 77 d.b.m >>= t 78 s := int32(1) << t 79 x := int32(d.b.a>>uint8(d.b.n)) & (s - 1) 80 if x < s>>1 { 81 x += ((-1) << t) + 1 82 } 83 return x, nil 84 } 85 86 // Processes a Define Huffman Table marker, and initializes a huffman struct from its contents. 87 // Specified in section B.2.4.2. 88 func (d *decoder) processDHT(n int) error { 89 for n > 0 { 90 if n < 17 { 91 return FormatError("DHT has wrong length") 92 } 93 _, err := io.ReadFull(d.r, d.tmp[0:17]) 94 if err != nil { 95 return err 96 } 97 tc := d.tmp[0] >> 4 98 if tc > maxTc { 99 return FormatError("bad Tc value") 100 } 101 th := d.tmp[0] & 0x0f 102 if th > maxTh || !d.progressive && th > 1 { 103 return FormatError("bad Th value") 104 } 105 h := &d.huff[tc][th] 106 107 // Read l and val (and derive length). 108 h.length = 0 109 for i := 0; i < maxCodeLength; i++ { 110 h.l[i] = int(d.tmp[i+1]) 111 h.length += h.l[i] 112 } 113 if h.length == 0 { 114 return FormatError("Huffman table has zero length") 115 } 116 if h.length > maxNumValues { 117 return FormatError("Huffman table has excessive length") 118 } 119 n -= h.length + 17 120 if n < 0 { 121 return FormatError("DHT has wrong length") 122 } 123 _, err = io.ReadFull(d.r, h.val[0:h.length]) 124 if err != nil { 125 return err 126 } 127 128 // Derive size. 129 k := 0 130 for i := 0; i < maxCodeLength; i++ { 131 for j := 0; j < h.l[i]; j++ { 132 h.size[k] = i + 1 133 k++ 134 } 135 } 136 137 // Derive code. 138 code := 0 139 size := h.size[0] 140 for i := 0; i < h.length; i++ { 141 if size != h.size[i] { 142 code <<= uint8(h.size[i] - size) 143 size = h.size[i] 144 } 145 h.code[i] = code 146 code++ 147 } 148 149 // Derive minCode, maxCode, and valIndex. 150 k = 0 151 index := 0 152 for i := 0; i < maxCodeLength; i++ { 153 if h.l[i] == 0 { 154 h.minCode[i] = -1 155 h.maxCode[i] = -1 156 h.valIndex[i] = -1 157 } else { 158 h.minCode[i] = k 159 h.maxCode[i] = k + h.l[i] - 1 160 h.valIndex[i] = index 161 k += h.l[i] 162 index += h.l[i] 163 } 164 k <<= 1 165 } 166 } 167 return nil 168 } 169 170 // Returns the next Huffman-coded value from the bit stream, decoded according to h. 171 // TODO(nigeltao): This decoding algorithm is simple, but slow. A lookahead table, instead of always 172 // peeling off only 1 bit at time, ought to be faster. 173 func (d *decoder) decodeHuffman(h *huffman) (uint8, error) { 174 if h.length == 0 { 175 return 0, FormatError("uninitialized Huffman table") 176 } 177 for i, code := 0, 0; i < maxCodeLength; i++ { 178 if d.b.n == 0 { 179 if err := d.ensureNBits(1); err != nil { 180 return 0, err 181 } 182 } 183 if d.b.a&d.b.m != 0 { 184 code |= 1 185 } 186 d.b.n-- 187 d.b.m >>= 1 188 if code <= h.maxCode[i] { 189 return h.val[h.valIndex[i]+code-h.minCode[i]], nil 190 } 191 code <<= 1 192 } 193 return 0, FormatError("bad Huffman code") 194 } 195 196 func (d *decoder) decodeBit() (bool, error) { 197 if d.b.n == 0 { 198 if err := d.ensureNBits(1); err != nil { 199 return false, err 200 } 201 } 202 ret := d.b.a&d.b.m != 0 203 d.b.n-- 204 d.b.m >>= 1 205 return ret, nil 206 } 207 208 func (d *decoder) decodeBits(n int) (uint32, error) { 209 if d.b.n < n { 210 if err := d.ensureNBits(n); err != nil { 211 return 0, err 212 } 213 } 214 ret := d.b.a >> uint(d.b.n-n) 215 ret &= (1 << uint(n)) - 1 216 d.b.n -= n 217 d.b.m >>= uint(n) 218 return ret, nil 219 }