solarian-diffusion 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- diffusion_policy/__init__.py +0 -0
- diffusion_policy/codecs/__init__.py +0 -0
- diffusion_policy/codecs/imagecodecs_numcodecs.py +1408 -0
- diffusion_policy/common/__init__.py +0 -0
- diffusion_policy/common/checkpoint_util.py +64 -0
- diffusion_policy/common/json_logger.py +117 -0
- diffusion_policy/common/normalize_util.py +251 -0
- diffusion_policy/common/pose_repr_util.py +112 -0
- diffusion_policy/common/pytorch_util.py +82 -0
- diffusion_policy/common/q_critic_util.py +135 -0
- diffusion_policy/common/replay_buffer.py +672 -0
- diffusion_policy/common/sampler.py +204 -0
- diffusion_policy/config/legacy/task/blockpush_lowdim_seed.yaml +34 -0
- diffusion_policy/config/legacy/task/blockpush_lowdim_seed_abs.yaml +34 -0
- diffusion_policy/config/legacy/task/can_image.yaml +64 -0
- diffusion_policy/config/legacy/task/can_image_abs.yaml +64 -0
- diffusion_policy/config/legacy/task/can_lowdim.yaml +45 -0
- diffusion_policy/config/legacy/task/can_lowdim_abs.yaml +46 -0
- diffusion_policy/config/legacy/task/kitchen_lowdim.yaml +33 -0
- diffusion_policy/config/legacy/task/kitchen_lowdim_abs.yaml +38 -0
- diffusion_policy/config/legacy/task/lift_abs.yaml +72 -0
- diffusion_policy/config/legacy/task/lift_image.yaml +64 -0
- diffusion_policy/config/legacy/task/lift_image_abs.yaml +63 -0
- diffusion_policy/config/legacy/task/lift_lowdim.yaml +46 -0
- diffusion_policy/config/legacy/task/lift_lowdim_abs.yaml +47 -0
- diffusion_policy/config/legacy/task/pusht_image.yaml +40 -0
- diffusion_policy/config/legacy/task/pusht_lowdim.yaml +34 -0
- diffusion_policy/config/legacy/task/real_pusht_image.yaml +47 -0
- diffusion_policy/config/legacy/task/square_abs.yaml +72 -0
- diffusion_policy/config/legacy/task/square_image.yaml +64 -0
- diffusion_policy/config/legacy/task/square_image_abs.yaml +64 -0
- diffusion_policy/config/legacy/task/square_lowdim.yaml +46 -0
- diffusion_policy/config/legacy/task/square_lowdim_abs.yaml +47 -0
- diffusion_policy/config/legacy/task/tool_hang_abs.yaml +71 -0
- diffusion_policy/config/legacy/task/tool_hang_image.yaml +63 -0
- diffusion_policy/config/legacy/task/tool_hang_image_abs.yaml +63 -0
- diffusion_policy/config/legacy/task/tool_hang_lowdim.yaml +45 -0
- diffusion_policy/config/legacy/task/tool_hang_lowdim_abs.yaml +46 -0
- diffusion_policy/config/legacy/task/transport_image.yaml +75 -0
- diffusion_policy/config/legacy/task/transport_image_abs.yaml +75 -0
- diffusion_policy/config/legacy/task/transport_lowdim.yaml +49 -0
- diffusion_policy/config/legacy/task/transport_lowdim_abs.yaml +49 -0
- diffusion_policy/config/legacy/task/umi.yaml +45 -0
- diffusion_policy/config/legacy/task/umi_image.yaml +36 -0
- diffusion_policy/config/legacy/task/umi_teleop_image.yaml +41 -0
- diffusion_policy/config/legacy/train_bet_lowdim_workspace.yaml +132 -0
- diffusion_policy/config/legacy/train_diffusion_transformer_hybrid_workspace.yaml +141 -0
- diffusion_policy/config/legacy/train_diffusion_transformer_lowdim_kitchen_workspace.yaml +146 -0
- diffusion_policy/config/legacy/train_diffusion_transformer_lowdim_pusht_workspace.yaml +146 -0
- diffusion_policy/config/legacy/train_diffusion_transformer_lowdim_workspace.yaml +146 -0
- diffusion_policy/config/legacy/train_diffusion_transformer_real_hybrid_workspace.yaml +144 -0
- diffusion_policy/config/legacy/train_diffusion_transformer_umi_teleop_hybrid_workspace.yaml +156 -0
- diffusion_policy/config/legacy/train_diffusion_unet_ddim_hybrid_workspace.yaml +140 -0
- diffusion_policy/config/legacy/train_diffusion_unet_ddim_lowdim_workspace.yaml +146 -0
- diffusion_policy/config/legacy/train_diffusion_unet_hybrid_workspace.yaml +137 -0
- diffusion_policy/config/legacy/train_diffusion_unet_image_pretrained_workspace.yaml +149 -0
- diffusion_policy/config/legacy/train_diffusion_unet_image_workspace.yaml +150 -0
- diffusion_policy/config/legacy/train_diffusion_unet_lowdim_workspace.yaml +143 -0
- diffusion_policy/config/legacy/train_diffusion_unet_real_hybrid_workspace.yaml +141 -0
- diffusion_policy/config/legacy/train_diffusion_unet_real_image_workspace.yaml +152 -0
- diffusion_policy/config/legacy/train_diffusion_unet_real_pretrained_workspace.yaml +152 -0
- diffusion_policy/config/legacy/train_diffusion_unet_timm_workspace.yaml +145 -0
- diffusion_policy/config/legacy/train_diffusion_unet_umi_hybrid_workspace.yaml +141 -0
- diffusion_policy/config/legacy/train_diffusion_unet_umi_teleop_hybrid_workspace.yaml +153 -0
- diffusion_policy/config/legacy/train_diffusion_unet_umi_timm_workspace.yaml +145 -0
- diffusion_policy/config/legacy/train_diffusion_unet_umi_workspace.yaml +150 -0
- diffusion_policy/config/legacy/train_diffusion_unet_video_workspace.yaml +128 -0
- diffusion_policy/config/legacy/train_ibc_dfo_hybrid_workspace.yaml +111 -0
- diffusion_policy/config/legacy/train_ibc_dfo_lowdim_workspace.yaml +109 -0
- diffusion_policy/config/legacy/train_ibc_dfo_real_hybrid_workspace.yaml +111 -0
- diffusion_policy/config/legacy/train_robomimic_image_workspace.yaml +92 -0
- diffusion_policy/config/legacy/train_robomimic_lowdim_workspace.yaml +92 -0
- diffusion_policy/config/legacy/train_robomimic_real_image_workspace.yaml +92 -0
- diffusion_policy/config/rl_annotate.yaml +265 -0
- diffusion_policy/config/rl_annotate_per_frame.yaml +162 -0
- diffusion_policy/config/rl_annotate_per_frame_v4.yaml +162 -0
- diffusion_policy/config/rl_annotate_v2.yaml +141 -0
- diffusion_policy/config/rl_annotate_v3.yaml +165 -0
- diffusion_policy/config/task/lift.yaml +89 -0
- diffusion_policy/config/task/square.yaml +92 -0
- diffusion_policy/config/task/tool_hang.yaml +93 -0
- diffusion_policy/config/task/tool_hang_abs.yaml +71 -0
- diffusion_policy/config/task/umi.yaml +105 -0
- diffusion_policy/config/task/umi_bimanual.yaml +135 -0
- diffusion_policy/config/task/umi_cup_wild.yaml +106 -0
- diffusion_policy/config/task/umi_image.yaml +36 -0
- diffusion_policy/config/task/umi_pick_lego.yaml +106 -0
- diffusion_policy/config/task/umi_pick_single_lego_175.yaml +106 -0
- diffusion_policy/config/task/umi_pick_single_lego_bbox.yaml +135 -0
- diffusion_policy/config/task/umi_teleop_image.yaml +41 -0
- diffusion_policy/config/train_diffusion_transformer_umi_bimanual_workspace.yaml +185 -0
- diffusion_policy/config/train_diffusion_transformer_umi_workspace.yaml +185 -0
- diffusion_policy/config/train_diffusion_unet_image_workspace.yaml +152 -0
- diffusion_policy/config/train_diffusion_unet_timm_robomimic_workspace.yaml +152 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_cup_wild.yaml +260 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_pick_lego_pile_bbox.yaml +265 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_pick_lego_pile_bbox_awr.yaml +324 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_pick_lego_pile_bbox_parl.yaml +399 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_pick_single_lego.yaml +258 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_pick_single_lego_125.yaml +260 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_pick_single_lego_175.yaml +260 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_pick_single_lego_bbox.yaml +265 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_pick_two_legos_bbox.yaml +265 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_workspace.yaml +224 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_workspace_lego.yaml +213 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_workspace_lego_resume.yaml +213 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_workspace_pick_lego.yaml +254 -0
- diffusion_policy/config/train_diffusion_unet_timm_umi_workspace_red_cup.yaml +219 -0
- diffusion_policy/config/train_diffusion_unet_umi_bimanual_workspace.yaml +203 -0
- diffusion_policy/config/train_iql_umi.yaml +186 -0
- diffusion_policy/config/train_iql_umi_v2.yaml +212 -0
- diffusion_policy/config/train_iql_umi_v3.yaml +212 -0
- diffusion_policy/dataset/__init__.py +0 -0
- diffusion_policy/dataset/base_dataset.py +76 -0
- diffusion_policy/dataset/umi_dataset.py +652 -0
- diffusion_policy/env_runner/__init__.py +0 -0
- diffusion_policy/env_runner/base_image_runner.py +9 -0
- diffusion_policy/env_runner/real_pusht_image_runner.py +10 -0
- diffusion_policy/model/__init__.py +0 -0
- diffusion_policy/model/common/__init__.py +0 -0
- diffusion_policy/model/common/dict_of_tensor_mixin.py +38 -0
- diffusion_policy/model/common/lr_scheduler.py +46 -0
- diffusion_policy/model/common/module_attr_mixin.py +14 -0
- diffusion_policy/model/common/normalizer.py +353 -0
- diffusion_policy/model/common/tensor_util.py +960 -0
- diffusion_policy/model/diffusion/__init__.py +0 -0
- diffusion_policy/model/diffusion/conditional_unet1d.py +238 -0
- diffusion_policy/model/diffusion/conv1d_components.py +46 -0
- diffusion_policy/model/diffusion/ema_model.py +88 -0
- diffusion_policy/model/diffusion/mask_generator.py +221 -0
- diffusion_policy/model/diffusion/positional_embedding.py +17 -0
- diffusion_policy/model/vision/__init__.py +0 -0
- diffusion_policy/model/vision/crop_randomizer.py +288 -0
- diffusion_policy/model/vision/multi_image_obs_encoder.py +195 -0
- diffusion_policy/model/vision/timm_obs_encoder.py +525 -0
- diffusion_policy/policy/__init__.py +0 -0
- diffusion_policy/policy/base_image_policy.py +27 -0
- diffusion_policy/policy/diffusion_unet_image_policy.py +264 -0
- diffusion_policy/policy/diffusion_unet_timm_policy.py +607 -0
- diffusion_policy/policy/parl_action_optimizer.py +675 -0
- diffusion_policy/workspace/__init__.py +0 -0
- diffusion_policy/workspace/base_workspace.py +146 -0
- diffusion_policy/workspace/train_diffusion_unet_image_workspace.py +1585 -0
- solarian_diffusion/__init__.py +32 -0
- solarian_diffusion/config.py +31 -0
- solarian_diffusion/policy_server.py +316 -0
- solarian_diffusion/rtc.py +272 -0
- solarian_diffusion/service.py +16 -0
- solarian_diffusion/trainer.py +213 -0
- solarian_diffusion-0.1.0.dist-info/METADATA +126 -0
- solarian_diffusion-0.1.0.dist-info/RECORD +159 -0
- solarian_diffusion-0.1.0.dist-info/WHEEL +5 -0
- solarian_diffusion-0.1.0.dist-info/entry_points.txt +8 -0
- solarian_diffusion-0.1.0.dist-info/licenses/LICENSE +201 -0
- solarian_diffusion-0.1.0.dist-info/licenses/NOTICE +45 -0
- solarian_diffusion-0.1.0.dist-info/top_level.txt +3 -0
- umi/__init__.py +0 -0
- umi/common/__init__.py +0 -0
- umi/common/pose_util.py +115 -0
|
@@ -0,0 +1,1408 @@
|
|
|
1
|
+
|
|
2
|
+
# imagecodecs/numcodecs.py
|
|
3
|
+
|
|
4
|
+
# Copyright (c) 2021-2022, Christoph Gohlke
|
|
5
|
+
# All rights reserved.
|
|
6
|
+
#
|
|
7
|
+
# Redistribution and use in source and binary forms, with or without
|
|
8
|
+
# modification, are permitted provided that the following conditions are met:
|
|
9
|
+
#
|
|
10
|
+
# 1. Redistributions of source code must retain the above copyright notice,
|
|
11
|
+
# this list of conditions and the following disclaimer.
|
|
12
|
+
#
|
|
13
|
+
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
14
|
+
# this list of conditions and the following disclaimer in the documentation
|
|
15
|
+
# and/or other materials provided with the distribution.
|
|
16
|
+
#
|
|
17
|
+
# 3. Neither the name of the copyright holder nor the names of its
|
|
18
|
+
# contributors may be used to endorse or promote products derived from
|
|
19
|
+
# this software without specific prior written permission.
|
|
20
|
+
#
|
|
21
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
22
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
23
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
24
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
25
|
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
26
|
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
27
|
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
28
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
29
|
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
30
|
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
31
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
|
32
|
+
|
|
33
|
+
"""Additional numcodecs implemented using imagecodecs."""
|
|
34
|
+
|
|
35
|
+
__version__ = '2022.9.26'
|
|
36
|
+
|
|
37
|
+
__all__ = ('register_codecs',)
|
|
38
|
+
|
|
39
|
+
import numpy
|
|
40
|
+
from numcodecs.abc import Codec
|
|
41
|
+
from numcodecs.registry import register_codec, get_codec
|
|
42
|
+
|
|
43
|
+
import imagecodecs
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def protective_squeeze(x: numpy.ndarray):
|
|
47
|
+
"""
|
|
48
|
+
Squeeze dim only if it's not the last dim.
|
|
49
|
+
Image dim expected to be *, H, W, C
|
|
50
|
+
"""
|
|
51
|
+
img_shape = x.shape[-3:]
|
|
52
|
+
if len(x.shape) > 3:
|
|
53
|
+
n_imgs = numpy.prod(x.shape[:-3])
|
|
54
|
+
if n_imgs > 1:
|
|
55
|
+
img_shape = (-1,) + img_shape
|
|
56
|
+
return x.reshape(img_shape)
|
|
57
|
+
|
|
58
|
+
def get_default_image_compressor(**kwargs):
|
|
59
|
+
if imagecodecs.JPEGXL:
|
|
60
|
+
# has JPEGXL
|
|
61
|
+
this_kwargs = {
|
|
62
|
+
'effort': 3,
|
|
63
|
+
'distance': 0.3,
|
|
64
|
+
# bug in libjxl, invalid codestream for non-lossless
|
|
65
|
+
# when decoding speed > 1
|
|
66
|
+
'decodingspeed': 1
|
|
67
|
+
}
|
|
68
|
+
this_kwargs.update(kwargs)
|
|
69
|
+
return JpegXl(**this_kwargs)
|
|
70
|
+
else:
|
|
71
|
+
this_kwargs = {
|
|
72
|
+
'level': 50
|
|
73
|
+
}
|
|
74
|
+
this_kwargs.update(kwargs)
|
|
75
|
+
return Jpeg2k(**this_kwargs)
|
|
76
|
+
|
|
77
|
+
class Aec(Codec):
|
|
78
|
+
"""AEC codec for numcodecs."""
|
|
79
|
+
|
|
80
|
+
codec_id = 'imagecodecs_aec'
|
|
81
|
+
|
|
82
|
+
def __init__(
|
|
83
|
+
self, bitspersample=None, flags=None, blocksize=None, rsi=None
|
|
84
|
+
):
|
|
85
|
+
self.bitspersample = bitspersample
|
|
86
|
+
self.flags = flags
|
|
87
|
+
self.blocksize = blocksize
|
|
88
|
+
self.rsi = rsi
|
|
89
|
+
|
|
90
|
+
def encode(self, buf):
|
|
91
|
+
return imagecodecs.aec_encode(
|
|
92
|
+
buf,
|
|
93
|
+
bitspersample=self.bitspersample,
|
|
94
|
+
flags=self.flags,
|
|
95
|
+
blocksize=self.blocksize,
|
|
96
|
+
rsi=self.rsi,
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
def decode(self, buf, out=None):
|
|
100
|
+
return imagecodecs.aec_decode(
|
|
101
|
+
buf,
|
|
102
|
+
bitspersample=self.bitspersample,
|
|
103
|
+
flags=self.flags,
|
|
104
|
+
blocksize=self.blocksize,
|
|
105
|
+
rsi=self.rsi,
|
|
106
|
+
out=_flat(out),
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
class Apng(Codec):
|
|
111
|
+
"""APNG codec for numcodecs."""
|
|
112
|
+
|
|
113
|
+
codec_id = 'imagecodecs_apng'
|
|
114
|
+
|
|
115
|
+
def __init__(self, level=None, photometric=None, delay=None):
|
|
116
|
+
self.level = level
|
|
117
|
+
self.photometric = photometric
|
|
118
|
+
self.delay = delay
|
|
119
|
+
|
|
120
|
+
def encode(self, buf):
|
|
121
|
+
buf = protective_squeeze(numpy.asarray(buf))
|
|
122
|
+
return imagecodecs.apng_encode(
|
|
123
|
+
buf,
|
|
124
|
+
level=self.level,
|
|
125
|
+
photometric=self.photometric,
|
|
126
|
+
delay=self.delay,
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
def decode(self, buf, out=None):
|
|
130
|
+
return imagecodecs.apng_decode(buf, out=out)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
class Avif(Codec):
|
|
134
|
+
"""AVIF codec for numcodecs."""
|
|
135
|
+
|
|
136
|
+
codec_id = 'imagecodecs_avif'
|
|
137
|
+
|
|
138
|
+
def __init__(
|
|
139
|
+
self,
|
|
140
|
+
level=None,
|
|
141
|
+
speed=None,
|
|
142
|
+
tilelog2=None,
|
|
143
|
+
bitspersample=None,
|
|
144
|
+
pixelformat=None,
|
|
145
|
+
numthreads=None,
|
|
146
|
+
index=None,
|
|
147
|
+
):
|
|
148
|
+
self.level = level
|
|
149
|
+
self.speed = speed
|
|
150
|
+
self.tilelog2 = tilelog2
|
|
151
|
+
self.bitspersample = bitspersample
|
|
152
|
+
self.pixelformat = pixelformat
|
|
153
|
+
self.numthreads = numthreads
|
|
154
|
+
self.index = index
|
|
155
|
+
|
|
156
|
+
def encode(self, buf):
|
|
157
|
+
buf = protective_squeeze(numpy.asarray(buf))
|
|
158
|
+
return imagecodecs.avif_encode(
|
|
159
|
+
buf,
|
|
160
|
+
level=self.level,
|
|
161
|
+
speed=self.speed,
|
|
162
|
+
tilelog2=self.tilelog2,
|
|
163
|
+
bitspersample=self.bitspersample,
|
|
164
|
+
pixelformat=self.pixelformat,
|
|
165
|
+
numthreads=self.numthreads,
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
def decode(self, buf, out=None):
|
|
169
|
+
return imagecodecs.avif_decode(
|
|
170
|
+
buf, index=self.index, numthreads=self.numthreads, out=out
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
class Bitorder(Codec):
|
|
175
|
+
"""Bitorder codec for numcodecs."""
|
|
176
|
+
|
|
177
|
+
codec_id = 'imagecodecs_bitorder'
|
|
178
|
+
|
|
179
|
+
def encode(self, buf):
|
|
180
|
+
return imagecodecs.bitorder_encode(buf)
|
|
181
|
+
|
|
182
|
+
def decode(self, buf, out=None):
|
|
183
|
+
return imagecodecs.bitorder_decode(buf, out=_flat(out))
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
class Bitshuffle(Codec):
|
|
187
|
+
"""Bitshuffle codec for numcodecs."""
|
|
188
|
+
|
|
189
|
+
codec_id = 'imagecodecs_bitshuffle'
|
|
190
|
+
|
|
191
|
+
def __init__(self, itemsize=1, blocksize=0):
|
|
192
|
+
self.itemsize = itemsize
|
|
193
|
+
self.blocksize = blocksize
|
|
194
|
+
|
|
195
|
+
def encode(self, buf):
|
|
196
|
+
return imagecodecs.bitshuffle_encode(
|
|
197
|
+
buf, itemsize=self.itemsize, blocksize=self.blocksize
|
|
198
|
+
).tobytes()
|
|
199
|
+
|
|
200
|
+
def decode(self, buf, out=None):
|
|
201
|
+
return imagecodecs.bitshuffle_decode(
|
|
202
|
+
buf,
|
|
203
|
+
itemsize=self.itemsize,
|
|
204
|
+
blocksize=self.blocksize,
|
|
205
|
+
out=_flat(out),
|
|
206
|
+
)
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
class Blosc(Codec):
|
|
210
|
+
"""Blosc codec for numcodecs."""
|
|
211
|
+
|
|
212
|
+
codec_id = 'imagecodecs_blosc'
|
|
213
|
+
|
|
214
|
+
def __init__(
|
|
215
|
+
self,
|
|
216
|
+
level=None,
|
|
217
|
+
compressor=None,
|
|
218
|
+
typesize=None,
|
|
219
|
+
blocksize=None,
|
|
220
|
+
shuffle=None,
|
|
221
|
+
numthreads=None,
|
|
222
|
+
):
|
|
223
|
+
self.level = level
|
|
224
|
+
self.compressor = compressor
|
|
225
|
+
self.typesize = typesize
|
|
226
|
+
self.blocksize = blocksize
|
|
227
|
+
self.shuffle = shuffle
|
|
228
|
+
self.numthreads = numthreads
|
|
229
|
+
|
|
230
|
+
def encode(self, buf):
|
|
231
|
+
buf = protective_squeeze(numpy.asarray(buf))
|
|
232
|
+
return imagecodecs.blosc_encode(
|
|
233
|
+
buf,
|
|
234
|
+
level=self.level,
|
|
235
|
+
compressor=self.compressor,
|
|
236
|
+
typesize=self.typesize,
|
|
237
|
+
blocksize=self.blocksize,
|
|
238
|
+
shuffle=self.shuffle,
|
|
239
|
+
numthreads=self.numthreads,
|
|
240
|
+
)
|
|
241
|
+
|
|
242
|
+
def decode(self, buf, out=None):
|
|
243
|
+
return imagecodecs.blosc_decode(
|
|
244
|
+
buf, numthreads=self.numthreads, out=_flat(out)
|
|
245
|
+
)
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
class Blosc2(Codec):
|
|
249
|
+
"""Blosc2 codec for numcodecs."""
|
|
250
|
+
|
|
251
|
+
codec_id = 'imagecodecs_blosc2'
|
|
252
|
+
|
|
253
|
+
def __init__(
|
|
254
|
+
self,
|
|
255
|
+
level=None,
|
|
256
|
+
compressor=None,
|
|
257
|
+
typesize=None,
|
|
258
|
+
blocksize=None,
|
|
259
|
+
shuffle=None,
|
|
260
|
+
numthreads=None,
|
|
261
|
+
):
|
|
262
|
+
self.level = level
|
|
263
|
+
self.compressor = compressor
|
|
264
|
+
self.typesize = typesize
|
|
265
|
+
self.blocksize = blocksize
|
|
266
|
+
self.shuffle = shuffle
|
|
267
|
+
self.numthreads = numthreads
|
|
268
|
+
|
|
269
|
+
def encode(self, buf):
|
|
270
|
+
buf = protective_squeeze(numpy.asarray(buf))
|
|
271
|
+
return imagecodecs.blosc2_encode(
|
|
272
|
+
buf,
|
|
273
|
+
level=self.level,
|
|
274
|
+
compressor=self.compressor,
|
|
275
|
+
typesize=self.typesize,
|
|
276
|
+
blocksize=self.blocksize,
|
|
277
|
+
shuffle=self.shuffle,
|
|
278
|
+
numthreads=self.numthreads,
|
|
279
|
+
)
|
|
280
|
+
|
|
281
|
+
def decode(self, buf, out=None):
|
|
282
|
+
return imagecodecs.blosc2_decode(
|
|
283
|
+
buf, numthreads=self.numthreads, out=_flat(out)
|
|
284
|
+
)
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
class Brotli(Codec):
|
|
288
|
+
"""Brotli codec for numcodecs."""
|
|
289
|
+
|
|
290
|
+
codec_id = 'imagecodecs_brotli'
|
|
291
|
+
|
|
292
|
+
def __init__(self, level=None, mode=None, lgwin=None):
|
|
293
|
+
self.level = level
|
|
294
|
+
self.mode = mode
|
|
295
|
+
self.lgwin = lgwin
|
|
296
|
+
|
|
297
|
+
def encode(self, buf):
|
|
298
|
+
return imagecodecs.brotli_encode(
|
|
299
|
+
buf, level=self.level, mode=self.mode, lgwin=self.lgwin
|
|
300
|
+
)
|
|
301
|
+
|
|
302
|
+
def decode(self, buf, out=None):
|
|
303
|
+
return imagecodecs.brotli_decode(buf, out=_flat(out))
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
class ByteShuffle(Codec):
|
|
307
|
+
"""ByteShuffle codec for numcodecs."""
|
|
308
|
+
|
|
309
|
+
codec_id = 'imagecodecs_byteshuffle'
|
|
310
|
+
|
|
311
|
+
def __init__(
|
|
312
|
+
self, shape, dtype, axis=-1, dist=1, delta=False, reorder=False
|
|
313
|
+
):
|
|
314
|
+
self.shape = tuple(shape)
|
|
315
|
+
self.dtype = numpy.dtype(dtype).str
|
|
316
|
+
self.axis = axis
|
|
317
|
+
self.dist = dist
|
|
318
|
+
self.delta = bool(delta)
|
|
319
|
+
self.reorder = bool(reorder)
|
|
320
|
+
|
|
321
|
+
def encode(self, buf):
|
|
322
|
+
buf = protective_squeeze(numpy.asarray(buf))
|
|
323
|
+
assert buf.shape == self.shape
|
|
324
|
+
assert buf.dtype == self.dtype
|
|
325
|
+
return imagecodecs.byteshuffle_encode(
|
|
326
|
+
buf,
|
|
327
|
+
axis=self.axis,
|
|
328
|
+
dist=self.dist,
|
|
329
|
+
delta=self.delta,
|
|
330
|
+
reorder=self.reorder,
|
|
331
|
+
).tobytes()
|
|
332
|
+
|
|
333
|
+
def decode(self, buf, out=None):
|
|
334
|
+
if not isinstance(buf, numpy.ndarray):
|
|
335
|
+
buf = numpy.frombuffer(buf, dtype=self.dtype).reshape(*self.shape)
|
|
336
|
+
return imagecodecs.byteshuffle_decode(
|
|
337
|
+
buf,
|
|
338
|
+
axis=self.axis,
|
|
339
|
+
dist=self.dist,
|
|
340
|
+
delta=self.delta,
|
|
341
|
+
reorder=self.reorder,
|
|
342
|
+
out=out,
|
|
343
|
+
)
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
class Bz2(Codec):
|
|
347
|
+
"""Bz2 codec for numcodecs."""
|
|
348
|
+
|
|
349
|
+
codec_id = 'imagecodecs_bz2'
|
|
350
|
+
|
|
351
|
+
def __init__(self, level=None):
|
|
352
|
+
self.level = level
|
|
353
|
+
|
|
354
|
+
def encode(self, buf):
|
|
355
|
+
return imagecodecs.bz2_encode(buf, level=self.level)
|
|
356
|
+
|
|
357
|
+
def decode(self, buf, out=None):
|
|
358
|
+
return imagecodecs.bz2_decode(buf, out=_flat(out))
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
class Cms(Codec):
|
|
362
|
+
"""CMS codec for numcodecs."""
|
|
363
|
+
|
|
364
|
+
codec_id = 'imagecodecs_cms'
|
|
365
|
+
|
|
366
|
+
def __init__(self, *args, **kwargs):
|
|
367
|
+
pass
|
|
368
|
+
|
|
369
|
+
def encode(self, buf, out=None):
|
|
370
|
+
# return imagecodecs.cms_transform(buf)
|
|
371
|
+
raise NotImplementedError
|
|
372
|
+
|
|
373
|
+
def decode(self, buf, out=None):
|
|
374
|
+
# return imagecodecs.cms_transform(buf)
|
|
375
|
+
raise NotImplementedError
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
class Deflate(Codec):
|
|
379
|
+
"""Deflate codec for numcodecs."""
|
|
380
|
+
|
|
381
|
+
codec_id = 'imagecodecs_deflate'
|
|
382
|
+
|
|
383
|
+
def __init__(self, level=None, raw=False):
|
|
384
|
+
self.level = level
|
|
385
|
+
self.raw = bool(raw)
|
|
386
|
+
|
|
387
|
+
def encode(self, buf):
|
|
388
|
+
return imagecodecs.deflate_encode(buf, level=self.level, raw=self.raw)
|
|
389
|
+
|
|
390
|
+
def decode(self, buf, out=None):
|
|
391
|
+
return imagecodecs.deflate_decode(buf, out=_flat(out), raw=self.raw)
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
class Delta(Codec):
|
|
395
|
+
"""Delta codec for numcodecs."""
|
|
396
|
+
|
|
397
|
+
codec_id = 'imagecodecs_delta'
|
|
398
|
+
|
|
399
|
+
def __init__(self, shape=None, dtype=None, axis=-1, dist=1):
|
|
400
|
+
self.shape = None if shape is None else tuple(shape)
|
|
401
|
+
self.dtype = None if dtype is None else numpy.dtype(dtype).str
|
|
402
|
+
self.axis = axis
|
|
403
|
+
self.dist = dist
|
|
404
|
+
|
|
405
|
+
def encode(self, buf):
|
|
406
|
+
if self.shape is not None or self.dtype is not None:
|
|
407
|
+
buf = protective_squeeze(numpy.asarray(buf))
|
|
408
|
+
assert buf.shape == self.shape
|
|
409
|
+
assert buf.dtype == self.dtype
|
|
410
|
+
return imagecodecs.delta_encode(
|
|
411
|
+
buf, axis=self.axis, dist=self.dist
|
|
412
|
+
).tobytes()
|
|
413
|
+
|
|
414
|
+
def decode(self, buf, out=None):
|
|
415
|
+
if self.shape is not None or self.dtype is not None:
|
|
416
|
+
buf = numpy.frombuffer(buf, dtype=self.dtype).reshape(*self.shape)
|
|
417
|
+
return imagecodecs.delta_decode(
|
|
418
|
+
buf, axis=self.axis, dist=self.dist, out=out
|
|
419
|
+
)
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
class Float24(Codec):
|
|
423
|
+
"""Float24 codec for numcodecs."""
|
|
424
|
+
|
|
425
|
+
codec_id = 'imagecodecs_float24'
|
|
426
|
+
|
|
427
|
+
def __init__(self, byteorder=None, rounding=None):
|
|
428
|
+
self.byteorder = byteorder
|
|
429
|
+
self.rounding = rounding
|
|
430
|
+
|
|
431
|
+
def encode(self, buf):
|
|
432
|
+
buf = protective_squeeze(numpy.asarray(buf))
|
|
433
|
+
return imagecodecs.float24_encode(
|
|
434
|
+
buf, byteorder=self.byteorder, rounding=self.rounding
|
|
435
|
+
)
|
|
436
|
+
|
|
437
|
+
def decode(self, buf, out=None):
|
|
438
|
+
return imagecodecs.float24_decode(
|
|
439
|
+
buf, byteorder=self.byteorder, out=out
|
|
440
|
+
)
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
class FloatPred(Codec):
|
|
444
|
+
"""Floating Point Predictor codec for numcodecs."""
|
|
445
|
+
|
|
446
|
+
codec_id = 'imagecodecs_floatpred'
|
|
447
|
+
|
|
448
|
+
def __init__(self, shape, dtype, axis=-1, dist=1):
|
|
449
|
+
self.shape = tuple(shape)
|
|
450
|
+
self.dtype = numpy.dtype(dtype).str
|
|
451
|
+
self.axis = axis
|
|
452
|
+
self.dist = dist
|
|
453
|
+
|
|
454
|
+
def encode(self, buf):
|
|
455
|
+
buf = protective_squeeze(numpy.asarray(buf))
|
|
456
|
+
assert buf.shape == self.shape
|
|
457
|
+
assert buf.dtype == self.dtype
|
|
458
|
+
return imagecodecs.floatpred_encode(
|
|
459
|
+
buf, axis=self.axis, dist=self.dist
|
|
460
|
+
).tobytes()
|
|
461
|
+
|
|
462
|
+
def decode(self, buf, out=None):
|
|
463
|
+
if not isinstance(buf, numpy.ndarray):
|
|
464
|
+
buf = numpy.frombuffer(buf, dtype=self.dtype).reshape(*self.shape)
|
|
465
|
+
return imagecodecs.floatpred_decode(
|
|
466
|
+
buf, axis=self.axis, dist=self.dist, out=out
|
|
467
|
+
)
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
class Gif(Codec):
|
|
471
|
+
"""GIF codec for numcodecs."""
|
|
472
|
+
|
|
473
|
+
codec_id = 'imagecodecs_gif'
|
|
474
|
+
|
|
475
|
+
def encode(self, buf):
|
|
476
|
+
buf = protective_squeeze(numpy.asarray(buf))
|
|
477
|
+
return imagecodecs.gif_encode(buf)
|
|
478
|
+
|
|
479
|
+
def decode(self, buf, out=None):
|
|
480
|
+
return imagecodecs.gif_decode(buf, asrgb=False, out=out)
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
class Heif(Codec):
|
|
484
|
+
"""HEIF codec for numcodecs."""
|
|
485
|
+
|
|
486
|
+
codec_id = 'imagecodecs_heif'
|
|
487
|
+
|
|
488
|
+
def __init__(
|
|
489
|
+
self,
|
|
490
|
+
level=None,
|
|
491
|
+
bitspersample=None,
|
|
492
|
+
photometric=None,
|
|
493
|
+
compression=None,
|
|
494
|
+
numthreads=None,
|
|
495
|
+
index=None,
|
|
496
|
+
):
|
|
497
|
+
self.level = level
|
|
498
|
+
self.bitspersample = bitspersample
|
|
499
|
+
self.photometric = photometric
|
|
500
|
+
self.compression = compression
|
|
501
|
+
self.numthreads = numthreads
|
|
502
|
+
self.index = index
|
|
503
|
+
|
|
504
|
+
def encode(self, buf):
|
|
505
|
+
buf = protective_squeeze(numpy.asarray(buf))
|
|
506
|
+
return imagecodecs.heif_encode(
|
|
507
|
+
buf,
|
|
508
|
+
level=self.level,
|
|
509
|
+
bitspersample=self.bitspersample,
|
|
510
|
+
photometric=self.photometric,
|
|
511
|
+
compression=self.compression,
|
|
512
|
+
numthreads=self.numthreads,
|
|
513
|
+
)
|
|
514
|
+
|
|
515
|
+
def decode(self, buf, out=None):
|
|
516
|
+
return imagecodecs.heif_decode(
|
|
517
|
+
buf,
|
|
518
|
+
index=self.index,
|
|
519
|
+
photometric=self.photometric,
|
|
520
|
+
numthreads=self.numthreads,
|
|
521
|
+
out=out,
|
|
522
|
+
)
|
|
523
|
+
|
|
524
|
+
|
|
525
|
+
class Jetraw(Codec):
|
|
526
|
+
"""Jetraw codec for numcodecs."""
|
|
527
|
+
|
|
528
|
+
codec_id = 'imagecodecs_jetraw'
|
|
529
|
+
|
|
530
|
+
def __init__(
|
|
531
|
+
self,
|
|
532
|
+
shape,
|
|
533
|
+
identifier,
|
|
534
|
+
parameters=None,
|
|
535
|
+
verbosity=None,
|
|
536
|
+
errorbound=None,
|
|
537
|
+
):
|
|
538
|
+
self.shape = shape
|
|
539
|
+
self.identifier = identifier
|
|
540
|
+
self.errorbound = errorbound
|
|
541
|
+
imagecodecs.jetraw_init(parameters, verbosity)
|
|
542
|
+
|
|
543
|
+
def encode(self, buf):
|
|
544
|
+
return imagecodecs.jetraw_encode(
|
|
545
|
+
buf, identifier=self.identifier, errorbound=self.errorbound
|
|
546
|
+
)
|
|
547
|
+
|
|
548
|
+
def decode(self, buf, out=None):
|
|
549
|
+
if out is None:
|
|
550
|
+
out = numpy.empty(self.shape, numpy.uint16)
|
|
551
|
+
return imagecodecs.jetraw_decode(buf, out=out)
|
|
552
|
+
|
|
553
|
+
|
|
554
|
+
class Jpeg(Codec):
|
|
555
|
+
"""JPEG codec for numcodecs."""
|
|
556
|
+
|
|
557
|
+
codec_id = 'imagecodecs_jpeg'
|
|
558
|
+
|
|
559
|
+
def __init__(
|
|
560
|
+
self,
|
|
561
|
+
bitspersample=None,
|
|
562
|
+
tables=None,
|
|
563
|
+
header=None,
|
|
564
|
+
colorspace_data=None,
|
|
565
|
+
colorspace_jpeg=None,
|
|
566
|
+
level=None,
|
|
567
|
+
subsampling=None,
|
|
568
|
+
optimize=None,
|
|
569
|
+
smoothing=None,
|
|
570
|
+
):
|
|
571
|
+
self.tables = tables
|
|
572
|
+
self.header = header
|
|
573
|
+
self.bitspersample = bitspersample
|
|
574
|
+
self.colorspace_data = colorspace_data
|
|
575
|
+
self.colorspace_jpeg = colorspace_jpeg
|
|
576
|
+
self.level = level
|
|
577
|
+
self.subsampling = subsampling
|
|
578
|
+
self.optimize = optimize
|
|
579
|
+
self.smoothing = smoothing
|
|
580
|
+
|
|
581
|
+
def encode(self, buf):
|
|
582
|
+
buf = protective_squeeze(numpy.asarray(buf))
|
|
583
|
+
return imagecodecs.jpeg_encode(
|
|
584
|
+
buf,
|
|
585
|
+
level=self.level,
|
|
586
|
+
colorspace=self.colorspace_data,
|
|
587
|
+
outcolorspace=self.colorspace_jpeg,
|
|
588
|
+
subsampling=self.subsampling,
|
|
589
|
+
optimize=self.optimize,
|
|
590
|
+
smoothing=self.smoothing,
|
|
591
|
+
)
|
|
592
|
+
|
|
593
|
+
def decode(self, buf, out=None):
|
|
594
|
+
out_shape = None
|
|
595
|
+
if out is not None:
|
|
596
|
+
out_shape = out.shape
|
|
597
|
+
out = protective_squeeze(out)
|
|
598
|
+
img = imagecodecs.jpeg_decode(
|
|
599
|
+
buf,
|
|
600
|
+
bitspersample=self.bitspersample,
|
|
601
|
+
tables=self.tables,
|
|
602
|
+
header=self.header,
|
|
603
|
+
colorspace=self.colorspace_jpeg,
|
|
604
|
+
outcolorspace=self.colorspace_data,
|
|
605
|
+
out=out,
|
|
606
|
+
)
|
|
607
|
+
if out_shape is not None:
|
|
608
|
+
img = img.reshape(out_shape)
|
|
609
|
+
return img
|
|
610
|
+
|
|
611
|
+
def get_config(self):
|
|
612
|
+
"""Return dictionary holding configuration parameters."""
|
|
613
|
+
config = dict(id=self.codec_id)
|
|
614
|
+
for key in self.__dict__:
|
|
615
|
+
if not key.startswith('_'):
|
|
616
|
+
value = getattr(self, key)
|
|
617
|
+
if value is not None and key in ('header', 'tables'):
|
|
618
|
+
import base64
|
|
619
|
+
|
|
620
|
+
value = base64.b64encode(value).decode()
|
|
621
|
+
config[key] = value
|
|
622
|
+
return config
|
|
623
|
+
|
|
624
|
+
@classmethod
|
|
625
|
+
def from_config(cls, config):
|
|
626
|
+
"""Instantiate codec from configuration object."""
|
|
627
|
+
for key in ('header', 'tables'):
|
|
628
|
+
value = config.get(key, None)
|
|
629
|
+
if value is not None and isinstance(value, str):
|
|
630
|
+
import base64
|
|
631
|
+
|
|
632
|
+
config[key] = base64.b64decode(value.encode())
|
|
633
|
+
return cls(**config)
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
class Jpeg2k(Codec):
|
|
637
|
+
"""JPEG 2000 codec for numcodecs."""
|
|
638
|
+
|
|
639
|
+
codec_id = 'imagecodecs_jpeg2k'
|
|
640
|
+
|
|
641
|
+
def __init__(
|
|
642
|
+
self,
|
|
643
|
+
level=None,
|
|
644
|
+
codecformat=None,
|
|
645
|
+
colorspace=None,
|
|
646
|
+
tile=None,
|
|
647
|
+
reversible=None,
|
|
648
|
+
bitspersample=None,
|
|
649
|
+
resolutions=None,
|
|
650
|
+
numthreads=None,
|
|
651
|
+
verbose=0,
|
|
652
|
+
):
|
|
653
|
+
self.level = level
|
|
654
|
+
self.codecformat = codecformat
|
|
655
|
+
self.colorspace = colorspace
|
|
656
|
+
self.tile = None if tile is None else tuple(tile)
|
|
657
|
+
self.reversible = reversible
|
|
658
|
+
self.bitspersample = bitspersample
|
|
659
|
+
self.resolutions = resolutions
|
|
660
|
+
self.numthreads = numthreads
|
|
661
|
+
self.verbose = verbose
|
|
662
|
+
|
|
663
|
+
def encode(self, buf):
|
|
664
|
+
buf = protective_squeeze(numpy.asarray(buf))
|
|
665
|
+
return imagecodecs.jpeg2k_encode(
|
|
666
|
+
buf,
|
|
667
|
+
level=self.level,
|
|
668
|
+
codecformat=self.codecformat,
|
|
669
|
+
colorspace=self.colorspace,
|
|
670
|
+
tile=self.tile,
|
|
671
|
+
reversible=self.reversible,
|
|
672
|
+
bitspersample=self.bitspersample,
|
|
673
|
+
resolutions=self.resolutions,
|
|
674
|
+
numthreads=self.numthreads,
|
|
675
|
+
verbose=self.verbose,
|
|
676
|
+
)
|
|
677
|
+
|
|
678
|
+
def decode(self, buf, out=None):
|
|
679
|
+
return imagecodecs.jpeg2k_decode(
|
|
680
|
+
buf, verbose=self.verbose, numthreads=self.numthreads, out=out
|
|
681
|
+
)
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
class JpegLs(Codec):
|
|
685
|
+
"""JPEG LS codec for numcodecs."""
|
|
686
|
+
|
|
687
|
+
codec_id = 'imagecodecs_jpegls'
|
|
688
|
+
|
|
689
|
+
def __init__(self, level=None):
|
|
690
|
+
self.level = level
|
|
691
|
+
|
|
692
|
+
def encode(self, buf):
|
|
693
|
+
buf = protective_squeeze(numpy.asarray(buf))
|
|
694
|
+
return imagecodecs.jpegls_encode(buf, level=self.level)
|
|
695
|
+
|
|
696
|
+
def decode(self, buf, out=None):
|
|
697
|
+
return imagecodecs.jpegls_decode(buf, out=out)
|
|
698
|
+
|
|
699
|
+
|
|
700
|
+
class JpegXl(Codec):
|
|
701
|
+
"""JPEG XL codec for numcodecs."""
|
|
702
|
+
|
|
703
|
+
codec_id = 'imagecodecs_jpegxl'
|
|
704
|
+
|
|
705
|
+
def __init__(
|
|
706
|
+
self,
|
|
707
|
+
# encode
|
|
708
|
+
level=None,
|
|
709
|
+
effort=None,
|
|
710
|
+
distance=None,
|
|
711
|
+
lossless=None,
|
|
712
|
+
decodingspeed=None,
|
|
713
|
+
photometric=None,
|
|
714
|
+
planar=None,
|
|
715
|
+
usecontainer=None,
|
|
716
|
+
bitspersample=None,
|
|
717
|
+
# decode
|
|
718
|
+
index=None,
|
|
719
|
+
keeporientation=None,
|
|
720
|
+
# both
|
|
721
|
+
numthreads=None,
|
|
722
|
+
# Forward-compat: accept (and ignore) any other kwargs that newer
|
|
723
|
+
# versions of imagecodecs.numcodecs.JpegXl may store in the zarr
|
|
724
|
+
# codec config (e.g. 'primaries', 'transferfunction', etc.). This
|
|
725
|
+
# lets us decode datasets encoded with newer codec versions without
|
|
726
|
+
# needing to upgrade imagecodecs locally.
|
|
727
|
+
**extra_kwargs,
|
|
728
|
+
):
|
|
729
|
+
"""
|
|
730
|
+
Return JPEG XL image from numpy array.
|
|
731
|
+
Float must be in nominal range 0..1.
|
|
732
|
+
|
|
733
|
+
Currently L, LA, RGB, RGBA images are supported in contig mode.
|
|
734
|
+
Extra channels are only supported for grayscale images in planar mode.
|
|
735
|
+
|
|
736
|
+
Parameters
|
|
737
|
+
----------
|
|
738
|
+
level : Default to None, i.e. not overwriting lossess and decodingspeed options.
|
|
739
|
+
When < 0: Use lossless compression
|
|
740
|
+
When in [0,1,2,3,4]: Sets the decoding speed tier for the provided options.
|
|
741
|
+
Minimum is 0 (slowest to decode, best quality/density), and maximum
|
|
742
|
+
is 4 (fastest to decode, at the cost of some quality/density).
|
|
743
|
+
effort : Default to 3.
|
|
744
|
+
Sets encoder effort/speed level without affecting decoding speed.
|
|
745
|
+
Valid values are, from faster to slower speed: 1:lightning 2:thunder
|
|
746
|
+
3:falcon 4:cheetah 5:hare 6:wombat 7:squirrel 8:kitten 9:tortoise.
|
|
747
|
+
Speed: lightning, thunder, falcon, cheetah, hare, wombat, squirrel, kitten, tortoise
|
|
748
|
+
control the encoder effort in ascending order.
|
|
749
|
+
This also affects memory usage: using lower effort will typically reduce memory
|
|
750
|
+
consumption during encoding.
|
|
751
|
+
lightning and thunder are fast modes useful for lossless mode (modular).
|
|
752
|
+
falcon disables all of the following tools.
|
|
753
|
+
cheetah enables coefficient reordering, context clustering, and heuristics for selecting DCT sizes and quantization steps.
|
|
754
|
+
hare enables Gaborish filtering, chroma from luma, and an initial estimate of quantization steps.
|
|
755
|
+
wombat enables error diffusion quantization and full DCT size selection heuristics.
|
|
756
|
+
squirrel (default) enables dots, patches, and spline detection, and full context clustering.
|
|
757
|
+
kitten optimizes the adaptive quantization for a psychovisual metric.
|
|
758
|
+
tortoise enables a more thorough adaptive quantization search.
|
|
759
|
+
distance : Default to 1.0
|
|
760
|
+
Sets the distance level for lossy compression: target max butteraugli distance,
|
|
761
|
+
lower = higher quality. Range: 0 .. 15. 0.0 = mathematically lossless
|
|
762
|
+
(however, use JxlEncoderSetFrameLossless instead to use true lossless,
|
|
763
|
+
as setting distance to 0 alone is not the only requirement).
|
|
764
|
+
1.0 = visually lossless. Recommended range: 0.5 .. 3.0.
|
|
765
|
+
lossess : Default to False.
|
|
766
|
+
Use lossess encoding.
|
|
767
|
+
decodingspeed : Default to 0.
|
|
768
|
+
Duplicate to level. [0,4]
|
|
769
|
+
photometric : Return JxlColorSpace value.
|
|
770
|
+
Default logic is quite complicated but works most of the time.
|
|
771
|
+
Accepted value:
|
|
772
|
+
int: [-1,3]
|
|
773
|
+
str: ['RGB',
|
|
774
|
+
'WHITEISZERO', 'MINISWHITE',
|
|
775
|
+
'BLACKISZERO', 'MINISBLACK', 'GRAY',
|
|
776
|
+
'XYB', 'KNOWN']
|
|
777
|
+
planar : Enable multi-channel mode.
|
|
778
|
+
Default to false.
|
|
779
|
+
usecontainer :
|
|
780
|
+
Forces the encoder to use the box-based container format (BMFF)
|
|
781
|
+
even when not necessary.
|
|
782
|
+
When using JxlEncoderUseBoxes, JxlEncoderStoreJPEGMetadata or
|
|
783
|
+
JxlEncoderSetCodestreamLevel with level 10, the encoder will
|
|
784
|
+
automatically also use the container format, it is not necessary
|
|
785
|
+
to use JxlEncoderUseContainer for those use cases.
|
|
786
|
+
By default this setting is disabled.
|
|
787
|
+
index : Selectively decode frames for animation.
|
|
788
|
+
Default to 0, decode all frames.
|
|
789
|
+
When set to > 0, decode that frame index only.
|
|
790
|
+
keeporientation :
|
|
791
|
+
Enables or disables preserving of as-in-bitstream pixeldata orientation.
|
|
792
|
+
Some images are encoded with an Orientation tag indicating that the
|
|
793
|
+
decoder must perform a rotation and/or mirroring to the encoded image data.
|
|
794
|
+
|
|
795
|
+
If skip_reorientation is JXL_FALSE (the default): the decoder will apply
|
|
796
|
+
the transformation from the orientation setting, hence rendering the image
|
|
797
|
+
according to its specified intent. When producing a JxlBasicInfo, the decoder
|
|
798
|
+
will always set the orientation field to JXL_ORIENT_IDENTITY (matching the
|
|
799
|
+
returned pixel data) and also align xsize and ysize so that they correspond
|
|
800
|
+
to the width and the height of the returned pixel data.
|
|
801
|
+
|
|
802
|
+
If skip_reorientation is JXL_TRUE: the decoder will skip applying the
|
|
803
|
+
transformation from the orientation setting, returning the image in
|
|
804
|
+
the as-in-bitstream pixeldata orientation. This may be faster to decode
|
|
805
|
+
since the decoder doesnt have to apply the transformation, but can
|
|
806
|
+
cause wrong display of the image if the orientation tag is not correctly
|
|
807
|
+
taken into account by the user.
|
|
808
|
+
|
|
809
|
+
By default, this option is disabled, and the returned pixel data is
|
|
810
|
+
re-oriented according to the images Orientation setting.
|
|
811
|
+
threads : Default to 1.
|
|
812
|
+
If <= 0, use all cores.
|
|
813
|
+
If > 32, clipped to 32.
|
|
814
|
+
"""
|
|
815
|
+
|
|
816
|
+
self.level = level
|
|
817
|
+
self.effort = effort
|
|
818
|
+
self.distance = distance
|
|
819
|
+
self.lossless = bool(lossless)
|
|
820
|
+
self.decodingspeed = decodingspeed
|
|
821
|
+
self.photometric = photometric
|
|
822
|
+
self.planar = planar
|
|
823
|
+
self.usecontainer = usecontainer
|
|
824
|
+
# Accepted for forward-compat with newer imagecodecs.numcodecs.JpegXl
|
|
825
|
+
# (used during decode of datasets encoded with the newer codec).
|
|
826
|
+
self.bitspersample = bitspersample
|
|
827
|
+
self.index = index
|
|
828
|
+
self.keeporientation = keeporientation
|
|
829
|
+
self.numthreads = numthreads
|
|
830
|
+
self._extra_kwargs = dict(extra_kwargs)
|
|
831
|
+
if extra_kwargs:
|
|
832
|
+
import warnings
|
|
833
|
+
warnings.warn(
|
|
834
|
+
f"JpegXl: ignoring unknown codec kwargs from zarr config "
|
|
835
|
+
f"(likely written by a newer imagecodecs version): "
|
|
836
|
+
f"{sorted(extra_kwargs)}",
|
|
837
|
+
RuntimeWarning,
|
|
838
|
+
stacklevel=2,
|
|
839
|
+
)
|
|
840
|
+
|
|
841
|
+
def encode(self, buf):
|
|
842
|
+
# TODO: only squeeze all but last dim
|
|
843
|
+
buf = protective_squeeze(numpy.asarray(buf))
|
|
844
|
+
encode_kwargs = dict(
|
|
845
|
+
level=self.level,
|
|
846
|
+
effort=self.effort,
|
|
847
|
+
distance=self.distance,
|
|
848
|
+
lossless=self.lossless,
|
|
849
|
+
decodingspeed=self.decodingspeed,
|
|
850
|
+
photometric=self.photometric,
|
|
851
|
+
planar=self.planar,
|
|
852
|
+
usecontainer=self.usecontainer,
|
|
853
|
+
numthreads=self.numthreads,
|
|
854
|
+
)
|
|
855
|
+
if self.bitspersample is not None:
|
|
856
|
+
encode_kwargs['bitspersample'] = self.bitspersample
|
|
857
|
+
return imagecodecs.jpegxl_encode(buf, **encode_kwargs)
|
|
858
|
+
|
|
859
|
+
def decode(self, buf, out=None):
|
|
860
|
+
return imagecodecs.jpegxl_decode(
|
|
861
|
+
buf,
|
|
862
|
+
index=self.index,
|
|
863
|
+
keeporientation=self.keeporientation,
|
|
864
|
+
numthreads=self.numthreads,
|
|
865
|
+
out=out,
|
|
866
|
+
)
|
|
867
|
+
|
|
868
|
+
|
|
869
|
+
class JpegXr(Codec):
|
|
870
|
+
"""JPEG XR codec for numcodecs."""
|
|
871
|
+
|
|
872
|
+
codec_id = 'imagecodecs_jpegxr'
|
|
873
|
+
|
|
874
|
+
def __init__(
|
|
875
|
+
self,
|
|
876
|
+
level=None,
|
|
877
|
+
photometric=None,
|
|
878
|
+
hasalpha=None,
|
|
879
|
+
resolution=None,
|
|
880
|
+
fp2int=None,
|
|
881
|
+
):
|
|
882
|
+
self.level = level
|
|
883
|
+
self.photometric = photometric
|
|
884
|
+
self.hasalpha = hasalpha
|
|
885
|
+
self.resolution = resolution
|
|
886
|
+
self.fp2int = fp2int
|
|
887
|
+
|
|
888
|
+
def encode(self, buf):
|
|
889
|
+
buf = protective_squeeze(numpy.asarray(buf))
|
|
890
|
+
return imagecodecs.jpegxr_encode(
|
|
891
|
+
buf,
|
|
892
|
+
level=self.level,
|
|
893
|
+
photometric=self.photometric,
|
|
894
|
+
hasalpha=self.hasalpha,
|
|
895
|
+
resolution=self.resolution,
|
|
896
|
+
)
|
|
897
|
+
|
|
898
|
+
def decode(self, buf, out=None):
|
|
899
|
+
return imagecodecs.jpegxr_decode(buf, fp2int=self.fp2int, out=out)
|
|
900
|
+
|
|
901
|
+
|
|
902
|
+
class Lerc(Codec):
|
|
903
|
+
"""LERC codec for numcodecs."""
|
|
904
|
+
|
|
905
|
+
codec_id = 'imagecodecs_lerc'
|
|
906
|
+
|
|
907
|
+
def __init__(self, level=None, version=None, planar=None):
|
|
908
|
+
self.level = level
|
|
909
|
+
self.version = version
|
|
910
|
+
self.planar = bool(planar)
|
|
911
|
+
# TODO: support mask?
|
|
912
|
+
# self.mask = None
|
|
913
|
+
|
|
914
|
+
def encode(self, buf):
|
|
915
|
+
buf = protective_squeeze(numpy.asarray(buf))
|
|
916
|
+
return imagecodecs.lerc_encode(
|
|
917
|
+
buf,
|
|
918
|
+
level=self.level,
|
|
919
|
+
version=self.version,
|
|
920
|
+
planar=self.planar,
|
|
921
|
+
)
|
|
922
|
+
|
|
923
|
+
def decode(self, buf, out=None):
|
|
924
|
+
return imagecodecs.lerc_decode(buf, out=out)
|
|
925
|
+
|
|
926
|
+
|
|
927
|
+
class Ljpeg(Codec):
|
|
928
|
+
"""LJPEG codec for numcodecs."""
|
|
929
|
+
|
|
930
|
+
codec_id = 'imagecodecs_ljpeg'
|
|
931
|
+
|
|
932
|
+
def __init__(self, bitspersample=None):
|
|
933
|
+
self.bitspersample = bitspersample
|
|
934
|
+
|
|
935
|
+
def encode(self, buf):
|
|
936
|
+
buf = protective_squeeze(numpy.asarray(buf))
|
|
937
|
+
return imagecodecs.ljpeg_encode(buf, bitspersample=self.bitspersample)
|
|
938
|
+
|
|
939
|
+
def decode(self, buf, out=None):
|
|
940
|
+
return imagecodecs.ljpeg_decode(buf, out=out)
|
|
941
|
+
|
|
942
|
+
|
|
943
|
+
class Lz4(Codec):
|
|
944
|
+
"""LZ4 codec for numcodecs."""
|
|
945
|
+
|
|
946
|
+
codec_id = 'imagecodecs_lz4'
|
|
947
|
+
|
|
948
|
+
def __init__(self, level=None, hc=False, header=True):
|
|
949
|
+
self.level = level
|
|
950
|
+
self.hc = hc
|
|
951
|
+
self.header = bool(header)
|
|
952
|
+
|
|
953
|
+
def encode(self, buf):
|
|
954
|
+
return imagecodecs.lz4_encode(
|
|
955
|
+
buf, level=self.level, hc=self.hc, header=self.header
|
|
956
|
+
)
|
|
957
|
+
|
|
958
|
+
def decode(self, buf, out=None):
|
|
959
|
+
return imagecodecs.lz4_decode(buf, header=self.header, out=_flat(out))
|
|
960
|
+
|
|
961
|
+
|
|
962
|
+
class Lz4f(Codec):
|
|
963
|
+
"""LZ4F codec for numcodecs."""
|
|
964
|
+
|
|
965
|
+
codec_id = 'imagecodecs_lz4f'
|
|
966
|
+
|
|
967
|
+
def __init__(
|
|
968
|
+
self,
|
|
969
|
+
level=None,
|
|
970
|
+
blocksizeid=False,
|
|
971
|
+
contentchecksum=None,
|
|
972
|
+
blockchecksum=None,
|
|
973
|
+
):
|
|
974
|
+
self.level = level
|
|
975
|
+
self.blocksizeid = blocksizeid
|
|
976
|
+
self.contentchecksum = contentchecksum
|
|
977
|
+
self.blockchecksum = blockchecksum
|
|
978
|
+
|
|
979
|
+
def encode(self, buf):
|
|
980
|
+
return imagecodecs.lz4f_encode(
|
|
981
|
+
buf,
|
|
982
|
+
level=self.level,
|
|
983
|
+
blocksizeid=self.blocksizeid,
|
|
984
|
+
contentchecksum=self.contentchecksum,
|
|
985
|
+
blockchecksum=self.blockchecksum,
|
|
986
|
+
)
|
|
987
|
+
|
|
988
|
+
def decode(self, buf, out=None):
|
|
989
|
+
return imagecodecs.lz4f_decode(buf, out=_flat(out))
|
|
990
|
+
|
|
991
|
+
|
|
992
|
+
class Lzf(Codec):
|
|
993
|
+
"""LZF codec for numcodecs."""
|
|
994
|
+
|
|
995
|
+
codec_id = 'imagecodecs_lzf'
|
|
996
|
+
|
|
997
|
+
def __init__(self, header=True):
|
|
998
|
+
self.header = bool(header)
|
|
999
|
+
|
|
1000
|
+
def encode(self, buf):
|
|
1001
|
+
return imagecodecs.lzf_encode(buf, header=self.header)
|
|
1002
|
+
|
|
1003
|
+
def decode(self, buf, out=None):
|
|
1004
|
+
return imagecodecs.lzf_decode(buf, header=self.header, out=_flat(out))
|
|
1005
|
+
|
|
1006
|
+
|
|
1007
|
+
class Lzma(Codec):
|
|
1008
|
+
"""LZMA codec for numcodecs."""
|
|
1009
|
+
|
|
1010
|
+
codec_id = 'imagecodecs_lzma'
|
|
1011
|
+
|
|
1012
|
+
def __init__(self, level=None):
|
|
1013
|
+
self.level = level
|
|
1014
|
+
|
|
1015
|
+
def encode(self, buf):
|
|
1016
|
+
return imagecodecs.lzma_encode(buf, level=self.level)
|
|
1017
|
+
|
|
1018
|
+
def decode(self, buf, out=None):
|
|
1019
|
+
return imagecodecs.lzma_decode(buf, out=_flat(out))
|
|
1020
|
+
|
|
1021
|
+
|
|
1022
|
+
class Lzw(Codec):
|
|
1023
|
+
"""LZW codec for numcodecs."""
|
|
1024
|
+
|
|
1025
|
+
codec_id = 'imagecodecs_lzw'
|
|
1026
|
+
|
|
1027
|
+
def encode(self, buf):
|
|
1028
|
+
return imagecodecs.lzw_encode(buf)
|
|
1029
|
+
|
|
1030
|
+
def decode(self, buf, out=None):
|
|
1031
|
+
return imagecodecs.lzw_decode(buf, out=_flat(out))
|
|
1032
|
+
|
|
1033
|
+
|
|
1034
|
+
class PackBits(Codec):
|
|
1035
|
+
"""PackBits codec for numcodecs."""
|
|
1036
|
+
|
|
1037
|
+
codec_id = 'imagecodecs_packbits'
|
|
1038
|
+
|
|
1039
|
+
def __init__(self, axis=None):
|
|
1040
|
+
self.axis = axis
|
|
1041
|
+
|
|
1042
|
+
def encode(self, buf):
|
|
1043
|
+
if not isinstance(buf, (bytes, bytearray)):
|
|
1044
|
+
buf = protective_squeeze(numpy.asarray(buf))
|
|
1045
|
+
return imagecodecs.packbits_encode(buf, axis=self.axis)
|
|
1046
|
+
|
|
1047
|
+
def decode(self, buf, out=None):
|
|
1048
|
+
return imagecodecs.packbits_decode(buf, out=_flat(out))
|
|
1049
|
+
|
|
1050
|
+
|
|
1051
|
+
class Pglz(Codec):
|
|
1052
|
+
"""PGLZ codec for numcodecs."""
|
|
1053
|
+
|
|
1054
|
+
codec_id = 'imagecodecs_pglz'
|
|
1055
|
+
|
|
1056
|
+
def __init__(self, header=True, strategy=None):
|
|
1057
|
+
self.header = bool(header)
|
|
1058
|
+
self.strategy = strategy
|
|
1059
|
+
|
|
1060
|
+
def encode(self, buf):
|
|
1061
|
+
return imagecodecs.pglz_encode(
|
|
1062
|
+
buf, strategy=self.strategy, header=self.header
|
|
1063
|
+
)
|
|
1064
|
+
|
|
1065
|
+
def decode(self, buf, out=None):
|
|
1066
|
+
return imagecodecs.pglz_decode(buf, header=self.header, out=_flat(out))
|
|
1067
|
+
|
|
1068
|
+
|
|
1069
|
+
class Png(Codec):
|
|
1070
|
+
"""PNG codec for numcodecs."""
|
|
1071
|
+
|
|
1072
|
+
codec_id = 'imagecodecs_png'
|
|
1073
|
+
|
|
1074
|
+
def __init__(self, level=None):
|
|
1075
|
+
self.level = level
|
|
1076
|
+
|
|
1077
|
+
def encode(self, buf):
|
|
1078
|
+
buf = protective_squeeze(numpy.asarray(buf))
|
|
1079
|
+
return imagecodecs.png_encode(buf, level=self.level)
|
|
1080
|
+
|
|
1081
|
+
def decode(self, buf, out=None):
|
|
1082
|
+
return imagecodecs.png_decode(buf, out=out)
|
|
1083
|
+
|
|
1084
|
+
|
|
1085
|
+
class Qoi(Codec):
|
|
1086
|
+
"""QOI codec for numcodecs."""
|
|
1087
|
+
|
|
1088
|
+
codec_id = 'imagecodecs_qoi'
|
|
1089
|
+
|
|
1090
|
+
def __init__(self):
|
|
1091
|
+
pass
|
|
1092
|
+
|
|
1093
|
+
def encode(self, buf):
|
|
1094
|
+
buf = protective_squeeze(numpy.asarray(buf))
|
|
1095
|
+
return imagecodecs.qoi_encode(buf)
|
|
1096
|
+
|
|
1097
|
+
def decode(self, buf, out=None):
|
|
1098
|
+
return imagecodecs.qoi_decode(buf, out=out)
|
|
1099
|
+
|
|
1100
|
+
|
|
1101
|
+
class Rgbe(Codec):
|
|
1102
|
+
"""RGBE codec for numcodecs."""
|
|
1103
|
+
|
|
1104
|
+
codec_id = 'imagecodecs_rgbe'
|
|
1105
|
+
|
|
1106
|
+
def __init__(self, header=False, shape=None, rle=None):
|
|
1107
|
+
if not header and shape is None:
|
|
1108
|
+
raise ValueError('must specify data shape if no header')
|
|
1109
|
+
if shape and shape[-1] != 3:
|
|
1110
|
+
raise ValueError('invalid shape')
|
|
1111
|
+
self.shape = shape
|
|
1112
|
+
self.header = bool(header)
|
|
1113
|
+
self.rle = None if rle is None else bool(rle)
|
|
1114
|
+
|
|
1115
|
+
def encode(self, buf):
|
|
1116
|
+
buf = protective_squeeze(numpy.asarray(buf))
|
|
1117
|
+
return imagecodecs.rgbe_encode(buf, header=self.header, rle=self.rle)
|
|
1118
|
+
|
|
1119
|
+
def decode(self, buf, out=None):
|
|
1120
|
+
if out is None and not self.header:
|
|
1121
|
+
out = numpy.empty(self.shape, numpy.float32)
|
|
1122
|
+
return imagecodecs.rgbe_decode(
|
|
1123
|
+
buf, header=self.header, rle=self.rle, out=out
|
|
1124
|
+
)
|
|
1125
|
+
|
|
1126
|
+
|
|
1127
|
+
class Rcomp(Codec):
|
|
1128
|
+
"""Rcomp codec for numcodecs."""
|
|
1129
|
+
|
|
1130
|
+
codec_id = 'imagecodecs_rcomp'
|
|
1131
|
+
|
|
1132
|
+
def __init__(self, shape, dtype, nblock=None):
|
|
1133
|
+
self.shape = tuple(shape)
|
|
1134
|
+
self.dtype = numpy.dtype(dtype).str
|
|
1135
|
+
self.nblock = nblock
|
|
1136
|
+
|
|
1137
|
+
def encode(self, buf):
|
|
1138
|
+
return imagecodecs.rcomp_encode(buf, nblock=self.nblock)
|
|
1139
|
+
|
|
1140
|
+
def decode(self, buf, out=None):
|
|
1141
|
+
return imagecodecs.rcomp_decode(
|
|
1142
|
+
buf,
|
|
1143
|
+
shape=self.shape,
|
|
1144
|
+
dtype=self.dtype,
|
|
1145
|
+
nblock=self.nblock,
|
|
1146
|
+
out=out,
|
|
1147
|
+
)
|
|
1148
|
+
|
|
1149
|
+
|
|
1150
|
+
class Snappy(Codec):
|
|
1151
|
+
"""Snappy codec for numcodecs."""
|
|
1152
|
+
|
|
1153
|
+
codec_id = 'imagecodecs_snappy'
|
|
1154
|
+
|
|
1155
|
+
def encode(self, buf):
|
|
1156
|
+
return imagecodecs.snappy_encode(buf)
|
|
1157
|
+
|
|
1158
|
+
def decode(self, buf, out=None):
|
|
1159
|
+
return imagecodecs.snappy_decode(buf, out=_flat(out))
|
|
1160
|
+
|
|
1161
|
+
|
|
1162
|
+
class Spng(Codec):
|
|
1163
|
+
"""SPNG codec for numcodecs."""
|
|
1164
|
+
|
|
1165
|
+
codec_id = 'imagecodecs_spng'
|
|
1166
|
+
|
|
1167
|
+
def __init__(self, level=None):
|
|
1168
|
+
self.level = level
|
|
1169
|
+
|
|
1170
|
+
def encode(self, buf):
|
|
1171
|
+
buf = protective_squeeze(numpy.asarray(buf))
|
|
1172
|
+
return imagecodecs.spng_encode(buf, level=self.level)
|
|
1173
|
+
|
|
1174
|
+
def decode(self, buf, out=None):
|
|
1175
|
+
return imagecodecs.spng_decode(buf, out=out)
|
|
1176
|
+
|
|
1177
|
+
|
|
1178
|
+
class Tiff(Codec):
|
|
1179
|
+
"""TIFF codec for numcodecs."""
|
|
1180
|
+
|
|
1181
|
+
codec_id = 'imagecodecs_tiff'
|
|
1182
|
+
|
|
1183
|
+
def __init__(self, index=None, asrgb=None, verbose=0):
|
|
1184
|
+
self.index = index
|
|
1185
|
+
self.asrgb = bool(asrgb)
|
|
1186
|
+
self.verbose = verbose
|
|
1187
|
+
|
|
1188
|
+
def encode(self, buf):
|
|
1189
|
+
# TODO: not implemented
|
|
1190
|
+
buf = protective_squeeze(numpy.asarray(buf))
|
|
1191
|
+
return imagecodecs.tiff_encode(buf)
|
|
1192
|
+
|
|
1193
|
+
def decode(self, buf, out=None):
|
|
1194
|
+
return imagecodecs.tiff_decode(
|
|
1195
|
+
buf,
|
|
1196
|
+
index=self.index,
|
|
1197
|
+
asrgb=self.asrgb,
|
|
1198
|
+
verbose=self.verbose,
|
|
1199
|
+
out=out,
|
|
1200
|
+
)
|
|
1201
|
+
|
|
1202
|
+
|
|
1203
|
+
class Webp(Codec):
|
|
1204
|
+
"""WebP codec for numcodecs."""
|
|
1205
|
+
|
|
1206
|
+
codec_id = 'imagecodecs_webp'
|
|
1207
|
+
|
|
1208
|
+
def __init__(self, level=None, lossless=None, method=None, hasalpha=None):
|
|
1209
|
+
self.level = level
|
|
1210
|
+
self.hasalpha = bool(hasalpha)
|
|
1211
|
+
self.method = method
|
|
1212
|
+
self.lossless = lossless
|
|
1213
|
+
|
|
1214
|
+
def encode(self, buf):
|
|
1215
|
+
buf = protective_squeeze(numpy.asarray(buf))
|
|
1216
|
+
return imagecodecs.webp_encode(
|
|
1217
|
+
buf, level=self.level, lossless=self.lossless, method=self.method
|
|
1218
|
+
)
|
|
1219
|
+
|
|
1220
|
+
def decode(self, buf, out=None):
|
|
1221
|
+
return imagecodecs.webp_decode(buf, hasalpha=self.hasalpha, out=out)
|
|
1222
|
+
|
|
1223
|
+
|
|
1224
|
+
class Xor(Codec):
|
|
1225
|
+
"""XOR codec for numcodecs."""
|
|
1226
|
+
|
|
1227
|
+
codec_id = 'imagecodecs_xor'
|
|
1228
|
+
|
|
1229
|
+
def __init__(self, shape=None, dtype=None, axis=-1):
|
|
1230
|
+
self.shape = None if shape is None else tuple(shape)
|
|
1231
|
+
self.dtype = None if dtype is None else numpy.dtype(dtype).str
|
|
1232
|
+
self.axis = axis
|
|
1233
|
+
|
|
1234
|
+
def encode(self, buf):
|
|
1235
|
+
if self.shape is not None or self.dtype is not None:
|
|
1236
|
+
buf = protective_squeeze(numpy.asarray(buf))
|
|
1237
|
+
assert buf.shape == self.shape
|
|
1238
|
+
assert buf.dtype == self.dtype
|
|
1239
|
+
return imagecodecs.xor_encode(buf, axis=self.axis).tobytes()
|
|
1240
|
+
|
|
1241
|
+
def decode(self, buf, out=None):
|
|
1242
|
+
if self.shape is not None or self.dtype is not None:
|
|
1243
|
+
buf = numpy.frombuffer(buf, dtype=self.dtype).reshape(*self.shape)
|
|
1244
|
+
return imagecodecs.xor_decode(buf, axis=self.axis, out=_flat(out))
|
|
1245
|
+
|
|
1246
|
+
|
|
1247
|
+
class Zfp(Codec):
|
|
1248
|
+
"""ZFP codec for numcodecs."""
|
|
1249
|
+
|
|
1250
|
+
codec_id = 'imagecodecs_zfp'
|
|
1251
|
+
|
|
1252
|
+
def __init__(
|
|
1253
|
+
self,
|
|
1254
|
+
shape=None,
|
|
1255
|
+
dtype=None,
|
|
1256
|
+
strides=None,
|
|
1257
|
+
level=None,
|
|
1258
|
+
mode=None,
|
|
1259
|
+
execution=None,
|
|
1260
|
+
numthreads=None,
|
|
1261
|
+
chunksize=None,
|
|
1262
|
+
header=True,
|
|
1263
|
+
):
|
|
1264
|
+
if header:
|
|
1265
|
+
self.shape = None
|
|
1266
|
+
self.dtype = None
|
|
1267
|
+
self.strides = None
|
|
1268
|
+
elif shape is None or dtype is None:
|
|
1269
|
+
raise ValueError('invalid shape or dtype')
|
|
1270
|
+
else:
|
|
1271
|
+
self.shape = tuple(shape)
|
|
1272
|
+
self.dtype = numpy.dtype(dtype).str
|
|
1273
|
+
self.strides = None if strides is None else tuple(strides)
|
|
1274
|
+
self.level = level
|
|
1275
|
+
self.mode = mode
|
|
1276
|
+
self.execution = execution
|
|
1277
|
+
self.numthreads = numthreads
|
|
1278
|
+
self.chunksize = chunksize
|
|
1279
|
+
self.header = bool(header)
|
|
1280
|
+
|
|
1281
|
+
def encode(self, buf):
|
|
1282
|
+
buf = protective_squeeze(numpy.asarray(buf))
|
|
1283
|
+
if not self.header:
|
|
1284
|
+
assert buf.shape == self.shape
|
|
1285
|
+
assert buf.dtype == self.dtype
|
|
1286
|
+
return imagecodecs.zfp_encode(
|
|
1287
|
+
buf,
|
|
1288
|
+
level=self.level,
|
|
1289
|
+
mode=self.mode,
|
|
1290
|
+
execution=self.execution,
|
|
1291
|
+
header=self.header,
|
|
1292
|
+
numthreads=self.numthreads,
|
|
1293
|
+
chunksize=self.chunksize,
|
|
1294
|
+
)
|
|
1295
|
+
|
|
1296
|
+
def decode(self, buf, out=None):
|
|
1297
|
+
if self.header:
|
|
1298
|
+
return imagecodecs.zfp_decode(buf, out=out)
|
|
1299
|
+
return imagecodecs.zfp_decode(
|
|
1300
|
+
buf,
|
|
1301
|
+
shape=self.shape,
|
|
1302
|
+
dtype=numpy.dtype(self.dtype),
|
|
1303
|
+
strides=self.strides,
|
|
1304
|
+
numthreads=self.numthreads,
|
|
1305
|
+
out=out,
|
|
1306
|
+
)
|
|
1307
|
+
|
|
1308
|
+
|
|
1309
|
+
class Zlib(Codec):
|
|
1310
|
+
"""Zlib codec for numcodecs."""
|
|
1311
|
+
|
|
1312
|
+
codec_id = 'imagecodecs_zlib'
|
|
1313
|
+
|
|
1314
|
+
def __init__(self, level=None):
|
|
1315
|
+
self.level = level
|
|
1316
|
+
|
|
1317
|
+
def encode(self, buf):
|
|
1318
|
+
return imagecodecs.zlib_encode(buf, level=self.level)
|
|
1319
|
+
|
|
1320
|
+
def decode(self, buf, out=None):
|
|
1321
|
+
return imagecodecs.zlib_decode(buf, out=_flat(out))
|
|
1322
|
+
|
|
1323
|
+
|
|
1324
|
+
class Zlibng(Codec):
|
|
1325
|
+
"""Zlibng codec for numcodecs."""
|
|
1326
|
+
|
|
1327
|
+
codec_id = 'imagecodecs_zlibng'
|
|
1328
|
+
|
|
1329
|
+
def __init__(self, level=None):
|
|
1330
|
+
self.level = level
|
|
1331
|
+
|
|
1332
|
+
def encode(self, buf):
|
|
1333
|
+
return imagecodecs.zlibng_encode(buf, level=self.level)
|
|
1334
|
+
|
|
1335
|
+
def decode(self, buf, out=None):
|
|
1336
|
+
return imagecodecs.zlibng_decode(buf, out=_flat(out))
|
|
1337
|
+
|
|
1338
|
+
|
|
1339
|
+
class Zopfli(Codec):
|
|
1340
|
+
"""Zopfli codec for numcodecs."""
|
|
1341
|
+
|
|
1342
|
+
codec_id = 'imagecodecs_zopfli'
|
|
1343
|
+
|
|
1344
|
+
def encode(self, buf):
|
|
1345
|
+
return imagecodecs.zopfli_encode(buf)
|
|
1346
|
+
|
|
1347
|
+
def decode(self, buf, out=None):
|
|
1348
|
+
return imagecodecs.zopfli_decode(buf, out=_flat(out))
|
|
1349
|
+
|
|
1350
|
+
|
|
1351
|
+
class Zstd(Codec):
|
|
1352
|
+
"""ZStandard codec for numcodecs."""
|
|
1353
|
+
|
|
1354
|
+
codec_id = 'imagecodecs_zstd'
|
|
1355
|
+
|
|
1356
|
+
def __init__(self, level=None):
|
|
1357
|
+
self.level = level
|
|
1358
|
+
|
|
1359
|
+
def encode(self, buf):
|
|
1360
|
+
return imagecodecs.zstd_encode(buf, level=self.level)
|
|
1361
|
+
|
|
1362
|
+
def decode(self, buf, out=None):
|
|
1363
|
+
return imagecodecs.zstd_decode(buf, out=_flat(out))
|
|
1364
|
+
|
|
1365
|
+
|
|
1366
|
+
def _flat(out):
|
|
1367
|
+
"""Return numpy array as contiguous view of bytes if possible."""
|
|
1368
|
+
if out is None:
|
|
1369
|
+
return None
|
|
1370
|
+
view = memoryview(out)
|
|
1371
|
+
if view.readonly or not view.contiguous:
|
|
1372
|
+
return None
|
|
1373
|
+
return view.cast('B')
|
|
1374
|
+
|
|
1375
|
+
|
|
1376
|
+
def register_codecs(codecs=None, force=False, verbose=True):
|
|
1377
|
+
"""Register codecs in this module with numcodecs."""
|
|
1378
|
+
for name, cls in globals().items():
|
|
1379
|
+
if not hasattr(cls, 'codec_id') or name == 'Codec':
|
|
1380
|
+
continue
|
|
1381
|
+
if codecs is not None and cls.codec_id not in codecs:
|
|
1382
|
+
continue
|
|
1383
|
+
try:
|
|
1384
|
+
try:
|
|
1385
|
+
get_codec({'id': cls.codec_id})
|
|
1386
|
+
except TypeError:
|
|
1387
|
+
# registered, but failed
|
|
1388
|
+
pass
|
|
1389
|
+
except ValueError:
|
|
1390
|
+
# not registered yet
|
|
1391
|
+
pass
|
|
1392
|
+
else:
|
|
1393
|
+
if not force:
|
|
1394
|
+
if verbose:
|
|
1395
|
+
log_warning(
|
|
1396
|
+
f'numcodec {cls.codec_id!r} already registered'
|
|
1397
|
+
)
|
|
1398
|
+
continue
|
|
1399
|
+
if verbose:
|
|
1400
|
+
log_warning(f'replacing registered numcodec {cls.codec_id!r}')
|
|
1401
|
+
register_codec(cls)
|
|
1402
|
+
|
|
1403
|
+
|
|
1404
|
+
def log_warning(msg, *args, **kwargs):
|
|
1405
|
+
"""Log message with level WARNING."""
|
|
1406
|
+
import logging
|
|
1407
|
+
|
|
1408
|
+
logging.getLogger(__name__).warning(msg, *args, **kwargs)
|