-
Notifications
You must be signed in to change notification settings - Fork 28
/
pdfcodec.ml
1678 lines (1606 loc) · 64.3 KB
/
pdfcodec.ml
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
open Pdfutil
open Pdfio
let debug = ref false
(* Zlib inflate level *)
let flate_level = ref 6
(* Get the next non-whitespace character in a stream. *)
let rec get_streamchar s =
match s.input_byte () with
| x when x = Pdfio.no_more ->
raise End_of_file
| x ->
let chr = Char.unsafe_chr x in
if Pdf.is_whitespace chr then get_streamchar s else chr
(* Raised if there was bad data. *)
exception Couldn'tDecodeStream of string
(* Raised if the codec was not supported. *)
exception DecodeNotSupported of string
(* ASCIIHex *)
(* We build a list of decoded characters from the input stream, and then
convert this to the output stream. *)
let encode_ASCIIHex stream =
let size = bytes_size stream in
let stream' = mkbytes (size * 2 + 1) in
bset stream' (size * 2) (int_of_char '>'); (* ['>'] is end-of-data *)
for p = 0 to size - 1 do
let chars = explode (Printf.sprintf "%02X" (bget stream p)) in
bset stream' (p * 2) (int_of_char (hd chars));
bset stream' (p * 2 + 1) (int_of_char (hd (tl chars)))
done;
stream'
(* Decode ASCIIHex *)
(* Calulate a character from two hex digits a and b *)
let char_of_hex a b =
char_of_int (int_of_string ("0x" ^ string_of_char a ^ string_of_char b))
let decode_ASCIIHex i =
let output = ref []
in let enddata = ref false in
try
while not !enddata do
let b = get_streamchar i in
let b' = get_streamchar i in
match b, b' with
| '>', _ ->
(* Rewind for inline images *)
set enddata; rewind i
| c, '>'
when
(c >= '0' && c <= '9') ||
(c >= 'a' && c <= 'f') ||
(c >= 'A' && c <= 'F')
->
output =| char_of_hex c '0';
set enddata
| c, c'
when
((c >= '0' && c <= '9') ||
(c >= 'a' && c <= 'f') ||
(c >= 'A' && c <= 'F'))
&&
((c' >= '0' && c' <= '9') ||
(c' >= 'a' && c' <= 'f') ||
(c' >= 'A' && c' <= 'F'))
->
output =| char_of_hex c c'
| _ -> raise Not_found (*r Bad data. *)
done;
bytes_of_charlist (rev !output)
with
| End_of_file ->
(* We ran out of data. This is a normal exit. *)
bytes_of_charlist (rev !output)
| Not_found ->
raise (Couldn'tDecodeStream "ASCIIHex")
(* ASCII85 *)
let decode_5bytes c1 c2 c3 c4 c5 n o =
let d x p = i32mul (i32ofi (int_of_char x - 33)) (i32ofi (pow p 85)) in
let total = i32add (i32add (d c1 4) (d c2 3)) (i32add (d c3 2) (i32add (d c4 1) (d c5 0))) in
let extract t = char_of_int (i32toi (lsr32 (lsl32 total (24 - t)) 24)) in
match n with
| 4 -> extract 0::extract 8::extract 16::extract 24::o
| 3 -> extract 8::extract 16::extract 24::o
| 2 -> extract 16::extract 24::o
| 1 -> extract 24::o
| _ -> o
let conso cs o =
match cs with
| [c1; c2; c3; c4; c5] -> decode_5bytes c1 c2 c3 c4 c5 4 o
| [c1; c2; c3; c4] -> decode_5bytes c1 c2 c3 c4 '~' 3 o
| [c1; c2; c3] -> decode_5bytes c1 c2 c3 '~' '>' 2 o
| [c1; c2] -> decode_5bytes c1 c2 '~' '>' '!' 1 o
| _ -> o
let rec decode_ASCII85_i i cs o =
match get_streamchar i with
| 'z' ->
decode_ASCII85_i i [] ('\000'::'\000'::'\000'::'\000'::conso (rev cs) o)
| '~' ->
bytes_of_charlist (rev (conso (rev cs) o))
| c when c >= '!' && c <= 'u' ->
if length cs = 5
then (decode_ASCII85_i i [c] (conso (rev cs) o))
else (decode_ASCII85_i i (c::cs) o)
| _ ->
raise (Pdf.PDFError "decode_ASCII85_i")
let decode_ASCII85 i =
try decode_ASCII85_i i [] [] with
_ -> raise (Pdf.PDFError "Error decoding ASCII85 stream")
(* Encode a single symbol set. *)
let encode_4bytes = function
| [b1; b2; b3; b4] ->
let ( * ), ( - ), ( / ), rem = Int64.mul, Int64.sub, Int64.div, Int64.rem in
let numbers =
[i64ofi (int_of_char b1) * i64ofi (pow 3 256);
i64ofi (int_of_char b2) * i64ofi (pow 2 256);
i64ofi (int_of_char b3) * i64ofi (pow 1 256);
i64ofi (int_of_char b4) * i64ofi (pow 0 256)]
in
let t = fold_left Int64.add Int64.zero numbers in
let one85 = i64ofi (pow 1 85) in
let two85 = i64ofi (pow 2 85) in
let three85 = i64ofi (pow 3 85) in
let zero85 = i64ofi (pow 0 85) in
let four85 = i64ofi (pow 4 85) in
let t, c5 = t - rem t one85, rem t one85 / zero85 in
let t, c4 = t - rem t two85, rem t two85 / one85 in
let t, c3 = t - rem t three85, rem t three85 / two85 in
let t, c2 = t - rem t four85, rem t four85 / three85 in
i64toi (t / four85), i64toi c2, i64toi c3, i64toi c4, i64toi c5
| _ -> assert false
(* Encode a stream. *)
let encode_ASCII85 stream =
let output = ref []
in let enddata = ref false
in let istream = input_of_bytes stream in
while not !enddata do
let b1 = istream.input_char () in
let b2 = istream.input_char () in
let b3 = istream.input_char () in
let b4 = istream.input_char () in
match b1, b2, b3, b4 with
| Some b1, Some b2, Some b3, Some b4 ->
output := [b1; b2; b3; b4]::!output
| Some b1, Some b2, Some b3, None ->
set enddata; output := [b1; b2; b3]::!output
| Some b1, Some b2, None, None ->
set enddata; output := [b1; b2]::!output
| Some b1, None, None, None ->
set enddata; output := [b1]::!output
| None, _, _, _ -> set enddata
| _ -> assert false
done;
let fix k = char_of_int (k + 33) in
let charlists' =
rev_map
(fun l ->
let len = length l in
if len < 4
then
let l' = l @ (many '\000' (4 - len)) in
let c1, c2, c3, c4, c5 = encode_4bytes l' in
take [fix c1; fix c2; fix c3; fix c4; fix c5] (len + 1)
else
let c1, c2, c3, c4, c5 = encode_4bytes l in
if c1 + c2 + c3 + c4 + c5 = 0
then ['z']
else [fix c1; fix c2; fix c3; fix c4; fix c5])
!output
in
bytes_of_charlist (flatten charlists' @ ['~'; '>'])
(* Flate *)
(* Make a bytes from a list of strings by taking the contents, in order
from the items, in order. *)
let rec total_length n = function
| [] -> n
| h::t -> total_length (n + String.length h) t
let bytes_of_strings_rev strings =
let len = total_length 0 strings in
let s = mkbytes len
and pos = ref (len - 1) in
iter
(fun str ->
for x = String.length str - 1 downto 0 do
bset_unsafe s !pos (int_of_char (String.unsafe_get str x));
decr pos
done)
strings;
s
let flate_process f data =
let strings = ref []
and pos = ref 0
and inlength = bytes_size data in
let input =
(fun buf ->
let s = Bytes.length buf in
let towrite = min (inlength - !pos) s in
for x = 0 to towrite - 1 do
Bytes.unsafe_set
buf x (Char.unsafe_chr (bget_unsafe data !pos));
incr pos
done;
towrite)
and output =
(fun buf length ->
if length > 0 then strings =| Bytes.sub_string buf 0 length)
in
f input output;
bytes_of_strings_rev !strings
(* This is for reading zlib compressed things in streams where we need to
commence lexing straightaway afterwards. And so, we can only supply one byte at
a time, continuing until zlib has finished uncompressing. *)
let decode_flate_input i =
let strings = ref [] in
let input =
(fun buf ->
let s = Bytes.length buf in
if s > 0 then
begin
match i.input_byte () with
| x when x = Pdfio.no_more -> raise End_of_file
| x -> Bytes.unsafe_set buf 0 (char_of_int x); 1
end
else 0)
and output =
(fun buf length ->
if length > 0 then strings =| Bytes.sub_string buf 0 length)
in
Pdfflate.uncompress input output;
bytes_of_strings_rev !strings
(* js_of_ocaml only *)
external camlpdf_caml_zlib_compress : string -> string = "camlpdf_caml_zlib_compress"
external camlpdf_caml_zlib_decompress : string -> string = "camlpdf_caml_zlib_decompress"
let is_js =
match Sys.backend_type with Sys.Other "js_of_ocaml" -> true | _ -> false
let encode_flate stream =
if is_js then
Pdfio.bytes_of_string (camlpdf_caml_zlib_compress (Pdfio.string_of_bytes stream))
else
flate_process (Pdfflate.compress ~level:!flate_level) stream
let debug_stream_serial = ref 0
let debug_stream s =
Pdfe.log "First 50 bytes\n";
for x = 0 to 50 do
Pdfe.log (Printf.sprintf "%C = %i\n" (char_of_int (bget s x)) (bget s x))
done
(*(* Write stream to current directory as <length>_<serial>.zlib *)
let name = string_of_int (bytes_size s) ^ "_" ^ string_of_int !debug_stream_serial ^ ".zlib" in
Pdfe.log (Printf.sprintf "Writing %s\n" name);
debug_stream_serial += 1;
let fh = open_out_bin name in
for x = 0 to bytes_size s - 1 do
output_byte fh (bget s x)
done;
close_out fh*)
let decode_flate stream =
if bytes_size stream = 0 then mkbytes 0 else (* Accept the empty stream. *)
try
if is_js then
Pdfio.bytes_of_string (camlpdf_caml_zlib_decompress (Pdfio.string_of_bytes stream))
else
flate_process Pdfflate.uncompress stream
with
Pdfflate.Error (a, b) ->
if !debug then debug_stream stream;
raise (Couldn'tDecodeStream ("Flate" ^ " " ^ a ^ " " ^ b ^ " length " ^ string_of_int (bytes_size stream)))
(* LZW *)
(* Decode LZW. *)
let decode_lzw early i =
let prefix_code = Array.make 8192 0
and append_character = Array.make 8192 0
and bit_count = ref 0
and bit_buffer = ref 0l
and endflush = ref 4
and code_length = ref 9
and next_code = ref 258
and new_code = ref 0
and old_code = ref 256
and character = ref 0 in
let rec decode_string code str =
if code > 255 then
decode_string prefix_code.(code) (append_character.(code)::str)
else
code::str
and input_code stream =
while !bit_count <= 24 do
let streambyte =
match stream.input_byte () with
| b when b = Pdfio.no_more ->
if !endflush = 0 then raise End_of_file else (decr endflush; 0)
| b -> b
in
bit_buffer :=
lor32 !bit_buffer (lsl32 (i32ofi streambyte) (24 - !bit_count));
bit_count += 8
done;
let result = Int32.to_int (lsr32 !bit_buffer (32 - !code_length)) in
bit_buffer := lsl32 !bit_buffer !code_length;
bit_count -= !code_length;
result
and strip_cleartable_codes stream =
while !old_code = 256 do
old_code := input_code stream
done
and reset_table () =
next_code := 258;
code_length := 9;
old_code := 256
in
bit_count := 0; bit_buffer := 0l;
endflush := 4; reset_table ();
let outstream, data = input_output_of_bytes 16034
and finished = ref false in
strip_cleartable_codes i;
match !old_code with
| 257 -> mkbytes 0
| _ ->
character := !old_code;
outstream.output_byte !old_code;
while not !finished do
new_code := input_code i;
match !new_code with
| 257 -> set finished
| 256 ->
reset_table ();
set_array prefix_code 0;
set_array append_character 0;
strip_cleartable_codes i;
character := !old_code;
outstream.output_byte !old_code
| _ ->
let chars =
if !new_code >= !next_code
then (decode_string !old_code []) @ [!character]
else decode_string !new_code []
in
character := hd chars;
iter outstream.output_byte chars;
prefix_code.(!next_code) <- !old_code;
append_character.(!next_code) <- !character;
incr next_code;
old_code := !new_code;
match !next_code + early with
| 512 | 1024 | 2048 -> incr code_length
| _ -> ()
done;
extract_bytes_from_input_output outstream data
(* CCITT *)
(* Decode a CCITT-encoded stream. Parameter names:
eol -- /EndOfLine
eba -- /EncodedByteAlign
eob -- /EndOfBlock
bone -- /BlackIs1
dra -- /DamagedRowsBeforeError
c -- /Columns
r -- /Rows
*)
let rec write_white_code = function
| -1 -> [0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 1]
| 0 -> [0; 0; 1; 1; 0; 1; 0; 1]
| 1 -> [0; 0; 0; 1; 1; 1]
| 2 -> [0; 1; 1; 1]
| 3 -> [1; 0; 0; 0]
| 4 -> [1; 0; 1; 1]
| 5 -> [1; 1; 0; 0]
| 6 -> [1; 1; 1; 0]
| 7 -> [1; 1; 1; 1]
| 8 -> [1; 0; 0; 1; 1]
| 9 -> [1; 0; 1; 0; 0]
| 10 -> [0; 0; 1; 1; 1]
| 11 -> [0; 1; 0; 0; 0]
| 12 -> [0; 0; 1; 0; 0; 0]
| 13 -> [0; 0; 0; 0; 1; 1]
| 14 -> [1; 1; 0; 1; 0; 0]
| 15 -> [1; 1; 0; 1; 0; 1]
| 16 -> [1; 0; 1; 0; 1; 0]
| 17 -> [1; 0; 1; 0; 1; 1]
| 18 -> [0; 1; 0; 0; 1; 1; 1]
| 19 -> [0; 0; 0; 1; 1; 0; 0]
| 20 -> [0; 0; 0; 1; 0; 0; 0]
| 21 -> [0; 0; 1; 0; 1; 1; 1]
| 22 -> [0; 0; 0; 0; 0; 1; 1]
| 23 -> [0; 0; 0; 0; 1; 0; 0]
| 24 -> [0; 1; 0; 1; 0; 0; 0]
| 25 -> [0; 1; 0; 1; 0; 1; 1]
| 26 -> [0; 0; 1; 0; 0; 1; 1]
| 27 -> [0; 1; 0; 0; 1; 0; 0]
| 28 -> [0; 0; 1; 1; 0; 0; 0]
| 29 -> [0; 0; 0; 0; 0; 0; 1; 0]
| 30 -> [0; 0; 0; 0; 0; 0; 1; 1]
| 31 -> [0; 0; 0; 1; 1; 0; 1; 0]
|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 1; 1; 1; 1; 1] @ write_white_code (n - 2560)
| n when n >= 2496 -> [0; 0; 0; 0; 0; 0; 0; 1; 1; 1; 1; 0] @ write_white_code (n - 2496)
| n when n >= 2432 -> [0; 0; 0; 0; 0; 0; 0; 1; 1; 1; 0; 1] @ write_white_code (n - 2432)
| n when n >= 2368 -> [0; 0; 0; 0; 0; 0; 0; 1; 1; 1; 0; 0] @ write_white_code (n - 2368)
| n when n >= 2304 -> [0; 0; 0; 0; 0; 0; 0; 1; 0; 1; 1; 1] @ write_white_code (n - 2304)
| n when n >= 2240 -> [0; 0; 0; 0; 0; 0; 0; 1; 0; 1; 1; 0] @ write_white_code (n - 2240)
| n when n >= 2176 -> [0; 0; 0; 0; 0; 0; 0; 1; 0; 1; 0; 1] @ write_white_code (n - 2176)
| n when n >= 2112 -> [0; 0; 0; 0; 0; 0; 0; 1; 0; 1; 0; 0] @ write_white_code (n - 2112)
| n when n >= 2048 -> [0; 0; 0; 0; 0; 0; 0; 1; 0; 0; 1; 1] @ write_white_code (n - 2048)
| n when n >= 1984 -> [0; 0; 0; 0; 0; 0; 0; 1; 0; 0; 1; 0] @ write_white_code (n - 1984)
| n when n >= 1920 -> [0; 0; 0; 0; 0; 0; 0; 1; 1; 0; 1] @ write_white_code (n - 1920)
| n when n >= 1856 -> [0; 0; 0; 0; 0; 0; 0; 1; 1; 0; 0] @ write_white_code (n - 1856)
| n when n >= 1792 -> [0; 0; 0; 0; 0; 0; 0; 1; 0; 0; 0] @ write_white_code (n - 1792)
| n when n >= 1728 -> [0; 1; 0; 0; 1; 1; 0; 1; 1] @ write_white_code (n - 1728)
| n when n >= 1664 -> [0; 1; 1; 0; 0; 0] @ write_white_code (n - 1664)
| n when n >= 1600 -> [0; 1; 0; 0; 1; 1; 0; 1; 0] @ write_white_code (n - 1600)
| n when n >= 1536 -> [0; 1; 0; 0; 1; 1; 0; 0; 1] @ write_white_code (n - 1536)
| n when n >= 1472 -> [0; 1; 0; 0; 1; 1; 0; 0; 0] @ write_white_code (n - 1472)
| n when n >= 1408 -> [0; 1; 1; 0; 1; 1; 0; 1; 1] @ write_white_code (n - 1408)
| n when n >= 1344 -> [0; 1; 1; 0; 1; 1; 0; 1; 0] @ write_white_code (n - 1344)
| n when n >= 1280 -> [0; 1; 1; 0; 1; 1; 0; 0; 1] @ write_white_code (n - 1280)
| n when n >= 1216 -> [0; 1; 1; 0; 1; 1; 0; 0; 0] @ write_white_code (n - 1216)
| n when n >= 1152 -> [0; 1; 1; 0; 1; 0; 1; 1; 1] @ write_white_code (n - 1152)
| n when n >= 1088 -> [0; 1; 1; 0; 1; 0; 1; 1; 0] @ write_white_code (n - 1088)
| n when n >= 1024 -> [0; 1; 1; 0; 1; 0; 1; 0; 1] @ write_white_code (n - 1024)
| n when n >= 960 -> [0; 1; 1; 0; 1; 0; 1; 0; 0] @ write_white_code (n - 960)
| n when n >= 896 -> [0; 1; 1; 0; 1; 0; 0; 1; 1] @ write_white_code (n - 896)
| n when n >= 832 -> [0; 1; 1; 0; 1; 0; 0; 1; 0] @ write_white_code (n - 832)
| n when n >= 768 -> [0; 1; 1; 0; 0; 1; 1; 0; 1] @ write_white_code (n - 768)
| n when n >= 704 -> [0; 1; 1; 0; 0; 1; 1; 0; 0] @ write_white_code (n - 704)
| n when n >= 640 -> [0; 1; 1; 0; 0; 1; 1; 1] @ write_white_code (n - 640)
| n when n >= 576 -> [0; 1; 1; 0; 1; 0; 0; 0] @ write_white_code (n - 576)
| n when n >= 512 -> [0; 1; 1; 0; 0; 1; 0; 1] @ write_white_code (n - 512)
| n when n >= 448 -> [0; 1; 1; 0; 0; 1; 0; 0] @ write_white_code (n - 448)
| n when n >= 384 -> [0; 0; 1; 1; 0; 1; 1; 1] @ write_white_code (n - 384)
| n when n >= 320 -> [0; 0; 1; 1; 0; 1; 1; 0] @ write_white_code (n - 320)
| n when n >= 256 -> [0; 1; 1; 0; 1; 1; 1] @ write_white_code (n - 256)
| n when n >= 192 -> [0; 1; 0; 1; 1; 1] @ write_white_code (n - 192)
| n when n >= 128 -> [1; 0; 0; 1; 0] @ write_white_code (n - 128)
| n when n >= 64 -> [1; 1; 0; 1; 1] @ write_white_code (n - 64)
| _ -> raise (Pdf.PDFError "write_white_code: unknown")
let rec write_black_code = function
| -1 -> [0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 1]
| 0 -> [0; 0; 0; 0; 1; 1; 0; 1; 1; 1]
| 1 -> [0; 1; 0]
| 2 -> [1; 1]
| 3 -> [1; 0]
| 4 -> [0; 1; 1]
| 5 -> [0; 0; 1; 1]
| 6 -> [0; 0; 1; 0]
| 7 -> [0; 0; 0; 1; 1]
| 8 -> [0; 0; 0; 1; 0; 1]
| 9 -> [0; 0; 0; 1; 0; 0]
| 10 -> [0; 0; 0; 0; 1; 0; 0]
| 11 -> [0; 0; 0; 0; 1; 0; 1]
| 12 -> [0; 0; 0; 0; 1; 1; 1]
| 13 -> [0; 0; 0; 0; 0; 1; 0; 0]
| 14 -> [0; 0; 0; 0; 0; 1; 1; 1]
| 15 -> [0; 0; 0; 0; 1; 1; 0; 0; 0]
| 16 -> [0; 0; 0; 0; 0; 1; 0; 1; 1; 1]
| 17 -> [0; 0; 0; 0; 0; 1; 1; 0; 0; 0]
| 18 -> [0; 0; 0; 0; 0; 0; 1; 0; 0; 0]
| 19 -> [0; 0; 0; 0; 1; 1; 0; 0; 1; 1; 1]
| 20 -> [0; 0; 0; 0; 1; 1; 0; 1; 0; 0; 0]
| 21 -> [0; 0; 0; 0; 1; 1; 0; 1; 1; 0; 0]
| 22 -> [0; 0; 0; 0; 0; 1; 1; 0; 1; 1; 1]
| 23 -> [0; 0; 0; 0; 0; 1; 0; 1; 0; 0; 0]
| 24 -> [0; 0; 0; 0; 0; 0; 1; 0; 1; 1; 1]
| 25 -> [0; 0; 0; 0; 0; 0; 1; 1; 0; 0; 0]
| 26 -> [0; 0; 0; 0; 1; 1; 0; 0; 1; 0; 1; 0]
| 27 -> [0; 0; 0; 0; 1; 1; 0; 0; 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; 0; 1; 0; 0; 1; 1]
| 52 -> [0; 0; 0; 0; 0; 0; 1; 0; 0; 1; 0; 0]
| 53 -> [0; 0; 0; 0; 0; 0; 1; 1; 0; 1; 1; 1]
| 54 -> [0; 0; 0; 0; 0; 0; 1; 1; 1; 0; 0; 0]
| 55 -> [0; 0; 0; 0; 0; 0; 1; 0; 0; 1; 1; 1]
| 56 -> [0; 0; 0; 0; 0; 0; 1; 0; 1; 0; 0; 0]
| 57 -> [0; 0; 0; 0; 0; 1; 0; 1; 1; 0; 0; 0]
| 58 -> [0; 0; 0; 0; 0; 1; 0; 1; 1; 0; 0; 1]
| 59 -> [0; 0; 0; 0; 0; 0; 1; 0; 1; 0; 1; 1]
| 60 -> [0; 0; 0; 0; 0; 0; 1; 0; 1; 1; 0; 0]
| 61 -> [0; 0; 0; 0; 0; 1; 0; 1; 1; 0; 1; 0]
| 62 -> [0; 0; 0; 0; 0; 1; 1; 0; 0; 1; 1; 0]
| 63 -> [0; 0; 0; 0; 0; 1; 1; 0; 0; 1; 1; 1]
| n when n >= 2560 -> [0; 0; 0; 0; 0; 0; 0; 1; 1; 1; 1; 1] @ write_black_code (n - 2560)
| n when n >= 2496 -> [0; 0; 0; 0; 0; 0; 0; 1; 1; 1; 1; 0] @ write_black_code (n - 2496)
| n when n >= 2432 -> [0; 0; 0; 0; 0; 0; 0; 1; 1; 1; 0; 1] @ write_black_code (n - 2432)
| n when n >= 2368 -> [0; 0; 0; 0; 0; 0; 0; 1; 1; 1; 0; 0] @ write_black_code (n - 2368)
| n when n >= 2304 -> [0; 0; 0; 0; 0; 0; 0; 1; 0; 1; 1; 1] @ write_black_code (n - 2304)
| n when n >= 2240 -> [0; 0; 0; 0; 0; 0; 0; 1; 0; 1; 1; 0] @ write_black_code (n - 2240)
| n when n >= 2176 -> [0; 0; 0; 0; 0; 0; 0; 1; 0; 1; 0; 1] @ write_black_code (n - 2176)
| n when n >= 2112 -> [0; 0; 0; 0; 0; 0; 0; 1; 0; 1; 0; 0] @ write_black_code (n - 2112)
| n when n >= 2048 -> [0; 0; 0; 0; 0; 0; 0; 1; 0; 0; 1; 1] @ write_black_code (n - 2048)
| n when n >= 1984 -> [0; 0; 0; 0; 0; 0; 0; 1; 0; 0; 1; 0] @ write_black_code (n - 1984)
| n when n >= 1920 -> [0; 0; 0; 0; 0; 0; 0; 1; 1; 0; 1] @ write_black_code (n - 1920)
| n when n >= 1856 -> [0; 0; 0; 0; 0; 0; 0; 1; 1; 0; 0] @ write_black_code (n - 1856)
| n when n >= 1792 -> [0; 0; 0; 0; 0; 0; 0; 1; 0; 0; 0] @ write_black_code (n - 1792)
| n when n >= 1728 -> [0; 0; 0; 0; 0; 0; 1; 1; 0; 0; 1; 0; 1] @ write_black_code (n - 1728)
| n when n >= 1664 -> [0; 0; 0; 0; 0; 0; 1; 1; 0; 0; 1; 0; 0] @ write_black_code (n - 1664)
| n when n >= 1600 -> [0; 0; 0; 0; 0; 0; 1; 0; 1; 1; 0; 1; 1] @ write_black_code (n - 1600)
| n when n >= 1536 -> [0; 0; 0; 0; 0; 0; 1; 0; 1; 1; 0; 1; 0] @ write_black_code (n - 1536)
| n when n >= 1472 -> [0; 0; 0; 0; 0; 0; 1; 0; 1; 0; 1; 0; 1] @ write_black_code (n - 1472)
| n when n >= 1408 -> [0; 0; 0; 0; 0; 0; 1; 0; 1; 0; 1; 0; 0] @ write_black_code (n - 1408)
| n when n >= 1344 -> [0; 0; 0; 0; 0; 0; 1; 0; 1; 0; 0; 1; 1] @ write_black_code (n - 1344)
| n when n >= 1280 -> [0; 0; 0; 0; 0; 0; 1; 0; 1; 0; 0; 1; 0] @ write_black_code (n - 1280)
| n when n >= 1216 -> [0; 0; 0; 0; 0; 0; 1; 1; 1; 0; 1; 1; 1] @ write_black_code (n - 1216)
| n when n >= 1152 -> [0; 0; 0; 0; 0; 0; 1; 1; 1; 0; 1; 1; 0] @ write_black_code (n - 1152)
| n when n >= 1088 -> [0; 0; 0; 0; 0; 0; 1; 1; 1; 0; 1; 0; 1] @ write_black_code (n - 1088)
| n when n >= 1024 -> [0; 0; 0; 0; 0; 0; 1; 1; 1; 0; 1; 0; 0] @ write_black_code (n - 1024)
| n when n >= 960 -> [0; 0; 0; 0; 0; 0; 1; 1; 1; 0; 0; 1; 1] @ write_black_code (n - 960)
| n when n >= 896 -> [0; 0; 0; 0; 0; 0; 1; 1; 1; 0; 0; 1; 0] @ write_black_code (n - 896)
| n when n >= 832 -> [0; 0; 0; 0; 0; 0; 1; 0; 0; 1; 1; 0; 1] @ write_black_code (n - 832)
| n when n >= 768 -> [0; 0; 0; 0; 0; 0; 1; 0; 0; 1; 1; 0; 0] @ write_black_code (n - 768)
| n when n >= 704 -> [0; 0; 0; 0; 0; 0; 1; 0; 0; 1; 0; 1; 1] @ write_black_code (n - 704)
| n when n >= 640 -> [0; 0; 0; 0; 0; 0; 1; 0; 0; 1; 0; 1; 0] @ write_black_code (n - 640)
| n when n >= 576 -> [0; 0; 0; 0; 0; 0; 1; 1; 0; 1; 1; 0; 1] @ write_black_code (n - 576)
| n when n >= 512 -> [0; 0; 0; 0; 0; 0; 1; 1; 0; 1; 1; 0; 0] @ write_black_code (n - 512)
| n when n >= 448 -> [0; 0; 0; 0; 0; 0; 1; 1; 0; 1; 0; 1] @ write_black_code (n - 448)
| n when n >= 384 -> [0; 0; 0; 0; 0; 0; 1; 1; 0; 1; 0; 0] @ write_black_code (n - 384)
| n when n >= 320 -> [0; 0; 0; 0; 0; 0; 1; 1; 0; 0; 1; 1] @ write_black_code (n - 320)
| n when n >= 256 -> [0; 0; 0; 0; 0; 1; 0; 1; 1; 0; 1; 1] @ write_black_code (n - 256)
| n when n >= 192 -> [0; 0; 0; 0; 1; 1; 0; 0; 1; 0; 0; 1] @ write_black_code (n - 192)
| n when n >= 128 -> [0; 0; 0; 0; 1; 1; 0; 0; 1; 0; 0; 0] @ write_black_code (n - 128)
| n when n >= 64 -> [0; 0; 0; 0; 0; 0; 1; 1; 1; 1] @ write_black_code (n - 64)
| _ -> raise (Pdf.PDFError "write_black_code: unknown")
let rec read_white_code i =
let a = getbitint i in
let b = getbitint i in
let c = getbitint i in
let d = getbitint i in
match a, b, c, d with
| 0, 1, 1, 1 -> 2
| 1, 0, 0, 0 -> 3
| 1, 0, 1, 1 -> 4
| 1, 1, 0, 0 -> 5
| 1, 1, 1, 0 -> 6
| 1, 1, 1, 1 -> 7
| _ ->
let e = getbitint i in
match a, b, c, d, e with
| 1, 0, 0, 1, 1 -> 8
| 1, 0, 1, 0, 0 -> 9
| 0, 0, 1, 1, 1 -> 10
| 0, 1, 0, 0, 0 -> 11
| 1, 1, 0, 1, 1 -> 64 + read_white_code i
| 1, 0, 0, 1, 0 -> 128 + read_white_code i
| _ ->
let f = getbitint i in
match a, b, c, d, e, f with
| 0, 0, 0, 1, 1, 1 -> 1
| 0, 0, 1, 0, 0, 0 -> 12
| 0, 0, 0, 0, 1, 1 -> 13
| 1, 1, 0, 1, 0, 0 -> 14
| 1, 1, 0, 1, 0, 1 -> 15
| 1, 0, 1, 0, 1, 0 -> 16
| 1, 0, 1, 0, 1, 1 -> 17
| 0, 1, 0, 1, 1, 1 -> 192 + read_white_code i
| 0, 1, 1, 0, 0, 0 -> 1664 + read_white_code i
| _ ->
let g = getbitint i in
match a, b, c, d, e, f, g with
| 0, 1, 0, 0, 1, 1, 1 -> 18
| 0, 0, 0, 1, 1, 0, 0 -> 19
| 0, 0, 0, 1, 0, 0, 0 -> 20
| 0, 0, 1, 0, 1, 1, 1 -> 21
| 0, 0, 0, 0, 0, 1, 1 -> 22
| 0, 0, 0, 0, 1, 0, 0 -> 23
| 0, 1, 0, 1, 0, 0, 0 -> 24
| 0, 1, 0, 1, 0, 1, 1 -> 25
| 0, 0, 1, 0, 0, 1, 1 -> 26
| 0, 1, 0, 0, 1, 0, 0 -> 27
| 0, 0, 1, 1, 0, 0, 0 -> 28
| 0, 1, 1, 0, 1, 1, 1 -> 256 + read_white_code i
| _ ->
let h = getbitint i in
match a, b, c, d, e, f, g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| 0, 0, 1, 1, 0, 0, 1, 1 -> 62
| 0, 0, 1, 1, 0, 1, 0, 0 -> 63
| 0, 0, 1, 1, 0, 1, 1, 0 -> 320 + read_white_code i
| 0, 0, 1, 1, 0, 1, 1, 1 -> 384 + read_white_code i
| 0, 1, 1, 0, 0, 1, 0, 0 -> 448 + read_white_code i
| 0, 1, 1, 0, 0, 1, 0, 1 -> 512 + read_white_code i
| 0, 1, 1, 0, 1, 0, 0, 0 -> 576 + read_white_code i
| 0, 1, 1, 0, 0, 1, 1, 1 -> 640 + read_white_code i
| _ ->
let j = getbitint i in
match a, b, c, d, e, f, g, h, j with
| 0, 1, 1, 0, 0, 1, 1, 0, 0 -> 704 + read_white_code i
| 0, 1, 1, 0, 0, 1, 1, 0, 1 -> 768 + read_white_code i
| 0, 1, 1, 0, 1, 0, 0, 1, 0 -> 832 + read_white_code i
| 0, 1, 1, 0, 1, 0, 0, 1, 1 -> 896 + read_white_code i
| 0, 1, 1, 0, 1, 0, 1, 0, 0 -> 960 + read_white_code i
| 0, 1, 1, 0, 1, 0, 1, 0, 1 -> 1024 + read_white_code i
| 0, 1, 1, 0, 1, 0, 1, 1, 0 -> 1088 + read_white_code i
| 0, 1, 1, 0, 1, 0, 1, 1, 1 -> 1152 + read_white_code i
| 0, 1, 1, 0, 1, 1, 0, 0, 0 -> 1216 + read_white_code i
| 0, 1, 1, 0, 1, 1, 0, 0, 1 -> 1280 + read_white_code i
| 0, 1, 1, 0, 1, 1, 0, 1, 0 -> 1344 + read_white_code i
| 0, 1, 1, 0, 1, 1, 0, 1, 1 -> 1408 + read_white_code i
| 0, 1, 0, 0, 1, 1, 0, 0, 0 -> 1472 + read_white_code i
| 0, 1, 0, 0, 1, 1, 0, 0, 1 -> 1536 + read_white_code i
| 0, 1, 0, 0, 1, 1, 0, 1, 0 -> 1600 + read_white_code i
| 0, 1, 0, 0, 1, 1, 0, 1, 1 -> 1728 + read_white_code i
| _ ->
let k = getbitint i in
let l = getbitint i in
match a, b, c, d, e, f, g, h, j, k, l with
| 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 -> 1792 + read_white_code i
| 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 -> 1856 + read_white_code i
| 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1 -> 1920 + read_white_code i
| _ ->
let m = getbitint i in
match a, b, c, d, e, f, g, h, j, k, l, m with
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 -> -1
| 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0 -> 1984 + read_white_code i
| 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1 -> 2048 + read_white_code i
| 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0 -> 2112 + read_white_code i
| 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1 -> 2176 + read_white_code i
| 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0 -> 2240 + read_white_code i
| 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1 -> 2304 + read_white_code i
| 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0 -> 2368 + read_white_code i
| 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1 -> 2432 + read_white_code i
| 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0 -> 2496 + read_white_code i
| 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1 -> 2560 + read_white_code i
| _ -> raise (Failure "bad white code")
let rec read_black_code i =
let a = getbitint i in
let b = getbitint i in
match a, b with
| 1, 1 -> 2
| 1, 0 -> 3
| _ ->
let c = getbitint i in
match a, b, c with
| 0, 1, 0 -> 1
| 0, 1, 1 -> 4
| _ ->
let d = getbitint i in
match a, b, c, d with
| 0, 0, 1, 1 -> 5
| 0, 0, 1, 0 -> 6
| _ ->
let e = getbitint i in
match a, b, c, d, e with
| 0, 0, 0, 1, 1 -> 7
| _ ->
let f = getbitint i in
match a, b, c, d, e, f with
| 0, 0, 0, 1, 0, 1 -> 8
| 0, 0, 0, 1, 0, 0 -> 9
| _ ->
let g = getbitint i in
match a, b, c, d, e, f, g with
| 0, 0, 0, 0, 1, 0, 0 -> 10
| 0, 0, 0, 0, 1, 0, 1 -> 11
| 0, 0, 0, 0, 1, 1, 1 -> 12
| _ ->
let h = getbitint i in
match a, b, c, d, e, f, g, h with
| 0, 0, 0, 0, 0, 1, 0, 0 -> 13
| 0, 0, 0, 0, 0, 1, 1, 1 -> 14
| _ ->
let j = getbitint i in
match a, b, c, d, e, f, g, h, j with
| 0, 0, 0, 0, 1, 1, 0, 0, 0 -> 15
| _ ->
let k = getbitint i in
match a, b, c, d, e, f, g, h, j, k with
| 0, 0, 0, 0, 1, 1, 0, 1, 1, 1 -> 0
| 0, 0, 0, 0, 0, 1, 0, 1, 1, 1 -> 16
| 0, 0, 0, 0, 0, 1, 1, 0, 0, 0 -> 17
| 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 -> 18
| 0, 0, 0, 0, 0, 0, 1, 1, 1, 1 -> 64 + read_black_code i
| _ ->
let l = getbitint i in
match a, b, c, d, e, f, g, h, j, k, l with
| 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1 -> 19
| 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0 -> 20
| 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0 -> 21
| 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1 -> 22
| 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0 -> 23
| 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1 -> 24
| 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0 -> 25
| 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 -> 1792 + read_black_code i
| 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 -> 1856 + read_black_code i
| 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1 -> 1920 + read_black_code i
| _ ->
let m = getbit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 1, 0, 1 -> 49
| 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0 -> 50
| 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1 -> 51
| 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0 -> 52
| 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1 -> 53
| 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0 -> 54
| 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1 -> 55
| 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0 -> 56
| 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0 -> 57
| 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1 -> 58
| 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1 -> 59
| 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0 -> 60
| 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0 -> 61
| 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0 -> 62
| 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1 -> 63
| 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0 -> 128 + read_black_code i
| 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1 -> 192 + read_black_code i
| 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1 -> 256 + read_black_code i
| 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1 -> 320 + read_black_code i
| 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0 -> 384 + read_black_code i
| 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1 -> 448 + read_black_code i
| 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0 -> 1984 + read_black_code i
| 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1 -> 2048 + read_black_code i
| 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0 -> 2112 + read_black_code i
| 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1 -> 2176 + read_black_code i
| 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0 -> 2240 + read_black_code i
| 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1 -> 2304 + read_black_code i
| 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0 -> 2368 + read_black_code i
| 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1 -> 2432 + read_black_code i
| 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0 -> 2496 + read_black_code i
| 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1 -> 2560 + read_black_code i
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 -> -1
| _ ->
let n = getbitint i in
match a, b, c, d, e, f, g, h, j, k, l, m, n with
| 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0 -> 512 + read_black_code i
| 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1 -> 576 + read_black_code i
| 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0 -> 640 + read_black_code i
| 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1 -> 704 + read_black_code i
| 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0 -> 768 + read_black_code i
| 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1 -> 832 + read_black_code i
| 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0 -> 896 + read_black_code i
| 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1 -> 960 + read_black_code i
| 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0 -> 1024 + read_black_code i
| 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1 -> 1088 + read_black_code i
| 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0 -> 1152 + read_black_code i
| 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1 -> 1216 + read_black_code i
| 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0 -> 1280 + read_black_code i
| 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1 -> 1344 + read_black_code i
| 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0 -> 1408 + read_black_code i
| 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1 -> 1472 + read_black_code i
| 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0 -> 1536 + read_black_code i
| 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1 -> 1600 + read_black_code i
| 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0 -> 1664 + read_black_code i
| 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1 -> 1728 + read_black_code i
| _ -> raise (Failure "bad black code")
(* Group 4 Fax decoder. *)
type modes =
| Pass
| Horizontal
| Vertical of int
| Uncompressed
| EOFB
let read_mode i =
let a = getbitint i in
match a with
| 1 -> Vertical 0
| _ ->
let b = getbitint i in
let c = getbitint i in
match a, b, c with
| 0, 1, 1 -> Vertical (-1)
| 0, 1, 0 -> Vertical 1
| 0, 0, 1 -> Horizontal
| _ ->
let d = getbitint i in
match a, b, c, d with
| 0, 0, 0, 1 -> Pass
| _ ->
let e = getbitint i in
let f = getbitint i in
match a, b, c, d, e, f with
| 0, 0, 0, 0, 1, 1 -> Vertical (-2)
| 0, 0, 0, 0, 1, 0 -> Vertical 2
| _ ->
let g = getbitint i in
match a, b, c, d, e, f, g with
| 0, 0, 0, 0, 0, 1, 1 -> Vertical (-3)
| 0, 0, 0, 0, 0, 1, 0 -> Vertical 3
| _ ->
let h = getbitint i in
let j = getbitint i in
let k = getbitint i in
let l = getbitint i in
let m = getbitint i in
match a, b, c, d, e, f, g, h, j, k, l, m with
| 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1 -> Uncompressed
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ->
let a = getbitint i in
let b = getbitint i in
let c = getbitint i in
let d = getbitint i in
let e = getbitint i in
let f = getbitint i in
let g = getbitint i in
let h = getbitint i in
let j = getbitint i in
let k = getbitint i in
let l = getbitint i in
let m = getbitint i in
begin match a, b, c, d, e, f, g, h, j, k, l, m with
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 -> EOFB
| _ -> raise (Failure "Not a valid code on EOFB")
end
| _ -> raise (Failure "Not a valid code")
let decode_CCITTFax k eol eba c r eob bone dra input =
if k > 0 then raise (DecodeNotSupported "CCITTFax k > 0") else
let whiteval, blackval = if bone then 0, 1 else 1, 0
in let output = make_write_bitstream () in
let b = bitbytes_of_input input
in let column = ref 0
in let row = ref 0
in let refline = ref (Array.make c whiteval)
in let currline = ref (Array.make c 0)
in let white = ref true
in let output_line line =
Array.iter (putbit output) line;
align_write output
in
let output_span l v =
if l < 0 then raise (Failure "Bad CCITT stream") else
begin
for x = !column to !column + l - 1 do
let r = !currline in r.(x) <- v
done;
column += l
end
in let find_b1 () =
let pos = ref !column
in let curr, opp =
if !white then whiteval, blackval else blackval, whiteval
in
let find v =
while
let r = !refline in
if !pos >= 0 && !pos < Array.length r then
r.(!pos) <> v
else
false
do
incr pos
done; !pos
in
try
(* Careful to skip imaginary black at beginning *)
ignore (if !column = 0 && !white then 0 else find curr);
find opp
with
_ -> c
in let find_b2 () =
let pos = ref !column
in let curr, opp =
if !white then whiteval, blackval else blackval, whiteval
in
let find v =
while
let r = !refline in
if !pos >=0 && !pos < Array.length r then
r.(!pos) <> v
else
false
do
incr pos