use enough::Unstoppable;
use jpegli::decoder::{Decoder, PixelFormat};
use jpegli::encoder::{ChromaSubsampling, EncoderConfig, PixelLayout};
fn main() {
println!("Creating test image...");
let width = 64u32;
let height = 64u32;
let mut input = vec![0u8; (width * height * 3) as usize];
for y in 0..height as usize {
for x in 0..width as usize {
let idx = (y * width as usize + x) * 3;
input[idx] = (x * 4) as u8; input[idx + 1] = (y * 4) as u8; input[idx + 2] = 128; }
}
println!("Encoding JPEG...");
let config = EncoderConfig::ycbcr(90.0, ChromaSubsampling::None);
let mut enc = config
.encode_from_bytes(width, height, PixelLayout::Rgb8Srgb)
.expect("encoder creation failed");
enc.push_packed(&input, Unstoppable).expect("push failed");
let jpeg = enc.finish().expect("encoding failed");
println!("Encoded to {} bytes", jpeg.len());
println!("Decoding JPEG...");
let decoder = Decoder::new().output_format(PixelFormat::Rgb);
let decoded = decoder.decode(&jpeg).expect("decoding failed");
println!(
"Decoded {}x{} image with {} bytes",
decoded.width,
decoded.height,
decoded.data.len()
);
assert_eq!(decoded.width, width);
assert_eq!(decoded.height, height);
assert_eq!(decoded.data.len(), (width * height * 3) as usize);
let mut max_diff = 0i32;
for i in 0..input.len() {
let diff = (input[i] as i32 - decoded.data[i] as i32).abs();
max_diff = max_diff.max(diff);
}
println!("Max pixel difference: {}", max_diff);
assert!(max_diff < 30, "max_diff {} too large", max_diff);
println!("✓ WASM SIMD128 encode/decode test passed!");
}