-
Notifications
You must be signed in to change notification settings - Fork 20
/
_jpegxl.pyx
730 lines (612 loc) · 24.8 KB
/
_jpegxl.pyx
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
# imagecodecs/_jpegxl.pyx
# distutils: language = c
# cython: language_level = 3
# cython: boundscheck=False
# cython: wraparound=False
# cython: cdivision=True
# cython: nonecheck=False
# Copyright (c) 2021, Christoph Gohlke
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
"""JPEG XL codec for the imagecodecs package."""
__version__ = '2021.11.11'
include '_shared.pxi'
from libjxl cimport *
import enum
class JPEGXL:
"""JPEG XL Constants."""
class COLOR_SPACE(enum.IntEnum):
UNKNOWN = JXL_COLOR_SPACE_UNKNOWN
RGB = JXL_COLOR_SPACE_RGB
GRAY = JXL_COLOR_SPACE_GRAY
XYB = JXL_COLOR_SPACE_XYB
class CHANNEL(enum.IntEnum):
UNKNOWN = JXL_CHANNEL_UNKNOWN
ALPHA = JXL_CHANNEL_ALPHA
DEPTH = JXL_CHANNEL_DEPTH
SPOT_COLOR = JXL_CHANNEL_SPOT_COLOR
SELECTION_MASK = JXL_CHANNEL_SELECTION_MASK
BLACK = JXL_CHANNEL_BLACK
CFA = JXL_CHANNEL_CFA
THERMAL = JXL_CHANNEL_THERMAL
OPTIONAL = JXL_CHANNEL_OPTIONAL
class JpegxlError(RuntimeError):
"""JPEG XL Exceptions."""
def __init__(self, func, err):
if err is None:
msg = 'NULL'
elif 'Decoder' in func:
msg = {
JXL_DEC_SUCCESS: 'JXL_DEC_SUCCESS',
JXL_DEC_ERROR: 'JXL_DEC_ERROR',
JXL_DEC_NEED_MORE_INPUT: 'JXL_DEC_NEED_MORE_INPUT',
JXL_DEC_NEED_PREVIEW_OUT_BUFFER:
'JXL_DEC_NEED_PREVIEW_OUT_BUFFER',
JXL_DEC_NEED_DC_OUT_BUFFER: 'JXL_DEC_NEED_DC_OUT_BUFFER',
JXL_DEC_NEED_IMAGE_OUT_BUFFER: 'JXL_DEC_NEED_IMAGE_OUT_BUFFER',
JXL_DEC_JPEG_NEED_MORE_OUTPUT: 'JXL_DEC_JPEG_NEED_MORE_OUTPUT',
JXL_DEC_BASIC_INFO: 'JXL_DEC_BASIC_INFO',
JXL_DEC_EXTENSIONS: 'JXL_DEC_EXTENSIONS',
JXL_DEC_COLOR_ENCODING: 'JXL_DEC_COLOR_ENCODING',
JXL_DEC_PREVIEW_IMAGE: 'JXL_DEC_PREVIEW_IMAGE',
JXL_DEC_FRAME: 'JXL_DEC_FRAME',
JXL_DEC_DC_IMAGE: 'JXL_DEC_DC_IMAGE',
JXL_DEC_FULL_IMAGE: 'JXL_DEC_FULL_IMAGE',
JXL_DEC_JPEG_RECONSTRUCTION: 'JXL_DEC_JPEG_RECONSTRUCTION'
}.get(err, f'unknown error {err!r}')
elif 'Encoder' in func:
msg = {
JXL_ENC_SUCCESS: 'JXL_ENC_SUCCESS',
JXL_ENC_ERROR: 'JXL_ENC_ERROR',
JXL_ENC_NEED_MORE_OUTPUT: 'JXL_ENC_NEED_MORE_OUTPUT',
JXL_ENC_NOT_SUPPORTED: 'JXL_ENC_NOT_SUPPORTED',
}.get(err, f'unknown error {err!r}')
else:
msg = f'error {err!r}'
msg = f'{func} returned {msg}'
super().__init__(msg)
def jpegxl_version():
"""Return libjxl library version string."""
cdef:
uint32_t ver = JxlDecoderVersion()
return f'libjxl {ver / 1000000}.{(ver / 1000) % 1000}.{ver % 1000}'
def jpegxl_check(const uint8_t[::1] data):
"""Return True if data likely contains a JPEG XL image."""
cdef JxlSignature sig = JxlSignatureCheck(&data[0], min(data.size, 16))
return sig != JXL_SIG_NOT_ENOUGH_BYTES and sig != JXL_SIG_INVALID
def jpegxl_encode(
data,
level=None, # None or < 0: lossless, 0-4: tier/speed
effort=None,
distance=None,
photometric=None,
usecontainer=None,
numthreads=None,
out=None
):
"""Return JPEG XL image from numpy array.
"""
cdef:
numpy.ndarray src
numpy.dtype dtype
const uint8_t[::1] dst # must be const to write to bytes
ssize_t dstsize
ssize_t srcsize
ssize_t frame
ssize_t ysize, xsize
ssize_t frames = 1
ssize_t samples = 1
int colorspace = -1
size_t avail_out = 0
uint8_t* next_out = NULL
output_t* compressed = NULL
size_t buffer_size = 0
char* buffer = NULL
void* runner = NULL
JxlEncoder* encoder = NULL
JxlEncoderOptions* options = NULL
JxlEncoderStatus status = JXL_ENC_SUCCESS
JxlBasicInfo basic_info
JxlPixelFormat pixel_format
JxlColorEncoding color_encoding
JXL_BOOL use_container = bool(usecontainer)
JXL_BOOL option_lossless = level is None or level < 0
int option_tier = _default_value(level, 0, 0, 4)
int option_effort = _default_value(effort, 3, 3, 9) # 7 is too slow
float option_distance = _default_value(distance, 1.0, 0.0, 15.0)
size_t num_threads = <size_t> _default_threads(numthreads)
if data is out:
raise ValueError('cannot encode in-place')
if isinstance(data, (bytes, bytearray)):
# input is a JPEG stream
return jpegxl_from_jpeg(data, use_container, num_threads, out)
src = numpy.ascontiguousarray(data)
dtype = src.dtype
srcsize = src.nbytes
buffer_size = src.nbytes
buffer = src.data
if not (src.dtype.kind in 'uf' and src.ndim in (2, 3, 4)):
raise ValueError('invalid data shape or dtype')
out, dstsize, outgiven, outtype = _parse_output(out)
if out is not None:
dst = out
dstsize = dst.nbytes
compressed = output_new(<uint8_t*> &dst[0], dstsize)
elif dstsize > 0:
out = _create_output(outtype, dstsize)
dst = out
dstsize = dst.nbytes
compressed = output_new(<uint8_t*> &dst[0], dstsize)
else:
compressed = output_new(
NULL, max(32768, srcsize // (4 if option_lossless else 16))
)
if compressed == NULL:
raise MemoryError('output_new failed')
JxlEncoderInitBasicInfo(&basic_info)
memset(<void*> &pixel_format, 0, sizeof(JxlPixelFormat))
memset(<void*> &color_encoding, 0, sizeof(JxlColorEncoding))
if src.ndim < 2:
raise ValueError('not an image')
if src.ndim == 2:
frames = 1
ysize = src.shape[0]
xsize = src.shape[1]
samples = 1
elif src.ndim == 3:
frames = 1
ysize = src.shape[0]
xsize = src.shape[1]
samples = src.shape[2]
elif src.ndim == 4:
frames = src.shape[0]
ysize = src.shape[1]
xsize = src.shape[2]
samples = src.shape[3]
else:
raise ValueError(f'{src.ndim} dimensions not supported')
colorspace = jpegxl_encode_photometric(photometric)
if colorspace == -1:
if samples > 2:
colorspace = JXL_COLOR_SPACE_RGB
else:
colorspace = JXL_COLOR_SPACE_GRAY
elif samples < 3:
colorspace = JXL_COLOR_SPACE_GRAY
pixel_format.num_channels = <uint32_t> samples
pixel_format.endianness = JXL_NATIVE_ENDIAN
pixel_format.align = 0 # TODO: allow strides
basic_info.xsize = <uint32_t> xsize
basic_info.ysize = <uint32_t> ysize
if colorspace == JXL_COLOR_SPACE_GRAY:
basic_info.num_color_channels = 1
else:
basic_info.num_color_channels = 3
assert samples - basic_info.num_color_channels >= 0
basic_info.num_extra_channels = (
<uint32_t> samples - basic_info.num_color_channels
)
if dtype.byteorder == b'<':
pixel_format.endianness = JXL_LITTLE_ENDIAN
elif dtype.byteorder == b'>':
pixel_format.endianness = JXL_BIG_ENDIAN
if dtype == numpy.uint8:
pixel_format.data_type = JXL_TYPE_UINT8
basic_info.bits_per_sample = 8
elif dtype == numpy.uint16:
pixel_format.data_type = JXL_TYPE_UINT16
basic_info.bits_per_sample = 16
elif dtype == numpy.uint32:
# TODO: raises JXL_ENC_NOT_SUPPORTED ?
pixel_format.data_type = JXL_TYPE_UINT32
basic_info.bits_per_sample = 32
elif dtype == numpy.float32:
pixel_format.data_type = JXL_TYPE_FLOAT
basic_info.bits_per_sample = 32
basic_info.exponent_bits_per_sample = 8
elif dtype == numpy.float16:
pixel_format.data_type = JXL_TYPE_FLOAT16
basic_info.bits_per_sample = 16
basic_info.exponent_bits_per_sample = 5
else:
raise ValueError(f'dtype {dtype!r} not supported')
if option_lossless != 0:
# avoid lossy colorspace conversion
basic_info.uses_original_profile = JXL_TRUE
if basic_info.num_extra_channels > 0:
basic_info.alpha_bits = basic_info.bits_per_sample
basic_info.alpha_exponent_bits = basic_info.exponent_bits_per_sample
# TODO: there seems to be no API to add JxlExtraChannelInfo (?)
if basic_info.num_extra_channels > 1:
raise ValueError(
f'{basic_info.num_extra_channels} extra channels not supported'
)
try:
with nogil:
encoder = JxlEncoderCreate(NULL)
if encoder == NULL:
raise JpegxlError('JxlEncoderCreate', None)
if num_threads == 0:
num_threads = JxlThreadParallelRunnerDefaultNumWorkerThreads()
if num_threads > 1:
runner = JxlThreadParallelRunnerCreate(NULL, num_threads)
if runner == NULL:
raise JpegxlError('JxlThreadParallelRunnerCreate', None)
status = JxlEncoderSetParallelRunner(
encoder, JxlThreadParallelRunner, runner
)
if status != JXL_ENC_SUCCESS:
raise JpegxlError('JxlEncoderSetParallelRunner', status)
status = JxlEncoderSetBasicInfo(encoder, &basic_info)
if status != JXL_ENC_SUCCESS:
raise JpegxlError('JxlEncoderSetBasicInfo', status)
# TODO: review this
if pixel_format.data_type == JXL_TYPE_UINT8:
JxlColorEncodingSetToSRGB(
&color_encoding, colorspace == JXL_COLOR_SPACE_GRAY
)
else:
JxlColorEncodingSetToLinearSRGB(
&color_encoding, colorspace == JXL_COLOR_SPACE_GRAY
)
status = JxlEncoderSetColorEncoding(encoder, &color_encoding)
if status != JXL_ENC_SUCCESS:
raise JpegxlError('JxlEncoderSetColorEncoding', status)
status = JxlEncoderUseContainer(encoder, use_container)
if status != JXL_ENC_SUCCESS:
raise JpegxlError('JxlEncoderUseContainer', status)
options = JxlEncoderOptionsCreate(encoder, NULL)
if options == NULL:
raise JpegxlError('JxlEncoderOptionsCreate', None)
if option_lossless != 0:
status = JxlEncoderOptionsSetLossless(options, option_lossless)
if status != JXL_ENC_SUCCESS:
raise JpegxlError('JxlEncoderOptionsSetLossless', status)
elif option_distance != 1.0:
status = JxlEncoderOptionsSetDistance(options, option_distance)
if status != JXL_ENC_SUCCESS:
raise JpegxlError('JxlEncoderOptionsSetDistance', status)
if option_tier != 0:
status = JxlEncoderOptionsSetDecodingSpeed(
options, option_tier
)
if status != JXL_ENC_SUCCESS:
raise JpegxlError(
'JxlEncoderOptionsSetDecodingSpeed', status
)
if option_effort != 7:
status = JxlEncoderOptionsSetEffort(options, option_effort)
if status != JXL_ENC_SUCCESS:
raise JpegxlError('JxlEncoderOptionsSetEffort', status)
for frame in range(frames):
status = JxlEncoderAddImageFrame(
options,
&pixel_format,
<void*> (buffer + frame * buffer_size),
buffer_size
)
if status != JXL_ENC_SUCCESS:
raise JpegxlError('JxlEncoderAddImageFrame', status)
while True:
next_out = compressed.data + compressed.used
avail_out = compressed.size - compressed.used
status = JxlEncoderProcessOutput(
encoder, &next_out, &avail_out
)
if output_seek(
compressed, compressed.size - avail_out
) == 0:
raise RuntimeError('output_seek returned 0')
if (
status != JXL_ENC_NEED_MORE_OUTPUT
or compressed.owner == 0
):
break
if output_resize(
compressed,
min(
compressed.size + <size_t> 33554432, # 32 MB
compressed.size * 2
)
) == 0:
raise RuntimeError('output_resize returned 0')
if status != JXL_ENC_SUCCESS:
raise JpegxlError('JxlEncoderProcessOutput', status)
if compressed.owner:
out = _create_output(
out, compressed.used, <const char *> compressed.data
)
else:
out = _return_output(
out, compressed.size, compressed.used, outgiven
)
finally:
if encoder != NULL:
if next_out != NULL:
JxlEncoderCloseInput(encoder)
JxlEncoderDestroy(encoder)
if runner != NULL:
JxlThreadParallelRunnerDestroy(runner)
if compressed != NULL:
output_del(compressed)
return out
def jpegxl_decode(
data,
index=None,
keeporientation=None,
tojpeg=None,
numthreads=None,
out=None,
):
"""Return numpy array from JPEG XL image.
"""
cdef:
numpy.ndarray dst
const uint8_t[::1] src = data
size_t srcsize = <size_t> src.size
size_t dstsize = 0
size_t samples
size_t buffer_size = 0
void* buffer = NULL
void* runner = NULL
JxlDecoder* decoder = NULL
JxlDecoderStatus status = JXL_DEC_SUCCESS
JxlSignature signature
JxlBasicInfo basic_info
JxlPixelFormat pixel_format
size_t num_threads = _default_threads(numthreads)
bint keep_orientation = bool(keeporientation)
signature = JxlSignatureCheck(&src[0], srcsize)
if signature != JXL_SIG_CODESTREAM and signature != JXL_SIG_CONTAINER:
raise ValueError('not a JPEG XL codestream')
if tojpeg:
# output JPEG stream
return jpegxl_to_jpeg(src, num_threads, out)
try:
with nogil:
memset(<void*> &basic_info, 0, sizeof(JxlBasicInfo))
memset(<void*> &pixel_format, 0, sizeof(JxlPixelFormat))
decoder = JxlDecoderCreate(NULL)
if decoder == NULL:
raise JpegxlError('JxlDecoderCreate', None)
if num_threads == 0:
num_threads = JxlThreadParallelRunnerDefaultNumWorkerThreads()
if num_threads > 1:
runner = JxlThreadParallelRunnerCreate(NULL, num_threads)
if runner == NULL:
raise JpegxlError('JxlThreadParallelRunnerCreate', None)
status = JxlDecoderSetParallelRunner(
decoder, JxlThreadParallelRunner, runner
)
if status != JXL_DEC_SUCCESS:
raise JpegxlError('JxlDecoderSetParallelRunner', status)
status = JxlDecoderSubscribeEvents(
decoder, JXL_DEC_BASIC_INFO | JXL_DEC_FULL_IMAGE
# | JXL_DEC_COLOR_ENCODING
)
if status != JXL_DEC_SUCCESS:
raise JpegxlError('JxlDecoderSubscribeEvents', status)
if keep_orientation:
status = JxlDecoderSetKeepOrientation(decoder, JXL_TRUE)
if status != JXL_DEC_SUCCESS:
raise JpegxlError('JxlDecoderSetKeepOrientation', status)
status = JxlDecoderSetInput(decoder, &src[0], srcsize)
if status != JXL_DEC_SUCCESS:
raise JpegxlError('JxlDecoderSetInput', status)
while True:
status = JxlDecoderProcessInput(decoder)
if (
status == JXL_DEC_ERROR
or status == JXL_DEC_NEED_MORE_INPUT
):
raise JpegxlError('JxlDecoderProcessInput', status)
if status == JXL_DEC_FULL_IMAGE or status == JXL_DEC_SUCCESS:
break
if status == JXL_DEC_BASIC_INFO:
status = JxlDecoderGetBasicInfo(decoder, &basic_info)
if status != JXL_DEC_SUCCESS:
raise JpegxlError('JxlDecoderGetBasicInfo', status)
# TODO: handle animations
# if basic_info.have_animation:
samples = (
basic_info.num_color_channels
+ basic_info.num_extra_channels
)
with gil:
if samples > 1:
shape = (
int(basic_info.ysize),
int(basic_info.xsize),
int(samples)
)
else:
shape = (
int(basic_info.ysize),
int(basic_info.xsize)
)
pixel_format.num_channels = <uint32_t> samples
pixel_format.endianness = JXL_NATIVE_ENDIAN
pixel_format.align = 0 # TODO: allow strides
if basic_info.exponent_bits_per_sample > 0:
if basic_info.bits_per_sample == 32:
pixel_format.data_type = JXL_TYPE_FLOAT
dtype = numpy.float32
elif basic_info.bits_per_sample == 16:
pixel_format.data_type = JXL_TYPE_FLOAT16
dtype = numpy.float16
# elif basic_info.bits_per_sample == 64:
# pixel_format.data_type = JXL_TYPE_FLOAT
# dtype = numpy.float64
else:
raise ValueError(
f'float{basic_info.bits_per_sample}'
' not supported'
)
elif basic_info.bits_per_sample <= 8:
pixel_format.data_type = JXL_TYPE_UINT8
dtype = numpy.uint8
elif basic_info.bits_per_sample <= 16:
pixel_format.data_type = JXL_TYPE_UINT16
dtype = numpy.uint16
elif basic_info.bits_per_sample <= 32:
pixel_format.data_type = JXL_TYPE_UINT32
dtype = numpy.uint32
else:
dtype = numpy.float32
pixel_format.data_type = JXL_TYPE_FLOAT
out = _create_array(out, shape, dtype)
dst = out
dstsize = dst.nbytes
buffer = <void*> dst.data
# elif status == JXL_DEC_COLOR_ENCODING:
# pass
elif status == JXL_DEC_NEED_IMAGE_OUT_BUFFER:
if buffer == NULL:
raise RuntimeError('buffer == NULL')
status = JxlDecoderImageOutBufferSize(
decoder, &pixel_format, &buffer_size
)
if status != JXL_DEC_SUCCESS:
raise JpegxlError(
'JxlDecoderImageOutBufferSize', status
)
if buffer_size != <size_t> dstsize:
raise RuntimeError(
f'buffer_size {buffer_size} != dstsize {dstsize}'
)
status = JxlDecoderSetImageOutBuffer(
decoder, &pixel_format, buffer, buffer_size
)
if status != JXL_DEC_SUCCESS:
raise JpegxlError(
'JxlDecoderSetImageOutBuffer', status
)
else:
raise RuntimeError(f'unknown status {status}')
finally:
if decoder != NULL:
JxlDecoderDestroy(decoder)
if runner != NULL:
JxlThreadParallelRunnerDestroy(runner)
return out
cdef object jpegxl_from_jpeg(
const uint8_t[::1] src,
JXL_BOOL use_container,
size_t num_threads,
out
):
"""Return JPEG XL from JPEG stream."""
raise NotImplementedError # TODO: transcoding not yet supported by libjxl
cdef object jpegxl_to_jpeg(
const uint8_t[::1] src,
size_t num_threads,
out
):
"""Return JPEG from JPEG XL stream."""
raise NotImplementedError # TODO: transcoding not yet supported by libjxl
cdef int jpegxl_encode_photometric(photometric):
"""Return JxlColorSpace value from photometric argument."""
if photometric is None:
return -1
if isinstance(photometric, int):
if photometric not in (
-1,
JXL_COLOR_SPACE_RGB,
JXL_COLOR_SPACE_GRAY,
JXL_COLOR_SPACE_XYB,
JXL_COLOR_SPACE_UNKNOWN
):
raise ValueError('photometric interpretation not supported')
return photometric
photometric = photometric.upper()
if photometric[:3] == 'RGB':
return JXL_COLOR_SPACE_RGB
if photometric in ('WHITEISZERO', 'MINISWHITE'):
return JXL_COLOR_SPACE_GRAY
if photometric in ('BLACKISZERO', 'MINISBLACK', 'GRAY'):
return JXL_COLOR_SPACE_GRAY
if photometric == 'XYB':
return JXL_COLOR_SPACE_XYB
if photometric == 'UNKNOWN':
return JXL_COLOR_SPACE_UNKNOWN
raise ValueError(
'photometric interpretation {photometric!r} not supported'
)
ctypedef struct output_t:
uint8_t* data
size_t size
size_t pos
size_t used
int owner
cdef output_t* output_new(uint8_t* data, size_t size) nogil:
"""Return new output."""
cdef:
output_t* output = <output_t*> malloc(sizeof(output_t))
if output == NULL:
return NULL
output.size = size
output.used = 0
output.pos = 0
if data == NULL:
output.owner = 1
output.data = <uint8_t*> malloc(size)
else:
output.owner = 0
output.data = data
if output.data == NULL:
free(output)
return NULL
return output
cdef void output_del(output_t* output) nogil:
"""Free output."""
if output != NULL:
if output.owner != 0:
free(output.data)
free(output)
cdef int output_seek(output_t* output, size_t pos) nogil:
"""Seek output to position."""
if output == NULL or pos > output.size:
return 0
output.pos = pos
if pos > output.used:
output.used = pos
return 1
cdef int output_resize(output_t* output, size_t newsize) nogil:
"""Resize output."""
cdef:
uint8_t* tmp
if output == NULL or newsize == 0 or output.used > output.size:
return 0
if newsize == output.size or output.owner == 0:
return 1
tmp = <uint8_t*> realloc(<void*> output.data, newsize)
if tmp == NULL:
return 0
output.data = tmp
output.size = newsize
return 1