1
2
3
4
5 package jpeg
6
7 import (
8 "image"
9 )
10
11
12 func (d *decoder) makeImg(mxx, myy int) {
13 if d.nComp == 1 {
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
19
20
21
22
23
24 subsampleRatio := image.YCbCrSubsampleRatio444
25 if d.comp[1].h != d.comp[2].h || d.comp[1].v != d.comp[2].v ||
26 d.maxH != d.comp[0].h || d.maxV != d.comp[0].v {
27 d.flex = true
28 } else {
29 hRatio := d.maxH / d.comp[1].h
30 vRatio := d.maxV / d.comp[1].v
31 switch hRatio<<4 | vRatio {
32 case 0x11:
33 subsampleRatio = image.YCbCrSubsampleRatio444
34 case 0x12:
35 subsampleRatio = image.YCbCrSubsampleRatio440
36 case 0x21:
37 subsampleRatio = image.YCbCrSubsampleRatio422
38 case 0x22:
39 subsampleRatio = image.YCbCrSubsampleRatio420
40 case 0x41:
41 subsampleRatio = image.YCbCrSubsampleRatio411
42 case 0x42:
43 subsampleRatio = image.YCbCrSubsampleRatio410
44 default:
45 d.flex = true
46 }
47 }
48
49 m := image.NewYCbCr(image.Rect(0, 0, 8*d.maxH*mxx, 8*d.maxV*myy), subsampleRatio)
50 d.img3 = m.SubImage(image.Rect(0, 0, d.width, d.height)).(*image.YCbCr)
51
52 if d.nComp == 4 {
53 h3, v3 := d.comp[3].h, d.comp[3].v
54 d.blackPix = make([]byte, 8*h3*mxx*8*v3*myy)
55 d.blackStride = 8 * h3 * mxx
56 }
57 }
58
59
60 func (d *decoder) processSOS(n int) error {
61 if d.nComp == 0 {
62 return FormatError("missing SOF marker")
63 }
64 if n < 6 || 4+2*d.nComp < n || n%2 != 0 {
65 return FormatError("SOS has wrong length")
66 }
67 if err := d.readFull(d.tmp[:n]); err != nil {
68 return err
69 }
70 nComp := int(d.tmp[0])
71 if n != 4+2*nComp {
72 return FormatError("SOS length inconsistent with number of components")
73 }
74 var scan [maxComponents]struct {
75 compIndex uint8
76 td uint8
77 ta uint8
78 }
79 totalHV := 0
80 for i := 0; i < nComp; i++ {
81 cs := d.tmp[1+2*i]
82 compIndex := -1
83 for j, comp := range d.comp[:d.nComp] {
84 if cs == comp.c {
85 compIndex = j
86 }
87 }
88 if compIndex < 0 {
89 return FormatError("unknown component selector")
90 }
91 scan[i].compIndex = uint8(compIndex)
92
93
94
95
96
97 for j := 0; j < i; j++ {
98 if scan[i].compIndex == scan[j].compIndex {
99 return FormatError("repeated component selector")
100 }
101 }
102 totalHV += d.comp[compIndex].h * d.comp[compIndex].v
103
104
105 scan[i].td = d.tmp[2+2*i] >> 4
106 if t := scan[i].td; t > maxTh || (d.baseline && t > 1) {
107 return FormatError("bad Td value")
108 }
109 scan[i].ta = d.tmp[2+2*i] & 0x0f
110 if t := scan[i].ta; t > maxTh || (d.baseline && t > 1) {
111 return FormatError("bad Ta value")
112 }
113 }
114
115
116 if d.nComp > 1 && totalHV > 10 {
117 return FormatError("total sampling factors too large")
118 }
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137 zigStart, zigEnd, ah, al := int32(0), int32(blockSize-1), uint32(0), uint32(0)
138 if d.progressive {
139 zigStart = int32(d.tmp[1+2*nComp])
140 zigEnd = int32(d.tmp[2+2*nComp])
141 ah = uint32(d.tmp[3+2*nComp] >> 4)
142 al = uint32(d.tmp[3+2*nComp] & 0x0f)
143 if (zigStart == 0 && zigEnd != 0) || zigStart > zigEnd || blockSize <= zigEnd {
144 return FormatError("bad spectral selection bounds")
145 }
146 if zigStart != 0 && nComp != 1 {
147 return FormatError("progressive AC coefficients for more than one component")
148 }
149 if ah != 0 && ah != al+1 {
150 return FormatError("bad successive approximation values")
151 }
152 }
153
154
155
156
157
158 mxx := (d.width + 8*d.maxH - 1) / (8 * d.maxH)
159 myy := (d.height + 8*d.maxV - 1) / (8 * d.maxV)
160 if d.img1 == nil && d.img3 == nil {
161 d.makeImg(mxx, myy)
162 }
163 if d.progressive {
164 for i := 0; i < nComp; i++ {
165 compIndex := scan[i].compIndex
166 if d.progCoeffs[compIndex] == nil {
167 d.progCoeffs[compIndex] = make([]block, mxx*myy*d.comp[compIndex].h*d.comp[compIndex].v)
168 }
169 }
170 }
171
172 d.bits = bits{}
173 mcu, expectedRST := 0, uint8(rst0Marker)
174 var (
175
176 b block
177 dc [maxComponents]int32
178
179
180 bx, by int
181 blockCount int
182 )
183 for my := 0; my < myy; my++ {
184 for mx := 0; mx < mxx; mx++ {
185 for i := 0; i < nComp; i++ {
186 compIndex := scan[i].compIndex
187 hi := d.comp[compIndex].h
188 vi := d.comp[compIndex].v
189 for j := 0; j < hi*vi; j++ {
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215 if nComp != 1 {
216 bx = hi*mx + j%hi
217 by = vi*my + j/hi
218 } else {
219 q := mxx * hi
220 bx = blockCount % q
221 by = blockCount / q
222 blockCount++
223 if bx*8 >= d.width || by*8 >= d.height {
224 continue
225 }
226 }
227
228
229 if d.progressive {
230 b = d.progCoeffs[compIndex][by*mxx*hi+bx]
231 } else {
232 b = block{}
233 }
234
235 if ah != 0 {
236 if err := d.refine(&b, &d.huff[acTable][scan[i].ta], zigStart, zigEnd, 1<<al); err != nil {
237 return err
238 }
239 } else {
240 zig := zigStart
241 if zig == 0 {
242 zig++
243
244 value, err := d.decodeHuffman(&d.huff[dcTable][scan[i].td])
245 if err != nil {
246 return err
247 }
248 if value > 16 {
249 return UnsupportedError("excessive DC component")
250 }
251 dcDelta, err := d.receiveExtend(value)
252 if err != nil {
253 return err
254 }
255 dc[compIndex] += dcDelta
256 b[0] = dc[compIndex] << al
257 }
258
259 if zig <= zigEnd && d.eobRun > 0 {
260 d.eobRun--
261 } else {
262
263 huff := &d.huff[acTable][scan[i].ta]
264 for ; zig <= zigEnd; zig++ {
265 value, err := d.decodeHuffman(huff)
266 if err != nil {
267 return err
268 }
269 val0 := value >> 4
270 val1 := value & 0x0f
271 if val1 != 0 {
272 zig += int32(val0)
273 if zig > zigEnd {
274 break
275 }
276 ac, err := d.receiveExtend(val1)
277 if err != nil {
278 return err
279 }
280 b[unzig[zig]] = ac << al
281 } else {
282 if val0 != 0x0f {
283 d.eobRun = uint16(1 << val0)
284 if val0 != 0 {
285 bits, err := d.decodeBits(int32(val0))
286 if err != nil {
287 return err
288 }
289 d.eobRun |= uint16(bits)
290 }
291 d.eobRun--
292 break
293 }
294 zig += 0x0f
295 }
296 }
297 }
298 }
299
300 if d.progressive {
301
302 d.progCoeffs[compIndex][by*mxx*hi+bx] = b
303
304
305
306
307
308
309
310 continue
311 }
312 if err := d.reconstructBlock(&b, bx, by, int(compIndex)); err != nil {
313 return err
314 }
315 }
316 }
317 mcu++
318 if d.ri > 0 && mcu%d.ri == 0 && mcu < mxx*myy {
319
320
321
322 if err := d.readFull(d.tmp[:2]); err != nil {
323 return err
324 } else if d.tmp[0] != 0xff || d.tmp[1] != expectedRST {
325 if err := d.findRST(expectedRST); err != nil {
326 return err
327 }
328 }
329 expectedRST++
330 if expectedRST == rst7Marker+1 {
331 expectedRST = rst0Marker
332 }
333
334 d.bits = bits{}
335
336 dc = [maxComponents]int32{}
337
338 d.eobRun = 0
339 }
340 }
341 }
342
343 return nil
344 }
345
346
347
348 func (d *decoder) refine(b *block, h *huffman, zigStart, zigEnd, delta int32) error {
349
350 if zigStart == 0 {
351 if zigEnd != 0 {
352 panic("unreachable")
353 }
354 bit, err := d.decodeBit()
355 if err != nil {
356 return err
357 }
358 if bit {
359 b[0] |= delta
360 }
361 return nil
362 }
363
364
365 zig := zigStart
366 if d.eobRun == 0 {
367 loop:
368 for ; zig <= zigEnd; zig++ {
369 z := int32(0)
370 value, err := d.decodeHuffman(h)
371 if err != nil {
372 return err
373 }
374 val0 := value >> 4
375 val1 := value & 0x0f
376
377 switch val1 {
378 case 0:
379 if val0 != 0x0f {
380 d.eobRun = uint16(1 << val0)
381 if val0 != 0 {
382 bits, err := d.decodeBits(int32(val0))
383 if err != nil {
384 return err
385 }
386 d.eobRun |= uint16(bits)
387 }
388 break loop
389 }
390 case 1:
391 z = delta
392 bit, err := d.decodeBit()
393 if err != nil {
394 return err
395 }
396 if !bit {
397 z = -z
398 }
399 default:
400 return FormatError("unexpected Huffman code")
401 }
402
403 zig, err = d.refineNonZeroes(b, zig, zigEnd, int32(val0), delta)
404 if err != nil {
405 return err
406 }
407 if zig > zigEnd {
408 return FormatError("too many coefficients")
409 }
410 if z != 0 {
411 b[unzig[zig]] = z
412 }
413 }
414 }
415 if d.eobRun > 0 {
416 d.eobRun--
417 if _, err := d.refineNonZeroes(b, zig, zigEnd, -1, delta); err != nil {
418 return err
419 }
420 }
421 return nil
422 }
423
424
425
426 func (d *decoder) refineNonZeroes(b *block, zig, zigEnd, nz, delta int32) (int32, error) {
427 for ; zig <= zigEnd; zig++ {
428 u := unzig[zig]
429 if b[u] == 0 {
430 if nz == 0 {
431 break
432 }
433 nz--
434 continue
435 }
436 bit, err := d.decodeBit()
437 if err != nil {
438 return 0, err
439 }
440 if !bit {
441 continue
442 }
443 if b[u] >= 0 {
444 b[u] += delta
445 } else {
446 b[u] -= delta
447 }
448 }
449 return zig, nil
450 }
451
452 func (d *decoder) reconstructProgressiveImage() error {
453
454
455 mxx := (d.width + 8*d.maxH - 1) / (8 * d.maxH)
456 for i := 0; i < d.nComp; i++ {
457 if d.progCoeffs[i] == nil {
458 continue
459 }
460 v := 8 * d.maxV / d.comp[i].v
461 h := 8 * d.maxH / d.comp[i].h
462 stride := mxx * d.comp[i].h
463 for by := 0; by*v < d.height; by++ {
464 for bx := 0; bx*h < d.width; bx++ {
465 if err := d.reconstructBlock(&d.progCoeffs[i][by*stride+bx], bx, by, i); err != nil {
466 return err
467 }
468 }
469 }
470 }
471 return nil
472 }
473
474
475
476 func (d *decoder) reconstructBlock(b *block, bx, by, compIndex int) error {
477 qt := &d.quant[d.comp[compIndex].tq]
478 for zig := 0; zig < blockSize; zig++ {
479 b[unzig[zig]] *= qt[zig]
480 }
481 idct(b)
482
483 var h, v int
484 if d.flex {
485
486 h = d.comp[compIndex].expandH
487 v = d.comp[compIndex].expandV
488 bx, by = bx*h, by*v
489 }
490
491 dst, stride := []byte(nil), 0
492 if d.nComp == 1 {
493 dst, stride = d.img1.Pix[8*(by*d.img1.Stride+bx):], d.img1.Stride
494 } else {
495 switch compIndex {
496 case 0:
497 dst, stride = d.img3.Y[8*(by*d.img3.YStride+bx):], d.img3.YStride
498 case 1:
499 dst, stride = d.img3.Cb[8*(by*d.img3.CStride+bx):], d.img3.CStride
500 case 2:
501 dst, stride = d.img3.Cr[8*(by*d.img3.CStride+bx):], d.img3.CStride
502 case 3:
503 dst, stride = d.blackPix[8*(by*d.blackStride+bx):], d.blackStride
504 default:
505 return UnsupportedError("too many components")
506 }
507 }
508
509 if d.flex {
510
511 for y := 0; y < 8; y++ {
512 y8 := y * 8
513 yv := y * v
514 for x := 0; x < 8; x++ {
515 val := uint8(max(0, min(255, b[y8+x]+128)))
516 xh := x * h
517 for yy := 0; yy < v; yy++ {
518 for xx := 0; xx < h; xx++ {
519 dst[(yv+yy)*stride+xh+xx] = val
520 }
521 }
522 }
523 }
524 return nil
525 }
526
527
528 for y := 0; y < 8; y++ {
529 y8 := y * 8
530 yStride := y * stride
531 for x := 0; x < 8; x++ {
532 dst[yStride+x] = uint8(max(0, min(255, b[y8+x]+128)))
533 }
534 }
535 return nil
536 }
537
538
539
540
541
542
543
544
545
546
547 func (d *decoder) findRST(expectedRST uint8) error {
548 for {
549
550
551
552
553 i := 0
554
555 if d.tmp[0] == 0xff {
556 if d.tmp[1] == expectedRST {
557 return nil
558 } else if d.tmp[1] == 0xff {
559 i = 1
560 } else if d.tmp[1] != 0x00 {
561
562
563
564
565
566
567
568 return FormatError("bad RST marker")
569 }
570
571 } else if d.tmp[1] == 0xff {
572 d.tmp[0] = 0xff
573 i = 1
574 }
575
576 if err := d.readFull(d.tmp[i:2]); err != nil {
577 return err
578 }
579 }
580 }
581
View as plain text