1
2
3
4
5
6
7
8 package jpeg
9
10 import (
11 "image"
12 "image/color"
13 "image/internal/imageutil"
14 "io"
15 )
16
17
18 type FormatError string
19
20 func (e FormatError) Error() string { return "invalid JPEG format: " + string(e) }
21
22
23 type UnsupportedError string
24
25 func (e UnsupportedError) Error() string { return "unsupported JPEG feature: " + string(e) }
26
27 var errUnsupportedSubsamplingRatio = UnsupportedError("luma/chroma subsampling ratio")
28
29
30 type component struct {
31 h int
32 v int
33 c uint8
34 tq uint8
35 }
36
37 const (
38 dcTable = 0
39 acTable = 1
40 maxTc = 1
41 maxTh = 3
42 maxTq = 3
43
44 maxComponents = 4
45 )
46
47 const (
48 sof0Marker = 0xc0
49 sof1Marker = 0xc1
50 sof2Marker = 0xc2
51 dhtMarker = 0xc4
52 rst0Marker = 0xd0
53 rst7Marker = 0xd7
54 soiMarker = 0xd8
55 eoiMarker = 0xd9
56 sosMarker = 0xda
57 dqtMarker = 0xdb
58 driMarker = 0xdd
59 comMarker = 0xfe
60
61
62
63 app0Marker = 0xe0
64 app14Marker = 0xee
65 app15Marker = 0xef
66 )
67
68
69 const (
70 adobeTransformUnknown = 0
71 adobeTransformYCbCr = 1
72 adobeTransformYCbCrK = 2
73 )
74
75
76
77
78 var unzig = [blockSize]int{
79 0, 1, 8, 16, 9, 2, 3, 10,
80 17, 24, 32, 25, 18, 11, 4, 5,
81 12, 19, 26, 33, 40, 48, 41, 34,
82 27, 20, 13, 6, 7, 14, 21, 28,
83 35, 42, 49, 56, 57, 50, 43, 36,
84 29, 22, 15, 23, 30, 37, 44, 51,
85 58, 59, 52, 45, 38, 31, 39, 46,
86 53, 60, 61, 54, 47, 55, 62, 63,
87 }
88
89
90
91 type Reader interface {
92 io.ByteReader
93 io.Reader
94 }
95
96
97
98
99 type bits struct {
100 a uint32
101 m uint32
102 n int32
103 }
104
105 type decoder struct {
106 r io.Reader
107 bits bits
108
109
110
111 bytes struct {
112
113
114 buf [4096]byte
115 i, j int
116
117
118 nUnreadable int
119 }
120 width, height int
121
122 img1 *image.Gray
123 img3 *image.YCbCr
124 blackPix []byte
125 blackStride int
126
127 ri int
128 nComp int
129
130
131
132
133
134
135 baseline bool
136 progressive bool
137
138 jfif bool
139 adobeTransformValid bool
140 adobeTransform uint8
141 eobRun uint16
142
143 comp [maxComponents]component
144 progCoeffs [maxComponents][]block
145 huff [maxTc + 1][maxTh + 1]huffman
146 quant [maxTq + 1]block
147 tmp [2 * blockSize]byte
148 }
149
150
151
152 func (d *decoder) fill() error {
153 if d.bytes.i != d.bytes.j {
154 panic("jpeg: fill called when unread bytes exist")
155 }
156
157
158 if d.bytes.j > 2 {
159 d.bytes.buf[0] = d.bytes.buf[d.bytes.j-2]
160 d.bytes.buf[1] = d.bytes.buf[d.bytes.j-1]
161 d.bytes.i, d.bytes.j = 2, 2
162 }
163
164 n, err := d.r.Read(d.bytes.buf[d.bytes.j:])
165 d.bytes.j += n
166 if n > 0 {
167 return nil
168 }
169 if err == io.EOF {
170 err = io.ErrUnexpectedEOF
171 }
172 return err
173 }
174
175
176
177
178
179
180 func (d *decoder) unreadByteStuffedByte() {
181 d.bytes.i -= d.bytes.nUnreadable
182 d.bytes.nUnreadable = 0
183 if d.bits.n >= 8 {
184 d.bits.a >>= 8
185 d.bits.n -= 8
186 d.bits.m >>= 8
187 }
188 }
189
190
191
192 func (d *decoder) readByte() (x byte, err error) {
193 for d.bytes.i == d.bytes.j {
194 if err = d.fill(); err != nil {
195 return 0, err
196 }
197 }
198 x = d.bytes.buf[d.bytes.i]
199 d.bytes.i++
200 d.bytes.nUnreadable = 0
201 return x, nil
202 }
203
204
205
206 var errMissingFF00 = FormatError("missing 0xff00 sequence")
207
208
209 func (d *decoder) readByteStuffedByte() (x byte, err error) {
210
211 if d.bytes.i+2 <= d.bytes.j {
212 x = d.bytes.buf[d.bytes.i]
213 d.bytes.i++
214 d.bytes.nUnreadable = 1
215 if x != 0xff {
216 return x, err
217 }
218 if d.bytes.buf[d.bytes.i] != 0x00 {
219 return 0, errMissingFF00
220 }
221 d.bytes.i++
222 d.bytes.nUnreadable = 2
223 return 0xff, nil
224 }
225
226 d.bytes.nUnreadable = 0
227
228 x, err = d.readByte()
229 if err != nil {
230 return 0, err
231 }
232 d.bytes.nUnreadable = 1
233 if x != 0xff {
234 return x, nil
235 }
236
237 x, err = d.readByte()
238 if err != nil {
239 return 0, err
240 }
241 d.bytes.nUnreadable = 2
242 if x != 0x00 {
243 return 0, errMissingFF00
244 }
245 return 0xff, nil
246 }
247
248
249
250 func (d *decoder) readFull(p []byte) error {
251
252 if d.bytes.nUnreadable != 0 {
253 if d.bits.n >= 8 {
254 d.unreadByteStuffedByte()
255 }
256 d.bytes.nUnreadable = 0
257 }
258
259 for {
260 n := copy(p, d.bytes.buf[d.bytes.i:d.bytes.j])
261 p = p[n:]
262 d.bytes.i += n
263 if len(p) == 0 {
264 break
265 }
266 if err := d.fill(); err != nil {
267 return err
268 }
269 }
270 return nil
271 }
272
273
274 func (d *decoder) ignore(n int) error {
275
276 if d.bytes.nUnreadable != 0 {
277 if d.bits.n >= 8 {
278 d.unreadByteStuffedByte()
279 }
280 d.bytes.nUnreadable = 0
281 }
282
283 for {
284 m := d.bytes.j - d.bytes.i
285 if m > n {
286 m = n
287 }
288 d.bytes.i += m
289 n -= m
290 if n == 0 {
291 break
292 }
293 if err := d.fill(); err != nil {
294 return err
295 }
296 }
297 return nil
298 }
299
300
301 func (d *decoder) processSOF(n int) error {
302 if d.nComp != 0 {
303 return FormatError("multiple SOF markers")
304 }
305 switch n {
306 case 6 + 3*1:
307 d.nComp = 1
308 case 6 + 3*3:
309 d.nComp = 3
310 case 6 + 3*4:
311 d.nComp = 4
312 default:
313 return UnsupportedError("number of components")
314 }
315 if err := d.readFull(d.tmp[:n]); err != nil {
316 return err
317 }
318
319 if d.tmp[0] != 8 {
320 return UnsupportedError("precision")
321 }
322 d.height = int(d.tmp[1])<<8 + int(d.tmp[2])
323 d.width = int(d.tmp[3])<<8 + int(d.tmp[4])
324 if int(d.tmp[5]) != d.nComp {
325 return FormatError("SOF has wrong length")
326 }
327
328 for i := 0; i < d.nComp; i++ {
329 d.comp[i].c = d.tmp[6+3*i]
330
331
332 for j := 0; j < i; j++ {
333 if d.comp[i].c == d.comp[j].c {
334 return FormatError("repeated component identifier")
335 }
336 }
337
338 d.comp[i].tq = d.tmp[8+3*i]
339 if d.comp[i].tq > maxTq {
340 return FormatError("bad Tq value")
341 }
342
343 hv := d.tmp[7+3*i]
344 h, v := int(hv>>4), int(hv&0x0f)
345 if h < 1 || 4 < h || v < 1 || 4 < v {
346 return FormatError("luma/chroma subsampling ratio")
347 }
348 if h == 3 || v == 3 {
349 return errUnsupportedSubsamplingRatio
350 }
351 switch d.nComp {
352 case 1:
353
354
355
356
357
358
359
360
361
362
363
364 h, v = 1, 1
365
366 case 3:
367
368
369
370
371
372
373
374 switch i {
375 case 0:
376
377
378
379 if v == 4 {
380 return errUnsupportedSubsamplingRatio
381 }
382 case 1:
383 if d.comp[0].h%h != 0 || d.comp[0].v%v != 0 {
384 return errUnsupportedSubsamplingRatio
385 }
386 case 2:
387 if d.comp[1].h != h || d.comp[1].v != v {
388 return errUnsupportedSubsamplingRatio
389 }
390 }
391
392 case 4:
393
394
395
396
397
398
399
400
401
402 switch i {
403 case 0:
404 if hv != 0x11 && hv != 0x22 {
405 return errUnsupportedSubsamplingRatio
406 }
407 case 1, 2:
408 if hv != 0x11 {
409 return errUnsupportedSubsamplingRatio
410 }
411 case 3:
412 if d.comp[0].h != h || d.comp[0].v != v {
413 return errUnsupportedSubsamplingRatio
414 }
415 }
416 }
417
418 d.comp[i].h = h
419 d.comp[i].v = v
420 }
421 return nil
422 }
423
424
425 func (d *decoder) processDQT(n int) error {
426 loop:
427 for n > 0 {
428 n--
429 x, err := d.readByte()
430 if err != nil {
431 return err
432 }
433 tq := x & 0x0f
434 if tq > maxTq {
435 return FormatError("bad Tq value")
436 }
437 switch x >> 4 {
438 default:
439 return FormatError("bad Pq value")
440 case 0:
441 if n < blockSize {
442 break loop
443 }
444 n -= blockSize
445 if err := d.readFull(d.tmp[:blockSize]); err != nil {
446 return err
447 }
448 for i := range d.quant[tq] {
449 d.quant[tq][i] = int32(d.tmp[i])
450 }
451 case 1:
452 if n < 2*blockSize {
453 break loop
454 }
455 n -= 2 * blockSize
456 if err := d.readFull(d.tmp[:2*blockSize]); err != nil {
457 return err
458 }
459 for i := range d.quant[tq] {
460 d.quant[tq][i] = int32(d.tmp[2*i])<<8 | int32(d.tmp[2*i+1])
461 }
462 }
463 }
464 if n != 0 {
465 return FormatError("DQT has wrong length")
466 }
467 return nil
468 }
469
470
471 func (d *decoder) processDRI(n int) error {
472 if n != 2 {
473 return FormatError("DRI has wrong length")
474 }
475 if err := d.readFull(d.tmp[:2]); err != nil {
476 return err
477 }
478 d.ri = int(d.tmp[0])<<8 + int(d.tmp[1])
479 return nil
480 }
481
482 func (d *decoder) processApp0Marker(n int) error {
483 if n < 5 {
484 return d.ignore(n)
485 }
486 if err := d.readFull(d.tmp[:5]); err != nil {
487 return err
488 }
489 n -= 5
490
491 d.jfif = d.tmp[0] == 'J' && d.tmp[1] == 'F' && d.tmp[2] == 'I' && d.tmp[3] == 'F' && d.tmp[4] == '\x00'
492
493 if n > 0 {
494 return d.ignore(n)
495 }
496 return nil
497 }
498
499 func (d *decoder) processApp14Marker(n int) error {
500 if n < 12 {
501 return d.ignore(n)
502 }
503 if err := d.readFull(d.tmp[:12]); err != nil {
504 return err
505 }
506 n -= 12
507
508 if d.tmp[0] == 'A' && d.tmp[1] == 'd' && d.tmp[2] == 'o' && d.tmp[3] == 'b' && d.tmp[4] == 'e' {
509 d.adobeTransformValid = true
510 d.adobeTransform = d.tmp[11]
511 }
512
513 if n > 0 {
514 return d.ignore(n)
515 }
516 return nil
517 }
518
519
520 func (d *decoder) decode(r io.Reader, configOnly bool) (image.Image, error) {
521 d.r = r
522
523
524 if err := d.readFull(d.tmp[:2]); err != nil {
525 return nil, err
526 }
527 if d.tmp[0] != 0xff || d.tmp[1] != soiMarker {
528 return nil, FormatError("missing SOI marker")
529 }
530
531
532 for {
533 err := d.readFull(d.tmp[:2])
534 if err != nil {
535 return nil, err
536 }
537 for d.tmp[0] != 0xff {
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558 d.tmp[0] = d.tmp[1]
559 d.tmp[1], err = d.readByte()
560 if err != nil {
561 return nil, err
562 }
563 }
564 marker := d.tmp[1]
565 if marker == 0 {
566
567 continue
568 }
569 for marker == 0xff {
570
571
572 marker, err = d.readByte()
573 if err != nil {
574 return nil, err
575 }
576 }
577 if marker == eoiMarker {
578 break
579 }
580 if rst0Marker <= marker && marker <= rst7Marker {
581
582
583
584
585
586
587 continue
588 }
589
590
591
592 if err = d.readFull(d.tmp[:2]); err != nil {
593 return nil, err
594 }
595 n := int(d.tmp[0])<<8 + int(d.tmp[1]) - 2
596 if n < 0 {
597 return nil, FormatError("short segment length")
598 }
599
600 switch marker {
601 case sof0Marker, sof1Marker, sof2Marker:
602 d.baseline = marker == sof0Marker
603 d.progressive = marker == sof2Marker
604 err = d.processSOF(n)
605 if configOnly && d.jfif {
606 return nil, err
607 }
608 case dhtMarker:
609 if configOnly {
610 err = d.ignore(n)
611 } else {
612 err = d.processDHT(n)
613 }
614 case dqtMarker:
615 if configOnly {
616 err = d.ignore(n)
617 } else {
618 err = d.processDQT(n)
619 }
620 case sosMarker:
621 if configOnly {
622 return nil, nil
623 }
624 err = d.processSOS(n)
625 case driMarker:
626 if configOnly {
627 err = d.ignore(n)
628 } else {
629 err = d.processDRI(n)
630 }
631 case app0Marker:
632 err = d.processApp0Marker(n)
633 case app14Marker:
634 err = d.processApp14Marker(n)
635 default:
636 if app0Marker <= marker && marker <= app15Marker || marker == comMarker {
637 err = d.ignore(n)
638 } else if marker < 0xc0 {
639 err = FormatError("unknown marker")
640 } else {
641 err = UnsupportedError("unknown marker")
642 }
643 }
644 if err != nil {
645 return nil, err
646 }
647 }
648
649 if d.progressive {
650 if err := d.reconstructProgressiveImage(); err != nil {
651 return nil, err
652 }
653 }
654 if d.img1 != nil {
655 return d.img1, nil
656 }
657 if d.img3 != nil {
658 if d.blackPix != nil {
659 return d.applyBlack()
660 } else if d.isRGB() {
661 return d.convertToRGB()
662 }
663 return d.img3, nil
664 }
665 return nil, FormatError("missing SOS marker")
666 }
667
668
669
670
671
672
673
674
675 func (d *decoder) applyBlack() (image.Image, error) {
676 if !d.adobeTransformValid {
677 return nil, UnsupportedError("unknown color model: 4-component JPEG doesn't have Adobe APP14 metadata")
678 }
679
680
681
682
683
684 if d.adobeTransform != adobeTransformUnknown {
685
686
687
688
689 bounds := d.img3.Bounds()
690 img := image.NewRGBA(bounds)
691 imageutil.DrawYCbCr(img, bounds, d.img3, bounds.Min)
692 for iBase, y := 0, bounds.Min.Y; y < bounds.Max.Y; iBase, y = iBase+img.Stride, y+1 {
693 for i, x := iBase+3, bounds.Min.X; x < bounds.Max.X; i, x = i+4, x+1 {
694 img.Pix[i] = 255 - d.blackPix[(y-bounds.Min.Y)*d.blackStride+(x-bounds.Min.X)]
695 }
696 }
697 return &image.CMYK{
698 Pix: img.Pix,
699 Stride: img.Stride,
700 Rect: img.Rect,
701 }, nil
702 }
703
704
705
706
707
708
709 bounds := d.img3.Bounds()
710 img := image.NewCMYK(bounds)
711
712 translations := [4]struct {
713 src []byte
714 stride int
715 }{
716 {d.img3.Y, d.img3.YStride},
717 {d.img3.Cb, d.img3.CStride},
718 {d.img3.Cr, d.img3.CStride},
719 {d.blackPix, d.blackStride},
720 }
721 for t, translation := range translations {
722 subsample := d.comp[t].h != d.comp[0].h || d.comp[t].v != d.comp[0].v
723 for iBase, y := 0, bounds.Min.Y; y < bounds.Max.Y; iBase, y = iBase+img.Stride, y+1 {
724 sy := y - bounds.Min.Y
725 if subsample {
726 sy /= 2
727 }
728 for i, x := iBase+t, bounds.Min.X; x < bounds.Max.X; i, x = i+4, x+1 {
729 sx := x - bounds.Min.X
730 if subsample {
731 sx /= 2
732 }
733 img.Pix[i] = 255 - translation.src[sy*translation.stride+sx]
734 }
735 }
736 }
737 return img, nil
738 }
739
740 func (d *decoder) isRGB() bool {
741 if d.jfif {
742 return false
743 }
744 if d.adobeTransformValid && d.adobeTransform == adobeTransformUnknown {
745
746
747 return true
748 }
749 return d.comp[0].c == 'R' && d.comp[1].c == 'G' && d.comp[2].c == 'B'
750 }
751
752 func (d *decoder) convertToRGB() (image.Image, error) {
753 cScale := d.comp[0].h / d.comp[1].h
754 bounds := d.img3.Bounds()
755 img := image.NewRGBA(bounds)
756 for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
757 po := img.PixOffset(bounds.Min.X, y)
758 yo := d.img3.YOffset(bounds.Min.X, y)
759 co := d.img3.COffset(bounds.Min.X, y)
760 for i, iMax := 0, bounds.Max.X-bounds.Min.X; i < iMax; i++ {
761 img.Pix[po+4*i+0] = d.img3.Y[yo+i]
762 img.Pix[po+4*i+1] = d.img3.Cb[co+i/cScale]
763 img.Pix[po+4*i+2] = d.img3.Cr[co+i/cScale]
764 img.Pix[po+4*i+3] = 255
765 }
766 }
767 return img, nil
768 }
769
770
771 func Decode(r io.Reader) (image.Image, error) {
772 var d decoder
773 return d.decode(r, false)
774 }
775
776
777
778 func DecodeConfig(r io.Reader) (image.Config, error) {
779 var d decoder
780 if _, err := d.decode(r, true); err != nil {
781 return image.Config{}, err
782 }
783 switch d.nComp {
784 case 1:
785 return image.Config{
786 ColorModel: color.GrayModel,
787 Width: d.width,
788 Height: d.height,
789 }, nil
790 case 3:
791 cm := color.YCbCrModel
792 if d.isRGB() {
793 cm = color.RGBAModel
794 }
795 return image.Config{
796 ColorModel: cm,
797 Width: d.width,
798 Height: d.height,
799 }, nil
800 case 4:
801 return image.Config{
802 ColorModel: color.CMYKModel,
803 Width: d.width,
804 Height: d.height,
805 }, nil
806 }
807 return image.Config{}, FormatError("missing SOF marker")
808 }
809
810 func init() {
811 image.RegisterFormat("jpeg", "\xff\xd8", Decode, DecodeConfig)
812 }
813
View as plain text