Skip to content

Commit

Permalink
gainmap: Remove intermediary fraction structs from capi
Browse files Browse the repository at this point in the history
We don't really need to redefine avifSignedFraction and
avifUnsignedFraction structs since they are exactly the same as
Fraction and UFraction. Simply expose the original structs and
use cbindgen annotations for renaming the fields in the C
header file.

PiperOrigin-RevId: 681942730
  • Loading branch information
vigneshvg authored and copybara-github committed Oct 3, 2024
1 parent 53da690 commit a869859
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 68 deletions.
8 changes: 6 additions & 2 deletions include/avif/avif.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,16 +303,20 @@ struct avifImageMirror {
uint8_t axis;
};

struct avifSignedFraction {
struct Fraction {
int32_t n;
uint32_t d;
};

struct avifUnsignedFraction {
using avifSignedFraction = Fraction;

struct UFraction {
uint32_t n;
uint32_t d;
};

using avifUnsignedFraction = UFraction;

struct avifGainMap {
avifImage *image;
avifSignedFraction gainMapMin[3];
Expand Down
82 changes: 16 additions & 66 deletions src/capi/gainmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,8 @@ use crate::parser::mp4box::*;
use crate::*;

pub type avifContentLightLevelInformationBox = ContentLightLevelInformation;

#[repr(C)]
#[derive(Debug)]
pub struct avifSignedFraction {
pub n: i32,
pub d: u32,
}

impl From<&Fraction> for avifSignedFraction {
fn from(fraction: &Fraction) -> Self {
avifSignedFraction {
n: fraction.0,
d: fraction.1,
}
}
}

#[repr(C)]
#[derive(Debug)]
pub struct avifUnsignedFraction {
pub n: u32,
pub d: u32,
}

impl From<&UFraction> for avifUnsignedFraction {
fn from(fraction: &UFraction) -> Self {
avifUnsignedFraction {
n: fraction.0,
d: fraction.1,
}
}
}
pub type avifSignedFraction = Fraction;
pub type avifUnsignedFraction = UFraction;

#[repr(C)]
#[derive(Debug)]
Expand Down Expand Up @@ -82,33 +52,13 @@ impl Default for avifGainMap {
fn default() -> Self {
avifGainMap {
image: std::ptr::null_mut(),
gainMapMin: [
avifSignedFraction { n: 1, d: 1 },
avifSignedFraction { n: 1, d: 1 },
avifSignedFraction { n: 1, d: 1 },
],
gainMapMax: [
avifSignedFraction { n: 1, d: 1 },
avifSignedFraction { n: 1, d: 1 },
avifSignedFraction { n: 1, d: 1 },
],
gainMapGamma: [
avifUnsignedFraction { n: 1, d: 1 },
avifUnsignedFraction { n: 1, d: 1 },
avifUnsignedFraction { n: 1, d: 1 },
],
baseOffset: [
avifSignedFraction { n: 1, d: 64 },
avifSignedFraction { n: 1, d: 64 },
avifSignedFraction { n: 1, d: 64 },
],
alternateOffset: [
avifSignedFraction { n: 1, d: 64 },
avifSignedFraction { n: 1, d: 64 },
avifSignedFraction { n: 1, d: 64 },
],
baseHdrHeadroom: avifUnsignedFraction { n: 0, d: 1 },
alternateHdrHeadroom: avifUnsignedFraction { n: 1, d: 1 },
gainMapMin: [Fraction(1, 1), Fraction(1, 1), Fraction(1, 1)],
gainMapMax: [Fraction(1, 1), Fraction(1, 1), Fraction(1, 1)],
gainMapGamma: [UFraction(1, 1), UFraction(1, 1), UFraction(1, 1)],
baseOffset: [Fraction(1, 64), Fraction(1, 64), Fraction(1, 64)],
alternateOffset: [Fraction(1, 64), Fraction(1, 64), Fraction(1, 64)],
baseHdrHeadroom: UFraction(0, 1),
alternateHdrHeadroom: UFraction(1, 1),
useBaseColorSpace: to_avifBool(false),
altICC: avifRWData::default(),
altColorPrimaries: ColorPrimaries::default(),
Expand All @@ -125,13 +75,13 @@ impl Default for avifGainMap {
impl From<&GainMap> for avifGainMap {
fn from(gainmap: &GainMap) -> Self {
avifGainMap {
gainMapMin: gainmap.metadata.min.map(|v| (&v).into()),
gainMapMax: gainmap.metadata.max.map(|v| (&v).into()),
gainMapGamma: gainmap.metadata.gamma.map(|v| (&v).into()),
baseOffset: gainmap.metadata.base_offset.map(|v| (&v).into()),
alternateOffset: gainmap.metadata.alternate_offset.map(|v| (&v).into()),
baseHdrHeadroom: (&gainmap.metadata.base_hdr_headroom).into(),
alternateHdrHeadroom: (&gainmap.metadata.alternate_hdr_headroom).into(),
gainMapMin: gainmap.metadata.min,
gainMapMax: gainmap.metadata.max,
gainMapGamma: gainmap.metadata.gamma,
baseOffset: gainmap.metadata.base_offset,
alternateOffset: gainmap.metadata.alternate_offset,
baseHdrHeadroom: gainmap.metadata.base_hdr_headroom,
alternateHdrHeadroom: gainmap.metadata.alternate_hdr_headroom,
useBaseColorSpace: gainmap.metadata.use_base_color_space as avifBool,
altICC: (&gainmap.alt_icc).into(),
altColorPrimaries: gainmap.alt_color_primaries,
Expand Down
7 changes: 7 additions & 0 deletions src/internal_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@ use std::ops::Range;

// Some HEIF fractional fields can be negative, hence Fraction and UFraction.
// The denominator is always unsigned.

/// cbindgen:field-names=[n,d]
#[derive(Clone, Copy, Debug, Default)]
#[repr(C)]
pub struct Fraction(pub i32, pub u32);

/// cbindgen:field-names=[n,d]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[repr(C)]
pub struct UFraction(pub u32, pub u32);

// 'clap' fractions do not follow this pattern: both numerators and denominators
// are used as i32, but they are signalled as u32 according to the specification
// as of 2024. This may be fixed in later versions of the specification, see
Expand Down

0 comments on commit a869859

Please sign in to comment.