forked from pdfcpu/pdfcpu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
650 lines (534 loc) · 12.1 KB
/
reader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
/*
Copyright 2018 The pdfcpu Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package ccitt implements a CCITT Fax image decoder for Group3/1d and Group4.
package ccitt
import (
"errors"
"io"
"io/ioutil"
)
// The supported CCITT encoding modes.
const (
Group3 = iota
Group4
)
var errMissingTermCode = errors.New("ccitt: missing terminating code")
// 1 bit deep pixel buffer
type pixelBuf struct {
buf []uint8 // size = w * h / 8 +1
stride int // stride (in bytes) between vertically adjacent pixels.
w int // image width = row length
}
func (pb *pixelBuf) addRow() {
pb.buf = append(pb.buf, make([]uint8, pb.stride)...)
}
func (pb *pixelBuf) whitePix(row, x int) bool {
bit := uint8(7 - (x % 8))
off := row*pb.stride + x/8
mask := uint8(0x01 << bit)
//fmt.Printf("whitePix(row=%d, x=%d) off=%d mask=x%02x (bit=%d) %08b\n", row, x, off, mask, bit, pb.buf[off])
return pb.buf[off]&mask > 0
}
func (pb *pixelBuf) nextChangeElement(row, start int) int {
white := true
if start >= 0 {
white = pb.whitePix(row, start)
}
//fmt.Printf("nextChangeElement starts at row=%d start=%d with white=%t (end=%d)\n", row, start, white, pb.w-1)
i := start + 1
for i < pb.w && pb.whitePix(row, i) == white {
i++
}
//fmt.Printf("nextChangeElement: ends with changeElement at %d\n", i)
return i
}
func (pb *pixelBuf) nextChangeElementWithColor(row, start int, white bool) int {
//fmt.Printf("nextChangeElementWithColor begin at row=%d start=%d with white=%t (end=%d)\n", row, start, white, pb.w-1)
b := pb.nextChangeElement(row, start)
if b == 0 || b == pb.w || pb.whitePix(row, b) == white {
return b
}
e := pb.nextChangeElement(row, b)
//fmt.Printf("nextChangeElementWithColor returning %d\n", e)
return e
}
func (pb *pixelBuf) setPix(row, x int) {
bit := uint8(7 - (x % 8))
off := row*pb.stride + x/8
mask := uint8(0x01 << bit)
// fmt.Printf("setPix(%d,%d): off=%d mask=%d (bit=%d)\n", row, x, off, mask, bit)
pb.buf[off] |= mask
//fmt.Printf("i=%d off=%d mask=%d (bit=%d) %08b\n", pb.i, off, mask, bit, pb.buf[off])
}
func (pb *pixelBuf) addRun(row, a0, l int, white bool) {
//fmt.Printf("addRun: row=%d a0=%d l=%d white=%t\n", row, a0, l, white)
if white {
//fmt.Printf("addWhiteRun: row=%d a0=%d l=%d\n", row, a0, l)
for i := 0; i < l; i++ {
pb.setPix(row, a0+i)
}
}
}
func (pb *pixelBuf) invertBuf() {
//fmt.Println("invertBuf")
for i, b := range pb.buf {
pb.buf[i] = b ^ 0xff
}
}
type buf []uint8
func shiftFor(a, b int) uint8 {
shift := a
if b > 0 {
shift = a - 8 + b
}
return uint8(shift)
}
// getBitBuf provides the next 24 bits starting at pos
func (b buf) getBitBuf(pos int) (*bitBuf, error) {
bit := pos % 8
off := pos / 8
// fmt.Printf("getBitBuf(%d) off=%d bit=%d\n", pos, off, bit)
if off >= len(b) {
// fmt.Printf("off=%d len=%d\n", off, len(b))
return nil, errors.New("bitBuf overflow")
}
var bb bitBuf
if bit > 0 {
mask := uint8(0xff >> uint8(bit))
bb |= bitBuf(b[off] & mask)
if off == len(b)-1 {
bb <<= shiftFor(32, bit)
return &bb, nil
}
off++
bb <<= 8
}
bb |= bitBuf(b[off])
if off == len(b)-1 {
bb <<= shiftFor(24, bit)
return &bb, nil
}
off++
bb <<= 8
bb |= bitBuf(b[off])
if off == len(b)-1 {
bb <<= shiftFor(16, bit)
return &bb, nil
}
off++
bb <<= 8
bb |= bitBuf(b[off])
bb <<= shiftFor(8, bit)
return &bb, nil
}
type bitBuf uint32
func (b bitBuf) hasPrefix(code string) bool {
for i, bit := range code {
mask := uint32(0x01 << uint8(31-i))
k := b & bitBuf(mask)
j := int(bit) - 48
if j == 1 {
if k == 0 {
return false
}
} else {
if k > 0 {
return false
}
}
}
// fmt.Printf("has prefix = %s\n", code)
return true
}
type decoder struct {
mode int
r io.Reader
raw buf
pos int
err error
pb *pixelBuf
w int // image width = row length
white bool // current color, true for set pixel bit.
align bool // pad last row byte with 0s
inv bool // invert all pixels
row int // current row number 0..h-1
a0 int // current horizontal scan position -1..w
toRead []byte // bytes to return from Read prepared after decoding
}
func (d *decoder) nextMode() (mode string, err error) {
//fmt.Printf("nextMode: d.align=%t\n", d.align)
if d.align && d.a0 < 0 {
// Skip bits until next byte boundary.
if d.pos%8 > 0 {
d.pos += 8 - d.pos%8
}
}
// Get next 24 encoded bits.
bb, err := d.raw.getBitBuf(d.pos)
if err != nil {
return "", err
}
//fmt.Printf("%032b\n", *bb)
for _, v := range codes {
if !bb.hasPrefix(v) {
continue
}
d.pos += len(v)
return v, nil
}
return "", errors.New("ccitt: corrupt group4 data")
}
func (d *decoder) nextRunLength() (int, error) {
var err error
c := 0
// Process optional big markup codes.
ok := true
for ok {
ok = false
bb, err := d.raw.getBitBuf(d.pos)
if err != nil {
return c, err
}
//fmt.Printf("%032b\n", *bb)
for k, v := range makeupBig {
if !bb.hasPrefix(k) {
continue
}
d.pos += len(k)
c += v
ok = true
break
}
}
// Process optional markup code.
makeup := makeupW
if !d.white {
makeup = makeupB
}
bb, err := d.raw.getBitBuf(d.pos)
if err != nil {
return c, err
}
//fmt.Printf("%032b\n", *bb)
for k, v := range makeup {
if !bb.hasPrefix(k) {
continue
}
d.pos += len(k)
c += v
break
}
// Process terminal code.
term := termW
if !d.white {
term = termB
}
bb, err = d.raw.getBitBuf(d.pos)
if err != nil {
return c, err
}
//fmt.Printf("%032b\n", *bb)
ok = false
for k, v := range term {
if !bb.hasPrefix(k) {
continue
}
d.pos += len(k)
c += v
ok = true
break
}
if !ok {
err = errMissingTermCode
}
return c, err
}
func (d *decoder) calcb1(a0 int) int {
if d.row == 0 {
// b1 is 0 for first row
return d.w
}
return d.pb.nextChangeElementWithColor(d.row-1, a0, !d.white)
}
func (d *decoder) calcb2(a0 int) int {
b1 := d.calcb1(a0)
//fmt.Printf("calcb2(%d) b1=%d eol=%d\n", a0, b1, d.pb.w-1)
return d.pb.nextChangeElement(d.row-1, b1)
}
func (d *decoder) handlePass() {
//fmt.Printf("begin Pass\n")
b2 := d.calcb2(d.a0)
d.pb.addRun(d.row, d.a0, b2-d.a0, d.white)
d.a0 = b2
// fmt.Printf("end Pass a0=%d (b2=%d)\n", d.a0, b2)
}
func (d *decoder) handleHorizontal() {
//fmt.Printf("begin Horiz\n")
if d.a0 == -1 {
d.a0++
}
// One run using the current color.
// fmt.Println("nextRunLength")
r1, err := d.nextRunLength()
if err != nil {
d.err = err
return
}
d.pb.addRun(d.row, d.a0, r1, d.white)
d.a0 += r1
// Another run using the opposite color.
// fmt.Println("nextRunLength")
d.white = !d.white
r2, err := d.nextRunLength()
if err != nil {
// Note: group3 encoding skips trailing black runs for a line.
if err != nil && (d.mode == Group4 || d.mode == Group3 && err != errMissingTermCode) {
d.err = err
return
}
r2 = d.w - d.a0
}
d.pb.addRun(d.row, d.a0, r2, d.white)
d.a0 += r2
d.white = !d.white
//fmt.Printf("end Horiz(%d,%d) a0=%d\n", r1, r2, d.a0)
}
func (d *decoder) handleVertical(offa1b1 int) {
b1 := d.calcb1(d.a0)
//fmt.Printf("handleVertical: b1=%d\n", b1)
if b1 == 0 {
// at row start
a1 := b1 + offa1b1
d.a0 = a1
d.white = false
} else {
a1 := b1 + offa1b1
if a1 > d.w {
a1 = d.w
}
if d.a0 < 0 {
d.a0 = 0
}
d.pb.addRun(d.row, d.a0, a1-d.a0, d.white)
d.a0 = a1
d.white = !d.white
}
}
func (d *decoder) handleV0() {
//fmt.Println("V(0)")
d.handleVertical(0)
}
func (d *decoder) handleVL1() {
//fmt.Println("VL(1)")
d.handleVertical(-1)
}
func (d *decoder) handleVL2() {
//fmt.Println("VL(2)")
d.handleVertical(-2)
}
func (d *decoder) handleVL3() {
//fmt.Println("VL(3)")
d.handleVertical(-3)
}
func (d *decoder) handleVR1() {
//fmt.Println("VR(1)")
d.handleVertical(1)
}
func (d *decoder) handleVR2() {
//fmt.Println("VR(2)")
d.handleVertical(2)
}
func (d *decoder) handleVR3() {
//fmt.Println("VR(3)")
d.handleVertical(3)
}
func (d *decoder) handleExt() {
d.err = errors.New("ccitt: uncompressed mode not supported")
}
func (d *decoder) initBufs() {
// Prepare raw buf.
var err error
d.raw, err = ioutil.ReadAll(d.r)
if err != nil {
d.err = err
return
}
// Prepare pixelbuf.
stride := d.w / 8
if d.w%8 > 0 {
stride++
}
d.pb = &pixelBuf{buf: make([]uint8, stride), stride: stride, w: d.w}
// fmt.Printf("len(d.pb.buf)=%d stride=%d\n", len(d.pb.buf), stride)
}
func (d *decoder) readEOL() (bool, error) {
// Get next 24 compressed Bits.
bb, err := d.raw.getBitBuf(d.pos)
if err != nil {
return false, err
}
if !bb.hasPrefix(eol) {
return false, nil
}
d.pos += len(eol)
return true, nil
}
func (d *decoder) decodeGroup3OneDimensional() {
d.initBufs()
//fmt.Printf("%d(#%02x)bytes: \n%s\n", len(d.raw), len(d.raw), hex.Dump(d.raw))
// Check for leading eol.
ok, err := d.readEOL()
if err != nil {
d.err = err
return
}
if !ok {
d.err = errors.New("group3: missing eol page prefix")
return
}
for {
// fmt.Printf("\nrow=%d a0=%d white=%t\n", d.row, d.a0, d.white)
if d.a0 == d.w {
d.a0 = -1
d.row++
d.pb.addRow()
d.white = true
// Check for eol.
ok, err := d.readEOL()
if err != nil {
d.err = err
return
}
if !ok {
d.err = errors.New("group3: missing eol")
return
}
// Check for rtc.
ok, err = d.readEOL()
if err != nil {
d.err = err
return
}
if ok {
// Processed 2 eol's.
// Need another 4 eol's to complete rtc sequence.
for i := 0; i < 4; i++ {
ok, err = d.readEOL()
if err != nil {
d.err = err
return
}
if !ok {
d.err = errors.New("group3: corrupt rtc")
return
}
}
// Successfully parsed rtc.
break
}
// Move on to process next line.
}
d.handleHorizontal()
if d.err != nil {
return
}
}
if d.inv {
d.pb.invertBuf()
}
d.toRead = d.pb.buf
d.err = io.EOF
}
// decode decompresses bytes from r and leaves them in d.toRead.
func (d *decoder) decodeGroup4() {
d.initBufs()
//fmt.Printf("%d(#%02x)bytes: \n%s\n", len(d.raw), len(d.raw), hex.Dump(d.raw))
for {
//fmt.Printf("\nrow=%d a0=%d white=%t\n", d.row, d.a0, d.white)
if d.a0 == d.w {
d.a0 = -1
d.row++
d.pb.addRow()
d.white = true
//fmt.Printf("row=%d a0=%d white=%t\n", d.row, d.a0, d.white)
}
mode, err := d.nextMode()
if err != nil {
d.err = err
return
}
for k, v := range map[string]func(){
mP: d.handlePass,
mH: d.handleHorizontal,
mV0: d.handleV0,
mVL1: d.handleVL1,
mVL2: d.handleVL2,
mVL3: d.handleVL3,
mVR1: d.handleVR1,
mVR2: d.handleVR2,
mVR3: d.handleVR3,
ext: d.handleExt,
} {
if k == mode {
v()
break
}
}
if d.err != nil {
return
}
if mode == eofb {
break
}
}
if d.inv {
d.pb.invertBuf()
}
d.toRead = d.pb.buf
d.err = io.EOF
}
func (d *decoder) decode() {
switch d.mode {
case Group3:
d.decodeGroup3OneDimensional()
case Group4:
d.decodeGroup4()
}
}
func (d *decoder) Read(b []byte) (int, error) {
for {
if len(d.toRead) > 0 {
n := copy(b, d.toRead)
d.toRead = d.toRead[n:]
return n, nil
}
if d.err != nil {
return 0, d.err
}
d.decode()
}
}
var errClosed = errors.New("ccitt: reader/writer is closed")
func (d *decoder) Close() error {
d.err = errClosed // in case any Reads come along
return nil
}
// NewReader creates a new ReadCloser
// Reads from the returned io.ReadCloser read and decompress data from r.
// If r does not also implement io.ByteReader,
// the decompressor may read more data than necessary from r.
// It is the caller's responsibility to call Close on the ReadCloser when
// finished reading.
func NewReader(r io.Reader, mode, width int, inverse, align bool) io.ReadCloser {
return &decoder{r: r, mode: mode, white: true, w: width, inv: inverse, align: align}
}