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.
145 // For a baseline 32x16 pixel image, the Y blocks visiting order is:
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:
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:
164 // The non-interleaved scans will process only 6 Y blocks:
168 mx0
, my0
= d
.comp
[compIndex
].h
*mx
, d
.comp
[compIndex
].v
*my
176 q
:= mxx
* d
.comp
[compIndex
].h
180 if mx0
*8 >= d
.width || my0
*8 >= d
.height
{
185 // Load the previous partially decoded coefficients, if applicable.
187 b
= d
.progCoeffs
[compIndex
][my0
*mxx
*d
.comp
[compIndex
].h
+mx0
]
193 if err
:= d
.refine(&b
, &d
.huff
[acTable
][scan
[i
].ta
], zigStart
, zigEnd
, 1<<al
); err
!= nil {
200 // Decode the DC coefficient, as specified in section F.2.2.1.
201 value
, err
:= d
.decodeHuffman(&d
.huff
[dcTable
][scan
[i
].td
])
206 return UnsupportedError("excessive DC component")
208 dcDelta
, err
:= d
.receiveExtend(value
)
212 dc
[compIndex
] += dcDelta
213 b
[0] = dc
[compIndex
] << al
216 if zig
<= zigEnd
&& d
.eobRun
> 0 {
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
])
232 ac
, err
:= d
.receiveExtend(val1
)
236 b
[unzig
[zig
]] = ac
<< al
239 d
.eobRun
= uint16(1 << val0
)
241 bits
, err
:= d
.decodeBits(int(val0
))
245 d
.eobRun |
= uint16(bits
)
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.
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
]
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
280 dst
, stride
= d
.img3
.Y
[8*(my0
*d
.img3
.YStride
+mx0
):], d
.img3
.YStride
282 dst
, stride
= d
.img3
.Cb
[8*(my0
*d
.img3
.CStride
+mx0
):], d
.img3
.CStride
284 dst
, stride
= d
.img3
.Cr
[8*(my0
*d
.img3
.CStride
+mx0
):], d
.img3
.CStride
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
++ {
292 yStride
:= y
* stride
293 for x
:= 0; x
< 8; x
++ {
302 dst
[yStride
+x
] = uint8(c
)
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])
315 if d
.tmp
[0] != 0xff || d
.tmp
[1] != expectedRST
{
316 return FormatError("bad RST marker")
319 if expectedRST
== rst7Marker
+1 {
320 expectedRST
= rst0Marker
322 // Reset the Huffman decoder.
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.
335 // refine decodes a successive approximation refinement block, as specified in
337 func (d
*decoder
) refine(b
*block
, h
*huffman
, zigStart
, zigEnd
, delta
int32) error
{
338 // Refining a DC component is trivial.
343 bit
, err
:= d
.decodeBit()
353 // Refining AC components is more complicated; see sections G.1.2.2 and G.1.2.3.
357 for ; zig
<= zigEnd
; zig
++ {
359 value
, err
:= d
.decodeHuffman(h
)
369 d
.eobRun
= uint16(1 << val0
)
371 bits
, err
:= d
.decodeBits(int(val0
))
375 d
.eobRun |
= uint16(bits
)
381 bit
, err
:= d
.decodeBit()
389 return FormatError("unexpected Huffman code")
392 zig
, err
= d
.refineNonZeroes(b
, zig
, zigEnd
, int32(val0
), delta
)
397 return FormatError("too many coefficients")
406 if _
, err
:= d
.refineNonZeroes(b
, zig
, zigEnd
, -1, delta
); err
!= 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
++ {
425 bit
, err
:= d
.decodeBit()