Cover ec-image decode (all sniffed formats) and ec-inflate
Browse files at this commit1ee915b9e22ac945a2e4c6a0a8410201b5ecb02fParent: 4d8621c- feat
- cover ec-image decode (all sniffed formats) and ec-inflate
3 changed files +114 -0
- Cargo.lock +2 -0
- tools/ec-bench/Cargo.toml +2 -0
- tools/ec-bench/src/main.rs +110 -0
Cargo.lockmodified+2 -0
| @@ -281,6 +281,8 @@ dependencies = [ | |||
| 281 | "ec-h264", | 281 | "ec-h264", |
| 282 | "ec-h264-syntax", | 282 | "ec-h264-syntax", |
| 283 | "ec-h265", | 283 | "ec-h265", |
| 284 | + "ec-image", | ||
| 285 | + "ec-inflate", | ||
| 284 | "ec-probe", | 286 | "ec-probe", |
| 285 | ] | 287 | ] |
| 286 | 288 | ||
tools/ec-bench/Cargo.tomlmodified+2 -0
| @@ -20,3 +20,5 @@ ec-h265 = { workspace = true } | |||
| 20 | ec-av1 = { workspace = true } | 20 | ec-av1 = { workspace = true } |
| 21 | ec-probe = { workspace = true } | 21 | ec-probe = { workspace = true } |
| 22 | ec-h264-syntax = { workspace = true } | 22 | ec-h264-syntax = { workspace = true } |
| 23 | +ec-image = { workspace = true } | ||
| 24 | +ec-inflate = { workspace = true } | ||
tools/ec-bench/src/main.rsmodified+110 -0
| @@ -40,6 +40,8 @@ fn main() { | |||
| 40 | bench_h265_encode(&mut rows); | 40 | bench_h265_encode(&mut rows); |
| 41 | bench_av1_encode(&mut rows); | 41 | bench_av1_encode(&mut rows); |
| 42 | bench_audio_decode(&mut rows); | 42 | bench_audio_decode(&mut rows); |
| 43 | + bench_image_decode(&mut rows); | ||
| 44 | + bench_inflate(&mut rows); | ||
| 43 | 45 | ||
| 44 | rows.sort_by(|a, b| { | 46 | rows.sort_by(|a, b| { |
| 45 | a.rtf | 47 | a.rtf |
| @@ -439,6 +441,114 @@ fn bench_audio_decode(rows: &mut Vec<Row>) { | |||
| 439 | } | 441 | } |
| 440 | } | 442 | } |
| 441 | 443 | ||
| 444 | +// --------------------------------------------------------------------------- | ||
| 445 | +// images: one decode entry point (`ec_image::decode`) covers every format | ||
| 446 | +// the crate sniffs, so every real-library still is a candidate. | ||
| 447 | +// --------------------------------------------------------------------------- | ||
| 448 | + | ||
| 449 | +fn bench_image_decode(rows: &mut Vec<Row>) { | ||
| 450 | + let mut candidates: Vec<PathBuf> = Vec::new(); | ||
| 451 | + if let Ok(entries) = std::fs::read_dir(fixtures().join("stills")) { | ||
| 452 | + candidates.extend( | ||
| 453 | + entries | ||
| 454 | + .flatten() | ||
| 455 | + .map(|e| e.path()) | ||
| 456 | + .filter(|p| p.extension().is_some()), | ||
| 457 | + ); | ||
| 458 | + } | ||
| 459 | + for f in [ | ||
| 460 | + "IMG_20260804_170612.jpg", | ||
| 461 | + "drawing-1.png", | ||
| 462 | + "20260722-233942.png", | ||
| 463 | + "giphy.gif", | ||
| 464 | + ] { | ||
| 465 | + let p = home().join("Downloads").join(f); | ||
| 466 | + if p.exists() { | ||
| 467 | + candidates.push(p); | ||
| 468 | + } | ||
| 469 | + } | ||
| 470 | + candidates.sort(); | ||
| 471 | + | ||
| 472 | + for path in candidates { | ||
| 473 | + let Ok(bytes) = std::fs::read(&path) else { | ||
| 474 | + continue; | ||
| 475 | + }; | ||
| 476 | + let mp = bytes.len() as f64 / 1_000_000.0; | ||
| 477 | + let start = Instant::now(); | ||
| 478 | + let Ok(img) = ec_image::decode(&bytes) else { | ||
| 479 | + continue; | ||
| 480 | + }; | ||
| 481 | + let wall_ms = start.elapsed().as_secs_f64() * 1000.0; | ||
| 482 | + let (w, h) = img.dimensions(); | ||
| 483 | + let megapixels = (w as f64 * h as f64) / 1_000_000.0; | ||
| 484 | + let name = path.file_name().unwrap().to_string_lossy().into_owned(); | ||
| 485 | + rows.push(Row { | ||
| 486 | + component: "ec-image", | ||
| 487 | + direction: "decode", | ||
| 488 | + content: format!("{name} ({w}x{h}, {mp:.2} MB coded)"), | ||
| 489 | + media: format!("{megapixels:.2} MP"), | ||
| 490 | + wall_ms, | ||
| 491 | + rtf: (wall_ms > 0.0).then_some(megapixels / (wall_ms / 1000.0)), | ||
| 492 | + }); | ||
| 493 | + } | ||
| 494 | +} | ||
| 495 | + | ||
| 496 | +// --------------------------------------------------------------------------- | ||
| 497 | +// ec-inflate: the zlib stream a real PNG's IDAT chain already carries. | ||
| 498 | +// --------------------------------------------------------------------------- | ||
| 499 | + | ||
| 500 | +fn bench_inflate(rows: &mut Vec<Row>) { | ||
| 501 | + let Some(png) = [ | ||
| 502 | + fixtures().join("stills/gray16.png"), | ||
| 503 | + home().join("Downloads/drawing-1.png"), | ||
| 504 | + ] | ||
| 505 | + .into_iter() | ||
| 506 | + .find(|p| p.exists()) else { | ||
| 507 | + rows.push(missing("ec-inflate", "inflate")); | ||
| 508 | + return; | ||
| 509 | + }; | ||
| 510 | + let Ok(bytes) = std::fs::read(&png) else { | ||
| 511 | + rows.push(missing("ec-inflate", "inflate")); | ||
| 512 | + return; | ||
| 513 | + }; | ||
| 514 | + // Concatenate every IDAT chunk's payload: that byte string is exactly the | ||
| 515 | + // zlib stream `ec_image`'s own PNG path hands to `ec_inflate`. | ||
| 516 | + let mut idat = Vec::new(); | ||
| 517 | + let mut i = 8usize; // past the PNG signature | ||
| 518 | + while i + 8 <= bytes.len() { | ||
| 519 | + let len = u32::from_be_bytes(bytes[i..i + 4].try_into().unwrap()) as usize; | ||
| 520 | + let kind = &bytes[i + 4..i + 8]; | ||
| 521 | + let start = i + 8; | ||
| 522 | + if kind == b"IDAT" && start + len <= bytes.len() { | ||
| 523 | + idat.extend_from_slice(&bytes[start..start + len]); | ||
| 524 | + } | ||
| 525 | + i = start + len + 4; // skip CRC | ||
| 526 | + } | ||
| 527 | + if idat.is_empty() { | ||
| 528 | + rows.push(missing("ec-inflate", "inflate")); | ||
| 529 | + return; | ||
| 530 | + } | ||
| 531 | + let compressed_mb = idat.len() as f64 / 1_000_000.0; | ||
| 532 | + let start = Instant::now(); | ||
| 533 | + let Ok(out) = ec_inflate::inflate_zlib(&idat, 1 << 30) else { | ||
| 534 | + rows.push(missing("ec-inflate", "inflate")); | ||
| 535 | + return; | ||
| 536 | + }; | ||
| 537 | + let wall_ms = start.elapsed().as_secs_f64() * 1000.0; | ||
| 538 | + let out_mb = out.len() as f64 / 1_000_000.0; | ||
| 539 | + rows.push(Row { | ||
| 540 | + component: "ec-inflate", | ||
| 541 | + direction: "inflate", | ||
| 542 | + content: format!( | ||
| 543 | + "{} IDAT stream, {compressed_mb:.2} MB in", | ||
| 544 | + png.file_name().unwrap().to_string_lossy() | ||
| 545 | + ), | ||
| 546 | + media: format!("{out_mb:.2} MB out"), | ||
| 547 | + wall_ms, | ||
| 548 | + rtf: (wall_ms > 0.0).then_some(out_mb / (wall_ms / 1000.0)), | ||
| 549 | + }); | ||
| 550 | +} | ||
| 551 | + | ||
| 442 | fn missing(component: &'static str, direction: &'static str) -> Row { | 552 | fn missing(component: &'static str, direction: &'static str) -> Row { |
| 443 | missing_direction(component, direction, "no real-media candidate found") | 553 | missing_direction(component, direction, "no real-media candidate found") |
| 444 | } | 554 | } |