1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
use std::collections::HashMap; use std::ffi::CString; use libheif_sys as lh; use crate::utils::cstr_to_str; use crate::{ EncoderParameterType, EncoderParameterValue, EncoderQuality, HeifError, HeifErrorCode, HeifErrorSubCode, Result, }; pub type EncoderParametersTypes = HashMap<String, EncoderParameterType>; fn parameters_types(c_encoder: *mut lh::heif_encoder) -> Result<EncoderParametersTypes> { let mut res = EncoderParametersTypes::new(); unsafe { let mut param_pointers = lh::heif_encoder_list_parameters(c_encoder); if !param_pointers.is_null() { while let Some(raw_param) = (*param_pointers).as_ref() { let c_param_type = lh::heif_encoder_parameter_get_type(raw_param); let param_type: EncoderParameterType; match num::FromPrimitive::from_u32(c_param_type) { Some(res) => { param_type = res; } None => { return Err(HeifError { code: HeifErrorCode::EncoderPluginError, sub_code: HeifErrorSubCode::UnsupportedParameter, message: format!("{} is unknown type of parameter", c_param_type), }); } } let c_param_name = lh::heif_encoder_parameter_get_name(raw_param); let name = cstr_to_str(c_param_name).unwrap_or("").to_string(); res.insert(name, param_type); param_pointers = param_pointers.offset(1); } } } Ok(res) } pub struct Encoder { pub(crate) inner: *mut lh::heif_encoder, pub(crate) parameters_types: EncoderParametersTypes, } impl Encoder { pub(crate) fn new(c_encoder: *mut lh::heif_encoder) -> Result<Encoder> { Ok(Encoder { inner: c_encoder, parameters_types: parameters_types(c_encoder)?, }) } pub fn name(&self) -> &str { let res = unsafe { lh::heif_encoder_get_name(self.inner) }; cstr_to_str(res).unwrap_or("") } pub fn set_quality(&mut self, quality: EncoderQuality) -> Result<()> { let err; match quality { EncoderQuality::LossLess => { err = unsafe { lh::heif_encoder_set_lossless(self.inner, 1) }; } EncoderQuality::Lossy(value) => { unsafe { let middle_err = lh::heif_encoder_set_lossless(self.inner, 0); HeifError::from_heif_error(middle_err)?; err = lh::heif_encoder_set_lossy_quality(self.inner, i32::from(value)) }; } } HeifError::from_heif_error(err) } fn parameter_value( &self, name: &str, parameter_type: EncoderParameterType, ) -> Result<EncoderParameterValue> { let c_param_name = CString::new(name).unwrap(); let param_value; match parameter_type { EncoderParameterType::Int => { let mut value = 0; let err = unsafe { lh::heif_encoder_get_parameter_integer( self.inner, c_param_name.as_ptr(), &mut value as _, ) }; HeifError::from_heif_error(err)?; param_value = EncoderParameterValue::Int(value); } EncoderParameterType::Bool => { let mut value = 0; let err = unsafe { lh::heif_encoder_get_parameter_boolean( self.inner, c_param_name.as_ptr(), &mut value as _, ) }; HeifError::from_heif_error(err)?; param_value = EncoderParameterValue::Bool(value > 0); } EncoderParameterType::String => { let value: Vec<u8> = vec![0; 51]; let err = unsafe { lh::heif_encoder_get_parameter_string( self.inner, c_param_name.as_ptr(), value.as_ptr() as _, 50, ) }; HeifError::from_heif_error(err)?; param_value = EncoderParameterValue::String( cstr_to_str(value.as_ptr() as _).unwrap_or("").to_string(), ); } }; Ok(param_value) } pub fn parameters_names(&self) -> Vec<String> { self.parameters_types.keys().cloned().collect() } pub fn parameter(&self, name: &str) -> Result<Option<EncoderParameterValue>> { match self.parameters_types.get(name) { Some(param_type) => { let value = self.parameter_value(name, *param_type)?; Ok(Some(value)) } None => Ok(None), } } } impl Drop for Encoder { fn drop(&mut self) { unsafe { lh::heif_encoder_release(self.inner) }; } } #[repr(C)] #[derive(Debug, Copy, Clone)] pub struct EncodingOptions { pub version: u8, pub save_alpha_channel: bool, } impl Default for EncodingOptions { fn default() -> Self { unsafe { let heif_opt = lh::heif_encoding_options_alloc(); let res = EncodingOptions { version: (*heif_opt).version, save_alpha_channel: (*heif_opt).save_alpha_channel != 0, }; lh::heif_encoding_options_free(heif_opt); res } } } impl EncodingOptions { pub(crate) fn heif_encoding_options(self) -> lh::heif_encoding_options { lh::heif_encoding_options { version: self.version, save_alpha_channel: if self.save_alpha_channel { 1 } else { 0 }, } } }