webpx 0.3.3

Complete WebP encoding/decoding with ICC profiles, streaming, and animation support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
//! Memory formula derivation for webpx.
//!
//! Run with heaptrack to capture per-size memory usage:
//! ```
//! cargo build --release --all-features --example mem_formula
//!
//! # Single run:
//! heaptrack ./target/release/examples/mem_formula --size 1024 --mode lossy --quality 85 --method 4
//!
//! # Batch collection script:
//! for size in 128 256 512 1024 2048; do
//!   for mode in lossy lossless; do
//!     for method in 0 4 6; do
//!       for quality in 50 75 85 95; do
//!         echo "=== size=$size mode=$mode method=$method quality=$quality ==="
//!         heaptrack ./target/release/examples/mem_formula \
//!           --size $size --mode $mode --method $method --quality $quality 2>&1 | grep "peak heap"
//!       done
//!     done
//!   done
//! done
//!
//! # Quick sweep for formula fitting:
//! ./target/release/examples/mem_formula --sweep
//! ```

use std::env;
use std::fs;
use std::io::{self, Write};
use webpx::{Decoder, Encoder, ImageInfo, Unstoppable, decode_rgba, decode_rgba_into};

#[derive(Debug, Clone)]
struct Config {
    width: u32,
    height: u32,
    mode: String,
    quality: f32,
    method: u8,
    near_lossless: u8,
    bpp: u8,         // 3 for RGB, 4 for RGBA
    content: String, // gradient, noise, solid
}

impl Default for Config {
    fn default() -> Self {
        Self {
            width: 512,
            height: 512,
            mode: "lossy".to_string(),
            quality: 85.0,
            method: 4,
            near_lossless: 100,
            bpp: 4,
            content: "gradient".to_string(),
        }
    }
}

fn generate_gradient_rgba(width: u32, height: u32) -> Vec<u8> {
    let mut data = Vec::with_capacity((width * height * 4) as usize);
    for y in 0..height {
        for x in 0..width {
            let r = ((x * 255) / width.max(1)) as u8;
            let g = ((y * 255) / height.max(1)) as u8;
            let b = (((x + y) * 127) / (width + height).max(1)) as u8;
            data.push(r);
            data.push(g);
            data.push(b);
            data.push(255);
        }
    }
    data
}

fn generate_noise_rgba(width: u32, height: u32, seed: u64) -> Vec<u8> {
    // Simple LCG for reproducible "random" noise
    let mut state = seed;
    let mut data = Vec::with_capacity((width * height * 4) as usize);
    for _ in 0..(width * height) {
        state = state.wrapping_mul(6364136223846793005).wrapping_add(1);
        let r = (state >> 56) as u8;
        state = state.wrapping_mul(6364136223846793005).wrapping_add(1);
        let g = (state >> 56) as u8;
        state = state.wrapping_mul(6364136223846793005).wrapping_add(1);
        let b = (state >> 56) as u8;
        data.push(r);
        data.push(g);
        data.push(b);
        data.push(255);
    }
    data
}

fn generate_solid_rgba(width: u32, height: u32) -> Vec<u8> {
    vec![128u8; (width * height * 4) as usize]
}

fn generate_gradient_rgb(width: u32, height: u32) -> Vec<u8> {
    let mut data = Vec::with_capacity((width * height * 3) as usize);
    for y in 0..height {
        for x in 0..width {
            let r = ((x * 255) / width.max(1)) as u8;
            let g = ((y * 255) / height.max(1)) as u8;
            let b = (((x + y) * 127) / (width + height).max(1)) as u8;
            data.push(r);
            data.push(g);
            data.push(b);
        }
    }
    data
}

fn run_encode(cfg: &Config) {
    let pixels = (cfg.width as u64) * (cfg.height as u64);
    let input_bytes = pixels * (cfg.bpp as u64);

    eprintln!(
        "Config: {}x{} mode={} q={} m={} nl={} bpp={} content={}",
        cfg.width,
        cfg.height,
        cfg.mode,
        cfg.quality,
        cfg.method,
        cfg.near_lossless,
        cfg.bpp,
        cfg.content
    );
    eprintln!("Pixels: {} Input: {} bytes", pixels, input_bytes);

    let rgba = match cfg.content.as_str() {
        "noise" => generate_noise_rgba(cfg.width, cfg.height, 12345),
        "solid" => generate_solid_rgba(cfg.width, cfg.height),
        _ => generate_gradient_rgba(cfg.width, cfg.height),
    };
    let rgb = generate_gradient_rgb(cfg.width, cfg.height);

    match cfg.mode.as_str() {
        "lossy" => {
            let data = if cfg.bpp == 4 { &rgba } else { &rgb };
            let encoder = if cfg.bpp == 4 {
                Encoder::new_rgba(data, cfg.width, cfg.height)
            } else {
                Encoder::new_rgb(data, cfg.width, cfg.height)
            };
            let result = encoder
                .quality(cfg.quality)
                .method(cfg.method)
                .encode(Unstoppable)
                .unwrap();
            eprintln!(
                "Output: {} bytes ({:.2}% of input)",
                result.len(),
                (result.len() as f64 / input_bytes as f64) * 100.0
            );
        }
        "lossless" => {
            let encoder = Encoder::new_rgba(&rgba, cfg.width, cfg.height);
            let result = encoder
                .lossless(true)
                .method(cfg.method)
                .encode(Unstoppable)
                .unwrap();
            eprintln!(
                "Output: {} bytes ({:.2}% of input)",
                result.len(),
                (result.len() as f64 / input_bytes as f64) * 100.0
            );
        }
        "near-lossless" => {
            let encoder = Encoder::new_rgba(&rgba, cfg.width, cfg.height);
            let result = encoder
                .lossless(true)
                .near_lossless(cfg.near_lossless)
                .method(cfg.method)
                .encode(Unstoppable)
                .unwrap();
            eprintln!(
                "Output: {} bytes ({:.2}% of input)",
                result.len(),
                (result.len() as f64 / input_bytes as f64) * 100.0
            );
        }
        "decode" | "decode-lossy" => {
            let webp = Encoder::new_rgba(&rgba, cfg.width, cfg.height)
                .quality(cfg.quality)
                .encode(Unstoppable)
                .unwrap();
            drop(rgba);
            drop(rgb);
            let (decoded, w, h) = decode_rgba(&webp).unwrap();
            eprintln!("Decoded: {}x{}, {} bytes", w, h, decoded.len());
        }
        "decode-lossless" => {
            let webp = Encoder::new_rgba(&rgba, cfg.width, cfg.height)
                .lossless(true)
                .encode(Unstoppable)
                .unwrap();
            drop(rgba);
            drop(rgb);
            let (decoded, w, h) = decode_rgba(&webp).unwrap();
            eprintln!("Decoded: {}x{}, {} bytes", w, h, decoded.len());
        }
        // Prepare modes: create WebP files for isolated decode testing
        "prepare-lossy" => {
            let webp = Encoder::new_rgba(&rgba, cfg.width, cfg.height)
                .quality(cfg.quality)
                .method(cfg.method)
                .encode(Unstoppable)
                .unwrap();
            let filename = format!("mem_data/{}x{}_lossy.webp", cfg.width, cfg.height);
            fs::write(&filename, &webp).unwrap();
            eprintln!("Wrote {} ({} bytes)", filename, webp.len());
        }
        "prepare-lossless" => {
            let webp = Encoder::new_rgba(&rgba, cfg.width, cfg.height)
                .lossless(true)
                .method(cfg.method)
                .encode(Unstoppable)
                .unwrap();
            let filename = format!("mem_data/{}x{}_lossless.webp", cfg.width, cfg.height);
            fs::write(&filename, &webp).unwrap();
            eprintln!("Wrote {} ({} bytes)", filename, webp.len());
        }
        // Decode-only modes: load from pre-created files (isolates decode memory)
        "decode-only-lossy" => {
            let filename = format!("mem_data/{}x{}_lossy.webp", cfg.width, cfg.height);
            let webp = fs::read(&filename).expect("Run with --mode prepare-lossy first");
            let (decoded, w, h) = decode_rgba(&webp).unwrap();
            eprintln!(
                "Decoded from {}: {}x{}, {} bytes",
                filename,
                w,
                h,
                decoded.len()
            );
        }
        "decode-only-lossless" => {
            let filename = format!("mem_data/{}x{}_lossless.webp", cfg.width, cfg.height);
            let webp = fs::read(&filename).expect("Run with --mode prepare-lossless first");
            let (decoded, w, h) = decode_rgba(&webp).unwrap();
            eprintln!(
                "Decoded from {}: {}x{}, {} bytes",
                filename,
                w,
                h,
                decoded.len()
            );
        }
        // Zero-copy decode variants (decode into pre-allocated buffer)
        "decode-into-lossy" => {
            let filename = format!("mem_data/{}x{}_lossy.webp", cfg.width, cfg.height);
            let webp = fs::read(&filename).expect("Run with --mode prepare-lossy first");
            let info = ImageInfo::from_webp(&webp).unwrap();
            let stride = info.width as usize * 4;
            let mut buffer = vec![0u8; stride * info.height as usize];
            let (w, h) = decode_rgba_into(&webp, &mut buffer, stride as u32).unwrap();
            eprintln!(
                "Decoded into buffer from {}: {}x{}, {} bytes",
                filename,
                w,
                h,
                buffer.len()
            );
        }
        "decode-into-lossless" => {
            let filename = format!("mem_data/{}x{}_lossless.webp", cfg.width, cfg.height);
            let webp = fs::read(&filename).expect("Run with --mode prepare-lossless first");
            let info = ImageInfo::from_webp(&webp).unwrap();
            let stride = info.width as usize * 4;
            let mut buffer = vec![0u8; stride * info.height as usize];
            let (w, h) = decode_rgba_into(&webp, &mut buffer, stride as u32).unwrap();
            eprintln!(
                "Decoded into buffer from {}: {}x{}, {} bytes",
                filename,
                w,
                h,
                buffer.len()
            );
        }
        // Decoder builder API
        "decoder-builder-lossy" => {
            let filename = format!("mem_data/{}x{}_lossy.webp", cfg.width, cfg.height);
            let webp = fs::read(&filename).expect("Run with --mode prepare-lossy first");
            let img = Decoder::new(&webp).unwrap().decode_rgba().unwrap();
            let (w, h) = (img.width(), img.height());
            let pixel_bytes = img.pixels().len() * 4;
            eprintln!(
                "Decoder builder from {}: {}x{}, {} bytes",
                filename, w, h, pixel_bytes
            );
        }
        "decoder-builder-lossless" => {
            let filename = format!("mem_data/{}x{}_lossless.webp", cfg.width, cfg.height);
            let webp = fs::read(&filename).expect("Run with --mode prepare-lossless first");
            let img = Decoder::new(&webp).unwrap().decode_rgba().unwrap();
            let (w, h) = (img.width(), img.height());
            let pixel_bytes = img.pixels().len() * 4;
            eprintln!(
                "Decoder builder from {}: {}x{}, {} bytes",
                filename, w, h, pixel_bytes
            );
        }
        _ => {
            eprintln!("Unknown mode: {}", cfg.mode);
        }
    }
}

fn run_sweep() {
    // Quick sweep to collect data for formula fitting
    // Outputs CSV-style data for analysis
    println!("mode,width,height,pixels,method,quality,near_lossless,bpp");
    println!("# Run each line with heaptrack and record peak heap memory");

    let sizes = [128, 256, 384, 512, 768, 1024, 1536, 2048];
    let methods = [0, 2, 4, 6];
    let qualities = [50.0, 75.0, 85.0, 95.0, 100.0];

    for &size in &sizes {
        // Lossy: vary method and quality
        for &method in &methods {
            for &quality in &qualities {
                let pixels = (size as u64) * (size as u64);
                println!(
                    "lossy,{},{},{},{},{},100,4",
                    size, size, pixels, method, quality
                );
            }
        }

        // Lossless: vary method only (quality doesn't apply)
        for &method in &methods {
            let pixels = (size as u64) * (size as u64);
            println!("lossless,{},{},{},{},100,100,4", size, size, pixels, method);
        }

        // Decode: from lossy and lossless sources
        let pixels = (size as u64) * (size as u64);
        println!("decode-lossy,{},{},{},4,85,100,4", size, size, pixels);
        println!("decode-lossless,{},{},{},4,100,100,4", size, size, pixels);
    }
}

fn run_batch() {
    // Run a batch of configurations and print results
    // Use with: ./mem_formula --batch 2>&1 | tee results.txt
    // Then analyze with heaptrack separately

    let sizes = [256, 512, 1024, 2048];
    let methods = [0, 4, 6];

    eprintln!("=== LOSSY ENCODING ===");
    for &size in &sizes {
        for &method in &methods {
            let cfg = Config {
                width: size,
                height: size,
                mode: "lossy".to_string(),
                quality: 85.0,
                method,
                ..Default::default()
            };
            eprintln!("\n--- {}x{} method={} ---", size, size, method);
            run_encode(&cfg);
        }
    }

    eprintln!("\n=== LOSSLESS ENCODING ===");
    for &size in &sizes {
        for &method in &methods {
            let cfg = Config {
                width: size,
                height: size,
                mode: "lossless".to_string(),
                method,
                ..Default::default()
            };
            eprintln!("\n--- {}x{} method={} ---", size, size, method);
            run_encode(&cfg);
        }
    }

    eprintln!("\n=== DECODING ===");
    for &size in &sizes {
        let cfg = Config {
            width: size,
            height: size,
            mode: "decode-lossy".to_string(),
            ..Default::default()
        };
        eprintln!("\n--- {}x{} decode-lossy ---", size, size);
        run_encode(&cfg);

        let cfg = Config {
            width: size,
            height: size,
            mode: "decode-lossless".to_string(),
            ..Default::default()
        };
        eprintln!("\n--- {}x{} decode-lossless ---", size, size);
        run_encode(&cfg);
    }
}

fn print_usage() {
    eprintln!("Usage: mem_formula [OPTIONS]");
    eprintln!();
    eprintln!("Options:");
    eprintln!("  --size <N>           Image size (NxN), default: 512");
    eprintln!("  --width <N>          Image width, default: 512");
    eprintln!("  --height <N>         Image height, default: 512");
    eprintln!("  --mode <MODE>        Encode: lossy, lossless, near-lossless");
    eprintln!("                       Decode (includes encode): decode-lossy, decode-lossless");
    eprintln!("                       Prepare files: prepare-lossy, prepare-lossless");
    eprintln!(
        "                       Decode only (isolate memory): decode-only-lossy, decode-only-lossless"
    );
    eprintln!("  --quality <Q>        Quality 0-100, default: 85");
    eprintln!("  --method <M>         Method 0-6, default: 4");
    eprintln!("  --near-lossless <N>  Near-lossless 0-100, default: 100");
    eprintln!("  --bpp <N>            Bytes per pixel (3=RGB, 4=RGBA), default: 4");
    eprintln!("  --content <TYPE>     Image content: gradient, noise, solid");
    eprintln!("  --sweep              Print CSV of all configs for batch testing");
    eprintln!("  --batch              Run batch of common configurations");
    eprintln!();
    eprintln!("Examples:");
    eprintln!("  heaptrack ./mem_formula --size 1024 --mode lossy --quality 85");
    eprintln!("  heaptrack ./mem_formula --size 1024 --mode lossless --content noise");
    eprintln!("  ./mem_formula --sweep > configs.csv");
}

fn main() {
    let args: Vec<String> = env::args().collect();

    if args.len() < 2 {
        print_usage();
        return;
    }

    // Check for special modes
    if args.contains(&"--sweep".to_string()) {
        run_sweep();
        return;
    }
    if args.contains(&"--batch".to_string()) {
        run_batch();
        io::stderr().flush().unwrap();
        return;
    }
    if args.contains(&"--help".to_string()) || args.contains(&"-h".to_string()) {
        print_usage();
        return;
    }

    // Parse arguments
    let mut cfg = Config::default();
    let mut i = 1;
    while i < args.len() {
        match args[i].as_str() {
            "--size" => {
                i += 1;
                let size: u32 = args[i].parse().unwrap_or(512);
                cfg.width = size;
                cfg.height = size;
            }
            "--width" => {
                i += 1;
                cfg.width = args[i].parse().unwrap_or(512);
            }
            "--height" => {
                i += 1;
                cfg.height = args[i].parse().unwrap_or(512);
            }
            "--mode" => {
                i += 1;
                cfg.mode = args[i].clone();
            }
            "--quality" | "-q" => {
                i += 1;
                cfg.quality = args[i].parse().unwrap_or(85.0);
            }
            "--method" | "-m" => {
                i += 1;
                cfg.method = args[i].parse().unwrap_or(4);
            }
            "--near-lossless" | "--nl" => {
                i += 1;
                cfg.near_lossless = args[i].parse().unwrap_or(100);
            }
            "--bpp" => {
                i += 1;
                cfg.bpp = args[i].parse().unwrap_or(4);
            }
            "--content" => {
                i += 1;
                cfg.content = args[i].clone();
            }
            // Legacy positional args support
            arg if !arg.starts_with('-') => {
                if cfg.width == 512 && cfg.height == 512 {
                    // First positional = size
                    let size: u32 = arg.parse().unwrap_or(512);
                    cfg.width = size;
                    cfg.height = size;
                } else if cfg.mode == "lossy" {
                    // Second positional = mode
                    cfg.mode = arg.to_string();
                }
            }
            _ => {}
        }
        i += 1;
    }

    if cfg.mode.starts_with("time-") {
        run_timing(&cfg);
    } else {
        run_encode(&cfg);
    }

    io::stdout().flush().unwrap();
    io::stderr().flush().unwrap();
}

// Time measurement helper
fn run_timing(cfg: &Config) {
    match cfg.mode.as_str() {
        "time-decode-file" => {
            // Decode timing for arbitrary WebP file specified in --content
            let webp = fs::read(&cfg.content).expect("File not found");
            let info = ImageInfo::from_webp(&webp).unwrap();
            let pixels = info.width as u64 * info.height as u64;

            // Warmup
            for _ in 0..3 {
                let _ = decode_rgba(&webp);
            }

            let iterations = 20;
            let start = std::time::Instant::now();
            for _ in 0..iterations {
                let _ = decode_rgba(&webp);
            }
            let elapsed = start.elapsed();
            let per_op = elapsed / iterations;
            let mpix_per_sec = (pixels as f64 / 1_000_000.0) / per_op.as_secs_f64();

            let name = std::path::Path::new(&cfg.content)
                .file_stem()
                .map(|s| s.to_string_lossy().chars().take(16).collect::<String>())
                .unwrap_or_default();
            eprintln!(
                "{} {}x{}: {:.2}ms, {:.1} Mpix/s",
                name,
                info.width,
                info.height,
                per_op.as_secs_f64() * 1000.0,
                mpix_per_sec
            );
        }
        "time-decode-lossy" | "time-decode-lossless" => {
            let base = if cfg.mode.contains("lossy") {
                "lossy"
            } else {
                "lossless"
            };
            let filename = format!(
                "mem_data/{}x{}_{}{}.webp",
                cfg.width,
                cfg.height,
                base,
                if cfg.content != "gradient" {
                    format!("_{}", cfg.content)
                } else {
                    String::new()
                }
            );
            let webp = fs::read(&filename).expect("Run prepare-* first");

            // Warmup
            for _ in 0..3 {
                let _ = decode_rgba(&webp);
            }

            // Measure
            let iterations = 20;
            let start = std::time::Instant::now();
            for _ in 0..iterations {
                let _ = decode_rgba(&webp);
            }
            let elapsed = start.elapsed();
            let per_op = elapsed / iterations;

            let pixels = cfg.width as u64 * cfg.height as u64;
            let mpix_per_sec = (pixels as f64 / 1_000_000.0) / per_op.as_secs_f64();

            eprintln!(
                "{}x{} {} {}: {:.2}ms, {:.1} Mpix/s",
                cfg.width,
                cfg.height,
                cfg.mode.strip_prefix("time-").unwrap(),
                cfg.content,
                per_op.as_secs_f64() * 1000.0,
                mpix_per_sec
            );
        }
        "time-encode-lossy" | "time-encode-lossless" => {
            let rgba = match cfg.content.as_str() {
                "noise" => generate_noise_rgba(cfg.width, cfg.height, 12345),
                "solid" => generate_solid_rgba(cfg.width, cfg.height),
                _ => generate_gradient_rgba(cfg.width, cfg.height),
            };
            let is_lossless = cfg.mode.contains("lossless");

            // Warmup
            for _ in 0..2 {
                let enc = Encoder::new_rgba(&rgba, cfg.width, cfg.height)
                    .lossless(is_lossless)
                    .method(cfg.method);
                let _ = enc.encode(Unstoppable);
            }

            // Measure (fewer iterations since encode is slower)
            let iterations = if cfg.width >= 1024 { 5 } else { 10 };
            let start = std::time::Instant::now();
            for _ in 0..iterations {
                let enc = Encoder::new_rgba(&rgba, cfg.width, cfg.height)
                    .lossless(is_lossless)
                    .method(cfg.method);
                let _ = enc.encode(Unstoppable);
            }
            let elapsed = start.elapsed();
            let per_op = elapsed / iterations;

            let pixels = cfg.width as u64 * cfg.height as u64;
            let mpix_per_sec = (pixels as f64 / 1_000_000.0) / per_op.as_secs_f64();

            eprintln!(
                "{}x{} {} m{} {}: {:.2}ms, {:.1} Mpix/s",
                cfg.width,
                cfg.height,
                cfg.mode.strip_prefix("time-").unwrap(),
                cfg.method,
                cfg.content,
                per_op.as_secs_f64() * 1000.0,
                mpix_per_sec
            );
        }
        _ => {}
    }
}