Skip to content

Commit 34eac05

Browse files
authored
Handle 16-bit containers with 8-bit samples when encoding bytes (#21)
1 parent fcd6807 commit 34eac05

File tree

5 files changed

+198
-183
lines changed

5 files changed

+198
-183
lines changed

jpeg_ls/CharLS.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,9 @@ def encode_buffer(
198198
Parameters
199199
----------
200200
src : bytes
201-
The image data to be JPEG-LS encoded.
201+
The little-endian ordered image data to be JPEG-LS encoded. May use
202+
either 8- or 16-bits per pixel, as long as the bit-depth is sufficient
203+
for `bits_stored`.
202204
rows : int
203205
The number of rows of pixels in the image.
204206
columns : int
@@ -275,11 +277,15 @@ def encode_buffer(
275277
length_src = len(src)
276278
length_expected = rows * columns * samples_per_pixel * bytes_per_pixel
277279
if length_expected != length_src:
278-
raise ValueError(
279-
f"The 'src' length of {length_src} bytes does not match the expected "
280-
"length determined from 'rows', 'columns', 'samples_per_pixel' and "
281-
"'bits_stored'"
282-
)
280+
if length_expected * 2 != length_src:
281+
raise ValueError(
282+
f"The 'src' length of {length_src} bytes does not match the expected "
283+
"length determined from 'rows', 'columns', 'samples_per_pixel' and "
284+
"'bits_stored'"
285+
)
286+
287+
# 16-bits per pixel for bits_stored <= 8
288+
src = src[::2]
283289

284290
return _CharLS._encode(
285291
src,

0 commit comments

Comments
 (0)