-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy path_compression.py
More file actions
1524 lines (1313 loc) · 55.2 KB
/
_compression.py
File metadata and controls
1524 lines (1313 loc) · 55.2 KB
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
"""Compression codecs: deflate (zlib) and LZW (Numba), plus horizontal predictor."""
from __future__ import annotations
import zlib
import numpy as np
from xrspatial.utils import ngjit
# -- Decompression-bomb defenses ---------------------------------------------
#
# A malicious TIFF can declare a small strip/tile compressed payload that
# expands to multiple gigabytes when decoded. Without a cap the reader is
# OOM-killed before the post-decode size check (in ``_decode_strip_or_tile``)
# ever runs, since by then the bomb has already been allocated. Each codec
# below takes an ``expected_size`` (the byte length the caller computed from
# the IFD's declared dimensions) and refuses to produce more than
# ``expected_size * _DECOMPRESS_MARGIN`` bytes. The margin allows for the
# small amount of legitimate codec metadata that some encoders emit, while
# still rejecting the 1000:1 ratios characteristic of bomb attacks.
_DECOMPRESS_MARGIN = 1.05
def _max_output_with_margin(expected_size: int) -> int:
"""Return the cap (in bytes) for a codec given the caller's expected size.
A positive ``expected_size`` returns ``int(expected_size * 1.05) + 1``,
leaving a one-byte sentinel above the legitimate margin so a single
byte of overflow is detectable.
A non-positive ``expected_size`` (the default ``0``) returns ``0``,
which the codec wrappers interpret as "no cap": they fall through to
the unbounded library decode for backward compatibility with callers
that don't supply a size. The reader always supplies a size; this
branch is only hit by direct callers and round-trip tests.
"""
if expected_size <= 0:
return 0
return int(expected_size * _DECOMPRESS_MARGIN) + 1
# -- Deflate (zlib wrapper) --------------------------------------------------
def deflate_decompress(data: bytes, expected_size: int = 0) -> bytes:
"""Decompress deflate/zlib data with an optional output-size cap.
Parameters
----------
data : bytes
Deflate/zlib compressed payload.
expected_size : int, optional
Caller's expected uncompressed byte count. When > 0, the decoder
refuses to produce more than ``expected_size * 1.05 + 1`` bytes
and raises ``ValueError`` on overflow (decompression-bomb guard).
Returns
-------
bytes
Uncompressed data.
"""
if expected_size <= 0:
# Backward-compat path: caller hasn't supplied an expected size.
return zlib.decompress(data)
cap = _max_output_with_margin(expected_size)
decompressor = zlib.decompressobj()
# Accumulate into a bytearray rather than reassigning bytes via ``+=``;
# the bytes path was O(n^2) for large strips because every chunk
# reallocated and copied the whole accumulated buffer.
out = bytearray()
# Read one byte beyond the cap so that an overflowing stream is detected
# rather than silently truncated.
out += decompressor.decompress(data, max_length=cap + 1)
# Drain any output the decompressor has buffered (including unconsumed
# input). We stop as soon as output exceeds the cap, or when the
# decompressor declares EOF, or when no further bytes are produced.
while not decompressor.eof and len(out) <= cap:
feed = decompressor.unconsumed_tail
more = decompressor.decompress(feed, max_length=cap + 1 - len(out))
if not more:
break
out += more
if len(out) > cap:
raise ValueError(
f"deflate decode exceeded expected size: {len(out)} bytes "
f"produced, cap is {cap} (expected {expected_size}). Likely "
f"a decompression bomb."
)
# Flush any remaining state to surface tail bytes / errors.
out += decompressor.flush()
if len(out) > cap:
raise ValueError(
f"deflate decode exceeded expected size: {len(out)} bytes "
f"produced, cap is {cap} (expected {expected_size}). Likely "
f"a decompression bomb."
)
return bytes(out)
def deflate_compress(data: bytes, level: int = 6) -> bytes:
"""Compress data with deflate/zlib."""
return zlib.compress(data, level)
# -- LZW constants -----------------------------------------------------------
LZW_CLEAR_CODE = 256
LZW_EOI_CODE = 257
LZW_FIRST_CODE = 258
LZW_MAX_CODE = 4095
LZW_MAX_BITS = 12
# -- LZW decode (Numba) ------------------------------------------------------
@ngjit
def _lzw_decode_kernel(src, src_len, dst, dst_len):
"""Decode TIFF-variant LZW (MSB-first) into dst buffer.
Parameters
----------
src : uint8 array
Compressed bytes.
src_len : int
Number of valid bytes in src.
dst : uint8 array
Output buffer (must be pre-allocated large enough).
dst_len : int
Maximum bytes to write.
Returns
-------
int
Number of bytes written to dst.
"""
# Table: prefix-chain representation
table_prefix = np.full(4096, -1, dtype=np.int32)
table_suffix = np.zeros(4096, dtype=np.uint8)
table_length = np.zeros(4096, dtype=np.int32)
# Small stack for chain reversal
stack = np.empty(4096, dtype=np.uint8)
# Bit reader state
bit_pos = 0
code_size = 9
next_code = LZW_FIRST_CODE
# Initialize table with single-byte entries
for i in range(256):
table_prefix[i] = -1
table_suffix[i] = np.uint8(i)
table_length[i] = 1
out_pos = 0
old_code = -1
while True:
# Read next code (MSB-first bit packing)
byte_offset = bit_pos >> 3
if byte_offset >= src_len:
break
# Gather up to 24 bits from available bytes
bits = np.int32(src[byte_offset]) << 16
if byte_offset + 1 < src_len:
bits |= np.int32(src[byte_offset + 1]) << 8
if byte_offset + 2 < src_len:
bits |= np.int32(src[byte_offset + 2])
bit_offset_in_byte = bit_pos & 7
# Shift to align the code_size bits at the LSB side
bits = (bits >> (24 - bit_offset_in_byte - code_size)) & ((1 << code_size) - 1)
bit_pos += code_size
code = bits
if code == LZW_EOI_CODE:
break
if code == LZW_CLEAR_CODE:
code_size = 9
next_code = LZW_FIRST_CODE
old_code = -1
continue
if old_code == -1:
# First code after clear
if code < 256:
if out_pos < dst_len:
dst[out_pos] = np.uint8(code)
out_pos += 1
old_code = code
continue
# Determine the string for this code
if code < next_code:
# Code is in table -- walk the chain, push to stack, emit reversed
c = code
stack_pos = 0
while c >= 0 and c < 4096 and stack_pos < 4096:
stack[stack_pos] = table_suffix[c]
stack_pos += 1
c = table_prefix[c]
# Emit in correct order
for i in range(stack_pos - 1, -1, -1):
if out_pos < dst_len:
dst[out_pos] = stack[i]
out_pos += 1
# Add new entry: old_code string + first char of code string
if next_code <= LZW_MAX_CODE and stack_pos > 0:
table_prefix[next_code] = old_code
table_suffix[next_code] = stack[stack_pos - 1] # first char
table_length[next_code] = table_length[old_code] + 1
next_code += 1
else:
# Special case: code == next_code
# String = old_code string + first char of old_code string
c = old_code
stack_pos = 0
while c >= 0 and c < 4096 and stack_pos < 4096:
stack[stack_pos] = table_suffix[c]
stack_pos += 1
c = table_prefix[c]
if stack_pos == 0:
old_code = code
continue
first_char = stack[stack_pos - 1]
# Emit old_code string
for i in range(stack_pos - 1, -1, -1):
if out_pos < dst_len:
dst[out_pos] = stack[i]
out_pos += 1
# Emit first char again
if out_pos < dst_len:
dst[out_pos] = first_char
out_pos += 1
# Add new entry
if next_code <= LZW_MAX_CODE:
table_prefix[next_code] = old_code
table_suffix[next_code] = first_char
table_length[next_code] = table_length[old_code] + 1
next_code += 1
# Bump code size (TIFF LZW uses "early change": bump one code before
# the table fills the current code_size capacity)
if next_code > (1 << code_size) - 2 and code_size < LZW_MAX_BITS:
code_size += 1
old_code = code
return out_pos
def lzw_decompress(data: bytes, expected_size: int = 0) -> np.ndarray:
"""Decompress TIFF-variant LZW data.
Parameters
----------
data : bytes
LZW compressed data.
expected_size : int
Expected decompressed size. If 0, uses 10x compressed size as buffer.
Returns
-------
np.ndarray
Mutable uint8 array of decompressed data.
"""
src = np.frombuffer(data, dtype=np.uint8)
if expected_size <= 0:
expected_size = len(data) * 10
dst = np.empty(expected_size, dtype=np.uint8)
n = _lzw_decode_kernel(src, len(src), dst, expected_size)
return dst[:n].copy() # owned, mutable slice
# -- LZW encode (Numba) ------------------------------------------------------
@ngjit
def _lzw_encode_kernel(src, src_len, dst, dst_len):
"""Encode data as TIFF-variant LZW (MSB-first).
Returns number of bytes written to dst.
"""
# Hash table for string matching
# Key: (prefix_code << 8) | suffix_byte -> code
# Uses generation counter to avoid clearing: an entry is valid only when
# ht_gen[slot] == current_gen.
HT_SIZE = 8209 # prime > 4096*2
ht_keys = np.empty(HT_SIZE, dtype=np.int64)
ht_values = np.empty(HT_SIZE, dtype=np.int32)
ht_gen = np.zeros(HT_SIZE, dtype=np.int32)
current_gen = np.int32(1)
# Bit accumulator: collect bits and flush whole bytes
bit_buf = np.int32(0) # up to 24 bits pending
bits_in_buf = np.int32(0)
out_pos = 0
code_size = 9
next_code = LZW_FIRST_CODE
def flush_code(code, code_size, bit_buf, bits_in_buf, dst, dst_len, out_pos):
"""Pack a code into the bit accumulator and flush complete bytes."""
# Merge code bits (MSB-first) into accumulator
bit_buf = (bit_buf << code_size) | code
bits_in_buf += code_size
# Flush whole bytes from the top of the accumulator
while bits_in_buf >= 8:
bits_in_buf -= 8
if out_pos < dst_len:
dst[out_pos] = np.uint8((bit_buf >> bits_in_buf) & 0xFF)
out_pos += 1
return bit_buf, bits_in_buf, out_pos
# Write initial clear code
bit_buf, bits_in_buf, out_pos = flush_code(
LZW_CLEAR_CODE, code_size, bit_buf, bits_in_buf, dst, dst_len, out_pos)
if src_len == 0:
bit_buf, bits_in_buf, out_pos = flush_code(
LZW_EOI_CODE, code_size, bit_buf, bits_in_buf, dst, dst_len, out_pos)
# Flush remaining bits
if bits_in_buf > 0 and out_pos < dst_len:
dst[out_pos] = np.uint8((bit_buf << (8 - bits_in_buf)) & 0xFF)
out_pos += 1
return out_pos
prefix = np.int32(src[0])
pos = 1
while pos < src_len:
suffix = np.int32(src[pos])
# Look up (prefix, suffix) in hash table
key = np.int64(prefix) * 256 + np.int64(suffix)
h = int(key % HT_SIZE)
if h < 0:
h += HT_SIZE
found = False
for _ in range(HT_SIZE):
if ht_gen[h] == current_gen and ht_keys[h] == key:
prefix = ht_values[h]
found = True
break
elif ht_gen[h] != current_gen:
break
h = (h + 1) % HT_SIZE
if not found:
# Output the prefix code
bit_buf, bits_in_buf, out_pos = flush_code(
prefix, code_size, bit_buf, bits_in_buf, dst, dst_len, out_pos)
# Add new entry to table
if next_code <= LZW_MAX_CODE:
ht_gen[h] = current_gen
ht_keys[h] = key
ht_values[h] = next_code
next_code += 1
# Encoder bumps one entry later than decoder (decoder trails by 1)
if next_code > (1 << code_size) - 1 and code_size < LZW_MAX_BITS:
code_size += 1
else:
# Table full, emit clear code and reset
bit_buf, bits_in_buf, out_pos = flush_code(
LZW_CLEAR_CODE, code_size, bit_buf, bits_in_buf, dst, dst_len, out_pos)
code_size = 9
next_code = LZW_FIRST_CODE
current_gen += 1
prefix = suffix
pos += 1
# Output last prefix
bit_buf, bits_in_buf, out_pos = flush_code(
prefix, code_size, bit_buf, bits_in_buf, dst, dst_len, out_pos)
bit_buf, bits_in_buf, out_pos = flush_code(
LZW_EOI_CODE, code_size, bit_buf, bits_in_buf, dst, dst_len, out_pos)
# Flush remaining bits
if bits_in_buf > 0 and out_pos < dst_len:
dst[out_pos] = np.uint8((bit_buf << (8 - bits_in_buf)) & 0xFF)
out_pos += 1
return out_pos
def lzw_compress(data: bytes) -> bytes:
"""Compress data using TIFF-variant LZW.
Parameters
----------
data : bytes
Raw data to compress.
Returns
-------
bytes
"""
src = np.frombuffer(data, dtype=np.uint8)
# Worst case: output slightly larger than input
max_out = len(data) + len(data) // 2 + 256
dst = np.empty(max_out, dtype=np.uint8)
n = _lzw_encode_kernel(src, len(src), dst, max_out)
return dst[:n].tobytes()
# -- Horizontal predictor (Numba) --------------------------------------------
#
# TIFF predictor=2 (horizontal differencing) operates at the *sample* level,
# not the byte level. Per TIFF Technical Note: "the difference is taken
# between adjacent samples (not between adjacent bytes) of the same
# component." This means for multi-byte samples the encoder computes
#
# diff[i] = sample[i] - sample[i - samples_per_pixel] (mod 2^bits)
#
# in the sample's natural bit width (uint16 wraps at 65536, etc.), not
# byte-by-byte. A byte-wise implementation drops the inter-byte carry
# and produces wrong values for any sample wider than one byte. This
# is what libtiff's horAcc16/horAcc32 do, and what GDAL/rasterio/tifffile
# write to disk. The byte-wise path remains correct (and faster) for the
# 8-bit case.
@ngjit
def _predictor_decode_u8(data, width, height, samples_per_pixel):
"""Undo predictor=2 for 8-bit samples (one byte per sample).
Stride is samples_per_pixel bytes. The byte-wise modular sum is
correct because each sample fits in a single byte.
"""
row_bytes = width * samples_per_pixel
for row in range(height):
row_start = row * row_bytes
for col in range(samples_per_pixel, row_bytes):
idx = row_start + col
data[idx] = np.uint8(
(np.int32(data[idx]) + np.int32(data[idx - samples_per_pixel])) & 0xFF)
@ngjit
def _predictor_decode_u16(view, width, height, samples_per_pixel):
"""Undo predictor=2 on a uint16 view (in-place, sample-resolution).
*view* indexes by sample. Stride is *samples_per_pixel* samples.
"""
row_samples = width * samples_per_pixel
for row in range(height):
row_start = row * row_samples
for col in range(samples_per_pixel, row_samples):
idx = row_start + col
view[idx] = np.uint16(
(np.int32(view[idx]) + np.int32(view[idx - samples_per_pixel])) & 0xFFFF)
@ngjit
def _predictor_decode_u32(view, width, height, samples_per_pixel):
"""Undo predictor=2 on a uint32 view (in-place, sample-resolution)."""
row_samples = width * samples_per_pixel
for row in range(height):
row_start = row * row_samples
for col in range(samples_per_pixel, row_samples):
idx = row_start + col
view[idx] = np.uint32(
(np.uint64(view[idx]) + np.uint64(view[idx - samples_per_pixel]))
& np.uint64(0xFFFFFFFF))
@ngjit
def _predictor_decode_u64(view, width, height, samples_per_pixel):
"""Undo predictor=2 on a uint64 view (in-place, sample-resolution)."""
row_samples = width * samples_per_pixel
for row in range(height):
row_start = row * row_samples
for col in range(samples_per_pixel, row_samples):
idx = row_start + col
view[idx] = np.uint64(view[idx]) + np.uint64(view[idx - samples_per_pixel])
@ngjit
def _predictor_encode_u8(data, width, height, samples_per_pixel):
"""Apply predictor=2 for 8-bit samples (right-to-left, in-place)."""
row_bytes = width * samples_per_pixel
for row in range(height):
row_start = row * row_bytes
for col in range(row_bytes - 1, samples_per_pixel - 1, -1):
idx = row_start + col
data[idx] = np.uint8(
(np.int32(data[idx]) - np.int32(data[idx - samples_per_pixel])) & 0xFF)
@ngjit
def _predictor_encode_u16(view, width, height, samples_per_pixel):
"""Apply predictor=2 on a uint16 view (right-to-left, in-place)."""
row_samples = width * samples_per_pixel
for row in range(height):
row_start = row * row_samples
for col in range(row_samples - 1, samples_per_pixel - 1, -1):
idx = row_start + col
view[idx] = np.uint16(
(np.int32(view[idx]) - np.int32(view[idx - samples_per_pixel])) & 0xFFFF)
@ngjit
def _predictor_encode_u32(view, width, height, samples_per_pixel):
"""Apply predictor=2 on a uint32 view (right-to-left, in-place)."""
row_samples = width * samples_per_pixel
for row in range(height):
row_start = row * row_samples
for col in range(row_samples - 1, samples_per_pixel - 1, -1):
idx = row_start + col
view[idx] = np.uint32(
(np.uint64(view[idx]) - np.uint64(view[idx - samples_per_pixel]))
& np.uint64(0xFFFFFFFF))
@ngjit
def _predictor_encode_u64(view, width, height, samples_per_pixel):
"""Apply predictor=2 on a uint64 view (right-to-left, in-place)."""
row_samples = width * samples_per_pixel
for row in range(height):
row_start = row * row_samples
for col in range(row_samples - 1, samples_per_pixel - 1, -1):
idx = row_start + col
view[idx] = np.uint64(view[idx]) - np.uint64(view[idx - samples_per_pixel])
import sys as _sys
_NATIVE_BO = '<' if _sys.byteorder == 'little' else '>'
def _sample_view(buf: np.ndarray, bytes_per_sample: int) -> np.ndarray:
"""Return *buf* viewed as a 1-D array of native-byte-order samples.
The view shares memory with *buf*. Numba's nopython mode rejects arrays
with a non-native byte order ("Unsupported array dtype: >u2" etc.), so
callers that need to operate on data from a big-endian file must
byteswap the underlying bytes before and after invoking the kernels.
"""
if bytes_per_sample == 1:
return buf
sample_dtype = np.dtype(f'u{bytes_per_sample}')
return buf.view(sample_dtype)
def predictor_decode(data: np.ndarray, width: int, height: int,
bytes_per_sample: int, samples: int = 1,
byte_order: str = '<') -> np.ndarray:
"""Undo horizontal differencing predictor (predictor=2).
Operates at the sample level, per TIFF Technical Note: differences
are taken between adjacent same-component samples in the sample's
natural bit width. For multi-band chunky data the stride is the
number of samples per pixel.
Parameters
----------
data : np.ndarray
Flat uint8 array of decompressed pixel data (modified in-place).
width, height : int
Image dimensions in pixels.
bytes_per_sample : int
Bytes per sample (1, 2, 4, or 8).
samples : int
Samples per pixel (1 for single-band, 3 for RGB, ...).
byte_order : str
'<' for little-endian, '>' for big-endian. Used to interpret
multi-byte sample bytes during the in-place differencing.
Returns
-------
np.ndarray
The same uint8 array with predictor=2 inverted.
"""
buf = np.ascontiguousarray(data)
if bytes_per_sample == 1:
_predictor_decode_u8(buf, width, height, samples)
return buf
# Numba can't compile non-native-byte-order arrays. For big-endian
# files, swap the bytes in place so the native-order kernel sees
# correct sample values, then swap back so the caller's downstream
# ``chunk.view(file_dtype)`` reads the bytes in the file's order.
swap = (byte_order != _NATIVE_BO)
view = _sample_view(buf, bytes_per_sample)
if swap:
view.byteswap(inplace=True)
if bytes_per_sample == 2:
_predictor_decode_u16(view, width, height, samples)
elif bytes_per_sample == 4:
_predictor_decode_u32(view, width, height, samples)
elif bytes_per_sample == 8:
_predictor_decode_u64(view, width, height, samples)
else:
if swap:
view.byteswap(inplace=True)
raise ValueError(
f"predictor=2 with bytes_per_sample={bytes_per_sample} is not "
"supported (TIFF specifies sample-level differencing for 1/2/4/8 "
"byte samples)."
)
if swap:
view.byteswap(inplace=True)
return buf
def predictor_encode(data: np.ndarray, width: int, height: int,
bytes_per_sample: int, samples: int = 1,
byte_order: str = '<') -> np.ndarray:
"""Apply horizontal differencing predictor (predictor=2).
See :func:`predictor_decode` for the sample-level semantics.
Parameters
----------
data : np.ndarray
Flat uint8 array of pixel data (modified in-place).
width, height : int
Image dimensions in pixels.
bytes_per_sample : int
Bytes per sample.
samples : int
Samples per pixel.
byte_order : str
'<' or '>'. Defaults to little-endian, the writer's output order.
Returns
-------
np.ndarray
The same uint8 array with predictor=2 applied.
"""
buf = np.ascontiguousarray(data)
if bytes_per_sample == 1:
_predictor_encode_u8(buf, width, height, samples)
return buf
# Mirror the decode path: byteswap to native around the kernel call so
# numba can compile the in-place differencing. The writer always emits
# ``BO='<'`` today so this is normally a no-op, but the function stays
# robust to callers that pass the BE branch.
swap = (byte_order != _NATIVE_BO)
view = _sample_view(buf, bytes_per_sample)
if swap:
view.byteswap(inplace=True)
if bytes_per_sample == 2:
_predictor_encode_u16(view, width, height, samples)
elif bytes_per_sample == 4:
_predictor_encode_u32(view, width, height, samples)
elif bytes_per_sample == 8:
_predictor_encode_u64(view, width, height, samples)
else:
if swap:
view.byteswap(inplace=True)
raise ValueError(
f"predictor=2 with bytes_per_sample={bytes_per_sample} is not "
"supported (TIFF specifies sample-level differencing for 1/2/4/8 "
"byte samples)."
)
if swap:
view.byteswap(inplace=True)
return buf
# -- Floating-point predictor (predictor=3) -----------------------------------
#
# TIFF predictor=3 (floating-point horizontal differencing):
# During encoding, bytes of each sample are rearranged into byte-lane order
# (MSB lane first, LSB lane last), then horizontal differencing is applied
# across the entire transposed row.
#
# For little-endian float32 with N samples:
# Swizzled layout: [MSB_s0..MSB_sN-1, byte2_s0..byte2_sN-1,
# byte1_s0..byte1_sN-1, LSB_s0..LSB_sN-1]
# i.e. lane 0 = native byte (bps-1), lane 1 = native byte (bps-2), etc.
#
# Decode: undo differencing, then un-transpose (lane b -> native byte bps-1-b).
@ngjit
def _fp_predictor_decode_row(row_data, width, bps, big_endian):
"""Undo floating-point predictor for one row (in-place).
row_data: uint8 array of length width * bps.
Per TIFF Tech Note 3, lane 0 contains the most significant byte of
each sample regardless of the file's byte order. After
un-transposing, the MSB has to land at the byte position that
corresponds to "most significant" *in the file's byte order*: index 0
for big-endian files and index ``bps-1`` for little-endian files.
The previous implementation always wrote MSB at index ``bps-1``,
which scrambled big-endian predictor=3 reads.
"""
n = width * bps
# Step 1: undo horizontal differencing on the byte-swizzled row
for i in range(1, n):
row_data[i] = np.uint8((np.int32(row_data[i]) + np.int32(row_data[i - 1])) & 0xFF)
# Step 2: un-transpose bytes back to the file's native sample order
tmp = np.empty(n, dtype=np.uint8)
if big_endian:
# MSB lane (lane 0) -> byte index 0 in each output sample.
for sample in range(width):
for b in range(bps):
tmp[sample * bps + b] = row_data[b * width + sample]
else:
# MSB lane -> byte index ``bps-1`` (the high-order byte in LE).
for sample in range(width):
for b in range(bps):
tmp[sample * bps + b] = row_data[(bps - 1 - b) * width + sample]
for i in range(n):
row_data[i] = tmp[i]
@ngjit
def _fp_predictor_decode_rows(data, width, height, bps, big_endian):
"""Dispatch per-row decode from Numba, avoiding Python loop overhead."""
row_len = width * bps
for row in range(height):
start = row * row_len
_fp_predictor_decode_row(
data[start:start + row_len], width, bps, big_endian)
def fp_predictor_decode(data: np.ndarray, width: int, height: int,
bytes_per_sample: int,
big_endian: bool = False) -> np.ndarray:
"""Undo floating-point predictor (predictor=3).
Parameters
----------
data : np.ndarray
Flat uint8 array of decompressed tile/strip data.
width, height : int
Tile/strip dimensions.
bytes_per_sample : int
Bytes per sample (e.g. 4 for float32, 8 for float64).
big_endian : bool
Whether the source file is big-endian (BOM ``MM``). The
un-transpose puts the MSB lane at byte index 0 of each output
sample for BE files, and at byte index ``bps-1`` for LE files.
Returns
-------
np.ndarray
Corrected array.
"""
buf = np.ascontiguousarray(data)
_fp_predictor_decode_rows(buf, width, height, bytes_per_sample,
big_endian)
return buf
@ngjit
def _fp_predictor_encode_row(row_data, width, bps):
"""Apply floating-point predictor for one row (in-place)."""
n = width * bps
# Step 1: transpose to byte-swizzled layout (MSB lane first)
# Native byte b of each sample goes to lane (bps-1-b).
tmp = np.empty(n, dtype=np.uint8)
for sample in range(width):
for b in range(bps):
tmp[(bps - 1 - b) * width + sample] = row_data[sample * bps + b]
for i in range(n):
row_data[i] = tmp[i]
# Step 2: horizontal differencing on the swizzled row (right to left)
for i in range(n - 1, 0, -1):
row_data[i] = np.uint8((np.int32(row_data[i]) - np.int32(row_data[i - 1])) & 0xFF)
@ngjit
def _fp_predictor_encode_rows(data, width, height, bps):
"""Dispatch per-row encode from Numba, avoiding Python loop overhead."""
row_len = width * bps
for row in range(height):
start = row * row_len
_fp_predictor_encode_row(data[start:start + row_len], width, bps)
def fp_predictor_encode(data: np.ndarray, width: int, height: int,
bytes_per_sample: int) -> np.ndarray:
"""Apply floating-point predictor (predictor=3).
Parameters
----------
data : np.ndarray
Flat uint8 array of pixel data.
width, height : int
Dimensions.
bytes_per_sample : int
Bytes per sample.
Returns
-------
np.ndarray
Encoded array.
"""
buf = np.ascontiguousarray(data)
_fp_predictor_encode_rows(buf, width, height, bytes_per_sample)
return buf
# -- Sub-byte bit unpacking ---------------------------------------------------
def unpack_bits(data: np.ndarray, bps: int, pixel_count: int) -> np.ndarray:
"""Unpack sub-byte pixel data into one value per array element.
Parameters
----------
data : np.ndarray
Flat uint8 array of packed bytes.
bps : int
Bits per sample (1, 2, 4, or 12).
pixel_count : int
Number of pixels to unpack.
Returns
-------
np.ndarray
uint8 for bps <= 8, uint16 for bps=12.
"""
if bps == 1:
# MSB-first: each byte holds 8 pixels
out = np.unpackbits(data)[:pixel_count]
return out.astype(np.uint8)
elif bps == 2:
# 4 pixels per byte, MSB-first
out = np.empty(pixel_count, dtype=np.uint8)
for i in range(min(len(data), (pixel_count + 3) // 4)):
b = data[i]
base = i * 4
if base < pixel_count:
out[base] = (b >> 6) & 0x03
if base + 1 < pixel_count:
out[base + 1] = (b >> 4) & 0x03
if base + 2 < pixel_count:
out[base + 2] = (b >> 2) & 0x03
if base + 3 < pixel_count:
out[base + 3] = b & 0x03
return out
elif bps == 4:
# 2 pixels per byte, high nibble first
out = np.empty(pixel_count, dtype=np.uint8)
for i in range(min(len(data), (pixel_count + 1) // 2)):
b = data[i]
base = i * 2
if base < pixel_count:
out[base] = (b >> 4) & 0x0F
if base + 1 < pixel_count:
out[base + 1] = b & 0x0F
return out
elif bps == 12:
# 2 pixels per 3 bytes, MSB-first
out = np.empty(pixel_count, dtype=np.uint16)
n_pairs = pixel_count // 2
remainder = pixel_count % 2
for i in range(n_pairs):
off = i * 3
if off + 2 < len(data):
b0 = int(data[off])
b1 = int(data[off + 1])
b2 = int(data[off + 2])
out[i * 2] = (b0 << 4) | (b1 >> 4)
out[i * 2 + 1] = ((b1 & 0x0F) << 8) | b2
if remainder and n_pairs * 3 + 1 < len(data):
off = n_pairs * 3
out[pixel_count - 1] = (int(data[off]) << 4) | (int(data[off + 1]) >> 4)
return out
else:
raise ValueError(f"Unsupported sub-byte bit depth: {bps}")
# -- PackBits (simple RLE) ----------------------------------------------------
def packbits_decompress(data: bytes, expected_size: int = 0) -> bytes:
"""Decompress PackBits (TIFF compression tag 32773).
Simple RLE: read a header byte n.
- 0 <= n <= 127: copy the next n+1 bytes literally.
- -127 <= n <= -1: repeat the next byte 1-n times.
- n == -128: no-op.
When ``expected_size`` > 0, the decoder refuses to emit more than
``expected_size * 1.05 + 1`` bytes and raises ``ValueError`` on overflow
(decompression-bomb guard).
"""
src = data if isinstance(data, (bytes, bytearray)) else bytes(data)
cap = _max_output_with_margin(expected_size)
out = bytearray()
i = 0
length = len(src)
while i < length:
n = src[i]
if n > 127:
n = n - 256 # interpret as signed
i += 1
if 0 <= n <= 127:
count = n + 1
out.extend(src[i:i + count])
i += count
elif -127 <= n <= -1:
if i < length:
out.extend(bytes([src[i]]) * (1 - n))
i += 1
# n == -128: skip
if cap and len(out) > cap:
raise ValueError(
f"packbits decode exceeded expected size: {len(out)} bytes "
f"produced, cap is {cap} (expected {expected_size}). "
f"Likely a decompression bomb."
)
return bytes(out)
def packbits_compress(data: bytes) -> bytes:
"""Compress data using PackBits."""
src = data if isinstance(data, (bytes, bytearray)) else bytes(data)
out = bytearray()
i = 0
length = len(src)
while i < length:
# Check for a run of identical bytes
j = i + 1
while j < length and j - i < 128 and src[j] == src[i]:
j += 1
run_len = j - i
if run_len >= 3:
# Encode as run
out.append((256 - (run_len - 1)) & 0xFF)
out.append(src[i])
i = j
else:
# Literal run: accumulate non-repeating bytes
lit_start = i
i = j
while i < length and i - lit_start < 128:
# Check if a run starts here
if i + 2 < length and src[i] == src[i + 1] == src[i + 2]:
break
i += 1
lit_len = i - lit_start
out.append(lit_len - 1)
out.extend(src[lit_start:lit_start + lit_len])
return bytes(out)
# -- JPEG codec (via Pillow) --------------------------------------------------
JPEG_AVAILABLE = False
try:
from PIL import Image
JPEG_AVAILABLE = True
except ImportError:
pass
def _splice_jpeg_tables(tile_data: bytes,
jpeg_tables: bytes | None) -> bytes:
"""Splice a JPEGTables stream into a tile's JPEG fragment.
GDAL-style tiled JPEG TIFFs store DQT/DHT tables once in tag 347
(an abbreviated JPEG: SOI + tables + EOI) and each tile is a JPEG
fragment whose own DQT/DHT segments were stripped. To make a tile
self-contained, drop the tables stream's leading SOI and trailing
EOI and insert what remains after the tile's SOI marker.
Both buffers must start with SOI (FF D8). If either does not, the
tile data is returned unchanged so libjpeg sees its original input
and raises a meaningful error.
"""
if not jpeg_tables:
return tile_data
if len(tile_data) < 2 or tile_data[0] != 0xFF or tile_data[1] != 0xD8:
return tile_data
if len(jpeg_tables) < 4:
return tile_data
if jpeg_tables[0] != 0xFF or jpeg_tables[1] != 0xD8:
return tile_data
# Strip SOI from the tables stream, and EOI if present at the end.
tables_body = jpeg_tables[2:]
if len(tables_body) >= 2 and tables_body[-2] == 0xFF and tables_body[-1] == 0xD9:
tables_body = tables_body[:-2]
return tile_data[:2] + tables_body + tile_data[2:]
def jpeg_decompress(data: bytes, width: int = 0, height: int = 0,
samples: int = 1, jpeg_tables: bytes | None = None) -> bytes:
"""Decompress JPEG tile/strip data. Requires Pillow.