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