pub fn md5checksum(bytes: &mut [u8]) -> [u8; 16] {
if bytes.len() < 128 {
panic!("ICC profile data must be at least 128 bytes long");
}
let flags: [u8; 4] = bytes[44..=47].try_into().unwrap();
bytes[44..=47].fill(0);
let rendering_intent: [u8; 4] = bytes[64..=67].try_into().unwrap();
bytes[64..=67].fill(0);
bytes[84..=99].fill(0);
let digest = md5::compute(&bytes);
let checksum: [u8; 16] = digest.into();
bytes[44..=47].copy_from_slice(&flags);
bytes[64..=67].copy_from_slice(&rendering_intent);
checksum
}
pub fn set_profile_id(bytes: &mut [u8]) -> [u8; 16] {
let checksum = md5checksum(bytes);
bytes[84..=99].copy_from_slice(&checksum);
checksum
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_with_profile_id() {
let icc_data = include_bytes!("../../tests/profiles/Display P3.icc");
let mut icc_data_mut = icc_data.to_vec();
let result = set_profile_id(&mut icc_data_mut);
let expected_checksum: [u8; 16] = [
0xca, 0x1a, 0x95, 0x82, 0x25, 0x7f, 0x10, 0x4d, 0x38, 0x99, 0x13, 0xd5, 0xd1, 0xea,
0x15, 0x82,
];
assert_eq!(&result, &expected_checksum);
}
}