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.
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)
19
var subsampleRatio image.YCbCrSubsampleRatio
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
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 {
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])
48
nComp := int(d.tmp[0])
50
return FormatError("SOS length inconsistent with number of components")
52
var scan [nColorComponent]struct {
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.
60
for j, comp := range d.comp {
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
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)
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)
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)
123
mcu, expectedRST := 0, uint8(rst0Marker)
125
// b is the decoded coefficients, in natural (not zig-zag) order.
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?
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.
144
// For a baseline 32x16 pixel image, the Y blocks visiting order is:
148
// For progressive images, the DC data blocks (zigStart == 0) are traversed
149
// as above, but AC data blocks are traversed left to right, top to bottom:
153
// To further complicate matters, there is no AC data for any blocks that
154
// are inside the image at the MCU level but outside the image at the pixel
155
// level. For example, a 24x16 pixel 4:2:0 progressive image consists of
156
// two 16x16 MCUs. The earlier scans will process 8 Y blocks:
159
// The later scans will process only 6 Y blocks:
163
mx0, my0 = d.comp[compIndex].h*mx, d.comp[compIndex].v*my
171
q := mxx * d.comp[compIndex].h
175
if mx0*8 >= d.width || my0*8 >= d.height {
180
// Load the previous partially decoded coefficients, if applicable.
182
b = d.progCoeffs[compIndex][my0*mxx*d.comp[compIndex].h+mx0]
188
if err := d.refine(&b, &d.huff[acTable][scan[i].ta], zigStart, zigEnd, 1<<al); err != nil {
195
// Decode the DC coefficient, as specified in section F.2.2.1.
196
value, err := d.decodeHuffman(&d.huff[dcTable][scan[i].td])
201
return UnsupportedError("excessive DC component")
203
dcDelta, err := d.receiveExtend(value)
207
dc[compIndex] += dcDelta
208
b[0] = dc[compIndex] << al
211
if zig <= zigEnd && d.eobRun > 0 {
214
// Decode the AC coefficients, as specified in section F.2.2.2.
215
for ; zig <= zigEnd; zig++ {
216
value, err := d.decodeHuffman(&d.huff[acTable][scan[i].ta])
227
ac, err := d.receiveExtend(val1)
231
b[unzig[zig]] = ac << al
234
d.eobRun = uint16(1 << val0)
236
bits, err := d.decodeBits(int(val0))
240
d.eobRun |= uint16(bits)
252
if zigEnd != blockSize-1 || al != 0 {
253
// We haven't completely decoded this 8x8 block. Save the coefficients.
254
d.progCoeffs[compIndex][my0*mxx*d.comp[compIndex].h+mx0] = b
255
// At this point, we could execute the rest of the loop body to dequantize and
256
// perform the inverse DCT, to save early stages of a progressive image to the
257
// *image.YCbCr buffers (the whole point of progressive encoding), but in Go,
258
// the jpeg.Decode function does not return until the entire image is decoded,
259
// so we "continue" here to avoid wasted computation.
264
// Dequantize, perform the inverse DCT and store the block to the image.
265
for zig := 0; zig < blockSize; zig++ {
266
b[unzig[zig]] *= qt[zig]
269
dst, stride := []byte(nil), 0
270
if d.nComp == nGrayComponent {
271
dst, stride = d.img1.Pix[8*(my0*d.img1.Stride+mx0):], d.img1.Stride
275
dst, stride = d.img3.Y[8*(my0*d.img3.YStride+mx0):], d.img3.YStride
277
dst, stride = d.img3.Cb[8*(my0*d.img3.CStride+mx0):], d.img3.CStride
279
dst, stride = d.img3.Cr[8*(my0*d.img3.CStride+mx0):], d.img3.CStride
281
return UnsupportedError("too many components")
284
// Level shift by +128, clip to [0, 255], and write to dst.
285
for y := 0; y < 8; y++ {
287
yStride := y * stride
288
for x := 0; x < 8; x++ {
297
dst[yStride+x] = uint8(c)
303
if d.ri > 0 && mcu%d.ri == 0 && mcu < mxx*myy {
304
// A more sophisticated decoder could use RST[0-7] markers to resynchronize from corrupt input,
305
// but this one assumes well-formed input, and hence the restart marker follows immediately.
306
_, err := io.ReadFull(d.r, d.tmp[0:2])
310
if d.tmp[0] != 0xff || d.tmp[1] != expectedRST {
311
return FormatError("bad RST marker")
314
if expectedRST == rst7Marker+1 {
315
expectedRST = rst0Marker
317
// Reset the Huffman decoder.
319
// Reset the DC components, as per section F.2.1.3.1.
320
dc = [nColorComponent]int32{}
321
// Reset the progressive decoder state, as per section G.1.2.2.
330
// refine decodes a successive approximation refinement block, as specified in
332
func (d *decoder) refine(b *block, h *huffman, zigStart, zigEnd, delta int32) error {
333
// Refining a DC component is trivial.
338
bit, err := d.decodeBit()
348
// Refining AC components is more complicated; see sections G.1.2.2 and G.1.2.3.
352
for ; zig <= zigEnd; zig++ {
354
value, err := d.decodeHuffman(h)
364
d.eobRun = uint16(1 << val0)
366
bits, err := d.decodeBits(int(val0))
370
d.eobRun |= uint16(bits)
376
bit, err := d.decodeBit()
384
return FormatError("unexpected Huffman code")
387
zig, err = d.refineNonZeroes(b, zig, zigEnd, int32(val0), delta)
392
return FormatError("too many coefficients")
401
if _, err := d.refineNonZeroes(b, zig, zigEnd, -1, delta); err != nil {
408
// refineNonZeroes refines non-zero entries of b in zig-zag order. If nz >= 0,
409
// the first nz zero entries are skipped over.
410
func (d *decoder) refineNonZeroes(b *block, zig, zigEnd, nz, delta int32) (int32, error) {
411
for ; zig <= zigEnd; zig++ {
420
bit, err := d.decodeBit()