Skip to content

Commit b736382

Browse files
authored
Optimize Option<Flash>::from(FlashValue) using bitwise AND (#2843)
1 parent 7d05d18 commit b736382

File tree

1 file changed

+27
-100
lines changed
  • crates/media-metadata/src/exif/flash

1 file changed

+27
-100
lines changed

crates/media-metadata/src/exif/flash/data.rs

Lines changed: 27 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -57,112 +57,39 @@ impl From<u32> for FlashMode {
5757
}
5858

5959
impl From<FlashValue> for Option<Flash> {
60-
// TODO(brxken128): This can be heavily optimized with bitwise AND
61-
// e.g. to see if flash was fired, `(value & 1) != 0`
62-
// or to see if red eye reduction was enabled, `(value & 64) != 0`
63-
// May not be worth it as some states may be invalid according to `https://www.awaresystems.be/imaging/tiff/tifftags/privateifd/exif/flash.html`
6460
fn from(value: FlashValue) -> Self {
65-
#[allow(clippy::as_conversions)]
66-
let mut data = Flash {
67-
mode: FlashMode::from(value as u32),
68-
..Default::default()
61+
let numeric_value = value as u32;
62+
63+
// Bit 0 indicates if flash fired
64+
let fired = (numeric_value & 0x01) != 0;
65+
66+
// Bits 1-2 indicate return state
67+
let returned = match numeric_value & 0x06 {
68+
0x06 => Some(true), // Returned
69+
0x02 => Some(false), // No return
70+
_ => None, // No data
6971
};
7072

71-
#[allow(clippy::match_same_arms)]
72-
match value {
73-
FlashValue::Fired => {
74-
data.fired = Some(true);
75-
}
76-
FlashValue::NoFire => {
77-
data.fired = Some(false);
78-
}
79-
FlashValue::FiredReturn => {
80-
data.fired = Some(true);
81-
data.returned = Some(true);
82-
}
83-
FlashValue::FiredNoReturn => {
84-
data.fired = Some(true);
85-
data.returned = Some(false);
86-
}
87-
FlashValue::AutoFired => {
88-
data.fired = Some(true);
89-
}
90-
FlashValue::AutoFiredNoReturn => {
91-
data.fired = Some(true);
92-
data.returned = Some(false);
93-
}
94-
FlashValue::OffNoFire => data.fired = Some(false),
95-
FlashValue::AutoNoFire => data.fired = Some(false),
96-
FlashValue::NoFlashFunction | FlashValue::OffNoFlashFunction | FlashValue::Unknown => {
97-
data = Flash::default();
98-
}
99-
FlashValue::AutoFiredRedEyeReduction => {
100-
data.fired = Some(true);
101-
data.red_eye_reduction = Some(true);
102-
}
103-
FlashValue::AutoFiredRedEyeReductionNoReturn => {
104-
data.fired = Some(true);
105-
data.red_eye_reduction = Some(true);
106-
data.returned = Some(false);
107-
}
108-
FlashValue::AutoFiredRedEyeReductionReturn => {
109-
data.fired = Some(true);
110-
data.red_eye_reduction = Some(true);
111-
data.returned = Some(true);
112-
}
113-
FlashValue::OnFired => {
114-
data.fired = Some(true);
115-
}
116-
FlashValue::OnNoFire => {
117-
data.fired = Some(false);
118-
}
119-
FlashValue::AutoFiredReturn => {
120-
data.fired = Some(true);
121-
data.returned = Some(true);
122-
}
123-
FlashValue::OnReturn => {
124-
data.returned = Some(true);
125-
}
126-
FlashValue::OnNoReturn => data.returned = Some(false),
127-
FlashValue::AutoNoFireRedEyeReduction => {
128-
data.fired = Some(false);
129-
data.red_eye_reduction = Some(true);
130-
}
131-
FlashValue::OffNoFireNoReturn => {
132-
data.fired = Some(false);
133-
data.returned = Some(false);
134-
}
135-
FlashValue::OffRedEyeReduction => data.red_eye_reduction = Some(true),
136-
FlashValue::OnRedEyeReduction => data.red_eye_reduction = Some(true),
137-
FlashValue::FiredRedEyeReductionNoReturn => {
138-
data.fired = Some(true);
139-
data.red_eye_reduction = Some(true);
140-
data.returned = Some(false);
141-
}
142-
FlashValue::FiredRedEyeReduction => {
143-
data.fired = Some(true);
144-
data.red_eye_reduction = Some(true);
145-
}
146-
FlashValue::FiredRedEyeReductionReturn => {
147-
data.fired = Some(true);
148-
data.red_eye_reduction = Some(true);
149-
data.returned = Some(false);
150-
}
151-
FlashValue::OnRedEyeReductionReturn => {
152-
data.red_eye_reduction = Some(true);
153-
data.returned = Some(true);
154-
}
155-
FlashValue::OnRedEyeReductionNoReturn => {
156-
data.red_eye_reduction = Some(true);
157-
data.returned = Some(false);
158-
}
159-
}
73+
// Bit 6 indicates red-eye reduction
74+
let red_eye_reduction = if (numeric_value & 0x40) != 0 {
75+
Some(true)
76+
} else {
77+
None
78+
};
79+
80+
let mode = FlashMode::from(numeric_value);
81+
82+
let flash = Flash {
83+
mode,
84+
fired: Some(fired),
85+
returned,
86+
red_eye_reduction,
87+
};
16088

161-
// this means it had a value of Flash::NoFlashFunctionality
162-
if data == Flash::default() {
89+
if flash == Flash::default() {
16390
None
16491
} else {
165-
Some(data)
92+
Some(flash)
16693
}
16794
}
16895
}

0 commit comments

Comments
 (0)