-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy path_compression.py
More file actions
1025 lines (859 loc) · 33.2 KB
/
_compression.py
File metadata and controls
1025 lines (859 loc) · 33.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
# -- Deflate (zlib wrapper) --------------------------------------------------
def deflate_decompress(data: bytes) -> bytes:
"""Decompress deflate/zlib data."""
return zlib.decompress(data)
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) --------------------------------------------
@ngjit
def _predictor_decode(data, width, height, bytes_per_sample):
"""Undo horizontal differencing predictor (TIFF predictor=2).
Operates in-place on the flat byte array, performing cumulative sum
per row at the sample level.
"""
row_bytes = width * bytes_per_sample
for row in range(height):
row_start = row * row_bytes
for col in range(bytes_per_sample, row_bytes):
idx = row_start + col
data[idx] = np.uint8((np.int32(data[idx]) + np.int32(data[idx - bytes_per_sample])) & 0xFF)
@ngjit
def _predictor_encode(data, width, height, bytes_per_sample):
"""Apply horizontal differencing predictor (TIFF predictor=2).
Operates in-place, converting absolute values to differences.
Process right-to-left to avoid overwriting values we still need.
"""
row_bytes = width * bytes_per_sample
for row in range(height):
row_start = row * row_bytes
for col in range(row_bytes - 1, bytes_per_sample - 1, -1):
idx = row_start + col
data[idx] = np.uint8((np.int32(data[idx]) - np.int32(data[idx - bytes_per_sample])) & 0xFF)
def predictor_decode(data: np.ndarray, width: int, height: int,
bytes_per_sample: int) -> np.ndarray:
"""Undo horizontal differencing predictor (predictor=2).
Parameters
----------
data : np.ndarray
Flat uint8 array of decompressed pixel data (modified in-place).
width, height : int
Image dimensions.
bytes_per_sample : int
Bytes per sample (e.g. 1 for uint8, 4 for float32).
Returns
-------
np.ndarray
Same array, modified in-place.
"""
buf = np.ascontiguousarray(data)
_predictor_decode(buf, width, height, bytes_per_sample)
return buf
def predictor_encode(data: np.ndarray, width: int, height: int,
bytes_per_sample: int) -> np.ndarray:
"""Apply horizontal differencing predictor (predictor=2).
Parameters
----------
data : np.ndarray
Flat uint8 array of pixel data (modified in-place).
width, height : int
Image dimensions.
bytes_per_sample : int
Bytes per sample.
Returns
-------
np.ndarray
Same array, modified in-place.
"""
buf = np.ascontiguousarray(data)
_predictor_encode(buf, width, height, bytes_per_sample)
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):
"""Undo floating-point predictor for one row (in-place).
row_data: uint8 array of length width * bps
"""
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 native sample order
tmp = np.empty(n, dtype=np.uint8)
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):
"""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)
def fp_predictor_decode(data: np.ndarray, width: int, height: int,
bytes_per_sample: int) -> 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).
Returns
-------
np.ndarray
Corrected array.
"""
buf = np.ascontiguousarray(data)
_fp_predictor_decode_rows(buf, width, height, bytes_per_sample)
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)
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)
row_len = width * bytes_per_sample
for row in range(height):
start = row * row_len
_fp_predictor_encode_row(buf[start:start + row_len], width, 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) -> 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.
"""
src = data if isinstance(data, (bytes, bytearray)) else bytes(data)
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
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 jpeg_decompress(data: bytes, width: int = 0, height: int = 0,
samples: int = 1) -> bytes:
"""Decompress JPEG tile/strip data. Requires Pillow."""
if not JPEG_AVAILABLE:
raise ImportError(
"Pillow is required to read JPEG-compressed TIFFs. "
"Install it with: pip install Pillow")
import io
img = Image.open(io.BytesIO(data))
return np.asarray(img).tobytes()
def jpeg_compress(data: bytes, width: int, height: int,
samples: int = 1, quality: int = 75) -> bytes:
"""Compress raw pixel data as JPEG. Requires Pillow."""
if not JPEG_AVAILABLE:
raise ImportError(
"Pillow is required to write JPEG-compressed TIFFs. "
"Install it with: pip install Pillow")
import io
if samples == 1:
arr = np.frombuffer(data, dtype=np.uint8).reshape(height, width)
img = Image.fromarray(arr, mode='L')
elif samples == 3:
arr = np.frombuffer(data, dtype=np.uint8).reshape(height, width, 3)
img = Image.fromarray(arr, mode='RGB')
else:
raise ValueError(f"JPEG compression requires 1 or 3 bands, got {samples}")
buf = io.BytesIO()
img.save(buf, format='JPEG', quality=quality)
return buf.getvalue()
# -- ZSTD codec (via zstandard) -----------------------------------------------
ZSTD_AVAILABLE = False
try:
import zstandard as _zstd
ZSTD_AVAILABLE = True
except ImportError:
_zstd = None
def zstd_decompress(data: bytes) -> bytes:
"""Decompress Zstandard data. Requires the ``zstandard`` package."""
if not ZSTD_AVAILABLE:
raise ImportError(
"zstandard is required to read ZSTD-compressed TIFFs. "
"Install it with: pip install zstandard")
return _zstd.ZstdDecompressor().decompress(data)
def zstd_compress(data: bytes, level: int = 3) -> bytes:
"""Compress data with Zstandard. Requires the ``zstandard`` package."""
if not ZSTD_AVAILABLE:
raise ImportError(
"zstandard is required to write ZSTD-compressed TIFFs. "
"Install it with: pip install zstandard")
return _zstd.ZstdCompressor(level=level).compress(data)
# -- JPEG 2000 codec (via glymur) --------------------------------------------
JPEG2000_AVAILABLE = False
try:
import glymur as _glymur
JPEG2000_AVAILABLE = True
except ImportError:
_glymur = None
def jpeg2000_decompress(data: bytes, width: int = 0, height: int = 0,
samples: int = 1) -> bytes:
"""Decompress a JPEG 2000 codestream. Requires ``glymur``."""
if not JPEG2000_AVAILABLE:
raise ImportError(
"glymur is required to read JPEG 2000-compressed TIFFs. "
"Install it with: pip install glymur")
import tempfile
import os
# glymur reads from files, so write the codestream to a temp file
fd, tmp = tempfile.mkstemp(suffix='.j2k')
try:
os.write(fd, data)
os.close(fd)
jp2 = _glymur.Jp2k(tmp)
arr = jp2[:]
return arr.tobytes()
finally:
os.unlink(tmp)
def jpeg2000_compress(data: bytes, width: int, height: int,
samples: int = 1, dtype: np.dtype = np.dtype('uint8'),
lossless: bool = True) -> bytes:
"""Compress raw pixel data as JPEG 2000 codestream. Requires ``glymur``."""
if not JPEG2000_AVAILABLE:
raise ImportError(
"glymur is required to write JPEG 2000-compressed TIFFs. "
"Install it with: pip install glymur")
import math
import tempfile
import os
if samples == 1:
arr = np.frombuffer(data, dtype=dtype).reshape(height, width)
else:
arr = np.frombuffer(data, dtype=dtype).reshape(height, width, samples)
fd, tmp = tempfile.mkstemp(suffix='.j2k')
os.close(fd)
os.unlink(tmp) # glymur needs the file to not exist
try:
cratios = [1] if lossless else [20]
# numres must be <= log2(min_dim) + 1 to avoid OpenJPEG errors
min_dim = max(min(width, height), 1)
numres = min(6, int(math.log2(min_dim)) + 1)
numres = max(numres, 1)
_glymur.Jp2k(tmp, data=arr, cratios=cratios, numres=numres)
with open(tmp, 'rb') as f:
return f.read()
finally:
if os.path.exists(tmp):
os.unlink(tmp)
# -- LERC codec (via lerc) ----------------------------------------------------
LERC_AVAILABLE = False
try:
import lerc as _lerc
LERC_AVAILABLE = True
except ImportError:
_lerc = None
def lerc_decompress(data: bytes, width: int = 0, height: int = 0,
samples: int = 1) -> bytes:
"""Decompress LERC data. Requires the ``lerc`` package."""
if not LERC_AVAILABLE:
raise ImportError(
"lerc is required to read LERC-compressed TIFFs. "
"Install it with: pip install lerc")
result = _lerc.decode(data)
# lerc.decode returns (result_code, data_array, valid_mask, ...)
if result[0] != 0:
raise RuntimeError(f"LERC decode failed with error code {result[0]}")
arr = result[1]
return arr.tobytes()
def lerc_compress(data: bytes, width: int, height: int,
samples: int = 1, dtype: np.dtype = np.dtype('float32'),
max_z_error: float = 0.0) -> bytes:
"""Compress raw pixel data with LERC. Requires the ``lerc`` package.
Parameters
----------
max_z_error : float
Maximum encoding error per pixel. 0 = lossless.
"""
if not LERC_AVAILABLE:
raise ImportError(
"lerc is required to write LERC-compressed TIFFs. "
"Install it with: pip install lerc")
if samples == 1:
arr = np.frombuffer(data, dtype=dtype).reshape(height, width)
else:
arr = np.frombuffer(data, dtype=dtype).reshape(height, width, samples)
n_values_per_pixel = samples
# lerc.encode(npArr, nValuesPerPixel, bHasMask, npValidMask,
# maxZErr, nBytesHint)
# nBytesHint=1 triggers actual encoding (0 = compute size only)
result = _lerc.encode(arr, n_values_per_pixel, False, None,
max_z_error, 1)
if result[0] != 0:
raise RuntimeError(f"LERC encode failed with error code {result[0]}")
# result is (error_code, nBytesWritten, ctypes_buffer)
return bytes(result[2])
# -- LZ4 codec (via python-lz4) -----------------------------------------------
LZ4_AVAILABLE = False
try:
import lz4.frame as _lz4
LZ4_AVAILABLE = True
except ImportError:
_lz4 = None
def lz4_decompress(data: bytes) -> bytes:
"""Decompress LZ4 frame data. Requires the ``lz4`` package."""
if not LZ4_AVAILABLE:
raise ImportError(
"lz4 is required to read LZ4-compressed TIFFs. "
"Install it with: pip install lz4")
return _lz4.decompress(data)
def lz4_compress(data: bytes, level: int = 0) -> bytes:
"""Compress data with LZ4 frame format. Requires the ``lz4`` package."""
if not LZ4_AVAILABLE:
raise ImportError(
"lz4 is required to write LZ4-compressed TIFFs. "
"Install it with: pip install lz4")
return _lz4.compress(data, compression_level=level)
# -- LERC codec (via lerc) ----------------------------------------------------
LERC_AVAILABLE = False
try:
import lerc as _lerc
LERC_AVAILABLE = True
except ImportError:
_lerc = None
def lerc_decompress(data: bytes, width: int = 0, height: int = 0,
samples: int = 1) -> bytes:
"""Decompress LERC data. Requires the ``lerc`` package."""
if not LERC_AVAILABLE:
raise ImportError(
"lerc is required to read LERC-compressed TIFFs. "
"Install it with: pip install lerc")
result = _lerc.decode(data)
# lerc.decode returns (result_code, data_array, valid_mask, ...)
if result[0] != 0:
raise RuntimeError(f"LERC decode failed with error code {result[0]}")
arr = result[1]
return arr.tobytes()
def lerc_compress(data: bytes, width: int, height: int,
samples: int = 1, dtype: np.dtype = np.dtype('float32'),
max_z_error: float = 0.0) -> bytes:
"""Compress raw pixel data with LERC. Requires the ``lerc`` package.
Parameters
----------
max_z_error : float
Maximum encoding error per pixel. 0 = lossless.
"""
if not LERC_AVAILABLE:
raise ImportError(
"lerc is required to write LERC-compressed TIFFs. "
"Install it with: pip install lerc")
if samples == 1:
arr = np.frombuffer(data, dtype=dtype).reshape(height, width)
else:
arr = np.frombuffer(data, dtype=dtype).reshape(height, width, samples)
n_values_per_pixel = samples
# lerc.encode(npArr, nValuesPerPixel, bHasMask, npValidMask,
# maxZErr, nBytesHint)
# nBytesHint=1 triggers actual encoding (0 = compute size only)
result = _lerc.encode(arr, n_values_per_pixel, False, None,
max_z_error, 1)
if result[0] != 0:
raise RuntimeError(f"LERC encode failed with error code {result[0]}")
# result is (error_code, nBytesWritten, ctypes_buffer)
return bytes(result[2])
# -- Dispatch helpers ---------------------------------------------------------
# TIFF compression tag values
COMPRESSION_NONE = 1
COMPRESSION_LZW = 5
COMPRESSION_JPEG = 7
COMPRESSION_DEFLATE = 8
COMPRESSION_JPEG2000 = 34712
COMPRESSION_ZSTD = 50000
COMPRESSION_LZ4 = 50004
COMPRESSION_PACKBITS = 32773
COMPRESSION_LERC = 34887
COMPRESSION_ADOBE_DEFLATE = 32946
def decompress(data, compression: int, expected_size: int = 0,
width: int = 0, height: int = 0, samples: int = 1) -> np.ndarray:
"""Decompress tile/strip data based on TIFF compression tag.
Parameters
----------
data : bytes
Compressed data.
compression : int
TIFF compression tag value.
expected_size : int
Expected decompressed size (used for LZW buffer allocation).
Returns
-------
np.ndarray
uint8 array. Mutable for LZW/deflate; may be read-only view for
uncompressed data (caller must .copy() if mutation is needed).
"""
if compression == COMPRESSION_NONE:
return np.frombuffer(data, dtype=np.uint8)
elif compression in (COMPRESSION_DEFLATE, COMPRESSION_ADOBE_DEFLATE):
return np.frombuffer(deflate_decompress(data), dtype=np.uint8)
elif compression == COMPRESSION_LZW:
return lzw_decompress(data, expected_size)
elif compression == COMPRESSION_PACKBITS:
return np.frombuffer(packbits_decompress(data), dtype=np.uint8)
elif compression == COMPRESSION_JPEG:
return np.frombuffer(jpeg_decompress(data, width, height, samples),
dtype=np.uint8)
elif compression == COMPRESSION_ZSTD:
return np.frombuffer(zstd_decompress(data), dtype=np.uint8)
elif compression == COMPRESSION_JPEG2000:
return np.frombuffer(
jpeg2000_decompress(data, width, height, samples), dtype=np.uint8)
elif compression == COMPRESSION_LZ4:
return np.frombuffer(lz4_decompress(data), dtype=np.uint8)
elif compression == COMPRESSION_LERC:
return np.frombuffer(
lerc_decompress(data, width, height, samples), dtype=np.uint8)
else:
raise ValueError(f"Unsupported compression type: {compression}")
def compress(data: bytes, compression: int, level: int = 6) -> bytes:
"""Compress data based on TIFF compression tag.
Parameters
----------
data : bytes
Raw data.
compression : int
TIFF compression tag value.
level : int
Compression level (for deflate).