284 lines
9.6 KiB
Rust
284 lines
9.6 KiB
Rust
//! Fixtures built in code: nothing binary lives in git.
|
|
#![allow(dead_code)]
|
|
|
|
use std::io::Cursor;
|
|
|
|
use exif::experimental::Writer;
|
|
use exif::{Field, In, Rational, Tag, Value};
|
|
use image::{ImageEncoder, RgbImage, RgbaImage};
|
|
|
|
pub const AUTHOR: &str = "Jan Novak Fixtureauthor";
|
|
pub const MAKE: &str = "Apple";
|
|
pub const MODEL: &str = "iPhone 15";
|
|
pub const SOFTWARE: &str = "PhotoEditor 17.1 Fixture";
|
|
pub const COMMENT: &str = "Holiday at the secret cottage";
|
|
pub const XMP_NS: &str = "http://ns.adobe.com/xap";
|
|
|
|
pub fn rgb(w: u32, h: u32) -> RgbImage {
|
|
RgbImage::from_fn(w, h, |x, y| image::Rgb([(x * 13 + y * 7) as u8, (x * 31) as u8 ^ (y * 5) as u8, (y * 17) as u8]))
|
|
}
|
|
|
|
pub fn rgba(w: u32, h: u32) -> RgbaImage {
|
|
RgbaImage::from_fn(w, h, |x, y| image::Rgba([(x * 29) as u8, (y * 41) as u8, (x ^ y) as u8 * 9, 128 + (x as u8)]))
|
|
}
|
|
|
|
pub fn ascii(tag: Tag, s: &str) -> Field {
|
|
Field { tag, ifd_num: In::PRIMARY, value: Value::Ascii(vec![s.as_bytes().to_vec()]) }
|
|
}
|
|
|
|
pub fn rationals(tag: Tag, v: &[(u32, u32)]) -> Field {
|
|
Field {
|
|
tag,
|
|
ifd_num: In::PRIMARY,
|
|
value: Value::Rational(v.iter().map(|&(num, denom)| Rational { num, denom }).collect()),
|
|
}
|
|
}
|
|
|
|
pub fn short(tag: Tag, v: &[u16]) -> Field {
|
|
Field { tag, ifd_num: In::PRIMARY, value: Value::Short(v.to_vec()) }
|
|
}
|
|
|
|
/// GPS 50.08 N, 14.42 E; Apple iPhone 15; software; artist; date taken; Orientation 6.
|
|
pub fn photo_fields() -> Vec<Field> {
|
|
vec![
|
|
ascii(Tag::Make, MAKE),
|
|
ascii(Tag::Model, MODEL),
|
|
ascii(Tag::Software, SOFTWARE),
|
|
ascii(Tag::Artist, AUTHOR),
|
|
ascii(Tag::DateTimeOriginal, "2024:05:01 10:20:30"),
|
|
short(Tag::Orientation, &[6]),
|
|
ascii(Tag::GPSLatitudeRef, "N"),
|
|
rationals(Tag::GPSLatitude, &[(50, 1), (4, 1), (48, 1)]),
|
|
ascii(Tag::GPSLongitudeRef, "E"),
|
|
rationals(Tag::GPSLongitude, &[(14, 1), (25, 1), (12, 1)]),
|
|
]
|
|
}
|
|
|
|
/// A raw EXIF block (TIFF header onwards).
|
|
pub fn exif_tiff(fields: &[Field]) -> Vec<u8> {
|
|
let mut w = Writer::new();
|
|
for f in fields {
|
|
w.push_field(f);
|
|
}
|
|
let mut c = Cursor::new(Vec::new());
|
|
w.write(&mut c, false).expect("exif writer");
|
|
c.into_inner()
|
|
}
|
|
|
|
pub fn xmp_packet() -> String {
|
|
format!(
|
|
r#"<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?>
|
|
<x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
|
|
<rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xmp="{XMP_NS}/1.0/"
|
|
xmp:CreatorTool="{SOFTWARE}">
|
|
<dc:creator><rdf:Seq><rdf:li>{AUTHOR}</rdf:li></rdf:Seq></dc:creator>
|
|
</rdf:Description></rdf:RDF></x:xmpmeta>
|
|
<?xpacket end="w"?>"#
|
|
)
|
|
}
|
|
|
|
/// Photoshop 3.0 + 8BIM 0x0404 with an IPTC by-line (2:80).
|
|
pub fn photoshop_iptc() -> Vec<u8> {
|
|
let mut iim = vec![0x1C, 2, 80];
|
|
iim.extend_from_slice(&(AUTHOR.len() as u16).to_be_bytes());
|
|
iim.extend_from_slice(AUTHOR.as_bytes());
|
|
let mut p = b"Photoshop 3.0\0".to_vec();
|
|
p.extend_from_slice(b"8BIM");
|
|
p.extend_from_slice(&0x0404u16.to_be_bytes());
|
|
p.extend_from_slice(&[0, 0]); // empty name, padded
|
|
p.extend_from_slice(&(iim.len() as u32).to_be_bytes());
|
|
p.extend_from_slice(&iim);
|
|
if iim.len() % 2 == 1 {
|
|
p.push(0);
|
|
}
|
|
p
|
|
}
|
|
|
|
pub fn segment(marker: u8, payload: &[u8]) -> Vec<u8> {
|
|
let mut s = vec![0xFF, marker];
|
|
s.extend_from_slice(&((payload.len() + 2) as u16).to_be_bytes());
|
|
s.extend_from_slice(payload);
|
|
s
|
|
}
|
|
|
|
pub fn plain_jpeg() -> Vec<u8> {
|
|
let img = rgb(24, 16);
|
|
let mut out = Vec::new();
|
|
image::codecs::jpeg::JpegEncoder::new_with_quality(&mut out, 90)
|
|
.write_image(img.as_raw(), 24, 16, image::ExtendedColorType::Rgb8)
|
|
.expect("jpeg encode");
|
|
out
|
|
}
|
|
|
|
/// A JPEG with the given extra segments inserted right after SOI.
|
|
pub fn jpeg_with(segments: &[Vec<u8>]) -> Vec<u8> {
|
|
let plain = plain_jpeg();
|
|
let mut out = plain[..2].to_vec();
|
|
for s in segments {
|
|
out.extend_from_slice(s);
|
|
}
|
|
out.extend_from_slice(&plain[2..]);
|
|
out
|
|
}
|
|
|
|
pub fn exif_app1(raw: &[u8]) -> Vec<u8> {
|
|
let mut p = b"Exif\0\0".to_vec();
|
|
p.extend_from_slice(raw);
|
|
segment(0xE1, &p)
|
|
}
|
|
|
|
pub fn xmp_app1() -> Vec<u8> {
|
|
let mut p = b"http://ns.adobe.com/xap/1.0/\0".to_vec();
|
|
p.extend_from_slice(xmp_packet().as_bytes());
|
|
segment(0xE1, &p)
|
|
}
|
|
|
|
/// The full test photo: EXIF (GPS, camera, author, software, date, Orientation 6), XMP, IPTC, a comment.
|
|
pub fn photo_jpeg() -> Vec<u8> {
|
|
jpeg_with(&[
|
|
exif_app1(&exif_tiff(&photo_fields())),
|
|
xmp_app1(),
|
|
segment(0xED, &photoshop_iptc()),
|
|
segment(0xFE, COMMENT.as_bytes()),
|
|
])
|
|
}
|
|
|
|
pub fn plain_png() -> Vec<u8> {
|
|
let img = rgba(9, 7);
|
|
let mut out = Vec::new();
|
|
image::codecs::png::PngEncoder::new(&mut out)
|
|
.write_image(img.as_raw(), 9, 7, image::ExtendedColorType::Rgba8)
|
|
.expect("png encode");
|
|
out
|
|
}
|
|
|
|
/// PNG with eXIf, iTXt XMP, tEXt Author and Software, tIME.
|
|
pub fn photo_png() -> Vec<u8> {
|
|
use img_parts::Bytes;
|
|
use img_parts::png::{Png, PngChunk};
|
|
let mut png = Png::from_bytes(Bytes::from(plain_png())).expect("png parse");
|
|
let mut itxt = b"XML:com.adobe.xmp\0\0\0\0\0".to_vec();
|
|
itxt.extend_from_slice(xmp_packet().as_bytes());
|
|
let mut author = b"Author\0".to_vec();
|
|
author.extend_from_slice(AUTHOR.as_bytes());
|
|
let mut software = b"Software\0".to_vec();
|
|
software.extend_from_slice(SOFTWARE.as_bytes());
|
|
let chunks = [
|
|
PngChunk::new(*b"eXIf", Bytes::from(exif_tiff(&photo_fields()))),
|
|
PngChunk::new(*b"iTXt", Bytes::from(itxt)),
|
|
PngChunk::new(*b"tEXt", Bytes::from(author)),
|
|
PngChunk::new(*b"tEXt", Bytes::from(software)),
|
|
PngChunk::new(*b"tIME", Bytes::from_static(&[0x07, 0xE8, 5, 1, 10, 20, 30])),
|
|
];
|
|
for (i, c) in chunks.into_iter().enumerate() {
|
|
png.chunks_mut().insert(1 + i, c);
|
|
}
|
|
png.encoder().bytes().to_vec()
|
|
}
|
|
|
|
pub fn plain_webp() -> Vec<u8> {
|
|
let img = rgba(10, 6);
|
|
let mut out = Vec::new();
|
|
image::codecs::webp::WebPEncoder::new_lossless(&mut out)
|
|
.write_image(img.as_raw(), 10, 6, image::ExtendedColorType::Rgba8)
|
|
.expect("webp encode");
|
|
out
|
|
}
|
|
|
|
/// VP8X WebP with EXIF and XMP chunks (built with img-parts, which also sets the VP8X flags).
|
|
pub fn photo_webp() -> Vec<u8> {
|
|
use img_parts::riff::{RiffChunk, RiffContent};
|
|
use img_parts::webp::{CHUNK_XMP, WebP};
|
|
use img_parts::{Bytes, ImageEXIF};
|
|
let mut webp = WebP::from_bytes(Bytes::from(plain_webp())).expect("webp parse");
|
|
webp.chunks_mut().push(RiffChunk::new(CHUNK_XMP, RiffContent::Data(Bytes::from(xmp_packet().into_bytes()))));
|
|
webp.set_exif(Some(Bytes::from(exif_tiff(&photo_fields()))));
|
|
webp.encoder().bytes().to_vec()
|
|
}
|
|
|
|
/// A 5x4 RGB TIFF written by the EXIF writer, with Make, Model, Software and Artist tags.
|
|
pub fn photo_tiff() -> (Vec<u8>, RgbImage) {
|
|
let (w, h) = (5u32, 4u32);
|
|
let img = rgb(w, h);
|
|
let fields = vec![
|
|
Field { tag: Tag::ImageWidth, ifd_num: In::PRIMARY, value: Value::Long(vec![w]) },
|
|
Field { tag: Tag::ImageLength, ifd_num: In::PRIMARY, value: Value::Long(vec![h]) },
|
|
short(Tag::BitsPerSample, &[8, 8, 8]),
|
|
short(Tag::Compression, &[1]),
|
|
short(Tag::PhotometricInterpretation, &[2]),
|
|
short(Tag::SamplesPerPixel, &[3]),
|
|
Field { tag: Tag::RowsPerStrip, ifd_num: In::PRIMARY, value: Value::Long(vec![h]) },
|
|
short(Tag::PlanarConfiguration, &[1]),
|
|
ascii(Tag::Make, MAKE),
|
|
ascii(Tag::Model, MODEL),
|
|
ascii(Tag::Software, SOFTWARE),
|
|
ascii(Tag::Artist, AUTHOR),
|
|
];
|
|
let strip: &[u8] = img.as_raw();
|
|
let strips = [strip];
|
|
let mut wr = Writer::new();
|
|
for f in &fields {
|
|
wr.push_field(f);
|
|
}
|
|
wr.set_strips(&strips, In::PRIMARY);
|
|
let mut c = Cursor::new(Vec::new());
|
|
wr.write(&mut c, false).expect("tiff writer");
|
|
(c.into_inner(), img)
|
|
}
|
|
|
|
/// A two-frame looping GIF with a comment extension and an XMP application extension.
|
|
pub fn photo_gif() -> Vec<u8> {
|
|
use image::codecs::gif::{GifEncoder, Repeat};
|
|
use image::{Delay, Frame};
|
|
let mut out = Vec::new();
|
|
{
|
|
let mut enc = GifEncoder::new(&mut out);
|
|
enc.set_repeat(Repeat::Infinite).expect("repeat");
|
|
let a = RgbaImage::from_fn(6, 5, |x, y| {
|
|
if (x + y) % 2 == 0 { image::Rgba([255, 0, 0, 255]) } else { image::Rgba([0, 0, 255, 255]) }
|
|
});
|
|
let b = RgbaImage::from_fn(6, 5, |x, _| {
|
|
if x < 3 { image::Rgba([0, 255, 0, 255]) } else { image::Rgba([255, 255, 255, 255]) }
|
|
});
|
|
let d = Delay::from_numer_denom_ms(100, 1);
|
|
enc.encode_frames(vec![Frame::from_parts(a, 0, 0, d), Frame::from_parts(b, 0, 0, d)]).expect("gif encode");
|
|
}
|
|
assert_eq!(out.last(), Some(&0x3B));
|
|
out.pop();
|
|
// Comment extension.
|
|
out.extend_from_slice(&[0x21, 0xFE, COMMENT.len() as u8]);
|
|
out.extend_from_slice(COMMENT.as_bytes());
|
|
out.push(0);
|
|
// XMP application extension: raw packet plus the 258-byte magic trailer.
|
|
out.extend_from_slice(&[0x21, 0xFF, 11]);
|
|
out.extend_from_slice(b"XMP DataXMP");
|
|
out.extend_from_slice(xmp_packet().as_bytes());
|
|
out.push(1);
|
|
out.extend((0..=255u8).rev());
|
|
out.push(0);
|
|
out.push(0x3B);
|
|
out
|
|
}
|
|
|
|
pub fn contains(hay: &[u8], needle: &[u8]) -> bool {
|
|
hay.windows(needle.len()).any(|w| w == needle)
|
|
}
|
|
|
|
/// A deterministic xorshift generator.
|
|
pub struct XorShift(pub u64);
|
|
|
|
impl XorShift {
|
|
pub fn next(&mut self) -> u64 {
|
|
let mut x = self.0;
|
|
x ^= x << 13;
|
|
x ^= x >> 7;
|
|
x ^= x << 17;
|
|
self.0 = x;
|
|
x
|
|
}
|
|
|
|
pub fn below(&mut self, n: usize) -> usize {
|
|
if n == 0 { 0 } else { (self.next() % n as u64) as usize }
|
|
}
|
|
}
|