Rebase.
[official-gcc.git] / libgo / go / image / jpeg / scan.go
blob559235d51272a3f0f93bc1cc1736ad6e9ebd9dfc
1 // Copyright 2012 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.
5 package jpeg
7 import (
8 "image"
9 "io"
12 // makeImg allocates and initializes the destination image.
13 func (d *decoder) makeImg(h0, v0, mxx, myy int) {
14 if d.nComp == nGrayComponent {
15 m := image.NewGray(image.Rect(0, 0, 8*mxx, 8*myy))
16 d.img1 = m.SubImage(image.Rect(0, 0, d.width, d.height)).(*image.Gray)
17 return
19 var subsampleRatio image.YCbCrSubsampleRatio
20 switch {
21 case h0 == 1 && v0 == 1:
22 subsampleRatio = image.YCbCrSubsampleRatio444
23 case h0 == 1 && v0 == 2:
24 subsampleRatio = image.YCbCrSubsampleRatio440
25 case h0 == 2 && v0 == 1:
26 subsampleRatio = image.YCbCrSubsampleRatio422
27 case h0 == 2 && v0 == 2:
28 subsampleRatio = image.YCbCrSubsampleRatio420
29 default:
30 panic("unreachable")
32 m := image.NewYCbCr(image.Rect(0, 0, 8*h0*mxx, 8*v0*myy), subsampleRatio)
33 d.img3 = m.SubImage(image.Rect(0, 0, d.width, d.height)).(*image.YCbCr)
36 // Specified in section B.2.3.
37 func (d *decoder) processSOS(n int) error {
38 if d.nComp == 0 {
39 return FormatError("missing SOF marker")
41 if n < 6 || 4+2*d.nComp < n || n%2 != 0 {
42 return FormatError("SOS has wrong length")
44 _, err := io.ReadFull(d.r, d.tmp[:n])
45 if err != nil {
46 return err
48 nComp := int(d.tmp[0])
49 if n != 4+2*nComp {
50 return FormatError("SOS length inconsistent with number of components")
52 var scan [nColorComponent]struct {
53 compIndex uint8
54 td uint8 // DC table selector.
55 ta uint8 // AC table selector.
57 for i := 0; i < nComp; i++ {
58 cs := d.tmp[1+2*i] // Component selector.
59 compIndex := -1
60 for j, comp := range d.comp {
61 if cs == comp.c {
62 compIndex = j
65 if compIndex < 0 {
66 return FormatError("unknown component selector")
68 scan[i].compIndex = uint8(compIndex)
69 scan[i].td = d.tmp[2+2*i] >> 4
70 scan[i].ta = d.tmp[2+2*i] & 0x0f
73 // zigStart and zigEnd are the spectral selection bounds.
74 // ah and al are the successive approximation high and low values.
75 // The spec calls these values Ss, Se, Ah and Al.
77 // For progressive JPEGs, these are the two more-or-less independent
78 // aspects of progression. Spectral selection progression is when not
79 // all of a block's 64 DCT coefficients are transmitted in one pass.
80 // For example, three passes could transmit coefficient 0 (the DC
81 // component), coefficients 1-5, and coefficients 6-63, in zig-zag
82 // order. Successive approximation is when not all of the bits of a
83 // band of coefficients are transmitted in one pass. For example,
84 // three passes could transmit the 6 most significant bits, followed
85 // by the second-least significant bit, followed by the least
86 // significant bit.
88 // For baseline JPEGs, these parameters are hard-coded to 0/63/0/0.
89 zigStart, zigEnd, ah, al := int32(0), int32(blockSize-1), uint32(0), uint32(0)
90 if d.progressive {
91 zigStart = int32(d.tmp[1+2*nComp])
92 zigEnd = int32(d.tmp[2+2*nComp])
93 ah = uint32(d.tmp[3+2*nComp] >> 4)
94 al = uint32(d.tmp[3+2*nComp] & 0x0f)
95 if (zigStart == 0 && zigEnd != 0) || zigStart > zigEnd || blockSize <= zigEnd {
96 return FormatError("bad spectral selection bounds")
98 if zigStart != 0 && nComp != 1 {
99 return FormatError("progressive AC coefficients for more than one component")
101 if ah != 0 && ah != al+1 {
102 return FormatError("bad successive approximation values")
106 // mxx and myy are the number of MCUs (Minimum Coded Units) in the image.
107 h0, v0 := d.comp[0].h, d.comp[0].v // The h and v values from the Y components.
108 mxx := (d.width + 8*h0 - 1) / (8 * h0)
109 myy := (d.height + 8*v0 - 1) / (8 * v0)
110 if d.img1 == nil && d.img3 == nil {
111 d.makeImg(h0, v0, mxx, myy)
113 if d.progressive {
114 for i := 0; i < nComp; i++ {
115 compIndex := scan[i].compIndex
116 if d.progCoeffs[compIndex] == nil {
117 d.progCoeffs[compIndex] = make([]block, mxx*myy*d.comp[compIndex].h*d.comp[compIndex].v)
122 d.b = bits{}
123 mcu, expectedRST := 0, uint8(rst0Marker)
124 var (
125 // b is the decoded coefficients, in natural (not zig-zag) order.
126 b block
127 dc [nColorComponent]int32
128 // mx0 and my0 are the location of the current (in terms of 8x8 blocks).
129 // For example, with 4:2:0 chroma subsampling, the block whose top left
130 // pixel co-ordinates are (16, 8) is the third block in the first row:
131 // mx0 is 2 and my0 is 0, even though the pixel is in the second MCU.
132 // TODO(nigeltao): rename mx0 and my0 to bx and by?
133 mx0, my0 int
134 blockCount int
136 for my := 0; my < myy; my++ {
137 for mx := 0; mx < mxx; mx++ {
138 for i := 0; i < nComp; i++ {
139 compIndex := scan[i].compIndex
140 qt := &d.quant[d.comp[compIndex].tq]
141 for j := 0; j < d.comp[compIndex].h*d.comp[compIndex].v; j++ {
142 // The blocks are traversed one MCU at a time. For 4:2:0 chroma
143 // subsampling, there are four Y 8x8 blocks in every 16x16 MCU.
145 // For a baseline 32x16 pixel image, the Y blocks visiting order is:
146 // 0 1 4 5
147 // 2 3 6 7
149 // For progressive images, the interleaved scans (those with nComp > 1)
150 // are traversed as above, but non-interleaved scans are traversed left
151 // to right, top to bottom:
152 // 0 1 2 3
153 // 4 5 6 7
154 // Only DC scans (zigStart == 0) can be interleaved. AC scans must have
155 // only one component.
157 // To further complicate matters, for non-interleaved scans, there is no
158 // data for any blocks that are inside the image at the MCU level but
159 // outside the image at the pixel level. For example, a 24x16 pixel 4:2:0
160 // progressive image consists of two 16x16 MCUs. The interleaved scans
161 // will process 8 Y blocks:
162 // 0 1 4 5
163 // 2 3 6 7
164 // The non-interleaved scans will process only 6 Y blocks:
165 // 0 1 2
166 // 3 4 5
167 if nComp != 1 {
168 mx0, my0 = d.comp[compIndex].h*mx, d.comp[compIndex].v*my
169 if h0 == 1 {
170 my0 += j
171 } else {
172 mx0 += j % 2
173 my0 += j / 2
175 } else {
176 q := mxx * d.comp[compIndex].h
177 mx0 = blockCount % q
178 my0 = blockCount / q
179 blockCount++
180 if mx0*8 >= d.width || my0*8 >= d.height {
181 continue
185 // Load the previous partially decoded coefficients, if applicable.
186 if d.progressive {
187 b = d.progCoeffs[compIndex][my0*mxx*d.comp[compIndex].h+mx0]
188 } else {
189 b = block{}
192 if ah != 0 {
193 if err := d.refine(&b, &d.huff[acTable][scan[i].ta], zigStart, zigEnd, 1<<al); err != nil {
194 return err
196 } else {
197 zig := zigStart
198 if zig == 0 {
199 zig++
200 // Decode the DC coefficient, as specified in section F.2.2.1.
201 value, err := d.decodeHuffman(&d.huff[dcTable][scan[i].td])
202 if err != nil {
203 return err
205 if value > 16 {
206 return UnsupportedError("excessive DC component")
208 dcDelta, err := d.receiveExtend(value)
209 if err != nil {
210 return err
212 dc[compIndex] += dcDelta
213 b[0] = dc[compIndex] << al
216 if zig <= zigEnd && d.eobRun > 0 {
217 d.eobRun--
218 } else {
219 // Decode the AC coefficients, as specified in section F.2.2.2.
220 for ; zig <= zigEnd; zig++ {
221 value, err := d.decodeHuffman(&d.huff[acTable][scan[i].ta])
222 if err != nil {
223 return err
225 val0 := value >> 4
226 val1 := value & 0x0f
227 if val1 != 0 {
228 zig += int32(val0)
229 if zig > zigEnd {
230 break
232 ac, err := d.receiveExtend(val1)
233 if err != nil {
234 return err
236 b[unzig[zig]] = ac << al
237 } else {
238 if val0 != 0x0f {
239 d.eobRun = uint16(1 << val0)
240 if val0 != 0 {
241 bits, err := d.decodeBits(int(val0))
242 if err != nil {
243 return err
245 d.eobRun |= uint16(bits)
247 d.eobRun--
248 break
250 zig += 0x0f
256 if d.progressive {
257 if zigEnd != blockSize-1 || al != 0 {
258 // We haven't completely decoded this 8x8 block. Save the coefficients.
259 d.progCoeffs[compIndex][my0*mxx*d.comp[compIndex].h+mx0] = b
260 // At this point, we could execute the rest of the loop body to dequantize and
261 // perform the inverse DCT, to save early stages of a progressive image to the
262 // *image.YCbCr buffers (the whole point of progressive encoding), but in Go,
263 // the jpeg.Decode function does not return until the entire image is decoded,
264 // so we "continue" here to avoid wasted computation.
265 continue
269 // Dequantize, perform the inverse DCT and store the block to the image.
270 for zig := 0; zig < blockSize; zig++ {
271 b[unzig[zig]] *= qt[zig]
273 idct(&b)
274 dst, stride := []byte(nil), 0
275 if d.nComp == nGrayComponent {
276 dst, stride = d.img1.Pix[8*(my0*d.img1.Stride+mx0):], d.img1.Stride
277 } else {
278 switch compIndex {
279 case 0:
280 dst, stride = d.img3.Y[8*(my0*d.img3.YStride+mx0):], d.img3.YStride
281 case 1:
282 dst, stride = d.img3.Cb[8*(my0*d.img3.CStride+mx0):], d.img3.CStride
283 case 2:
284 dst, stride = d.img3.Cr[8*(my0*d.img3.CStride+mx0):], d.img3.CStride
285 default:
286 return UnsupportedError("too many components")
289 // Level shift by +128, clip to [0, 255], and write to dst.
290 for y := 0; y < 8; y++ {
291 y8 := y * 8
292 yStride := y * stride
293 for x := 0; x < 8; x++ {
294 c := b[y8+x]
295 if c < -128 {
296 c = 0
297 } else if c > 127 {
298 c = 255
299 } else {
300 c += 128
302 dst[yStride+x] = uint8(c)
305 } // for j
306 } // for i
307 mcu++
308 if d.ri > 0 && mcu%d.ri == 0 && mcu < mxx*myy {
309 // A more sophisticated decoder could use RST[0-7] markers to resynchronize from corrupt input,
310 // but this one assumes well-formed input, and hence the restart marker follows immediately.
311 _, err := io.ReadFull(d.r, d.tmp[0:2])
312 if err != nil {
313 return err
315 if d.tmp[0] != 0xff || d.tmp[1] != expectedRST {
316 return FormatError("bad RST marker")
318 expectedRST++
319 if expectedRST == rst7Marker+1 {
320 expectedRST = rst0Marker
322 // Reset the Huffman decoder.
323 d.b = bits{}
324 // Reset the DC components, as per section F.2.1.3.1.
325 dc = [nColorComponent]int32{}
326 // Reset the progressive decoder state, as per section G.1.2.2.
327 d.eobRun = 0
329 } // for mx
330 } // for my
332 return nil
335 // refine decodes a successive approximation refinement block, as specified in
336 // section G.1.2.
337 func (d *decoder) refine(b *block, h *huffman, zigStart, zigEnd, delta int32) error {
338 // Refining a DC component is trivial.
339 if zigStart == 0 {
340 if zigEnd != 0 {
341 panic("unreachable")
343 bit, err := d.decodeBit()
344 if err != nil {
345 return err
347 if bit {
348 b[0] |= delta
350 return nil
353 // Refining AC components is more complicated; see sections G.1.2.2 and G.1.2.3.
354 zig := zigStart
355 if d.eobRun == 0 {
356 loop:
357 for ; zig <= zigEnd; zig++ {
358 z := int32(0)
359 value, err := d.decodeHuffman(h)
360 if err != nil {
361 return err
363 val0 := value >> 4
364 val1 := value & 0x0f
366 switch val1 {
367 case 0:
368 if val0 != 0x0f {
369 d.eobRun = uint16(1 << val0)
370 if val0 != 0 {
371 bits, err := d.decodeBits(int(val0))
372 if err != nil {
373 return err
375 d.eobRun |= uint16(bits)
377 break loop
379 case 1:
380 z = delta
381 bit, err := d.decodeBit()
382 if err != nil {
383 return err
385 if !bit {
386 z = -z
388 default:
389 return FormatError("unexpected Huffman code")
392 zig, err = d.refineNonZeroes(b, zig, zigEnd, int32(val0), delta)
393 if err != nil {
394 return err
396 if zig > zigEnd {
397 return FormatError("too many coefficients")
399 if z != 0 {
400 b[unzig[zig]] = z
404 if d.eobRun > 0 {
405 d.eobRun--
406 if _, err := d.refineNonZeroes(b, zig, zigEnd, -1, delta); err != nil {
407 return err
410 return nil
413 // refineNonZeroes refines non-zero entries of b in zig-zag order. If nz >= 0,
414 // the first nz zero entries are skipped over.
415 func (d *decoder) refineNonZeroes(b *block, zig, zigEnd, nz, delta int32) (int32, error) {
416 for ; zig <= zigEnd; zig++ {
417 u := unzig[zig]
418 if b[u] == 0 {
419 if nz == 0 {
420 break
422 nz--
423 continue
425 bit, err := d.decodeBit()
426 if err != nil {
427 return 0, err
429 if !bit {
430 continue
432 if b[u] >= 0 {
433 b[u] += delta
434 } else {
435 b[u] -= delta
438 return zig, nil