74
70
comMarker = 0xfe // COMment.
77
// Maps from the zig-zag ordering to the natural ordering.
73
// unzig maps from the zig-zag ordering to the natural ordering. For example,
74
// unzig[3] is the column and row of the fourth element in zig-zag order. The
75
// value is 16, which means first column (16%8 == 0) and third row (16/8 == 2).
78
76
var unzig = [blockSize]int{
79
77
0, 1, 8, 16, 9, 2, 3, 10,
80
78
17, 24, 32, 25, 18, 11, 4, 5,
146
147
return UnsupportedError("SOF has wrong number of image components")
148
149
for i := 0; i < d.nComp; i++ {
150
d.comp[i].c = d.tmp[6+3*i]
151
d.comp[i].tq = d.tmp[8+3*i]
152
if d.nComp == nGrayComponent {
153
// If a JPEG image has only one component, section A.2 says "this data
154
// is non-interleaved by definition" and section A.2.2 says "[in this
155
// case...] the order of data units within a scan shall be left-to-right
156
// and top-to-bottom... regardless of the values of H_1 and V_1". Section
157
// 4.8.2 also says "[for non-interleaved data], the MCU is defined to be
158
// one data unit". Similarly, section A.1.1 explains that it is the ratio
159
// of H_i to max_j(H_j) that matters, and similarly for V. For grayscale
160
// images, H_1 is the maximum H_j for all components j, so that ratio is
161
// always 1. The component's (h, v) is effectively always (1, 1): even if
162
// the nominal (h, v) is (2, 1), a 20x5 image is encoded in three 8x8
163
// MCUs, not two 16x8 MCUs.
149
168
hv := d.tmp[7+3*i]
150
169
d.comp[i].h = int(hv >> 4)
151
170
d.comp[i].v = int(hv & 0x0f)
152
d.comp[i].c = d.tmp[6+3*i]
153
d.comp[i].tq = d.tmp[8+3*i]
154
if d.nComp == nGrayComponent {
157
// For color images, we only support 4:4:4, 4:2:2 or 4:2:0 chroma
171
// For color images, we only support 4:4:4, 4:4:0, 4:2:2 or 4:2:0 chroma
158
172
// downsampling ratios. This implies that the (h, v) values for the Y
159
// component are either (1, 1), (2, 1) or (2, 2), and the (h, v)
173
// component are either (1, 1), (1, 2), (2, 1) or (2, 2), and the (h, v)
160
174
// values for the Cr and Cb components must be (1, 1).
162
if hv != 0x11 && hv != 0x21 && hv != 0x22 {
176
if hv != 0x11 && hv != 0x21 && hv != 0x22 && hv != 0x12 {
163
177
return UnsupportedError("luma downsample ratio")
165
179
} else if hv != 0x11 {
198
// makeImg allocates and initializes the destination image.
199
func (d *decoder) makeImg(h0, v0, mxx, myy int) {
200
if d.nComp == nGrayComponent {
201
m := image.NewGray(image.Rect(0, 0, 8*mxx, 8*myy))
202
d.img1 = m.SubImage(image.Rect(0, 0, d.width, d.height)).(*image.Gray)
205
var subsampleRatio image.YCbCrSubsampleRatio
208
subsampleRatio = image.YCbCrSubsampleRatio444
210
subsampleRatio = image.YCbCrSubsampleRatio422
212
subsampleRatio = image.YCbCrSubsampleRatio420
216
m := image.NewYCbCr(image.Rect(0, 0, 8*h0*mxx, 8*v0*myy), subsampleRatio)
217
d.img3 = m.SubImage(image.Rect(0, 0, d.width, d.height)).(*image.YCbCr)
220
// Specified in section B.2.3.
221
func (d *decoder) processSOS(n int) error {
223
return FormatError("missing SOF marker")
225
if n != 4+2*d.nComp {
226
return UnsupportedError("SOS has wrong length")
228
_, err := io.ReadFull(d.r, d.tmp[0:4+2*d.nComp])
232
if int(d.tmp[0]) != d.nComp {
233
return UnsupportedError("SOS has wrong number of image components")
235
var scan [nColorComponent]struct {
236
td uint8 // DC table selector.
237
ta uint8 // AC table selector.
239
for i := 0; i < d.nComp; i++ {
240
cs := d.tmp[1+2*i] // Component selector.
241
if cs != d.comp[i].c {
242
return UnsupportedError("scan components out of order")
244
scan[i].td = d.tmp[2+2*i] >> 4
245
scan[i].ta = d.tmp[2+2*i] & 0x0f
247
// mxx and myy are the number of MCUs (Minimum Coded Units) in the image.
248
h0, v0 := d.comp[0].h, d.comp[0].v // The h and v values from the Y components.
249
mxx := (d.width + 8*h0 - 1) / (8 * h0)
250
myy := (d.height + 8*v0 - 1) / (8 * v0)
251
if d.img1 == nil && d.img3 == nil {
252
d.makeImg(h0, v0, mxx, myy)
255
mcu, expectedRST := 0, uint8(rst0Marker)
258
dc [nColorComponent]int
260
for my := 0; my < myy; my++ {
261
for mx := 0; mx < mxx; mx++ {
262
for i := 0; i < d.nComp; i++ {
263
qt := &d.quant[d.comp[i].tq]
264
for j := 0; j < d.comp[i].h*d.comp[i].v; j++ {
265
// TODO(nigeltao): make this a "var b block" once the compiler's escape
266
// analysis is good enough to allocate it on the stack, not the heap.
269
// Decode the DC coefficient, as specified in section F.2.2.1.
270
value, err := d.decodeHuffman(&d.huff[dcTable][scan[i].td])
275
return UnsupportedError("excessive DC component")
277
dcDelta, err := d.receiveExtend(value)
284
// Decode the AC coefficients, as specified in section F.2.2.2.
285
for k := 1; k < blockSize; k++ {
286
value, err := d.decodeHuffman(&d.huff[acTable][scan[i].ta])
295
return FormatError("bad DCT index")
297
ac, err := d.receiveExtend(val1)
301
b[unzig[k]] = ac * qt[k]
310
// Perform the inverse DCT and store the MCU component to the image.
311
if d.nComp == nGrayComponent {
312
idct(d.img1.Pix[8*(my*d.img1.Stride+mx):], d.img1.Stride, &b)
316
mx0 := h0*mx + (j % 2)
317
my0 := v0*my + (j / 2)
318
idct(d.img3.Y[8*(my0*d.img3.YStride+mx0):], d.img3.YStride, &b)
320
idct(d.img3.Cb[8*(my*d.img3.CStride+mx):], d.img3.CStride, &b)
322
idct(d.img3.Cr[8*(my*d.img3.CStride+mx):], d.img3.CStride, &b)
328
if d.ri > 0 && mcu%d.ri == 0 && mcu < mxx*myy {
329
// A more sophisticated decoder could use RST[0-7] markers to resynchronize from corrupt input,
330
// but this one assumes well-formed input, and hence the restart marker follows immediately.
331
_, err := io.ReadFull(d.r, d.tmp[0:2])
335
if d.tmp[0] != 0xff || d.tmp[1] != expectedRST {
336
return FormatError("bad RST marker")
339
if expectedRST == rst7Marker+1 {
340
expectedRST = rst0Marker
342
// Reset the Huffman decoder.
344
// Reset the DC components, as per section F.2.1.3.1.
345
dc = [nColorComponent]int{}
353
212
// Specified in section B.2.4.4.
354
213
func (d *decoder) processDRI(n int) error {
389
if d.tmp[0] != 0xff {
390
return nil, FormatError("missing 0xff marker start")
248
for d.tmp[0] != 0xff {
249
// Strictly speaking, this is a format error. However, libjpeg is
250
// liberal in what it accepts. As of version 9, next_marker in
251
// jdmarker.c treats this as a warning (JWRN_EXTRANEOUS_DATA) and
252
// continues to decode the stream. Even before next_marker sees
253
// extraneous data, jpeg_fill_bit_buffer in jdhuff.c reads as many
254
// bytes as it can, possibly past the end of a scan's data. It
255
// effectively puts back any markers that it overscanned (e.g. an
256
// "\xff\xd9" EOI marker), but it does not put back non-marker data,
257
// and thus it can silently ignore a small number of extraneous
258
// non-marker bytes before next_marker has a chance to see them (and
261
// We are therefore also liberal in what we accept. Extraneous data
262
// is silently ignored.
264
// This is similar to, but not exactly the same as, the restart
265
// mechanism within a scan (the RST[0-7] markers).
267
// Note that extraneous 0xff bytes in e.g. SOS data are escaped as
268
// "\xff\x00", and so are detected a little further down below.
270
d.tmp[1], err = d.r.ReadByte()
392
275
marker := d.tmp[1]
277
// Treat "\xff\x00" as extraneous data.
281
// Section B.1.1.2 says, "Any marker may optionally be preceded by any
282
// number of fill bytes, which are bytes assigned code X'FF'".
283
marker, err = d.r.ReadByte()
393
288
if marker == eoiMarker { // End Of Image.
291
if rst0Marker <= marker && marker <= rst7Marker {
292
// Figures B.2 and B.16 of the specification suggest that restart markers should
293
// only occur between Entropy Coded Segments and not after the final ECS.
294
// However, some encoders may generate incorrect JPEGs with a final restart
295
// marker. That restart marker will be seen here instead of inside the processSOS
296
// method, and is ignored as a harmless error. Restart markers have no extra data,
297
// so we check for this before we read the 16-bit length of the segment.
397
301
// Read the 16-bit length of the segment. The value includes the 2 bytes for the
398
302
// length itself, so we subtract 2 to get the number of remaining bytes.