1
2
3
4
5
6
7
8 package ccitt
9
10 import (
11 "encoding/binary"
12 "errors"
13 "image"
14 "io"
15 "math/bits"
16 )
17
18 var (
19 errIncompleteCode = errors.New("ccitt: incomplete code")
20 errInvalidBounds = errors.New("ccitt: invalid bounds")
21 errInvalidCode = errors.New("ccitt: invalid code")
22 errInvalidMode = errors.New("ccitt: invalid mode")
23 errInvalidOffset = errors.New("ccitt: invalid offset")
24 errMissingEOL = errors.New("ccitt: missing End-of-Line")
25 errRunLengthOverflowsWidth = errors.New("ccitt: run length overflows width")
26 errRunLengthTooLong = errors.New("ccitt: run length too long")
27 errUnsupportedMode = errors.New("ccitt: unsupported mode")
28 errUnsupportedSubFormat = errors.New("ccitt: unsupported sub-format")
29 errUnsupportedWidth = errors.New("ccitt: unsupported width")
30 )
31
32
33 type Order uint32
34
35 const (
36
37 LSB Order = iota
38
39 MSB
40 )
41
42
43
44
45 type SubFormat uint32
46
47 const (
48 Group3 SubFormat = iota
49 Group4
50 )
51
52
53
54 const AutoDetectHeight = -1
55
56
57 type Options struct {
58
59 Align bool
60
61 Invert bool
62 }
63
64
65
66
67 const maxWidth = 1 << 20
68
69 func invertBytes(b []byte) {
70 for i, c := range b {
71 b[i] = ^c
72 }
73 }
74
75 func reverseBitsWithinBytes(b []byte) {
76 for i, c := range b {
77 b[i] = bits.Reverse8(c)
78 }
79 }
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 func highBits(dst []byte, src []byte, invert bool) (d int, s int) {
96
97 n := len(src) / 8
98 if n > len(dst) {
99 n = len(dst)
100 }
101 dstN := dst[:n]
102 for i := range dstN {
103 src8 := src[i*8 : i*8+8]
104 dstN[i] = ((src8[0] & 0x80) >> 0) |
105 ((src8[1] & 0x80) >> 1) |
106 ((src8[2] & 0x80) >> 2) |
107 ((src8[3] & 0x80) >> 3) |
108 ((src8[4] & 0x80) >> 4) |
109 ((src8[5] & 0x80) >> 5) |
110 ((src8[6] & 0x80) >> 6) |
111 ((src8[7] & 0x80) >> 7)
112 }
113 d, s = n, 8*n
114 dst, src = dst[d:], src[s:]
115
116
117 if (len(dst) > 0) && (len(src) > 0) {
118 dstByte := byte(0)
119 if invert {
120 dstByte = 0xFF >> uint(len(src))
121 }
122 for n, srcByte := range src {
123 dstByte |= (srcByte & 0x80) >> uint(n)
124 }
125 dst[0] = dstByte
126 d, s = d+1, s+len(src)
127 }
128 return d, s
129 }
130
131 type bitReader struct {
132 r io.Reader
133
134
135
136
137 readErr error
138
139
140 order Order
141
142
143 bits uint64
144 nBits uint32
145
146
147 br uint32
148 bw uint32
149 bytes [1024]uint8
150 }
151
152 func (b *bitReader) alignToByteBoundary() {
153 n := b.nBits & 7
154 b.bits <<= n
155 b.nBits -= n
156 }
157
158
159
160
161
162
163
164 const nextBitMaxNBits = 31
165
166 func (b *bitReader) nextBit() (uint64, error) {
167 for {
168 if b.nBits > 0 {
169 bit := b.bits >> 63
170 b.bits <<= 1
171 b.nBits--
172 return bit, nil
173 }
174
175 if available := b.bw - b.br; available >= 4 {
176
177
178
179
180
181
182 b.bits = uint64(binary.BigEndian.Uint32(b.bytes[b.br:])) << 32
183 b.br += 4
184 b.nBits = 32
185 continue
186 } else if available > 0 {
187 b.bits = uint64(b.bytes[b.br]) << (7 * 8)
188 b.br++
189 b.nBits = 8
190 continue
191 }
192
193 if b.readErr != nil {
194 return 0, b.readErr
195 }
196
197 n, err := b.r.Read(b.bytes[:])
198 b.br = 0
199 b.bw = uint32(n)
200 b.readErr = err
201
202 if b.order != MSB {
203 reverseBitsWithinBytes(b.bytes[:b.bw])
204 }
205 }
206 }
207
208 func decode(b *bitReader, decodeTable [][2]int16) (uint32, error) {
209 nBitsRead, bitsRead, state := uint32(0), uint64(0), int32(1)
210 for {
211 bit, err := b.nextBit()
212 if err != nil {
213 if err == io.EOF {
214 err = errIncompleteCode
215 }
216 return 0, err
217 }
218 bitsRead |= bit << (63 - nBitsRead)
219 nBitsRead++
220
221
222 state = int32(decodeTable[state][bit&1])
223 if state < 0 {
224 return uint32(^state), nil
225 } else if state == 0 {
226
227 b.bits = (b.bits >> nBitsRead) | bitsRead
228 b.nBits += nBitsRead
229 return 0, errInvalidCode
230 }
231 }
232 }
233
234
235 func decodeEOL(b *bitReader) error {
236 nBitsRead, bitsRead := uint32(0), uint64(0)
237 for {
238 bit, err := b.nextBit()
239 if err != nil {
240 if err == io.EOF {
241 err = errMissingEOL
242 }
243 return err
244 }
245 bitsRead |= bit << (63 - nBitsRead)
246 nBitsRead++
247
248 if nBitsRead < 12 {
249 if bit&1 == 0 {
250 continue
251 }
252 } else if bit&1 != 0 {
253 return nil
254 }
255
256
257 b.bits = (b.bits >> nBitsRead) | bitsRead
258 b.nBits += nBitsRead
259 return errMissingEOL
260 }
261 }
262
263 type reader struct {
264 br bitReader
265 subFormat SubFormat
266
267
268 width int
269
270
271
272
273
274
275
276 rowsRemaining int
277
278
279
280
281
282 curr []byte
283 prev []byte
284
285
286
287
288
289
290 ri int
291
292
293
294
295
296
297 wi int
298
299
300 align bool
301 invert bool
302
303
304
305 atStartOfRow bool
306
307
308 penColorIsWhite bool
309
310
311 seenStartOfImage bool
312
313
314
315
316
317
318
319
320
321
322
323
324 truncated bool
325
326
327 readErr error
328 }
329
330 func (z *reader) Read(p []byte) (int, error) {
331 if z.readErr != nil {
332 return 0, z.readErr
333 }
334 originalP := p
335
336 for len(p) > 0 {
337
338
339 if z.curr == nil {
340 if !z.seenStartOfImage {
341 if z.readErr = z.startDecode(); z.readErr != nil {
342 break
343 }
344 z.atStartOfRow = true
345 }
346 z.curr = make([]byte, z.width)
347 }
348
349
350 if z.atStartOfRow {
351 if z.rowsRemaining < 0 {
352
353
354
355
356
357
358
359
360
361
362 if z.align && (z.subFormat == Group4) {
363 z.br.alignToByteBoundary()
364 }
365
366 if err := z.decodeEOL(); err == errMissingEOL {
367
368 } else if err != nil {
369 z.readErr = err
370 break
371 } else {
372 if z.readErr = z.finishDecode(true); z.readErr != nil {
373 break
374 }
375 z.readErr = io.EOF
376 break
377 }
378
379 } else if z.rowsRemaining == 0 {
380
381
382 if z.readErr = z.finishDecode(false); z.readErr != nil {
383 break
384 }
385 z.readErr = io.EOF
386 break
387
388 } else {
389 z.rowsRemaining--
390 }
391
392 if z.readErr = z.decodeRow(z.rowsRemaining == 0); z.readErr != nil {
393 break
394 }
395 }
396
397
398 packD, packS := highBits(p, z.curr[z.ri:], z.invert)
399 p = p[packD:]
400 z.ri += packS
401
402
403 if z.ri == len(z.curr) {
404 z.ri, z.curr, z.prev = 0, z.prev, z.curr
405 z.atStartOfRow = true
406 }
407 }
408
409 n := len(originalP) - len(p)
410 if z.invert {
411 invertBytes(originalP[:n])
412 }
413 return n, z.readErr
414 }
415
416 func (z *reader) penColor() byte {
417 if z.penColorIsWhite {
418 return 0xFF
419 }
420 return 0x00
421 }
422
423 func (z *reader) startDecode() error {
424 switch z.subFormat {
425 case Group3:
426 if err := z.decodeEOL(); err != nil {
427 return err
428 }
429
430 case Group4:
431
432
433 default:
434 return errUnsupportedSubFormat
435 }
436
437 z.seenStartOfImage = true
438 return nil
439 }
440
441 func (z *reader) finishDecode(alreadySeenEOL bool) error {
442 numberOfEOLs := 0
443 switch z.subFormat {
444 case Group3:
445 if z.truncated {
446 return nil
447 }
448
449
450
451 numberOfEOLs = 5
452
453 case Group4:
454 autoDetectHeight := z.rowsRemaining < 0
455 if autoDetectHeight {
456
457 } else if z.align {
458 z.br.alignToByteBoundary()
459 }
460
461
462
463 if err := z.decodeEOL(); err != nil {
464 if (err == errMissingEOL) && !autoDetectHeight {
465 z.truncated = true
466 return nil
467 }
468 return err
469 }
470 numberOfEOLs = 1
471
472 default:
473 return errUnsupportedSubFormat
474 }
475
476 if alreadySeenEOL {
477 numberOfEOLs--
478 }
479 for ; numberOfEOLs > 0; numberOfEOLs-- {
480 if err := z.decodeEOL(); err != nil {
481 return err
482 }
483 }
484 return nil
485 }
486
487 func (z *reader) decodeEOL() error {
488 return decodeEOL(&z.br)
489 }
490
491 func (z *reader) decodeRow(finalRow bool) error {
492 z.wi = 0
493 z.atStartOfRow = true
494 z.penColorIsWhite = true
495
496 if z.align {
497 z.br.alignToByteBoundary()
498 }
499
500 switch z.subFormat {
501 case Group3:
502 for ; z.wi < len(z.curr); z.atStartOfRow = false {
503 if err := z.decodeRun(); err != nil {
504 return err
505 }
506 }
507 err := z.decodeEOL()
508 if finalRow && (err == errMissingEOL) {
509 z.truncated = true
510 return nil
511 }
512 return err
513
514 case Group4:
515 for ; z.wi < len(z.curr); z.atStartOfRow = false {
516 mode, err := decode(&z.br, modeDecodeTable[:])
517 if err != nil {
518 return err
519 }
520 rm := readerMode{}
521 if mode < uint32(len(readerModes)) {
522 rm = readerModes[mode]
523 }
524 if rm.function == nil {
525 return errInvalidMode
526 }
527 if err := rm.function(z, rm.arg); err != nil {
528 return err
529 }
530 }
531 return nil
532 }
533
534 return errUnsupportedSubFormat
535 }
536
537 func (z *reader) decodeRun() error {
538 table := blackDecodeTable[:]
539 if z.penColorIsWhite {
540 table = whiteDecodeTable[:]
541 }
542
543 total := 0
544 for {
545 n, err := decode(&z.br, table)
546 if err != nil {
547 return err
548 }
549 if n > maxWidth {
550 panic("unreachable")
551 }
552 total += int(n)
553 if total > maxWidth {
554 return errRunLengthTooLong
555 }
556
557 if n <= 0x3F {
558 break
559 }
560 }
561
562 if total > (len(z.curr) - z.wi) {
563 return errRunLengthOverflowsWidth
564 }
565 dst := z.curr[z.wi : z.wi+total]
566 penColor := z.penColor()
567 for i := range dst {
568 dst[i] = penColor
569 }
570 z.wi += total
571 z.penColorIsWhite = !z.penColorIsWhite
572
573 return nil
574 }
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612 const (
613 findB1 = false
614 findB2 = true
615 )
616
617
618 func (z *reader) findB(whichB bool) int {
619
620
621
622 if len(z.prev) != len(z.curr) {
623 return len(z.curr)
624 }
625
626 i := z.wi
627
628 if z.atStartOfRow {
629
630
631 for ; (i < len(z.prev)) && (z.prev[i] == 0xFF); i++ {
632 }
633 if whichB == findB2 {
634 for ; (i < len(z.prev)) && (z.prev[i] == 0x00); i++ {
635 }
636 }
637 return i
638 }
639
640
641
642 oppositeColor := ^z.penColor()
643 for ; (i < len(z.prev)) && (z.prev[i] == oppositeColor); i++ {
644 }
645
646
647 penColor := ^oppositeColor
648 for ; (i < len(z.prev)) && (z.prev[i] == penColor); i++ {
649 }
650
651
652 if whichB == findB2 {
653
654
655 oppositeColor := ^penColor
656 for ; (i < len(z.prev)) && (z.prev[i] == oppositeColor); i++ {
657 }
658 }
659
660 return i
661 }
662
663 type readerMode struct {
664 function func(z *reader, arg int) error
665 arg int
666 }
667
668 var readerModes = [...]readerMode{
669 modePass: {function: readerModePass},
670 modeH: {function: readerModeH},
671 modeV0: {function: readerModeV, arg: +0},
672 modeVR1: {function: readerModeV, arg: +1},
673 modeVR2: {function: readerModeV, arg: +2},
674 modeVR3: {function: readerModeV, arg: +3},
675 modeVL1: {function: readerModeV, arg: -1},
676 modeVL2: {function: readerModeV, arg: -2},
677 modeVL3: {function: readerModeV, arg: -3},
678 modeExt: {function: readerModeExt},
679 }
680
681 func readerModePass(z *reader, arg int) error {
682 b2 := z.findB(findB2)
683 if (b2 < z.wi) || (len(z.curr) < b2) {
684 return errInvalidOffset
685 }
686 dst := z.curr[z.wi:b2]
687 penColor := z.penColor()
688 for i := range dst {
689 dst[i] = penColor
690 }
691 z.wi = b2
692 return nil
693 }
694
695 func readerModeH(z *reader, arg int) error {
696
697 for i := 0; i < 2; i++ {
698 if err := z.decodeRun(); err != nil {
699 return err
700 }
701 }
702 return nil
703 }
704
705 func readerModeV(z *reader, arg int) error {
706 a1 := z.findB(findB1) + arg
707 if (a1 < z.wi) || (len(z.curr) < a1) {
708 return errInvalidOffset
709 }
710 dst := z.curr[z.wi:a1]
711 penColor := z.penColor()
712 for i := range dst {
713 dst[i] = penColor
714 }
715 z.wi = a1
716 z.penColorIsWhite = !z.penColorIsWhite
717 return nil
718 }
719
720 func readerModeExt(z *reader, arg int) error {
721 return errUnsupportedMode
722 }
723
724
725
726
727
728 func DecodeIntoGray(dst *image.Gray, r io.Reader, order Order, sf SubFormat, opts *Options) error {
729 bounds := dst.Bounds()
730 if (bounds.Dx() < 0) || (bounds.Dy() < 0) {
731 return errInvalidBounds
732 }
733 if bounds.Dx() > maxWidth {
734 return errUnsupportedWidth
735 }
736
737 z := reader{
738 br: bitReader{r: r, order: order},
739 subFormat: sf,
740 align: (opts != nil) && opts.Align,
741 invert: (opts != nil) && opts.Invert,
742 width: bounds.Dx(),
743 }
744 if err := z.startDecode(); err != nil {
745 return err
746 }
747
748 width := bounds.Dx()
749 for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
750 p := (y - bounds.Min.Y) * dst.Stride
751 z.curr = dst.Pix[p : p+width]
752 if err := z.decodeRow(y+1 == bounds.Max.Y); err != nil {
753 return err
754 }
755 z.curr, z.prev = nil, z.curr
756 }
757
758 if err := z.finishDecode(false); err != nil {
759 return err
760 }
761
762 if z.invert {
763 for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
764 p := (y - bounds.Min.Y) * dst.Stride
765 invertBytes(dst.Pix[p : p+width])
766 }
767 }
768
769 return nil
770 }
771
772
773
774
775
776
777
778 func NewReader(r io.Reader, order Order, sf SubFormat, width int, height int, opts *Options) io.Reader {
779 readErr := error(nil)
780 if width < 0 {
781 readErr = errInvalidBounds
782 } else if width > maxWidth {
783 readErr = errUnsupportedWidth
784 }
785
786 return &reader{
787 br: bitReader{r: r, order: order},
788 subFormat: sf,
789 align: (opts != nil) && opts.Align,
790 invert: (opts != nil) && opts.Invert,
791 width: width,
792 rowsRemaining: height,
793 readErr: readErr,
794 }
795 }
796
View as plain text