Skip to content

Commit

Permalink
converting & saving 16 bits to 12 bits
Browse files Browse the repository at this point in the history
  • Loading branch information
bigcat88 committed Jul 3, 2022
1 parent 3ab234f commit 03c42f9
Show file tree
Hide file tree
Showing 5 changed files with 280 additions and 257 deletions.
13 changes: 12 additions & 1 deletion pillow_heif/_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,21 @@ def ctx_in_memory(self) -> bool:
def ctx_in_memory(self, value):
self._cfg["ctx_in_memory"] = value

@property
def save_to_12bit(self) -> bool:
"""Should 16 bit images be saved to 12 bit instead of 10. Default = ``False``"""

return self._cfg["save_to_12bit"]

@save_to_12bit.setter
def save_to_12bit(self, value):
self._cfg["save_to_12bit"] = value

def update(self, **kwargs) -> None:
"""Method for at once update multiply values in config."""

_keys = kwargs.keys()
for k in ("avif", "strict", "thumbnails", "quality", "ctx_in_memory"):
for k in ("avif", "strict", "thumbnails", "quality", "ctx_in_memory", "save_to_12bit"):
if k in _keys:
setattr(self, k, kwargs[k])

Expand All @@ -105,6 +115,7 @@ def reset(self) -> None:
self._cfg["thumbnails"] = True
self._cfg["quality"] = None
self._cfg["ctx_in_memory"] = True
self._cfg["save_to_12bit"] = False


CFG_OPTIONS: PyLibHeifOptions = PyLibHeifOptions()
Expand Down
7 changes: 5 additions & 2 deletions pillow_heif/heif.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def __init__(self, heif_ctx: Union[LibHeifCtx, HeifCtxAsDict], handle, for_encod
self.mode = heif_ctx.mode
if heif_ctx.data:
new_mode = MODE_INFO[self.mode][4] if for_encoding else None
if isinstance(new_mode, tuple):
new_mode = new_mode[1] if options().save_to_12bit else new_mode[0]
_img = self._create_image(heif_ctx.data, heif_ctx.stride, new_mode)
self._img_to_img_data_dict(_img)

Expand Down Expand Up @@ -224,11 +226,12 @@ def _create_image(self, src_data, src_stride: int, new_mode=None):
p_dest_stride = ffi.new("int *")
dest_data = lib.heif_image_get_plane(new_img, self._channel(), p_dest_stride)
dest_stride = p_dest_stride[0]
src_data = ffi.from_buffer(src_data)
if new_mode and new_mode != self.mode:
MODE_CONVERT[self.mode][new_mode](dest_data, src_data, dest_stride, src_stride, height)
MODE_CONVERT[self.mode][new_mode](src_data, src_stride, dest_data, dest_stride, height)
self.mode = new_mode
else:
lib.copy_image_data(ffi.from_buffer(src_data), src_stride, dest_data, dest_stride, height)
lib.copy_image_data(src_data, src_stride, dest_data, dest_stride, height)
return new_img

def _get_pure_data(self):
Expand Down
Loading

0 comments on commit 03c42f9

Please sign in to comment.