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