1use std::ffi::CStr;
2use std::fmt;
3
4use libheif_sys as lh;
5
6#[derive(Debug, Copy, Clone, enumn::N)]
7#[non_exhaustive]
8#[repr(C)]
9pub enum HeifErrorCode {
10 InputDoesNotExist = lh::heif_error_code_heif_error_Input_does_not_exist as _,
12 InvalidInput = lh::heif_error_code_heif_error_Invalid_input as _,
14 UnsupportedFileType = lh::heif_error_code_heif_error_Unsupported_filetype as _,
15 UnsupportedFeature = lh::heif_error_code_heif_error_Unsupported_feature as _,
17 UsageError = lh::heif_error_code_heif_error_Usage_error as _,
19 MemoryAllocationError = lh::heif_error_code_heif_error_Memory_allocation_error as _,
21 DecoderPluginError = lh::heif_error_code_heif_error_Decoder_plugin_error as _,
23 EncoderPluginError = lh::heif_error_code_heif_error_Encoder_plugin_error as _,
25 EncodingError = lh::heif_error_code_heif_error_Encoding_error as _,
27 ColorProfileDoesNotExist = lh::heif_error_code_heif_error_Color_profile_does_not_exist as _,
29 PluginLoadingError = lh::heif_error_code_heif_error_Plugin_loading_error as _,
31 #[cfg(feature = "v1_19")]
33 Canceled = lh::heif_error_code_heif_error_Canceled as _,
34 ContextCreateFailed,
35 Unknown,
37}
38
39#[derive(Debug, Copy, Clone, enumn::N)]
40#[non_exhaustive]
41#[repr(C)]
42pub enum HeifErrorSubCode {
43 Unspecified = lh::heif_suberror_code_heif_suberror_Unspecified as _,
44 EndOfData = lh::heif_suberror_code_heif_suberror_End_of_data as _,
45 InvalidBoxSize = lh::heif_suberror_code_heif_suberror_Invalid_box_size as _,
46 NoFtypBox = lh::heif_suberror_code_heif_suberror_No_ftyp_box as _,
47 NoIdatBox = lh::heif_suberror_code_heif_suberror_No_idat_box as _,
48 NoMetaBox = lh::heif_suberror_code_heif_suberror_No_meta_box as _,
49 NoHdlrBox = lh::heif_suberror_code_heif_suberror_No_hdlr_box as _,
50 NoHvccBox = lh::heif_suberror_code_heif_suberror_No_hvcC_box as _,
51 NoPitmBox = lh::heif_suberror_code_heif_suberror_No_pitm_box as _,
52 NoIpcoBox = lh::heif_suberror_code_heif_suberror_No_ipco_box as _,
53 NoIpmaBox = lh::heif_suberror_code_heif_suberror_No_ipma_box as _,
54 NoIlocBox = lh::heif_suberror_code_heif_suberror_No_iloc_box as _,
55 NoIinfBox = lh::heif_suberror_code_heif_suberror_No_iinf_box as _,
56 NoIprpBox = lh::heif_suberror_code_heif_suberror_No_iprp_box as _,
57 NoIrefBox = lh::heif_suberror_code_heif_suberror_No_iref_box as _,
58 NoPictHandler = lh::heif_suberror_code_heif_suberror_No_pict_handler as _,
59 IpmaBoxReferencesNonExistingProperty =
60 lh::heif_suberror_code_heif_suberror_Ipma_box_references_nonexisting_property as _,
61 NoPropertiesAssignedToItem =
62 lh::heif_suberror_code_heif_suberror_No_properties_assigned_to_item as _,
63 NoItemData = lh::heif_suberror_code_heif_suberror_No_item_data as _,
64 InvalidGridData = lh::heif_suberror_code_heif_suberror_Invalid_grid_data as _,
65 MissingGridImages = lh::heif_suberror_code_heif_suberror_Missing_grid_images as _,
66 InvalidCleanAperture = lh::heif_suberror_code_heif_suberror_Invalid_clean_aperture as _,
67 InvalidOverlayData = lh::heif_suberror_code_heif_suberror_Invalid_overlay_data as _,
68 OverlayImageOutsideOfCanvas =
69 lh::heif_suberror_code_heif_suberror_Overlay_image_outside_of_canvas as _,
70 AuxiliaryImageTypeUnspecified =
71 lh::heif_suberror_code_heif_suberror_Auxiliary_image_type_unspecified as _,
72 NoOrInvalidPrimaryItem = lh::heif_suberror_code_heif_suberror_No_or_invalid_primary_item as _,
73 NoInfeBox = lh::heif_suberror_code_heif_suberror_No_infe_box as _,
74 UnknownColorProfileType = lh::heif_suberror_code_heif_suberror_Unknown_color_profile_type as _,
75 WrongTileImageChromaFormat =
76 lh::heif_suberror_code_heif_suberror_Wrong_tile_image_chroma_format as _,
77 InvalidFractionalNumber = lh::heif_suberror_code_heif_suberror_Invalid_fractional_number as _,
78 InvalidImageSize = lh::heif_suberror_code_heif_suberror_Invalid_image_size as _,
79 InvalidPixiBox = lh::heif_suberror_code_heif_suberror_Invalid_pixi_box as _,
80 NoAv1cBox = lh::heif_suberror_code_heif_suberror_No_av1C_box as _,
81 WrongTileImagePixelDepth =
82 lh::heif_suberror_code_heif_suberror_Wrong_tile_image_pixel_depth as _,
83 SecurityLimitExceeded = lh::heif_suberror_code_heif_suberror_Security_limit_exceeded as _,
84 NonExistingItemReferenced =
85 lh::heif_suberror_code_heif_suberror_Nonexisting_item_referenced as _,
86 NullPointerArgument = lh::heif_suberror_code_heif_suberror_Null_pointer_argument as _,
87 NonExistingImageChannelReferenced =
88 lh::heif_suberror_code_heif_suberror_Nonexisting_image_channel_referenced as _,
89 UnsupportedPluginVersion = lh::heif_suberror_code_heif_suberror_Unsupported_plugin_version as _,
90 UnsupportedWriterVersion = lh::heif_suberror_code_heif_suberror_Unsupported_writer_version as _,
91 UnsupportedParameter = lh::heif_suberror_code_heif_suberror_Unsupported_parameter as _,
92 InvalidParameterValue = lh::heif_suberror_code_heif_suberror_Invalid_parameter_value as _,
93 UnsupportedCodec = lh::heif_suberror_code_heif_suberror_Unsupported_codec as _,
94 UnsupportedImageType = lh::heif_suberror_code_heif_suberror_Unsupported_image_type as _,
95 UnsupportedDataVersion = lh::heif_suberror_code_heif_suberror_Unsupported_data_version as _,
96 UnsupportedColorConversion =
97 lh::heif_suberror_code_heif_suberror_Unsupported_color_conversion as _,
98 UnsupportedItemConstructionMethod =
99 lh::heif_suberror_code_heif_suberror_Unsupported_item_construction_method as _,
100 UnsupportedBitDepth = lh::heif_suberror_code_heif_suberror_Unsupported_bit_depth as _,
101 CannotWriteOutputData = lh::heif_suberror_code_heif_suberror_Cannot_write_output_data as _,
102 UnknownNclxColorPrimaries =
103 lh::heif_suberror_code_heif_suberror_Unknown_NCLX_color_primaries as _,
104 UnknownNclxTransferCharacteristics =
105 lh::heif_suberror_code_heif_suberror_Unknown_NCLX_transfer_characteristics as _,
106 UnknownNclxMatrixCoefficients =
107 lh::heif_suberror_code_heif_suberror_Unknown_NCLX_matrix_coefficients as _,
108 UnsupportedHeaderCompressionMethod =
109 lh::heif_suberror_code_heif_suberror_Unsupported_header_compression_method as _,
110 PluginLoadingError = lh::heif_suberror_code_heif_suberror_Plugin_loading_error as _,
111 PluginIsNotLoaded = lh::heif_suberror_code_heif_suberror_Plugin_is_not_loaded as _,
112 CannotReadPluginDirectory =
113 lh::heif_suberror_code_heif_suberror_Cannot_read_plugin_directory as _,
114 InvalidRegionData = lh::heif_suberror_code_heif_suberror_Invalid_region_data as _,
115 InvalidProperty = lh::heif_suberror_code_heif_suberror_Invalid_property as _,
116 ItemReferenceCycle = lh::heif_suberror_code_heif_suberror_Item_reference_cycle as _,
117 EncoderInitialization = lh::heif_suberror_code_heif_suberror_Encoder_initialization as _,
118 EncoderEncoding = lh::heif_suberror_code_heif_suberror_Encoder_encoding as _,
119 EncoderCleanup = lh::heif_suberror_code_heif_suberror_Encoder_cleanup as _,
120 TooManyRegions = lh::heif_suberror_code_heif_suberror_Too_many_regions as _,
121 #[cfg(feature = "v1_18")]
122 NoIspeProperty = lh::heif_suberror_code_heif_suberror_No_ispe_property as _,
123 #[cfg(feature = "v1_18")]
124 CameraIntrinsicMatrixUndefined =
125 lh::heif_suberror_code_heif_suberror_Camera_intrinsic_matrix_undefined as _,
126 #[cfg(feature = "v1_18")]
127 CameraExtrinsicMatrixUndefined =
128 lh::heif_suberror_code_heif_suberror_Camera_extrinsic_matrix_undefined as _,
129 #[cfg(feature = "v1_18")]
130 InvalidJ2kCodeStream = lh::heif_suberror_code_heif_suberror_Invalid_J2K_codestream as _,
131 #[cfg(feature = "v1_18")]
132 NoVvccBox = lh::heif_suberror_code_heif_suberror_No_vvcC_box as _,
133 #[cfg(feature = "v1_18")]
134 NoIcbrBox = lh::heif_suberror_code_heif_suberror_No_icbr_box as _,
135 #[cfg(feature = "v1_18")]
136 DecompressionInvalidData = lh::heif_suberror_code_heif_suberror_Decompression_invalid_data as _,
137 #[cfg(feature = "v1_18")]
138 CompressionInitialisationError =
139 lh::heif_suberror_code_heif_suberror_Compression_initialisation_error as _,
140 #[cfg(feature = "v1_18")]
141 UnsupportedGenericCompressionMethod =
142 lh::heif_suberror_code_heif_suberror_Unsupported_generic_compression_method as _,
143 #[cfg(feature = "v1_18")]
144 NoMatchingDecoderInstalled =
145 lh::heif_suberror_code_heif_suberror_No_matching_decoder_installed as _,
146 #[cfg(feature = "v1_19")]
147 NoAvccBox = lh::heif_suberror_code_heif_suberror_No_avcC_box as _,
148 #[cfg(feature = "v1_19")]
149 InvalidMiniBox = lh::heif_suberror_code_heif_suberror_Invalid_mini_box as _,
150 #[cfg(feature = "v1_19")]
151 UnsupportedEssentialProperty =
152 lh::heif_suberror_code_heif_suberror_Unsupported_essential_property as _,
153 Unknown,
155}
156
157#[derive(Debug, Clone)]
158pub struct HeifError {
159 pub code: HeifErrorCode,
160 pub sub_code: HeifErrorSubCode,
161 pub message: String,
162}
163
164pub type Result<T> = std::result::Result<T, HeifError>;
165
166impl std::error::Error for HeifError {}
167
168impl fmt::Display for HeifError {
169 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
170 write!(f, "{:?}({:?}) {}", self.code, self.sub_code, self.message)
171 }
172}
173
174impl HeifError {
175 pub fn from_heif_error(err: lh::heif_error) -> Result<()> {
176 if err.code == 0 {
177 return Ok(());
178 }
179
180 let message = if err.message.is_null() {
181 ""
182 } else {
183 let res = unsafe { CStr::from_ptr(err.message).to_str() };
184 res.unwrap_or("")
185 };
186
187 Err(HeifError {
188 code: HeifErrorCode::n(err.code).unwrap_or(HeifErrorCode::Unknown),
189 sub_code: HeifErrorSubCode::n(err.subcode).unwrap_or(HeifErrorSubCode::Unknown),
190 message: String::from(message),
191 })
192 }
193}