@@ -114,24 +114,38 @@ def get_pure_stride(mode: str, width: int) -> int:
114114}
115115
116116
117+ NCLX_FIELDS = ("color_primaries" , "transfer_characteristics" , "matrix_coefficients" , "full_range_flag" )
118+ NCLX_DECODE_ONLY_FIELDS = (
119+ "color_primary_red_x" ,
120+ "color_primary_red_y" ,
121+ "color_primary_green_x" ,
122+ "color_primary_green_y" ,
123+ "color_primary_blue_x" ,
124+ "color_primary_blue_y" ,
125+ "color_primary_white_x" ,
126+ "color_primary_white_y" ,
127+ )
128+
129+
117130def read_color_profile (handle ) -> dict :
118131 profile_type = lib .heif_image_handle_get_color_profile_type (handle )
119132 if profile_type == HeifColorProfileType .NOT_PRESENT :
120133 return {}
121134 if profile_type == HeifColorProfileType .NCLX :
122- _type = "nclx"
123135 pp_data = ffi .new ("struct heif_color_profile_nclx **" )
124- data_length = ffi .sizeof ("struct heif_color_profile_nclx" )
125136 error = lib .heif_image_handle_get_nclx_color_profile (handle , pp_data )
126- p_data = pp_data [0 ]
137+ check_libheif_error (error )
138+ libheif_nclx_profile = pp_data [0 ]
127139 ffi .release (pp_data )
128- else :
129- _type = "prof" if profile_type == HeifColorProfileType .PROF else "rICC"
130- data_length = lib .heif_image_handle_get_raw_color_profile_size (handle )
131- if data_length == 0 :
132- return {"type" : _type , "data" : b"" }
133- p_data = ffi .new ("char[]" , data_length )
134- error = lib .heif_image_handle_get_raw_color_profile (handle , p_data )
140+ nclx_profile = {i : getattr (libheif_nclx_profile , i ) for i in NCLX_FIELDS + NCLX_DECODE_ONLY_FIELDS }
141+ lib .heif_nclx_color_profile_free (libheif_nclx_profile )
142+ return {"type" : "nclx" , "data" : nclx_profile }
143+ _type = "prof" if profile_type == HeifColorProfileType .PROF else "rICC"
144+ data_length = lib .heif_image_handle_get_raw_color_profile_size (handle )
145+ if data_length == 0 :
146+ return {"type" : _type , "data" : b"" }
147+ p_data = ffi .new ("char[]" , data_length )
148+ error = lib .heif_image_handle_get_raw_color_profile (handle , p_data )
135149 check_libheif_error (error )
136150 data_buffer = ffi .buffer (p_data , data_length )
137151 return {"type" : _type , "data" : bytes (data_buffer )}
@@ -146,10 +160,14 @@ def set_color_profile(heif_img, info: dict) -> None:
146160 )
147161 check_libheif_error (error )
148162 elif info .get ("nclx_profile" , None ):
149- error = lib .heif_image_set_nclx_color_profile (
150- heif_img ,
151- ffi .cast ("const struct heif_color_profile_nclx*" , ffi .from_buffer (info ["nclx_profile" ])),
152- )
163+ nclx_profile = info ["nclx_profile" ]
164+ libheif_nclx_profile = lib .heif_nclx_color_profile_alloc ()
165+ for i in NCLX_FIELDS :
166+ v = nclx_profile .get (i , None )
167+ if v is not None :
168+ setattr (libheif_nclx_profile , i , v )
169+ error = lib .heif_image_set_nclx_color_profile (heif_img , libheif_nclx_profile )
170+ lib .heif_nclx_color_profile_free (libheif_nclx_profile )
153171 check_libheif_error (error )
154172
155173
0 commit comments