22Undocumented private functions for other code to look better.
33"""
44
5- from typing import Union
5+ from typing import Tuple , Union
66
77from _pillow_heif_cffi import ffi , lib
88
99from ._libheif_ctx import LibHeifCtxWrite
1010from .constants import HeifChannel , HeifChroma , HeifColorProfileType , HeifColorspace
1111from .error import check_libheif_error
1212
13+ MODE_CHANNELS = {
14+ "RGBA" : 4 ,
15+ "RGB" : 3 ,
16+ "L" : 1 ,
17+ "" : 0 ,
18+ }
19+
1320
1421# from dataclasses import dataclass
1522# @dataclass # Avalaible from Python 3.7
@@ -20,7 +27,7 @@ def __init__(self, bit_depth: int, mode: str, size: tuple, data, **kwargs):
2027 stride = kwargs .get ("stride" , None )
2128 if stride is None :
2229 factor = 1 if bit_depth == 8 else 2
23- stride = size [0 ] * 3 * factor if mode == "RGB" else size [ 0 ] * 4 * factor
30+ stride = size [0 ] * MODE_CHANNELS [ mode ] * factor
2431 self .bit_depth = bit_depth
2532 self .mode = mode
2633 self .size = size
@@ -29,16 +36,18 @@ def __init__(self, bit_depth: int, mode: str, size: tuple, data, **kwargs):
2936 self .additional_info = kwargs .get ("add_info" , {})
3037
3138
32- def create_image (size : tuple , chroma : HeifChroma , bit_depth : int , data , stride : int , ** kwargs ):
39+ def create_image (size : tuple , chroma_color : Tuple [ HeifChroma , HeifColorspace ], bit_depth : int , data , stride : int ):
3340 width , height = size
41+ chroma , color = chroma_color
3442 p_new_img = ffi .new ("struct heif_image **" )
35- error = lib .heif_image_create (width , height , kwargs . get ( " color" , HeifColorspace . RGB ) , chroma , p_new_img )
43+ error = lib .heif_image_create (width , height , color , chroma , p_new_img )
3644 check_libheif_error (error )
3745 new_img = ffi .gc (p_new_img [0 ], lib .heif_image_release )
38- error = lib .heif_image_add_plane (new_img , HeifChannel .INTERLEAVED , width , height , bit_depth )
46+ channel = HeifChannel .Y if color == HeifColorspace .MONOCHROME else HeifChannel .INTERLEAVED
47+ error = lib .heif_image_add_plane (new_img , channel , width , height , bit_depth )
3948 check_libheif_error (error )
4049 p_dest_stride = ffi .new ("int *" )
41- p_data = lib .heif_image_get_plane (new_img , HeifChannel . INTERLEAVED , p_dest_stride )
50+ p_data = lib .heif_image_get_plane (new_img , channel , p_dest_stride )
4251 dest_stride = p_dest_stride [0 ]
4352 copy_image_data (p_data , data , dest_stride , stride , height )
4453 return new_img
0 commit comments