webpx 0.1.4

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
//! Memory profiler for webpx encode/decode operations.
//!
//! Run with heaptrack to capture ALL allocations (including libwebp C code):
//! ```
//! heaptrack cargo run --release --all-features --example alloc_profile
//! heaptrack_print heaptrack.alloc_profile.*.zst
//! # Or for GUI: heaptrack_gui heaptrack.alloc_profile.*.zst
//! ```
//!
//! This profiles real memory usage including libwebp's internal allocations.

use rgb::RGBA8;
use std::io::{self, Write};
use std::time::Instant;
use webpx::{
    AnimationDecoder, AnimationEncoder, ColorMode, Decoder, Encoder, StreamingDecoder,
    StreamingEncoder, Unstoppable, decode, decode_append, decode_into, decode_rgba,
    decode_rgba_into,
};

/// Read peak RSS from /proc/self/status (Linux only)
#[cfg(target_os = "linux")]
fn get_peak_rss_kb() -> Option<u64> {
    std::fs::read_to_string("/proc/self/status")
        .ok()?
        .lines()
        .find(|line| line.starts_with("VmHWM:"))?
        .split_whitespace()
        .nth(1)?
        .parse()
        .ok()
}

#[cfg(not(target_os = "linux"))]
fn get_peak_rss_kb() -> Option<u64> {
    None
}

/// Read current RSS from /proc/self/status (Linux only)
#[cfg(target_os = "linux")]
fn get_current_rss_kb() -> Option<u64> {
    std::fs::read_to_string("/proc/self/status")
        .ok()?
        .lines()
        .find(|line| line.starts_with("VmRSS:"))?
        .split_whitespace()
        .nth(1)?
        .parse()
        .ok()
}

#[cfg(not(target_os = "linux"))]
fn get_current_rss_kb() -> Option<u64> {
    None
}

/// Generate a gradient RGBA image for benchmarking.
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
}

/// Generate ARGB u32 data (zero-copy fast path)
fn generate_gradient_argb(width: u32, height: u32) -> Vec<u32> {
    let mut data = Vec::with_capacity((width * height) as usize);
    for y in 0..height {
        for x in 0..width {
            let r = (x * 255) / width.max(1);
            let g = (y * 255) / height.max(1);
            let b = ((x + y) * 127) / (width + height).max(1);
            let pixel = 0xFF000000 | (r << 16) | (g << 8) | b;
            data.push(pixel);
        }
    }
    data
}

struct ProfileResult {
    name: &'static str,
    time_us: u64,
    rss_before_kb: u64,
    rss_after_kb: u64,
}

fn run_profiled<F, R>(name: &'static str, f: F) -> ProfileResult
where
    F: FnOnce() -> R,
{
    // Force cleanup
    std::hint::black_box(());

    let rss_before = get_current_rss_kb().unwrap_or(0);
    let start = Instant::now();
    let _result = std::hint::black_box(f());
    let elapsed = start.elapsed();
    let rss_after = get_current_rss_kb().unwrap_or(0);

    ProfileResult {
        name,
        time_us: elapsed.as_micros() as u64,
        rss_before_kb: rss_before,
        rss_after_kb: rss_after,
    }
}

fn print_result(r: &ProfileResult, pixels: u64) {
    let rss_delta_kb = r.rss_after_kb.saturating_sub(r.rss_before_kb);
    let throughput = (pixels as f64) / (r.time_us as f64 / 1_000_000.0) / 1_000_000.0;

    println!(
        "  {:<45} {:>8} µs  RSS: {:>6} -> {:>6} KB (Δ {:>+6})  {:.1} Mpx/s",
        r.name, r.time_us, r.rss_before_kb, r.rss_after_kb, rss_delta_kb as i64, throughput
    );
}

fn section(title: &str) {
    println!("\n{}", "=".repeat(100));
    println!("{}", title);
    println!("{}", "=".repeat(100));
}

fn profile_encoding(width: u32, height: u32) {
    let pixels = (width * height) as u64;
    section(&format!(
        "ENCODING {}x{} ({} pixels, {} MB input)",
        width,
        height,
        pixels,
        (pixels * 4) / 1_000_000
    ));

    let rgba = generate_gradient_rgba(width, height);
    let argb = generate_gradient_argb(width, height);

    // Different encode output methods
    let r = run_profiled("Encoder::new_rgba().encode() -> Vec", || {
        Encoder::new_rgba(&rgba, width, height)
            .quality(85.0)
            .encode(Unstoppable)
            .unwrap()
    });
    print_result(&r, pixels);

    let r = run_profiled("Encoder::new_rgba().encode_owned() -> WebPData", || {
        Encoder::new_rgba(&rgba, width, height)
            .quality(85.0)
            .encode_owned(Unstoppable)
            .unwrap()
    });
    print_result(&r, pixels);

    let r = run_profiled("Encoder::new_argb().encode() [zero-copy input]", || {
        Encoder::new_argb(&argb, width, height)
            .quality(85.0)
            .encode(Unstoppable)
            .unwrap()
    });
    print_result(&r, pixels);

    let mut output = Vec::with_capacity(width as usize * height as usize);
    let r = run_profiled("Encoder::encode_into() [preallocated output]", || {
        output.clear();
        Encoder::new_rgba(&rgba, width, height)
            .quality(85.0)
            .encode_into(Unstoppable, &mut output)
            .unwrap()
    });
    print_result(&r, pixels);

    // Lossless
    let r = run_profiled("Encoder lossless", || {
        Encoder::new_rgba(&rgba, width, height)
            .lossless(true)
            .encode(Unstoppable)
            .unwrap()
    });
    print_result(&r, pixels);

    // Different methods
    for method in [0, 4, 6] {
        let name = Box::leak(format!("Encoder method={}", method).into_boxed_str());
        let r = run_profiled(name, || {
            Encoder::new_rgba(&rgba, width, height)
                .quality(85.0)
                .method(method)
                .encode(Unstoppable)
                .unwrap()
        });
        print_result(&r, pixels);
    }
}

fn profile_decoding(width: u32, height: u32) {
    let pixels = (width * height) as u64;
    section(&format!(
        "DECODING {}x{} ({} pixels, {} MB output)",
        width,
        height,
        pixels,
        (pixels * 4) / 1_000_000
    ));

    let rgba = generate_gradient_rgba(width, height);
    let webp_lossy = Encoder::new_rgba(&rgba, width, height)
        .quality(85.0)
        .encode(Unstoppable)
        .unwrap();
    let webp_lossless = Encoder::new_rgba(&rgba, width, height)
        .lossless(true)
        .encode(Unstoppable)
        .unwrap();

    println!(
        "  Encoded sizes: lossy={} bytes, lossless={} bytes",
        webp_lossy.len(),
        webp_lossless.len()
    );
    println!();

    // Basic decode -> Vec
    let r = run_profiled("decode_rgba() -> (Vec<u8>, w, h)", || {
        decode_rgba(&webp_lossy).unwrap()
    });
    print_result(&r, pixels);

    // Typed decode -> Vec<RGBA8>
    let r = run_profiled("decode::<RGBA8>() -> (Vec<RGBA8>, w, h)", || {
        decode::<RGBA8>(&webp_lossy).unwrap()
    });
    print_result(&r, pixels);

    // Decode into pre-allocated buffer
    let stride = width * 4;
    let mut buffer = vec![0u8; (stride * height) as usize];
    let r = run_profiled("decode_rgba_into() [preallocated buffer]", || {
        decode_rgba_into(&webp_lossy, &mut buffer, stride).unwrap()
    });
    print_result(&r, pixels);

    // Typed decode into pre-allocated slice
    let mut typed_buffer: Vec<RGBA8> = vec![RGBA8::default(); (width * height) as usize];
    let r = run_profiled("decode_into::<RGBA8>() [preallocated typed]", || {
        decode_into::<RGBA8>(&webp_lossy, &mut typed_buffer, width).unwrap()
    });
    print_result(&r, pixels);

    // Append to existing Vec
    let mut append_buffer: Vec<RGBA8> = Vec::with_capacity((width * height) as usize);
    let r = run_profiled("decode_append::<RGBA8>() [preallocated Vec]", || {
        append_buffer.clear();
        decode_append::<RGBA8>(&webp_lossy, &mut append_buffer).unwrap()
    });
    print_result(&r, pixels);

    // Lossless decode
    let r = run_profiled("decode_rgba() [lossless source]", || {
        decode_rgba(&webp_lossless).unwrap()
    });
    print_result(&r, pixels);

    // Decoder builder
    let r = run_profiled("Decoder::new().decode_rgba_raw()", || {
        Decoder::new(&webp_lossy)
            .unwrap()
            .decode_rgba_raw()
            .unwrap()
    });
    print_result(&r, pixels);
}

fn profile_decoder_transforms(width: u32, height: u32) {
    let pixels = (width * height) as u64;
    section(&format!("DECODER TRANSFORMS {}x{}", width, height));

    let rgba = generate_gradient_rgba(width, height);
    let webp = Encoder::new_rgba(&rgba, width, height)
        .quality(85.0)
        .encode(Unstoppable)
        .unwrap();

    let r = run_profiled("Full decode", || {
        Decoder::new(&webp).unwrap().decode_rgba_raw().unwrap()
    });
    print_result(&r, pixels);

    let scaled_pixels = ((width / 2) * (height / 2)) as u64;
    let r = run_profiled("Decode + scale 50%", || {
        Decoder::new(&webp)
            .unwrap()
            .scale(width / 2, height / 2)
            .decode_rgba_raw()
            .unwrap()
    });
    print_result(&r, scaled_pixels);

    let scaled_pixels_25 = ((width / 4) * (height / 4)) as u64;
    let r = run_profiled("Decode + scale 25%", || {
        Decoder::new(&webp)
            .unwrap()
            .scale(width / 4, height / 4)
            .decode_rgba_raw()
            .unwrap()
    });
    print_result(&r, scaled_pixels_25);

    let crop_w = width / 2;
    let crop_h = height / 2;
    let crop_pixels = (crop_w * crop_h) as u64;
    let r = run_profiled("Decode + crop center 50%", || {
        Decoder::new(&webp)
            .unwrap()
            .crop(width / 4, height / 4, crop_w, crop_h)
            .decode_rgba_raw()
            .unwrap()
    });
    print_result(&r, crop_pixels);
}

fn profile_streaming(width: u32, height: u32) {
    let pixels = (width * height) as u64;
    section(&format!("STREAMING {}x{}", width, height));

    let rgba = generate_gradient_rgba(width, height);
    let webp = Encoder::new_rgba(&rgba, width, height)
        .quality(85.0)
        .encode(Unstoppable)
        .unwrap();

    let r = run_profiled("StreamingDecoder.update() [all at once]", || {
        let mut decoder = StreamingDecoder::new(ColorMode::Rgba).unwrap();
        decoder.update(&webp).unwrap();
        decoder.finish().unwrap()
    });
    print_result(&r, pixels);

    let r = run_profiled("StreamingDecoder.append() [4k chunks]", || {
        let mut decoder = StreamingDecoder::new(ColorMode::Rgba).unwrap();
        for chunk in webp.chunks(4096) {
            let _ = decoder.append(chunk);
        }
        decoder.finish().unwrap()
    });
    print_result(&r, pixels);

    let r = run_profiled("StreamingEncoder.encode_rgba_with_callback()", || {
        let mut output = Vec::new();
        let encoder = StreamingEncoder::new(width, height).unwrap();
        encoder
            .encode_rgba_with_callback(&rgba, |chunk| {
                output.extend_from_slice(chunk);
                Ok(())
            })
            .unwrap();
        output
    });
    print_result(&r, pixels);
}

fn profile_animation(width: u32, height: u32, frame_count: usize) {
    let pixels = (width * height) as u64;
    let total_pixels = pixels * frame_count as u64;
    section(&format!(
        "ANIMATION {}x{} x {} frames ({} total pixels)",
        width, height, frame_count, total_pixels
    ));

    let frames: Vec<Vec<u8>> = (0..frame_count)
        .map(|_| generate_gradient_rgba(width, height))
        .collect();

    let r = run_profiled("AnimationEncoder (encode all frames)", || {
        let mut encoder = AnimationEncoder::new(width, height).unwrap();
        encoder.set_quality(85.0);
        for (i, frame) in frames.iter().enumerate() {
            encoder.add_frame_rgba(frame, (i * 100) as i32).unwrap();
        }
        encoder.finish((frame_count * 100) as i32).unwrap()
    });
    print_result(&r, total_pixels);

    // Create animation for decode test
    let mut encoder = AnimationEncoder::new(width, height).unwrap();
    encoder.set_quality(85.0);
    for (i, frame) in frames.iter().enumerate() {
        encoder.add_frame_rgba(frame, (i * 100) as i32).unwrap();
    }
    let anim_data = encoder.finish((frame_count * 100) as i32).unwrap();
    println!("  Animation size: {} bytes", anim_data.len());

    let r = run_profiled("AnimationDecoder.decode_all()", || {
        let mut decoder = AnimationDecoder::new(&anim_data).unwrap();
        decoder.decode_all().unwrap()
    });
    print_result(&r, total_pixels);

    let r = run_profiled("AnimationDecoder.next_frame() [iterate]", || {
        let mut decoder = AnimationDecoder::new(&anim_data).unwrap();
        let mut frames = Vec::new();
        while let Some(frame) = decoder.next_frame().unwrap() {
            frames.push(frame);
        }
        frames
    });
    print_result(&r, total_pixels);
}

fn main() {
    println!(
        "╔════════════════════════════════════════════════════════════════════════════════════════════════════╗"
    );
    println!(
        "║                              WEBPX MEMORY PROFILER                                                 ║"
    );
    println!(
        "╚════════════════════════════════════════════════════════════════════════════════════════════════════╝"
    );
    println!();
    println!("For accurate allocation tracking, run with heaptrack:");
    println!("  heaptrack cargo run --release --all-features --example alloc_profile");
    println!("  heaptrack_print heaptrack.alloc_profile.*.zst");
    println!();
    println!("RSS values below are from /proc/self/status (approximate, not per-operation)");
    println!();

    if let Some(peak) = get_peak_rss_kb() {
        println!("Initial peak RSS: {} KB", peak);
    }

    // Warm up
    let _ = generate_gradient_rgba(64, 64);

    // Profile at different sizes
    for &(width, height) in &[(256, 256), (512, 512), (1024, 1024)] {
        profile_encoding(width, height);
        profile_decoding(width, height);
    }

    profile_decoder_transforms(1024, 1024);
    profile_streaming(512, 512);
    profile_animation(256, 256, 5);

    println!();
    section("SUMMARY");

    if let Some(peak) = get_peak_rss_kb() {
        println!(
            "Final peak RSS: {} KB ({:.1} MB)",
            peak,
            peak as f64 / 1024.0
        );
    }

    println!();
    println!("For detailed per-allocation data, analyze heaptrack output:");
    println!("  heaptrack_print heaptrack.alloc_profile.*.zst | less");
    println!("  heaptrack_gui heaptrack.alloc_profile.*.zst");

    // Flush to ensure heaptrack captures everything
    io::stdout().flush().unwrap();
}