1 // Copyright 2024 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 use crate::*; 16 17 pub mod clap; 18 19 // Some HEIF fractional fields can be negative, hence Fraction and UFraction. 20 // The denominator is always unsigned. 21 22 /// cbindgen:field-names=[n,d] 23 #[derive(Clone, Copy, Debug, Default)] 24 #[repr(C)] 25 pub struct Fraction(pub i32, pub u32); 26 27 /// cbindgen:field-names=[n,d] 28 #[derive(Clone, Copy, Debug, Default, PartialEq)] 29 #[repr(C)] 30 pub struct UFraction(pub u32, pub u32); 31 32 impl Fraction { is_valid(&self) -> AvifResult<()>33 pub(crate) fn is_valid(&self) -> AvifResult<()> { 34 match self.1 { 35 0 => Err(AvifError::InvalidArgument), 36 _ => Ok(()), 37 } 38 } 39 as_f64(&self) -> AvifResult<f64>40 pub(crate) fn as_f64(&self) -> AvifResult<f64> { 41 self.is_valid()?; 42 Ok(self.0 as f64 / self.1 as f64) 43 } 44 } 45 46 impl UFraction { is_valid(&self) -> AvifResult<()>47 pub(crate) fn is_valid(&self) -> AvifResult<()> { 48 match self.1 { 49 0 => Err(AvifError::InvalidArgument), 50 _ => Ok(()), 51 } 52 } 53 } 54