Skip to content

Commit

Permalink
Handle 16-bit containers with 8-bit samples when encoding bytes (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
scaramallion authored Feb 18, 2024
1 parent fcd6807 commit 34eac05
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 183 deletions.
18 changes: 12 additions & 6 deletions jpeg_ls/CharLS.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ def encode_buffer(
Parameters
----------
src : bytes
The image data to be JPEG-LS encoded.
The little-endian ordered image data to be JPEG-LS encoded. May use
either 8- or 16-bits per pixel, as long as the bit-depth is sufficient
for `bits_stored`.
rows : int
The number of rows of pixels in the image.
columns : int
Expand Down Expand Up @@ -275,11 +277,15 @@ def encode_buffer(
length_src = len(src)
length_expected = rows * columns * samples_per_pixel * bytes_per_pixel
if length_expected != length_src:
raise ValueError(
f"The 'src' length of {length_src} bytes does not match the expected "
"length determined from 'rows', 'columns', 'samples_per_pixel' and "
"'bits_stored'"
)
if length_expected * 2 != length_src:
raise ValueError(
f"The 'src' length of {length_src} bytes does not match the expected "
"length determined from 'rows', 'columns', 'samples_per_pixel' and "
"'bits_stored'"
)

# 16-bits per pixel for bits_stored <= 8
src = src[::2]

return _CharLS._encode(
src,
Expand Down
Loading

0 comments on commit 34eac05

Please sign in to comment.