use std::path::Path;
use crate::error::{Med64Error, Result};
const EMR_HEADER: u32 = 1;
const EMR_EOF: u32 = 14;
const EMR_STRETCHDIBITS: u32 = 81;
const ENHMETA_SIGNATURE: u32 = 0x464D_4520; const ENHMETA_VERSION: u32 = 0x0001_0000;
pub fn write_bgr_emf(path: impl AsRef<Path>, width: u32, height: u32, bgr: &[u8]) -> Result<()> {
let path = path.as_ref();
let bytes = encode_bgr_emf(width, height, bgr)?;
std::fs::write(path, bytes)
.map_err(|source| Med64Error::WriteIo { path: path.to_path_buf(), source })
}
pub fn encode_bgr_emf(width: u32, height: u32, bgr: &[u8]) -> Result<Vec<u8>> {
let w = width as usize;
let h = height as usize;
let expected = w.checked_mul(h).and_then(|n| n.checked_mul(3)).unwrap_or(0);
if bgr.len() < expected || width == 0 || height == 0 {
return Err(Med64Error::InvalidWrite {
message: format!("EMF BGR buffer length {} < {}x{}x3", bgr.len(), width, height),
});
}
let stride = (w * 3 + 3) & !3;
let mut dib_pixels = vec![0u8; stride * h];
for y in 0..h {
let src_y = y; let dst_y = h - 1 - y; let src = &bgr[src_y * w * 3..(src_y + 1) * w * 3];
let dst = &mut dib_pixels[dst_y * stride..dst_y * stride + w * 3];
dst.copy_from_slice(src);
}
let bmi_size = 40u32; let dib_bytes = bmi_size as usize + dib_pixels.len();
let stretch_fixed = 80u32; let stretch_size = 8 + stretch_fixed + dib_bytes as u32;
let stretch_pad = (4 - (stretch_size % 4)) % 4;
let stretch_size = stretch_size + stretch_pad;
let header_size = 88u32;
let eof_size = 20u32;
let n_records = 3u32;
let n_bytes = header_size + stretch_size + eof_size;
let mut out = Vec::with_capacity(n_bytes as usize);
push_u32(&mut out, EMR_HEADER);
push_u32(&mut out, header_size);
push_i32(&mut out, 0);
push_i32(&mut out, 0);
push_i32(&mut out, width as i32 - 1);
push_i32(&mut out, height as i32 - 1);
let frame_w = ((width as f64) * 2540.0 / 96.0).round() as i32;
let frame_h = ((height as f64) * 2540.0 / 96.0).round() as i32;
push_i32(&mut out, 0);
push_i32(&mut out, 0);
push_i32(&mut out, frame_w);
push_i32(&mut out, frame_h);
push_u32(&mut out, ENHMETA_SIGNATURE);
push_u32(&mut out, ENHMETA_VERSION);
push_u32(&mut out, n_bytes);
push_u32(&mut out, n_records);
push_u16(&mut out, 1); push_u16(&mut out, 0); push_u32(&mut out, 0); push_u32(&mut out, 0); push_u32(&mut out, 0); push_i32(&mut out, width as i32); push_i32(&mut out, height as i32);
push_i32(&mut out, 320); push_i32(&mut out, 240);
debug_assert_eq!(out.len(), header_size as usize);
let stretch_start = out.len();
push_u32(&mut out, EMR_STRETCHDIBITS);
push_u32(&mut out, stretch_size);
push_i32(&mut out, 0);
push_i32(&mut out, 0);
push_i32(&mut out, width as i32 - 1);
push_i32(&mut out, height as i32 - 1);
push_i32(&mut out, 0);
push_i32(&mut out, 0);
push_i32(&mut out, width as i32);
push_i32(&mut out, height as i32);
let off_bmi = 8 + stretch_fixed;
let off_bits = off_bmi + bmi_size;
push_u32(&mut out, off_bmi);
push_u32(&mut out, bmi_size);
push_u32(&mut out, off_bits);
push_u32(&mut out, dib_pixels.len() as u32);
push_u32(&mut out, 0);
push_u32(&mut out, 0x00CC_0020);
push_i32(&mut out, 0);
push_i32(&mut out, 0);
push_i32(&mut out, width as i32);
push_i32(&mut out, height as i32);
let written_fixed = (out.len() - stretch_start - 8) as u32;
if written_fixed < stretch_fixed {
out.resize(stretch_start + 8 + stretch_fixed as usize, 0);
}
push_u32(&mut out, 40); push_i32(&mut out, width as i32);
push_i32(&mut out, height as i32); push_u16(&mut out, 1); push_u16(&mut out, 24); push_u32(&mut out, 0); push_u32(&mut out, dib_pixels.len() as u32);
push_i32(&mut out, 0); push_i32(&mut out, 0);
push_u32(&mut out, 0); push_u32(&mut out, 0);
out.extend_from_slice(&dib_pixels);
out.resize(out.len() + stretch_pad as usize, 0);
let actual_stretch = (out.len() - stretch_start) as u32;
out[stretch_start + 4..stretch_start + 8].copy_from_slice(&actual_stretch.to_le_bytes());
push_u32(&mut out, EMR_EOF);
push_u32(&mut out, eof_size);
push_u32(&mut out, 0); push_u32(&mut out, 0); push_u32(&mut out, eof_size);
let total = out.len() as u32;
out[48..52].copy_from_slice(&total.to_le_bytes()); out[52..56].copy_from_slice(&n_records.to_le_bytes());
Ok(out)
}
fn push_u16(buf: &mut Vec<u8>, v: u16) {
buf.extend_from_slice(&v.to_le_bytes());
}
fn push_u32(buf: &mut Vec<u8>, v: u32) {
buf.extend_from_slice(&v.to_le_bytes());
}
fn push_i32(buf: &mut Vec<u8>, v: i32) {
buf.extend_from_slice(&v.to_le_bytes());
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn emf_has_signature_and_eof() {
let w = 16u32;
let h = 8u32;
let mut bgr = vec![0u8; (w * h * 3) as usize];
bgr[0] = 0;
bgr[1] = 0;
bgr[2] = 255;
let bytes = encode_bgr_emf(w, h, &bgr).unwrap();
assert_eq!(&bytes[0..4], &1u32.to_le_bytes()); assert_eq!(&bytes[40..44], &ENHMETA_SIGNATURE.to_le_bytes());
let eof = &bytes[bytes.len() - 20..];
assert_eq!(&eof[0..4], &EMR_EOF.to_le_bytes());
}
}