mod brotli;
mod coeff_order;
mod encode;
mod jbrd;
mod parse;
#[allow(unused_imports)]
pub(crate) use parse::{JpegError, parse_jpeg};
use std::num::NonZeroUsize;
use crate::util::EncodeError;
use crate::{Orientation, Speed};
pub use brotli::BrotliCompression;
fn push_box(out: &mut Vec<u8>, kind: &[u8; 4], payload: &[u8]) {
let size = 8 + payload.len();
if let Ok(small) = u32::try_from(size) {
out.extend_from_slice(&small.to_be_bytes());
out.extend_from_slice(kind);
} else {
out.extend_from_slice(&1u32.to_be_bytes());
out.extend_from_slice(kind);
out.extend_from_slice(&((size + 8) as u64).to_be_bytes());
}
out.extend_from_slice(payload);
}
#[derive(Debug, PartialEq)]
struct JpegIcc {
profile: Vec<u8>,
in_order_apps: Option<Vec<usize>>,
}
fn extract_icc(jpg: &JpegData) -> Option<JpegIcc> {
const TAG: &[u8] = b"ICC_PROFILE\0";
let mut chunks: Vec<(u8, usize, &[u8])> = Vec::new();
let mut expected = 0u8;
for (index, app) in jpg.app_data.iter().enumerate() {
if app.first() != Some(&0xE2) || app.len() < 3 + TAG.len() + 2 {
continue;
}
let payload = &app[3..];
if !payload.starts_with(TAG) {
continue;
}
let seq = payload[TAG.len()];
let count = payload[TAG.len() + 1];
if seq == 0 || count == 0 || (expected != 0 && count != expected) {
return None;
}
expected = count;
chunks.push((seq, index, &payload[TAG.len() + 2..]));
}
if chunks.is_empty() || chunks.len() != usize::from(expected) {
return None;
}
let in_order = chunks
.iter()
.enumerate()
.all(|(i, &(seq, _, _))| usize::from(seq) == i + 1);
chunks.sort_by_key(|&(seq, _, _)| seq);
if chunks
.iter()
.enumerate()
.any(|(i, &(seq, _, _))| usize::from(seq) != i + 1)
{
return None;
}
let profile: Vec<u8> = chunks
.iter()
.flat_map(|&(_, _, data)| data)
.copied()
.collect();
(!profile.is_empty()).then(|| JpegIcc {
profile,
in_order_apps: in_order.then(|| chunks.iter().map(|&(_, index, _)| index).collect()),
})
}
fn icc_is_well_formed(icc: &[u8]) -> bool {
const HEADER: usize = 128;
let be32 = |at: usize| -> Option<usize> {
let b = icc.get(at..at + 4)?;
Some(u32::from_be_bytes([b[0], b[1], b[2], b[3]]) as usize)
};
if icc.len() < HEADER + 4 || be32(0) != Some(icc.len()) || &icc[36..40] != b"acsp" {
return false;
}
let Some(num_tags) = be32(HEADER) else {
return false;
};
let table_end = (num_tags as u64) * 12 + HEADER as u64 + 4;
if table_end > icc.len() as u64 {
return false;
}
(0..num_tags).all(|i| {
let entry = HEADER + 4 + i * 12;
match (be32(entry + 4), be32(entry + 8)) {
(Some(offset), Some(size)) => (offset as u64 + size as u64) <= icc.len() as u64,
_ => false,
}
})
}
fn extract_exif(jpg: &JpegData) -> Option<(usize, &[u8])> {
const TAG: &[u8] = b"Exif\0\0";
jpg.app_data.iter().enumerate().find_map(|(index, app)| {
if app.first() != Some(&0xE1) || app.len() <= 3 + TAG.len() {
return None;
}
let payload = &app[3..];
payload
.starts_with(TAG)
.then(|| (index, &payload[TAG.len()..]))
})
}
fn exif_orientation(tiff: &[u8]) -> Option<Orientation> {
let big_endian = match tiff.get(..4)? {
[b'M', b'M', 0, 42] => true,
[b'I', b'I', 42, 0] => false,
_ => return None,
};
let u16_at = |at: usize| -> Option<u16> {
let b: [u8; 2] = tiff.get(at..at + 2)?.try_into().ok()?;
Some(if big_endian {
u16::from_be_bytes(b)
} else {
u16::from_le_bytes(b)
})
};
let u32_at = |at: usize| -> Option<u32> {
let b: [u8; 4] = tiff.get(at..at + 4)?.try_into().ok()?;
Some(if big_endian {
u32::from_be_bytes(b)
} else {
u32::from_le_bytes(b)
})
};
let ifd = u32_at(4)? as usize;
let entries = usize::from(u16_at(ifd)?);
(0..entries).find_map(|i| {
let entry = ifd + 2 + 12 * i;
if u16_at(entry)? != 0x0112 || u16_at(entry + 2)? != 3 || u32_at(entry + 4)? != 1 {
return None;
}
Orientation::from_exif(u8::try_from(u16_at(entry + 8)?).ok()?)
})
}
fn extract_xmp(jpg: &JpegData) -> Option<(usize, Vec<u8>)> {
const TAG: &[u8] = b"http://ns.adobe.com/xap/1.0/\0";
jpg.app_data.iter().enumerate().find_map(|(index, app)| {
if app.first() != Some(&0xE1) || app.len() < 3 + TAG.len() {
return None;
}
let payload = &app[3..];
payload
.starts_with(TAG)
.then(|| (index, payload[TAG.len()..].to_vec()))
})
}
pub struct JpegTranscodeConfig {
pub jpeg_reconstruction: bool,
pub num_threads: usize,
pub brotli_compression: Option<Box<dyn BrotliCompression>>,
pub speed: Speed,
}
impl std::fmt::Debug for JpegTranscodeConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("JpegTranscodeConfig")
.field("jpeg_reconstruction", &self.jpeg_reconstruction)
.field("num_threads", &self.num_threads)
.field("speed", &self.speed)
.field(
"brotli_compression",
&self.brotli_compression.as_ref().map(|_| "custom"),
)
.finish()
}
}
impl Default for JpegTranscodeConfig {
fn default() -> Self {
Self {
jpeg_reconstruction: true,
num_threads: std::thread::available_parallelism()
.unwrap_or(NonZeroUsize::new(1).unwrap())
.get(),
brotli_compression: None,
speed: Speed::default(),
}
}
}
impl JpegTranscodeConfig {
pub fn with_jpeg_reconstruction(mut self, enabled: bool) -> Self {
self.jpeg_reconstruction = enabled;
self
}
pub fn with_num_threads(mut self, threads: usize) -> Self {
self.num_threads = threads.max(1);
self
}
pub fn with_speed(mut self, speed: Speed) -> Self {
self.speed = speed;
self
}
pub fn with_brotli_compression(mut self, compressor: Box<dyn BrotliCompression>) -> Self {
self.brotli_compression = Some(compressor);
self
}
}
pub fn encode_jpeg_lossless(jpeg: &[u8]) -> Result<Vec<u8>, EncodeError> {
encode_jpeg_lossless_with_config(jpeg, &JpegTranscodeConfig::default())
}
pub fn encode_jpeg_lossless_with_config(
jpeg: &[u8],
config: &JpegTranscodeConfig,
) -> Result<Vec<u8>, EncodeError> {
let parsed = parse_jpeg(jpeg).map_err(|e| EncodeError::Jpeg(e.to_string()))?;
let icc = extract_icc(&parsed).filter(|icc| icc_is_well_formed(&icc.profile));
let xmp = extract_xmp(&parsed);
let exif = extract_exif(&parsed);
let orientation = exif
.and_then(|(_, tiff)| exif_orientation(tiff))
.unwrap_or_default();
let codestream = encode::encode_jpeg_codestream(
&parsed,
icc.as_ref().map(|icc| icc.profile.as_slice()),
orientation,
config.speed,
config.num_threads,
)?;
let reconstruction = if config.jpeg_reconstruction {
Some(jbrd::encode_jbrd(
&parsed,
xmp.as_ref().map(|(index, _)| *index),
exif.map(|(index, _)| index),
icc.as_ref()
.and_then(|icc| icc.in_order_apps.as_deref())
.unwrap_or_default(),
config.brotli_compression.as_deref(),
)?)
} else {
None
};
if reconstruction.is_none() && exif.is_none() && xmp.is_none() {
return Ok(codestream);
}
let mut out = Vec::with_capacity(codestream.len() + 4096);
out.extend_from_slice(&[
0, 0, 0, 0x0C, b'J', b'X', b'L', b' ', 0x0D, 0x0A, 0x87, 0x0A,
]);
push_box(
&mut out,
b"ftyp",
&[b'j', b'x', b'l', b' ', 0, 0, 0, 0, b'j', b'x', b'l', b' '],
);
if let Some(reconstruction) = &reconstruction {
push_box(&mut out, b"jbrd", reconstruction);
}
let compressor = config.brotli_compression.as_deref();
if let Some((_, tiff)) = exif {
let mut payload = Vec::with_capacity(4 + tiff.len());
payload.extend_from_slice(&[0; 4]);
payload.extend_from_slice(tiff);
push_metadata_box(&mut out, b"Exif", &payload, compressor)?;
}
push_box(&mut out, b"jxlc", &codestream);
if let Some((_, xmp)) = xmp {
push_metadata_box(&mut out, b"xml ", &xmp, compressor)?;
}
Ok(out)
}
fn push_metadata_box(
out: &mut Vec<u8>,
kind: &[u8; 4],
payload: &[u8],
compressor: Option<&dyn BrotliCompression>,
) -> Result<(), EncodeError> {
let compressed = brotli::compress(payload, compressor)?;
if compressed.len() + 4 < payload.len() {
let mut brob = Vec::with_capacity(4 + compressed.len());
brob.extend_from_slice(kind);
brob.extend_from_slice(&compressed);
push_box(out, b"brob", &brob);
} else {
push_box(out, kind, payload);
}
Ok(())
}
pub(crate) const DCT_BLOCK_SIZE: usize = 64;
pub(crate) const HUFF_MAX_BIT_LENGTH: usize = 16;
pub(crate) const HUFF_ALPHABET_SIZE: usize = 256;
pub(crate) const MAX_COMPONENTS: usize = 4;
pub(crate) static NATURAL_ORDER: [usize; DCT_BLOCK_SIZE] = [
0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20,
13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51, 58, 59,
52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63,
];
#[derive(Debug, Clone)]
pub(crate) struct JpegQuantTable {
pub(crate) values: [i32; DCT_BLOCK_SIZE],
pub(crate) precision: u32,
pub(crate) index: u32,
pub(crate) is_last: bool,
}
#[derive(Debug, Clone)]
pub(crate) struct JpegHuffmanCode {
pub(crate) counts: [u32; HUFF_MAX_BIT_LENGTH + 1],
pub(crate) values: [u32; HUFF_ALPHABET_SIZE + 1],
pub(crate) slot_id: u32,
pub(crate) is_last: bool,
}
impl Default for JpegHuffmanCode {
fn default() -> Self {
Self {
counts: [0; HUFF_MAX_BIT_LENGTH + 1],
values: [0; HUFF_ALPHABET_SIZE + 1],
slot_id: 0,
is_last: true,
}
}
}
#[derive(Debug, Clone, Copy, Default)]
pub(crate) struct JpegComponentScanInfo {
pub(crate) comp_idx: u32,
pub(crate) dc_tbl_idx: u32,
pub(crate) ac_tbl_idx: u32,
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct ExtraZeroRunInfo {
pub(crate) block_idx: u32,
pub(crate) num_extra_zero_runs: u32,
}
#[derive(Debug, Clone, Default)]
pub(crate) struct JpegScanInfo {
pub(crate) ss: u32,
pub(crate) se: u32,
pub(crate) ah: u32,
pub(crate) al: u32,
pub(crate) num_components: u32,
pub(crate) components: [JpegComponentScanInfo; MAX_COMPONENTS],
pub(crate) extra_zero_runs: Vec<ExtraZeroRunInfo>,
pub(crate) reset_points: Vec<u32>,
pub(crate) last_needed_pass: u32,
}
#[derive(Debug, Clone, Default)]
pub(crate) struct JpegComponent {
pub(crate) id: u32,
pub(crate) h_samp_factor: usize,
pub(crate) v_samp_factor: usize,
pub(crate) quant_idx: u32,
pub(crate) width_in_blocks: usize,
pub(crate) height_in_blocks: usize,
pub(crate) coeffs: Vec<i16>,
}
#[derive(Debug, Clone, Default)]
pub(crate) struct JpegData {
pub(crate) width: usize,
pub(crate) height: usize,
pub(crate) restart_interval: u32,
pub(crate) app_data: Vec<Vec<u8>>,
pub(crate) com_data: Vec<Vec<u8>>,
pub(crate) quant: Vec<JpegQuantTable>,
pub(crate) huffman_code: Vec<JpegHuffmanCode>,
pub(crate) components: Vec<JpegComponent>,
pub(crate) scan_info: Vec<JpegScanInfo>,
pub(crate) marker_order: Vec<u8>,
pub(crate) inter_marker_data: Vec<Vec<u8>>,
pub(crate) tail_data: Vec<u8>,
pub(crate) has_zero_padding_bit: bool,
pub(crate) padding_bits: Vec<u8>,
pub(crate) is_progressive: bool,
}
impl JpegData {
pub(crate) fn max_h_samp(&self) -> usize {
self.components
.iter()
.map(|c| c.h_samp_factor)
.max()
.unwrap_or(1)
}
pub(crate) fn max_v_samp(&self) -> usize {
self.components
.iter()
.map(|c| c.v_samp_factor)
.max()
.unwrap_or(1)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn icc_app2(seq: u8, count: u8, body: &[u8]) -> Vec<u8> {
let mut payload = b"ICC_PROFILE\0".to_vec();
payload.push(seq);
payload.push(count);
payload.extend_from_slice(body);
let mut seg = vec![0xE2u8];
seg.extend_from_slice(&((payload.len() + 2) as u16).to_be_bytes());
seg.extend_from_slice(&payload);
seg
}
fn xmp_app1(body: &[u8]) -> Vec<u8> {
let mut payload = b"http://ns.adobe.com/xap/1.0/\0".to_vec();
payload.extend_from_slice(body);
let mut seg = vec![0xE1u8];
seg.extend_from_slice(&((payload.len() + 2) as u16).to_be_bytes());
seg.extend_from_slice(&payload);
seg
}
fn with_app(segments: Vec<Vec<u8>>) -> JpegData {
JpegData {
app_data: segments,
..Default::default()
}
}
#[test]
fn reassembles_a_split_icc_profile() {
let jpg = with_app(vec![
icc_app2(2, 3, b"world"),
icc_app2(1, 3, b"hello "),
icc_app2(3, 3, b"!"),
]);
let icc = extract_icc(&jpg).unwrap();
assert_eq!(icc.profile, b"hello world!");
assert_eq!(icc.in_order_apps, None);
}
#[test]
fn reads_a_single_chunk_profile() {
let jpg = with_app(vec![icc_app2(1, 1, b"profile")]);
let icc = extract_icc(&jpg).unwrap();
assert_eq!(icc.profile, b"profile");
assert_eq!(icc.in_order_apps.as_deref(), Some(&[0][..]));
}
#[test]
fn rejects_incomplete_or_inconsistent_profiles() {
let missing = with_app(vec![icc_app2(1, 2, b"half")]);
assert_eq!(extract_icc(&missing), None);
let inconsistent = with_app(vec![icc_app2(1, 2, b"a"), icc_app2(2, 3, b"b")]);
assert_eq!(extract_icc(&inconsistent), None);
let bad_seq = with_app(vec![icc_app2(0, 1, b"a")]);
assert_eq!(extract_icc(&bad_seq), None);
}
#[test]
fn in_order_chunks_are_reported_for_regeneration() {
let mut exif = vec![0xE1u8, 0x00, 0x05];
exif.extend_from_slice(b"Exi");
let jpg = with_app(vec![
icc_app2(1, 2, b"hello "),
exif,
icc_app2(2, 2, b"world"),
]);
let icc = extract_icc(&jpg).unwrap();
assert_eq!(icc.profile, b"hello world");
assert_eq!(icc.in_order_apps.as_deref(), Some(&[0, 2][..]));
}
fn tiny_profile() -> Vec<u8> {
let mut icc = vec![0u8; 128];
icc[36..40].copy_from_slice(b"acsp");
icc.extend_from_slice(&1u32.to_be_bytes());
icc.extend_from_slice(b"desc");
icc.extend_from_slice(&144u32.to_be_bytes());
icc.extend_from_slice(&4u32.to_be_bytes());
icc.extend_from_slice(b"data");
let len = icc.len() as u32;
icc[..4].copy_from_slice(&len.to_be_bytes());
icc
}
#[test]
fn well_formed_profiles_pass_and_damaged_ones_do_not() {
let good = tiny_profile();
assert!(icc_is_well_formed(&good));
assert!(!icc_is_well_formed(b"hello world"));
let mut bad_magic = good.clone();
bad_magic[36] = b'x';
assert!(!icc_is_well_formed(&bad_magic));
let mut truncated = good.clone();
truncated.pop();
assert!(!icc_is_well_formed(&truncated));
let mut bad_tag = good.clone();
bad_tag[136..140].copy_from_slice(&200u32.to_be_bytes());
assert!(!icc_is_well_formed(&bad_tag));
let mut bad_count = good;
bad_count[128..132].copy_from_slice(&1000u32.to_be_bytes());
assert!(!icc_is_well_formed(&bad_count));
}
fn tiff(big_endian: bool, entries: &[(u16, u16, u32, u16)]) -> Vec<u8> {
let u16b = |v: u16| {
if big_endian {
v.to_be_bytes()
} else {
v.to_le_bytes()
}
};
let u32b = |v: u32| {
if big_endian {
v.to_be_bytes()
} else {
v.to_le_bytes()
}
};
let mut t = if big_endian {
b"MM\0*".to_vec()
} else {
b"II*\0".to_vec()
};
t.extend_from_slice(&u32b(8));
t.extend_from_slice(&u16b(entries.len() as u16));
for &(tag, kind, count, value) in entries {
t.extend_from_slice(&u16b(tag));
t.extend_from_slice(&u16b(kind));
t.extend_from_slice(&u32b(count));
t.extend_from_slice(&u16b(value));
t.extend_from_slice(&[0, 0]);
}
t.extend_from_slice(&u32b(0));
t
}
#[test]
fn reads_the_exif_orientation_in_either_byte_order() {
for big_endian in [true, false] {
let t = tiff(big_endian, &[(0x010F, 2, 4, 0), (0x0112, 3, 1, 8)]);
assert_eq!(exif_orientation(&t), Orientation::from_exif(8));
}
assert_eq!(exif_orientation(&tiff(true, &[(0x010F, 2, 4, 0)])), None);
assert_eq!(exif_orientation(&tiff(true, &[(0x0112, 4, 1, 6)])), None);
assert_eq!(exif_orientation(&tiff(false, &[(0x0112, 3, 1, 9)])), None);
assert_eq!(exif_orientation(b"MM\0*\0\0\0\x40"), None);
assert_eq!(exif_orientation(b"nonsense"), None);
}
#[test]
fn finds_the_first_exif_segment() {
let mut exif = vec![0xE1u8, 0, 0];
exif.extend_from_slice(b"Exif\0\0");
exif.extend_from_slice(&tiff(true, &[(0x0112, 3, 1, 6)]));
let jpg = with_app(vec![xmp_app1(b"<x/>"), exif.clone(), exif.clone()]);
let (index, data) = extract_exif(&jpg).unwrap();
assert_eq!(index, 1);
assert_eq!(exif_orientation(data), Orientation::from_exif(6));
assert_eq!(
extract_exif(&with_app(vec![vec![
0xE1, 0, 8, b'E', b'x', b'i', b'f', 0, 0
]])),
None
);
}
#[test]
fn ignores_unrelated_app_segments() {
let mut jfif = vec![0xE0u8, 0x00, 0x10];
jfif.extend_from_slice(b"JFIF\0\x01\x02\x00\x00\x01\x00\x01\x00\x00");
assert_eq!(extract_icc(&with_app(vec![jfif])), None);
}
#[test]
fn extracts_the_first_standard_xmp_packet() {
let jpg = with_app(vec![
vec![0xE1, 0, 5, b'n', b'o', b'p'],
xmp_app1(b"<x:xmpmeta>first</x:xmpmeta>"),
xmp_app1(b"<x:xmpmeta>second</x:xmpmeta>"),
]);
let (index, xmp) = extract_xmp(&jpg).expect("XMP packet");
assert_eq!(index, 1);
assert_eq!(xmp, b"<x:xmpmeta>first</x:xmpmeta>");
}
#[test]
fn reconstruction_is_on_by_default() {
let default = JpegTranscodeConfig::default();
assert!(default.jpeg_reconstruction);
assert!(default.brotli_compression.is_none());
assert!(
!JpegTranscodeConfig::default()
.with_jpeg_reconstruction(false)
.jpeg_reconstruction
);
}
}