-
Notifications
You must be signed in to change notification settings - Fork 16
/
pts_fax.c
3224 lines (2995 loc) · 79 KB
/
pts_fax.c
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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* pts_fax.c -- a compact CCITTFax compressor and uncompressor) implementation
* compiled by [email protected] at Sun Jul 7 19:51:42 CEST 2002
*
* algorithm ripped from GNU Ghostscript, implementation and (C):
*
Copyright (C) 1993, 1995, 1996, 1997, 1998, 1999 Aladdin Enterprises. All
rights reserved.
GNU Ghostscript is free software; you can redistribute it and/or
modify it under the terms of version 2 of the GNU General Public
License as published by the Free Software Foundation.
GNU Ghostscript is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program so you can know your rights and responsibilities.
It should be in a file named doc/COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place Suite 330, Boston, MA
02111-1307, USA.
*/
#include "pts_fax.h"
#if 0 /* doesn't work in C++ */
# define intern static
# define impl static
# define intern_const static const
# define impl_const static const
#else
# define intern static
# define impl static
# define intern_const extern const
# define impl_const const
#endif
/* #define cxxintern extern */
#ifdef __GNUC__
#ifndef __clang__
#ifdef __cplusplus
#pragma implementation
#endif
#endif
#endif
/* misc_types.h by [email protected] at Sat Jul 6 19:20:17 CEST 2002 */
#ifndef MISC_TYPES_H
#define MISC_TYPES_H 1
/* #include "config2.h" */
#define BEGIN do {
#define END } while (0)
#ifndef DO_NOTHING
# define DO_NOTHING BEGIN END
#endif
# define if_debug0(c,s) DO_NOTHING
# define if_debug1(c,s,a1) DO_NOTHING
# define if_debug2(c,s,a1,a2) DO_NOTHING
# define if_debug3(c,s,a1,a2,a3) DO_NOTHING
# define if_debug4(c,s,a1,a2,a3,a4) DO_NOTHING
# define if_debug5(c,s,a1,a2,a3,a4,a5) DO_NOTHING
# define if_debug6(c,s,a1,a2,a3,a4,a5,a6) DO_NOTHING
# define if_debug7(c,s,a1,a2,a3,a4,a5,a6,a7) DO_NOTHING
# define if_debug8(c,s,a1,a2,a3,a4,a5,a6,a7,a8) DO_NOTHING
# define if_debug9(c,s,a1,a2,a3,a4,a5,a6,a7,a8,a9) DO_NOTHING
# define if_debug10(c,s,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10) DO_NOTHING
# define if_debug11(c,s,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11) DO_NOTHING
# define if_debug12(c,s,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12) DO_NOTHING
/* to the one that all the compilers seem to have.... */
#ifndef min
# define min(a, b) (((a) < (b)) ? (a) : (b))
#endif
#ifndef max
# define max(a, b) (((a) > (b)) ? (a) : (b))
#endif
/* Define a standard way to round values to a (constant) modulus. */
#define ROUND_DOWN(value, modulus)\
( (modulus) & ((modulus) - 1) ? /* not a power of 2 */\
(value) - (value) % (modulus) :\
(value) & -(modulus) )
#define ROUND_UP(value, modulus)\
( (modulus) & ((modulus) - 1) ? /* not a power of 2 */\
((value) + ((modulus) - 1)) / (modulus) * (modulus) :\
((value) + ((modulus) - 1)) & -(modulus) )
#define countof(a) (sizeof(a) / sizeof((a)[0]))
#define gs_alloc_bytes(a, n, c) ss->xalloc_(n)
#define gs_free_object(a, ptr, c) ss->free_(ptr)
#endif /* gstypes.h */
/* end of former misc_types.h */
/* gsbittab.h */
/*$Id: pts_fax.c,v 1.3 2005/02/21 13:09:56 pts Exp $ */
/* Interface to tables for bit operations */
#ifndef gsbittab_INCLUDED
# define gsbittab_INCLUDED
/*
* Generate tables for transforming 2, 4, 6, or 8 bits.
*/
#define btab2_(v0,v2,v1)\
v0,v1+v0,v2+v0,v2+v1+v0
#define bit_table_2(v0,v2,v1) btab2_(v0,v2,v1)
#define btab4_(v0,v8,v4,v2,v1)\
btab2_(v0,v2,v1), btab2_(v4+v0,v2,v1),\
btab2_(v8+v0,v2,v1), btab2_(v8+v4+v0,v2,v1)
#define bit_table_4(v0,v8,v4,v2,v1) btab4_(v0,v8,v4,v2,v1)
#define btab6_(v0,v20,v10,v8,v4,v2,v1)\
btab4_(v0,v8,v4,v2,v1), btab4_(v10+v0,v8,v4,v2,v1),\
btab4_(v20+v0,v8,v4,v2,v1), btab4_(v20+v10+v0,v8,v4,v2,v1)
#define bit_table_6(v0,v20,v10,v8,v4,v2,v1) btab6_(v0,v20,v10,v8,v4,v2,v1)
#define bit_table_8(v0,v80,v40,v20,v10,v8,v4,v2,v1)\
btab6_(v0,v20,v10,v8,v4,v2,v1), btab6_(v40+v0,v20,v10,v8,v4,v2,v1),\
btab6_(v80+v0,v20,v10,v8,v4,v2,v1), btab6_(v80+v40+v0,v20,v10,v8,v4,v2,v1)
/*
* byte_reverse_bits[B] = the unsigned char B with the order of bits reversed.
*/
intern_const unsigned char byte_reverse_bits[256];
/*
* byte_right_mask[N] = a unsigned char with N trailing 1s, 0 <= N <= 8.
*/
intern_const unsigned char byte_right_mask[9];
/*
* byte_count_bits[B] = the number of 1-bits in a unsigned char with value B.
*/
intern_const unsigned char byte_count_bits[256];
/*
* byte_bit_run_length_N[B], for 0 <= N <= 7, gives the length of the
* run of 1-bits starting at bit N in a unsigned char with value B,
* numbering the bits in the unsigned char as 01234567. If the run includes
* the low-order bit (i.e., might be continued into a following unsigned char),
* the run length is increased by 8.
*/
intern_const unsigned char
byte_bit_run_length_0[256], byte_bit_run_length_1[256],
byte_bit_run_length_2[256], byte_bit_run_length_3[256],
byte_bit_run_length_4[256], byte_bit_run_length_5[256],
byte_bit_run_length_6[256], byte_bit_run_length_7[256];
/*
* byte_bit_run_length[N] points to byte_bit_run_length_N.
* byte_bit_run_length_neg[N] = byte_bit_run_length[-N & 7].
*/
intern_const unsigned char *const byte_bit_run_length[8];
intern_const unsigned char *const byte_bit_run_length_neg[8];
/*
* byte_acegbdfh_to_abcdefgh[acegbdfh] = abcdefgh, where the letters
* denote the individual bits of the unsigned char.
*/
intern_const unsigned char byte_acegbdfh_to_abcdefgh[256];
#endif /* gsbittab_INCLUDED */
/* end of former gsbittab.h */
/* shc.h */
/*$Id: pts_fax.c,v 1.3 2005/02/21 13:09:56 pts Exp $ */
/* Common definitions for filters using Huffman coding */
#ifndef shc_INCLUDED
# define shc_INCLUDED
/* #include "gsbittab.h" */
#define hc_bits_size (SIZEOF_INT * 8)
#define s_hce_init_inline(ss)\
((ss)->bits = 0, (ss)->bits_left = hc_bits_size)
#define s_hcd_init_inline(ss)\
((ss)->bits = 0, (ss)->bits_left = 0)
/*
* These definitions are valid for code lengths up to 16 bits
* and non-negative decoded values up to 15 bits.
*
* We define 3 different representations of the code: encoding tables,
* decoding tables, and a definition table which can be generated easily
* from frequency information and which in turn can easily generate
* the encoding and decoding tables.
*
* The definition table has two parts: a list of the number of i-bit
* codes for each i >= 1, and the decoded values corresponding to
* the code values in increasing lexicographic order (which will also
* normally be decreasing code frequency). Calling these two lists
* L[1..M] and V[0..N-1] respectively, we have the following invariants:
* - 1 <= M <= max_hc_length, N >= 2.
* - L[0] = 0.
* - for i=1..M, L[i] >= 0.
* - sum(i=1..M: L[i]) = N.
* - sum(i=1..M: L[i] * 2^-i) = 1.
* - V[0..N-1] are a permutation of the integers 0..N-1.
*/
#define max_hc_length 16
typedef struct hc_definition_s {
unsigned short *counts; /* [0..M] */
unsigned int num_counts; /* M */
unsigned short *values; /* [0..N-1] */
unsigned int num_values; /* N */
} hc_definition;
/**** pts ****/
struct stream_hc_state_s;
/* definition moved to pts_fax.h */
/* ------ Encoding tables ------ */
/* Define the structure for the encoding tables. */
typedef struct hce_code_s {
unsigned short code;
unsigned short code_length;
} hce_code;
#define hce_entry(c, len) { c, len }
typedef struct hce_table_s {
unsigned int count;
hce_code *codes;
} hce_table;
#define hce_bits_available(n)\
(ss->bits_left >= (n) || wlimit - q > ((n) - ss->bits_left - 1) >> 3)
/* ------ Encoding utilities ------ */
/*
* Put a code on the output. The client is responsible for ensuring
* that q does not exceed pw->limit.
*/
#ifdef DEBUG
# define hc_print_value(code, clen)\
(gs_debug_c('W') ?\
(dlprintf2("[W]0x%x,%d\n", code, clen), 0) : 0)
# define hc_print_value_then(code, clen) hc_print_value(code, clen),
#else
# define hc_print_value(code, clen) 0
# define hc_print_value_then(code, clen) /* */
#endif
#define hc_print_code(rp) hc_print_value((rp)->code, (rp)->code_length)
/* Declare variables that hold the encoder state. */
#define hce_declare_state\
register unsigned int bits;\
register int bits_left
/* Load the state from the stream. */
/* Free variables: ss, bits, bits_left. */
#define hce_load_state()\
bits = ss->bits, bits_left = ss->bits_left
/* Store the state back in the stream. */
/* Free variables: ss, bits, bits_left. */
#define hce_store_state()\
ss->bits = bits, ss->bits_left = bits_left
/* Put a code on the stream. */
intern void hc_put_code_proc _((bool, unsigned char *, unsigned int));
#define hc_put_value(ss, q, code, clen)\
(hc_print_value_then(code, clen)\
((bits_left -= (clen)) >= 0 ?\
(bits += (code) << bits_left) :\
(hc_put_code_proc((ss)->FirstBitLowOrder,\
q += hc_bits_size >> 3,\
(bits + ((code) >> -bits_left))),\
bits = (code) << (bits_left += hc_bits_size))))
#define hc_put_code(ss, q, cp)\
hc_put_value(ss, q, (cp)->code, (cp)->code_length)
/*
* Force out the final bits to the output.
* Note that this does a store_state, but not a load_state.
*/
intern unsigned char *hc_put_last_bits_proc _((struct stream_hc_state_s *, unsigned char *, unsigned int, int));
#define hc_put_last_bits(ss, q)\
hc_put_last_bits_proc(ss, q, bits, bits_left)
/* ------ Decoding tables ------ */
/*
* Define the structure for the decoding tables.
* First-level nodes are either leaves, which have
* value = decoded value
* code_length <= initial_bits
* or non-leaves, which have
* value = the index of a sub-table
* code_length = initial_bits + the number of additional dispatch bits
* Second-level nodes are always leaves, with
* code_length = the actual number of bits in the code - initial_bits.
*/
typedef struct hcd_code_s {
short value;
unsigned short code_length;
} hcd_code;
typedef struct hcd_table_s {
unsigned int count;
unsigned int initial_bits;
hcd_code *codes;
} hcd_table;
/* Declare variables that hold the decoder state. */
#define hcd_declare_state\
register const unsigned char *p;\
const unsigned char *rlimit;\
unsigned int bits;\
int bits_left
/* Load the state from the stream. */
/* Free variables: pr, ss, p, rlimit, bits, bits_left. */
#define hcd_load_state()\
p = pr->ptr,\
rlimit = pr->limit,\
bits = ss->bits,\
bits_left = ss->bits_left
/* Store the state back in the stream. */
/* Put back any complete bytes into the input buffer. */
/* Free variables: pr, ss, p, bits, bits_left. */
#define hcd_store_state()\
pr->ptr = p -= (bits_left >> 3),\
ss->bits = bits >>= (bits_left & ~7),\
ss->bits_left = bits_left &= 7
/* Macros to get blocks of bits from the input stream. */
/* Invariants: 0 <= bits_left <= bits_size; */
/* bits [bits_left-1..0] contain valid data. */
#define hcd_bits_available(n)\
(bits_left >= (n) || rlimit - p > ((n) - bits_left - 1) >> 3)
/* For hcd_ensure_bits, n must not be greater than 8. */
#define HCD_ENSURE_BITS_ELSE(n)\
if (bits_left >= n)\
DO_NOTHING;\
else HCD_MORE_BITS_ELSE
#define hcd_ensure_bits(n, outl)\
BEGIN HCD_ENSURE_BITS_ELSE(n) goto outl; END
/* Load more bits into the buffer. */
#define HCD_MORE_BITS_1_ELSE\
if (p < rlimit) {\
int c = *++p;\
\
if (ss->FirstBitLowOrder)\
c = byte_reverse_bits[c];\
bits = (bits << 8) + c, bits_left += 8;\
} else
#if hc_bits_size == 16
# define HCD_MORE_BITS_ELSE HCD_MORE_BITS_1_ELSE
#else /* hc_bits_size >= 32 */
# define HCD_MORE_BITS_ELSE\
if (rlimit - p >= 3) {\
if (ss->FirstBitLowOrder)\
bits = (bits << 24) + ((unsigned int)byte_reverse_bits[p[1]] << 16) + ((unsigned int)byte_reverse_bits[p[2]] << 8) + byte_reverse_bits[p[3]];\
else\
bits = (bits << 24) + ((unsigned int)p[1] << 16) + ((unsigned int)p[2] << 8) + p[3];\
bits_left += 24, p += 3;\
} else HCD_MORE_BITS_1_ELSE
#endif
#define hcd_more_bits(outl)\
BEGIN HCD_MORE_BITS_ELSE goto outl; END
#define hcd_peek_bits(n) ((bits >> (bits_left - (n))) & ((1 << (n)) - 1))
/* hcd_peek_var_bits requires bits_left <= 8. */
#define hcd_peek_var_bits(n)\
((bits >> (bits_left - (n))) & byte_right_mask[n])
/* hcd_peek_bits_left requires bits_left <= 8. */
#define hcd_peek_bits_left()\
(bits & byte_right_mask[bits_left])
#define hcd_skip_bits(n) (bits_left -= (n))
#endif /* shc_INCLUDED */
/* end of former shc.h */
/* scf.h */
/*$Id: pts_fax.c,v 1.3 2005/02/21 13:09:56 pts Exp $ */
/* Common definitions for CCITTFax encoding and decoding filters */
#ifndef scf_INCLUDED
# define scf_INCLUDED
/* #include "shc.h" */
/*
* The CCITT Group 3 (T.4) and Group 4 (T.6) fax specifications map
* run lengths to Huffman codes. White and black have different mappings.
* If the run length is 64 or greater, two or more codes are needed:
* - One or more 'make-up' codes for 2560 pixels;
* - A 'make-up' code that encodes the multiple of 64;
* - A 'termination' code for the remainder.
* For runs of 63 or less, only the 'termination' code is needed.
*/
/* ------ Encoding tables ------ */
/*
* The maximum possible length of a scan line is determined by the
* requirement that 3 runs have to fit into the stream buffer.
* A run of length N requires approximately ceil(N / 2560) makeup codes,
* hence 1.5 * ceil(N / 2560) bytes. Taking the largest safe stream
* buffer size as 32K, we arrive at the following maximum width:
*/
#if SIZEOF_INT > 2
# define cfe_max_width (2560 * 32000 * 2 / 3)
#else
# define cfe_max_width (max_int - 40) /* avoid overflows */
#endif
/* The +5 in cfe_max_code_bytes is a little conservative. */
#define cfe_max_code_bytes(width) ((width) / 2560 * 3 / 2 + 5)
typedef hce_code cfe_run;
/* Codes common to 1-D and 2-D encoding. */
/* The decoding algorithms know that EOL is 0....01. */
#define run_eol_code_length 12
#define run_eol_code_value 1
intern_const cfe_run cf_run_eol;
typedef struct cf_runs_s {
cfe_run termination[64];
cfe_run make_up[41];
} cf_runs;
intern_const cf_runs
cf_white_runs, cf_black_runs;
intern_const cfe_run cf_uncompressed[6];
intern_const cfe_run cf_uncompressed_exit[10]; /* indexed by 2 x length of */
/* white run + (1 if next run black, 0 if white) */
/* 1-D encoding. */
intern_const cfe_run cf1_run_uncompressed;
/* 2-D encoding. */
intern_const cfe_run cf2_run_pass;
#define cf2_run_pass_length 4
#define cf2_run_pass_value 0x1
#define cf2_run_vertical_offset 3
intern_const cfe_run cf2_run_vertical[7]; /* indexed by b1 - a1 + offset */
intern_const cfe_run cf2_run_horizontal;
#define cf2_run_horizontal_value 1
#define cf2_run_horizontal_length 3
intern_const cfe_run cf2_run_uncompressed;
/* 2-D Group 3 encoding. */
intern_const cfe_run cf2_run_eol_1d;
intern_const cfe_run cf2_run_eol_2d;
/* ------ Decoding tables ------ */
typedef hcd_code cfd_node;
#define run_length value
/*
* The value in the decoding tables is either a white or black run length,
* or a (negative) exceptional value.
*/
#define run_error (-1)
#define run_zeros (-2) /* EOL follows, possibly with more padding first */
#define run_uncompressed (-3)
/* 2-D codes */
#define run2_pass (-4)
#define run2_horizontal (-5)
#define cfd_white_initial_bits 8
#define cfd_white_min_bits 4 /* shortest white run */
/* intern_const cfd_node cf_white_decode[]; */
#define cfd_black_initial_bits 7
#define cfd_black_min_bits 2 /* shortest black run */
/* intern_const cfd_node cf_black_decode[]; */
#define cfd_2d_initial_bits 7
#define cfd_2d_min_bits 4 /* shortest non-H/V 2-D run */
/* intern_const cfd_node cf_2d_decode[]; */
#define cfd_uncompressed_initial_bits 6 /* must be 6 */
/* intern_const cfd_node cf_uncompressed_decode[]; */
/* ------ Run detection macros ------ */
/*
* For the run detection macros:
* white_byte is 0 or 0xff for BlackIs1 or !BlackIs1 respectively;
* data holds p[-1], inverted if !BlackIs1;
* count is the number of valid bits remaining in the scan line.
*/
/* Aliases for bit processing tables. */
#define cf_byte_run_length byte_bit_run_length_neg
#define cf_byte_run_length_0 byte_bit_run_length_0
/* Skip over white pixels to find the next black pixel in the input. */
/* Store the run length in rlen, and update data, p, and count. */
/* There are many more white pixels in typical input than black pixels, */
/* and the runs of white pixels tend to be much longer, so we use */
/* substantially different loops for the two cases. */
#define skip_white_pixels(data, p, count, white_byte, rlen)\
BEGIN\
rlen = cf_byte_run_length[count & 7][data ^ 0xff];\
if ( rlen >= 8 ) { /* run extends past unsigned char boundary */\
if ( white_byte == 0 ) {\
if ( p[0] ) { data = p[0]; p += 1; rlen -= 8; }\
else if ( p[1] ) { data = p[1]; p += 2; }\
else {\
while ( !(p[2] | p[3] | p[4] | p[5]) )\
p += 4, rlen += 32;\
if ( p[2] ) {\
data = p[2]; p += 3; rlen += 8;\
} else if ( p[3] ) {\
data = p[3]; p += 4; rlen += 16;\
} else if ( p[4] ) {\
data = p[4]; p += 5; rlen += 24;\
} else /* p[5] */ {\
data = p[5]; p += 6; rlen += 32;\
}\
}\
} else {\
if ( p[0] != 0xff ) { data = (unsigned char)~p[0]; p += 1; rlen -= 8; }\
else if ( p[1] != 0xff ) { data = (unsigned char)~p[1]; p += 2; }\
else {\
while ( (p[2] & p[3] & p[4] & p[5]) == 0xff )\
p += 4, rlen += 32;\
if ( p[2] != 0xff ) {\
data = (unsigned char)~p[2]; p += 3; rlen += 8;\
} else if ( p[3] != 0xff ) {\
data = (unsigned char)~p[3]; p += 4; rlen += 16;\
} else if ( p[4] != 0xff ) {\
data = (unsigned char)~p[4]; p += 5; rlen += 24;\
} else /* p[5] != 0xff */ {\
data = (unsigned char)~p[5]; p += 6; rlen += 32;\
}\
}\
}\
rlen += cf_byte_run_length_0[data ^ 0xff];\
}\
count -= rlen;\
END
/* Skip over black pixels to find the next white pixel in the input. */
/* Store the run length in rlen, and update data, p, and count. */
#define skip_black_pixels(data, p, count, white_byte, rlen)\
BEGIN\
rlen = cf_byte_run_length[count & 7][data];\
if ( rlen >= 8 ) {\
if ( white_byte == 0 )\
for ( ; ; p += 4, rlen += 32 ) {\
if ( p[0] != 0xff ) { data = p[0]; p += 1; rlen -= 8; break; }\
if ( p[1] != 0xff ) { data = p[1]; p += 2; break; }\
if ( p[2] != 0xff ) { data = p[2]; p += 3; rlen += 8; break; }\
if ( p[3] != 0xff ) { data = p[3]; p += 4; rlen += 16; break; }\
}\
else\
for ( ; ; p += 4, rlen += 32 ) {\
if ( p[0] ) { data = (unsigned char)~p[0]; p += 1; rlen -= 8; break; }\
if ( p[1] ) { data = (unsigned char)~p[1]; p += 2; break; }\
if ( p[2] ) { data = (unsigned char)~p[2]; p += 3; rlen += 8; break; }\
if ( p[3] ) { data = (unsigned char)~p[3]; p += 4; rlen += 16; break; }\
}\
rlen += cf_byte_run_length_0[data];\
}\
count -= rlen;\
END
#endif /* scf_INCLUDED */
/* end of former scf.h */
#if USE_BUILTIN_FAXE
#if OBJDEP
# warning PROVIDES: pts_faxe
#endif
/* scfe.c */
/*$Id: pts_fax.c,v 1.3 2005/02/21 13:09:56 pts Exp $ */
/* CCITTFax encoding filter */
/* #include "config2.h" */
/* #include "scf.h" */
/* #include "scfx.h" */
/* ------ Macros and support routines ------ */
/* Statistics */
#ifdef DEBUG
typedef struct stats_runs_s {
unsigned long termination[64];
unsigned long make_up[41];
} stats_runs_t;
static stats_runs_t stats_white_runs, stats_black_runs;
#define COUNT_RUN(tab, i) (tab)[i]++;
static void
print_run_stats(const stats_runs_t * stats)
{
int i;
unsigned long total;
for (i = 0, total = 0; i < 41; i++)
dprintf1(" %lu", stats->make_up[i]),
total += stats->make_up[i];
dprintf1(" total=%lu\n\t", total);
for (i = 0, total = 0; i < 64; i++)
dprintf1(" %lu", stats->termination[i]),
total += stats->termination[i];
dprintf1(" total=%lu\n", total);
}
#else /* !DEBUG */
#define COUNT_RUN(cnt, i) DO_NOTHING
#endif /* DEBUG */
/* Put a run onto the output stream. */
/* Free variables: q, bits, bits_left. */
#define CF_PUT_RUN(ss, lenv, rt, stats)\
BEGIN\
cfe_run rr;\
\
if ( lenv >= 64 ) {\
hce_store_state();\
q = cf_put_long_run(ss, q, lenv, &rt);\
hce_load_state();\
lenv &= 63;\
}\
rr = rt.termination[lenv];\
COUNT_RUN(stats.termination, lenv);\
hc_put_value(ss, q, rr.code, rr.code_length);\
END
static unsigned char *
cf_put_long_run(stream_CFE_state * ss, unsigned char * q, int lenv, const cf_runs * prt)
{
hce_declare_state;
cfe_run rr;
#ifdef DEBUG
stats_runs_t *pstats =
(prt == &cf_white_runs ? &stats_white_runs : &stats_black_runs);
#endif
hce_load_state();
while (lenv >= 2560 + 64) {
rr = prt->make_up[40];
COUNT_RUN(pstats->make_up, 40);
hc_put_value(ss, q, rr.code, rr.code_length);
lenv -= 2560;
}
rr = prt->make_up[lenv >> 6];
COUNT_RUN(pstats->make_up, lenv >> 6);
hc_put_value(ss, q, rr.code, rr.code_length);
hce_store_state();
return q;
}
#define CF_PUT_WHITE_RUN(ss, lenv)\
CF_PUT_RUN(ss, lenv, cf_white_runs, stats_white_runs)
#define CF_PUT_BLACK_RUN(ss, lenv)\
CF_PUT_RUN(ss, lenv, cf_black_runs, stats_black_runs)
/* ------ CCITTFaxEncode ------ */
/* private_st_CFE_state(); */
static void s_CFE_release _((stream_state *));
/* Set default parameter values. */
static void
s_CFE_set_defaults(register stream_state * st)
{
stream_CFE_state *const ss = (stream_CFE_state *) st;
s_CFE_set_defaults_inline(ss);
}
/* Initialize CCITTFaxEncode filter */
static int
s_CFE_init(register stream_state * st)
{
stream_CFE_state *const ss = (stream_CFE_state *) st;
int columns = ss->Columns;
/*
* The worst case for encoding is alternating white and black pixels.
* For 1-D encoding, the worst case is 9 bits per 2 pixels; for 2-D
* (horizontal), 12 bits per 2 pixels. To fill out a scan line,
* we may add up to 6 12-bit EOL codes.
*/
/**** pts: added UL ****/
int code_bytes =
((columns * (ss->K == 0 ? 9UL : 12UL)) >> 4) + 20; /* add slop */
int raster = ss->raster =
ROUND_UP((columns + 7) >> 3, ss->DecodedByteAlign);
s_hce_init_inline(ss);
ss->lbuf = ss->lprev = ss->lcode = 0; /* in case we have to release */
if (columns > cfe_max_width)
return PTSFAX_ERRC;
/****** WRONG ******/
/* Because skip_white_pixels can look as many as 4 bytes ahead, */
/* we need to allow 4 extra bytes at the end of the row buffers. */
ss->lbuf = (unsigned char*)gs_alloc_bytes(st->memory, raster + 4, "CFE lbuf");
ss->lcode = (unsigned char*)gs_alloc_bytes(st->memory, code_bytes, "CFE lcode");
if (ss->lbuf == 0 || ss->lcode == 0) {
s_CFE_release(st);
return PTSFAX_ERRC;
/****** WRONG ******/
}
if (ss->K != 0) {
ss->lprev = (unsigned char*)gs_alloc_bytes(st->memory, raster + 4, "CFE lprev");
if (ss->lprev == 0) {
s_CFE_release(st);
return PTSFAX_ERRC;
/****** WRONG ******/
}
/* Clear the initial reference line for 2-D encoding. */
/* Make sure it is terminated properly. */
ss->memset_(ss->lprev, (ss->BlackIs1 ? 0 : 0xff), raster);
if (columns & 7)
ss->lprev[raster - 1] ^= 0x80 >> (columns & 7);
else
ss->lprev[raster] = ~ss->lprev[0];
}
ss->read_count = raster;
ss->write_count = 0;
ss->k_left = (ss->K > 0 ? 1 : ss->K);
ss->max_code_bytes = code_bytes;
return 0;
}
/* Release the filter. */
static void
s_CFE_release(stream_state * st)
{
stream_CFE_state *const ss = (stream_CFE_state *) st;
gs_free_object(st->memory, ss->lprev, "CFE lprev(close)");
gs_free_object(st->memory, ss->lcode, "CFE lcode(close)");
gs_free_object(st->memory, ss->lbuf, "CFE lbuf(close)");
}
/* Flush the buffer */
static void cf_encode_1d _((stream_CFE_state *, const unsigned char *,
stream_cursor_write *));
static void cf_encode_2d _((stream_CFE_state *, const unsigned char *,
stream_cursor_write *, const unsigned char *));
static int
s_CFE_process(stream_state * st, stream_cursor_read * pr,
stream_cursor_write * pw, bool last)
{
stream_CFE_state *const ss = (stream_CFE_state *) st;
const unsigned char *rlimit = pr->limit;
unsigned char *wlimit = pw->limit;
int raster = ss->raster;
unsigned char end_mask = 1 << (-ss->Columns & 7);
int status = 0;
for (;;) {
stream_cursor_write w;
if_debug2('w', "[w]CFE: read_count = %d, write_count=%d,\n",
ss->read_count, ss->write_count);
if_debug6('w', " pr = 0x%lx(%d)0x%lx, pw = 0x%lx(%d)0x%lx\n",
(unsigned long) pr->ptr, (int)(rlimit - pr->ptr), (unsigned long) rlimit,
(unsigned long) pw->ptr, (int)(wlimit - pw->ptr), (unsigned long) wlimit);
if (ss->write_count) {
/* Copy more of an encoded line to the caller. */
int wcount = wlimit - pw->ptr;
int ccount = min(wcount, ss->write_count);
ss->memcpy_(pw->ptr + 1, ss->lcode + ss->code_bytes - ss->write_count,
ccount);
pw->ptr += ccount;
if ((ss->write_count -= ccount) > 0) {
status = 1;
break;
}
}
if (ss->read_count) {
/* Copy more of an unencoded line from the caller. */
int rcount = rlimit - pr->ptr;
int ccount = min(rcount, ss->read_count);
if (rcount == 0 && last)
break;
ss->memcpy_(ss->lbuf + raster - ss->read_count,
pr->ptr + 1, ccount);
pr->ptr += ccount;
if ((ss->read_count -= ccount) != 0)
break;
}
/*
* We have a full scan line in lbuf. Ensure that it ends with
* two polarity changes.
*/
{
unsigned char *end = ss->lbuf + raster - 1;
unsigned char end_bit = *end & end_mask;
unsigned char not_bit = end_bit ^ end_mask;
*end &= -end_mask;
if (end_mask == 1)
end[1] = (end_bit ? 0x40 : 0x80);
else if (end_mask == 2)
*end |= not_bit >> 1, end[1] = end_bit << 7;
else
*end |= (not_bit >> 1) | (end_bit >> 2);
}
/*
* Write the output directly to the caller's buffer if it's large
* enough, otherwise to our own buffer.
*/
if (wlimit - pw->ptr >= ss->max_code_bytes) {
w = *pw;
} else {
w.ptr = ss->lcode - 1;
w.limit = w.ptr + ss->max_code_bytes;
}
#ifdef DEBUG
if (ss->K > 0) {
if_debug1('w', "[w]new row, k_left=%d\n",
ss->k_left);
} else {
if_debug0('w', "[w]new row\n");
}
#endif
/*
* Write an EOL (actually a "beginning of line") if requested.
*/
if (ss->EndOfLine) {
const cfe_run *rp =
(ss->K <= 0 ? &cf_run_eol :
ss->k_left > 1 ? &cf2_run_eol_2d :
&cf2_run_eol_1d);
cfe_run run;
hce_declare_state;
hce_load_state();
if (ss->EncodedByteAlign) {
run = *rp;
/* Pad the run on the left */
/* so it winds up unsigned char-aligned. */
run.code_length +=
(bits_left - run_eol_code_length) & 7;
if (run.code_length > 16) /* <= 23 */
bits_left -= run.code_length & 7,
run.code_length = 16;
rp = &run;
}
hc_put_code(ss, w.ptr, rp);
hce_store_state();
} else if (ss->EncodedByteAlign)
ss->bits_left &= ~7;
/* Encode the line. */
if (ss->K == 0)
cf_encode_1d(ss, ss->lbuf, &w); /* pure 1-D */
else if (ss->K < 0)
cf_encode_2d(ss, ss->lbuf, &w, ss->lprev); /* pure 2-D */
else if (--(ss->k_left)) /* mixed, use 2-D */
cf_encode_2d(ss, ss->lbuf, &w, ss->lprev);
else { /* mixed, use 1-D */
cf_encode_1d(ss, ss->lbuf, &w);
ss->k_left = ss->K;
}
/*
* If we didn't write directly to the client's buffer, schedule
* the output data to be written.
*/
if (w.limit == wlimit)
pw->ptr = w.ptr;
else
ss->write_count = ss->code_bytes = w.ptr - (ss->lcode - 1);
if (ss->K != 0) {
/* In 2-D modes, swap the current and previous scan lines. */
unsigned char *temp = ss->lbuf;
ss->lbuf = ss->lprev;
ss->lprev = temp;
}
/* Note that the input buffer needs refilling. */
ss->read_count = raster;
}
/*
* When we exit from the loop, we know that write_count = 0, and
* there is no line waiting to be processed in the input buffer.
*/
if (last && status == 0) {
const cfe_run *rp =
(ss->K > 0 ? &cf2_run_eol_1d : &cf_run_eol);
int i = (!ss->EndOfBlock ? 0 : ss->K < 0 ? 2 : 6);
unsigned int bits_to_write =
hc_bits_size - ss->bits_left + i * rp->code_length;
unsigned char *q = pw->ptr;
hce_declare_state;
if (0U+(wlimit - q) < (bits_to_write + 7) >> 3) { /* PTS_UNSIGNED */
status = 1;
goto out;
}
hce_load_state();
if (ss->EncodedByteAlign)
bits_left &= ~7;
while (--i >= 0)
hc_put_code(ss, q, rp);
/* Force out the last unsigned char or bytes. */
pw->ptr = hc_put_last_bits((stream_hc_state *) ss, q);
}
out:
if_debug9('w', "[w]CFE exit %d: read_count = %d, write_count = %d,\n pr = 0x%lx(%d)0x%lx; pw = 0x%lx(%d)0x%lx\n",
status, ss->read_count, ss->write_count,
(unsigned long) pr->ptr, (int)(rlimit - pr->ptr), (unsigned long) rlimit,
(unsigned long) pw->ptr, (int)(wlimit - pw->ptr), (unsigned long) wlimit);
#ifdef DEBUG
if (pr->ptr > rlimit || pw->ptr > wlimit) {
lprintf("Pointer overrun!\n");
status = PTSFAX_ERRC;
}
if (gs_debug_c('w') && status == 1) {
dlputs("[w]white runs:");
print_run_stats(&stats_white_runs);
dlputs("[w]black runs:");
print_run_stats(&stats_black_runs);
}
#endif
return status;
}
/* Encode a 1-D scan line. */
static void
cf_encode_1d(stream_CFE_state * ss, const unsigned char * lbuf, stream_cursor_write * pw)
{
unsigned int count = ss->raster << 3;
unsigned char *q = pw->ptr;
int end_count = -ss->Columns & 7;
int rlen;
hce_declare_state;
const unsigned char *p = lbuf;
unsigned char invert = (ss->BlackIs1 ? 0 : 0xff);
/* Invariant: data = p[-1] ^ invert. */
unsigned int data = *p++ ^ invert;
hce_load_state();
while (count != 0U+end_count) { /* PTS_UNSIGNED */
/* Parse a white run. */
skip_white_pixels(data, p, count, invert, rlen);
CF_PUT_WHITE_RUN(ss, rlen);
if (count == 0U+end_count) /* PTS_UNSIGNED */
break;
/* Parse a black run. */
skip_black_pixels(data, p, count, invert, rlen);
CF_PUT_BLACK_RUN(ss, rlen);
}
hce_store_state();
pw->ptr = q;
}
/* Encode a 2-D scan line. */
static void
cf_encode_2d(stream_CFE_state * ss, const unsigned char * lbuf, stream_cursor_write * pw,
const unsigned char * lprev)
{
unsigned char invert_white = (ss->BlackIs1 ? 0 : 0xff);
unsigned char invert = invert_white;
unsigned int count = ss->raster << 3;
int end_count = -ss->Columns & 7;
const unsigned char *p = lbuf;
unsigned char *q = pw->ptr;
unsigned int data = *p++ ^ invert;
hce_declare_state;