Merge pull request #8202 from dotCooCoo/perf/legacy-ai-hex-samples

Legacy .ai: image samples are written as hex through a digit table
This commit is contained in:
BFlat
2026-11-17 19:51:37 +07:00
committed by GitHub
2 changed files with 65 additions and 17 deletions
+45
View File
@@ -728,6 +728,51 @@ fn images_are_written_in_the_form_the_specification_shows() {
assert!(rgb.iter().zip([247.0, 148.0, 29.0]).all(|(c, want)| (c * 255.0 - want).abs() < 0.1), "{rgb:?}");
}
/// An image's samples are written as upper-case hex on `%` comment lines (§8.1, §8.4) of 60
/// digits, the last line shorter, each line ending with a newline: no samples write no line, and
/// samples that fill their last line exactly write no empty `%` line after it.
#[test]
fn image_samples_are_written_as_hex_lines() {
let hex = |samples: &[u8]| {
let mut s = String::new();
super::writer::Writer::push_hex(&mut s, samples);
s
};
let mut samples = vec![0x00, 0x09, 0x0A, 0xAF, 0xFF];
samples.extend(0x10..=0x29);
assert_eq!(hex(&samples), "%00090AAFFF101112131415161718191A1B1C1D1E1F202122232425262728\n%29\n");
assert_eq!(hex(&samples[..30]), "%00090AAFFF101112131415161718191A1B1C1D1E1F202122232425262728\n");
assert_eq!(hex(&samples[..29]), "%00090AAFFF101112131415161718191A1B1C1D1E1F2021222324252627\n");
assert_eq!(hex(&[0xAB]), "%AB\n");
assert_eq!(hex(&[]), "");
// Appended after what is already written.
let mut s = String::from("0 0 1 1 1 1 8 3 0 0 0 0 XI\n");
super::writer::Writer::push_hex(&mut s, &[0xC0, 0xFF, 0xEE]);
assert_eq!(s, "0 0 1 1 1 1 8 3 0 0 0 0 XI\n%C0FFEE\n");
}
/// The hex lines are the same as formatting each sample with `{:02X}` and breaking the digits
/// into lines of [`HEX_LINE`], for every length up to three lines and one more sample, and for
/// every byte value.
#[test]
fn image_hex_matches_formatted_samples() {
let formatted = |samples: &[u8]| {
let digits: String = samples.iter().map(|c| format!("{c:02X}")).collect();
let lines: Vec<String> = digits.as_bytes().chunks(HEX_LINE).map(|l| format!("%{}\n", std::str::from_utf8(l).unwrap())).collect();
lines.concat()
};
let all: Vec<u8> = (0..=255u8).cycle().skip(7).take(3 * HEX_LINE / 2 + 1).collect();
for n in 0..=all.len() {
let mut s = String::new();
super::writer::Writer::push_hex(&mut s, &all[..n]);
assert_eq!(s, formatted(&all[..n]), "{n} samples");
}
let every: Vec<u8> = (0..=255u8).collect();
let mut s = String::new();
super::writer::Writer::push_hex(&mut s, &every);
assert_eq!(s, formatted(&every));
}
/// The pieces of an image cut into a raster mesh warp (as Object › Expand leaves a Warp on an
/// image) are written as the image whole once, then each as the part of it that its triangle shows,
/// and the file draws as the document does ([`write`] runs it).
+20 -17
View File
@@ -1213,32 +1213,35 @@ impl<'a> Writer<'a> {
coverage
}
/// Appends `samples` to `s` as upper-case hex digits on comment lines: each line starts with `%`,
/// holds [`HEX_LINE`] digits (the last line may hold fewer) and ends with a newline. An empty
/// `samples` appends no line.
pub(super) fn push_hex(s: &mut String, samples: &[u8]) {
const DIGITS: &[u8; 16] = b"0123456789ABCDEF";
const PER_LINE: usize = HEX_LINE / 2;
const _: () = assert!(HEX_LINE > 0 && HEX_LINE.is_multiple_of(2));
let lines = samples.len().div_ceil(PER_LINE);
s.reserve(samples.len().saturating_mul(2).saturating_add(lines.saturating_mul(2)));
for line in samples.chunks(PER_LINE) {
s.push('%');
for &c in line {
s.push(char::from(DIGITS[usize::from(c >> 4)]));
s.push(char::from(DIGITS[usize::from(c & 0x0F)]));
}
s.push('\n');
}
}
/// An image's lines (§8.4): `%AI5_File:`, `%AI5_BeginRaster`, matrix `m` as `[ a b c d tx ty ]`
/// and the numbers `operands` with `XI`, `samples` in hex on comment lines of [`HEX_LINE`]
/// digits, `%AI5_EndRaster`, then `F`.
fn raster(&mut self, m: Affine, operands: &str, samples: &[u8]) {
let mut hex = String::with_capacity(samples.len() * 2 + samples.len() / HEX_LINE * 2 + 2);
hex.push('%');
let mut on_line = 0;
for c in samples {
let _ = write!(hex, "{c:02X}");
on_line += 2;
if on_line >= HEX_LINE {
hex.push_str("\n%");
on_line = 0;
}
}
if hex.ends_with('%') {
hex.pop();
} else {
hex.push('\n');
}
let [a, b, c, d, e, f] = m.as_coeffs();
let s = &mut self.body;
s.push_str("%AI5_File:\n%AI5_BeginRaster\n[ ");
ps::push_nums(s, &[a, b, c, d, e, f]);
let _ = writeln!(s, "] {operands} XI");
s.push_str(&hex);
Self::push_hex(s, samples);
s.push_str("%AI5_EndRaster\nF\n");
}
}