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
# WebP Memory Usage Guide

This document describes the memory consumption patterns of libwebp encoding and decoding
operations, based on empirical measurements using heaptrack with libwebp 1.5.

## Quick Reference

### Encoding Memory Formulas

**Lossy Encoding:**
```
min = 115 KB + pixels × 13.4 bytes     (methods 0-2)
min = 220 KB + pixels × 13.7 bytes     (methods 3-6)
typ = min × 1.2                        (real photos)
max = min × 2.25                       (high-entropy/noise)
```

**Lossless Encoding:**
```
Method 0:
  base = 600 KB + pixels × 24 bytes
  min = base × 0.6    (solid color)
  typ = base × 1.2    (real photos)
  max = base × 1.5    (noise)

Methods 1-6:
  base = 1.5 MB + pixels × 34 bytes
  min = base × 0.6    (solid color)
  typ = base × 1.2    (real photos)
  max = base × 1.5    (noise)
```

### Quick Estimates

| Size | Lossy Typ | Lossy Max | Lossless M0 Typ | Lossless M4 Typ |
|------|-----------|-----------|-----------------|-----------------|
| 512×512 | 4.3 MB | 8.1 MB | 8.0 MB | 11.8 MB |
| 1024×1024 | 17.0 MB | 31.5 MB | 31.0 MB | 44.6 MB |
| 1920×1080 | 33.5 MB | 62.0 MB | 61.0 MB | 87.8 MB |
| 2048×2048 | 66.1 MB | 124.0 MB | 121.6 MB | 175.0 MB |
| 4096×4096 | 262.0 MB | 491.0 MB | 482.0 MB | 693.0 MB |

---

## Measurement Methodology

All measurements were collected using:
- **Tool:** heaptrack (intercepts all malloc/free including C library calls)
- **Library:** libwebp 1.5 via libwebp-sys
- **Platform:** Linux x86_64
- **Test images:** Synthetic (gradient, noise, solid) and real photos (CLIC2025)

### Why heaptrack?

Tools like Rust's dhat only intercept the Rust global allocator. Since libwebp is a C
library, its internal allocations would be missed. heaptrack uses LD_PRELOAD to intercept
all memory allocations system-wide, giving accurate measurements of actual peak memory.

---

## Lossy Encoding

### Method Impact

Method (0-6) has minimal impact on lossy memory usage (~3% variation):

| Size | Method 0-2 | Method 3-6 | Difference |
|------|------------|------------|------------|
| 512×512 | 3.46 MB | 3.58 MB | +3.5% |
| 1024×1024 | 13.52 MB | 14.01 MB | +3.6% |
| 2048×2048 | 53.73 MB | 55.06 MB | +2.5% |

**Formula (gradient baseline):**
```
Methods 0-2: peak = 115 KB + pixels × 13.4 bytes
Methods 3-6: peak = 220 KB + pixels × 13.7 bytes
```

### Content Type Impact (Major Finding)

Content complexity dramatically affects lossy encoding memory:

| Size | Gradient | Noise | Solid | Noise/Gradient |
|------|----------|-------|-------|----------------|
| 512×512 | 3.58 MB | 7.97 MB | 7.50 MB | 2.23× |
| 1024×1024 | 14.01 MB | 31.56 MB | 27.37 MB | 2.25× |

- **Gradient:** Best case (smooth color transitions)
- **Noise:** Worst case (2.25× more memory)
- **Solid:** Also high (1.95×, triggers different code paths)

### Real Photo Validation

Testing against CLIC2025 competition images:

| Image | Pixels | Est Typ | Actual | Error |
|-------|--------|---------|--------|-------|
| Photo 1 | 2.79M | 46.05 MB | 48.02 MB | -4% |
| Photo 2 | 2.79M | 46.05 MB | 39.79 MB | +16% |
| Photo 3 | 2.79M | 46.05 MB | 45.74 MB | +1% |
| Photo 4 | 2.79M | 46.05 MB | 48.93 MB | -6% |
| Photo 5 | 2.49M | 41.21 MB | 38.45 MB | +7% |

Real photos average ~1.17× the gradient baseline, so typical estimate uses 1.2× multiplier.

### Final Lossy Formulas

```
base = 220 KB + pixels × 13.7 bytes    (method 3-6, conservative)

min = base × 1.0     Best case: smooth gradients
typ = base × 1.2     Typical: real photographs
max = base × 2.25    Worst case: noise, high-entropy
```

---

## Lossless Encoding

### Method Impact (Significant)

Unlike lossy, method has major impact on lossless memory:

| Size | M0 | M1-2 | M3-6 | M0 vs M4 |
|------|-----|------|------|----------|
| 512×512 | 6.98 MB | 9.51 MB | 16.09 MB | -57% |
| 1024×1024 | 24.51 MB | 35.53 MB | 35.54 MB | -31% |
| 2048×2048 | 97.66 MB | 137.52 MB | 137.52 MB | -29% |

**Key finding:** Method 0 uses 30-57% less memory than methods 1+.

At large sizes (1024+), methods 1-6 converge to similar memory usage.

### Formulas by Method

**Method 0 (fastest, least memory):**
```
base = 600 KB + pixels × 24 bytes
```

**Methods 1-6 (better compression, more memory):**
```
base = 1.5 MB + pixels × 34 bytes
```

### Content Type Impact

| Size | Solid | Gradient | Noise | Solid/Gradient |
|------|-------|----------|-------|----------------|
| 512×512 | 5.98 MB | 16.09 MB | 19.06 MB | 0.37× |
| 1024×1024 | 21.36 MB | 35.54 MB | 49.42 MB | 0.60× |

- **Solid:** Best case (0.6× of gradient)
- **Gradient:** Baseline
- **Noise:** Worst case (1.4× of gradient)

### Final Lossless Formulas

**Method 0:**
```
base = 600 KB + pixels × 24 bytes

min = base × 0.6     Best case: solid color
typ = base × 1.2     Typical: real photographs
max = base × 1.5     Worst case: noise
```

**Methods 1-6:**
```
base = 1.5 MB + pixels × 34 bytes

min = base × 0.6     Best case: solid color
typ = base × 1.2     Typical: real photographs
max = base × 1.5     Worst case: noise
```

---

## Input Format Impact

### RGB vs RGBA

**No measurable difference.** libwebp internally converts to a common format regardless
of whether input is RGB (3 bytes/pixel) or RGBA (4 bytes/pixel).

| Size | RGB Input | RGBA Input |
|------|-----------|------------|
| 512×512 | 3.58 MB | 3.58 MB |
| 1024×1024 | 14.01 MB | 14.01 MB |
| 2048×2048 | 55.06 MB | 55.06 MB |

---

## Quality Impact

Quality setting (0-100) has negligible impact on peak memory for lossy encoding.
The memory is dominated by internal buffers sized for the image dimensions, not
the compression parameters.

---

## Decoding Memory

### Key Findings

**Decode memory is simpler than encode:**
- Lossy and lossless have nearly identical memory usage (~15 bytes/pixel)
- Content type has minimal impact (~5% variation, vs 2.25× for encode)
- Primary cost is output buffer + internal YUV/color conversion workspace

### Decoding Memory Formula

```
Lossy:    peak = 76 KB + pixels × 15 bytes
Lossless: peak = 133 KB + pixels × 15 bytes
```

### Quick Decode Estimates

| Size | Pixels | Lossy | Lossless | Output Buffer |
|------|--------|-------|----------|---------------|
| 256×256 | 65K | 1.06 MB | 1.11 MB | 0.26 MB |
| 512×512 | 262K | 4.01 MB | 4.08 MB | 1.0 MB |
| 1024×1024 | 1M | 15.81 MB | 15.91 MB | 4.0 MB |
| 1920×1080 | 2.1M | 31.2 MB | 31.3 MB | 8.3 MB |
| 2048×2048 | 4.2M | 63.02 MB | 63.22 MB | 16.8 MB |

### API Variants

| API | Memory | Notes |
|-----|--------|-------|
| `decode_rgba()` | Full formula | Standard decode, allocates output |
| `decode_rgba_into()` | Formula − output buffer | Zero-copy for lossy (saves ~4 bytes/pixel) |
| `Decoder::new().decode_rgba()` | Full formula | Builder API, same as decode_rgba() |

**Zero-copy savings:**
- Lossy: `decode_into` saves exactly the output buffer (pixels × 4 bytes for RGBA)
- Lossless: Internal VP8L allocations dominate; minimal savings observed

### Content Type Impact

Unlike encoding, content type has minimal impact on decode memory:

| Size | Gradient | Noise | Solid | Noise/Gradient |
|------|----------|-------|-------|----------------|
| 1024×1024 | 15.81 MB | 16.59 MB | 15.81 MB | 1.05× |

Decode memory varies only ~5% with content type, compared to 2.25× for lossy encoding.

### Decode vs Encode Comparison

At 1024×1024:
| Operation | Min | Typical | Max |
|-----------|-----|---------|-----|
| Lossy decode | 15.8 MB | 15.8 MB | 16.6 MB |
| Lossy encode | 14.0 MB | 16.8 MB | 31.5 MB |
| Lossless decode | 15.9 MB | 15.9 MB | 16.7 MB |
| Lossless encode (M0) | 14.7 MB | 29.4 MB | 36.8 MB |
| Lossless encode (M4) | 21.3 MB | 42.6 MB | 53.3 MB |

---

## CPU Timing

### Timing Methodology

Timing measurements use simple `Instant::now()` with warmup iterations and multiple samples.
Content type has a **dramatic impact** on CPU time, much more than on memory.

### Decode Timing (1024×1024)

| Content | Lossy Source | Lossless Source |
|---------|--------------|-----------------|
| Solid | 3.7ms (284 Mpix/s) | 6.7ms (156 Mpix/s) |
| Gradient | 8.4ms (125 Mpix/s) | 7.0ms (150 Mpix/s) |
| **Real photos** | **11-14ms (75-95 Mpix/s)** | **17-19ms (55-62 Mpix/s)** |
| Noise | 35ms (30 Mpix/s) | 15ms (71 Mpix/s) |

**Key finding:** Content type causes up to **4× variation** in lossy decode time!

### Encode Timing (1024×1024 Method 4)

| Content | Lossy | Lossless |
|---------|-------|----------|
| Solid | 29ms (37 Mpix/s) | 7ms (148 Mpix/s) |
| Gradient | 25ms (43 Mpix/s) | 135ms (7.8 Mpix/s) |
| **Real photos** | **65-83ms (13-16 Mpix/s)** | **327-355ms (3.4 Mpix/s)** |
| Noise | 117ms (9 Mpix/s) | 86ms (12 Mpix/s) |

### Throughput Summary

Throughput in megapixels per second (higher = faster):

**Decode Throughput:**

| Content | Mpix/s |
|---------|--------|
| Solid (best) | ~200 |
| Real photos (typical) | ~100 |
| Noise (worst) | ~30 |

**Lossy Encode Throughput (by method):**

| Method | Mpix/s |
|--------|--------|
| M0 | ~160 |
| M1-2 | 106-128 |
| M3-4 | ~40 |
| M5-6 | ~37 |

Content impact: Real photos ~15 Mpix/s, noise ~9 Mpix/s at M4.

**Lossless Encode Throughput (by method):**

| Method | Mpix/s |
|--------|--------|
| M0 | ~80 |
| M1-4 | ~6-8 |
| M5-6 | ~4 |

Content impact: Solid ~150 Mpix/s, real photos ~3 Mpix/s at M4.

---

## API Usage

### Getting Encode Estimates

```rust
use webpx::heuristics::estimate_encode;
use webpx::EncoderConfig;

let config = EncoderConfig::default()
    .quality(85.0)
    .method(4);

let est = estimate_encode(1920, 1080, 4, &config);

println!("Estimates for 1920×1080 lossy encode:");
println!("  Memory (typical): {:.1} MB", est.peak_memory_bytes as f64 / 1e6);
println!("  Time (typical):   {:.0} ms", est.time_ms);
println!("  Time range:       {:.0}-{:.0} ms",
    est.time_ms_min, est.time_ms_max);
```

### Getting Decode Estimates

```rust
use webpx::heuristics::{estimate_decode, estimate_decode_zerocopy};

// Standard decode (allocates output buffer)
let est = estimate_decode(1920, 1080, 4);

println!("Decode estimates for 1920×1080:");
println!("  Output buffer: {:.1} MB", est.output_bytes as f64 / 1e6);
println!("  Peak memory:   {:.1} MB", est.peak_memory_bytes as f64 / 1e6);
println!("  Time (typical): {:.0} ms", est.time_ms);
println!("  Time range:     {:.0}-{:.0} ms",
    est.time_ms_min, est.time_ms_max);

// Zero-copy decode (caller provides output buffer)
let est_zc = estimate_decode_zerocopy(1920, 1080);
println!("  Zero-copy peak: {:.1} MB", est_zc.peak_memory_bytes as f64 / 1e6);
```

### Choosing the Right Estimate

- **Memory budgeting:** Use `peak_memory_bytes_max` for safety
- **Time budgeting:** Use `time_ms_max` for worst case
- **Progress bars:** Use `time_ms` (typical)
- **Capacity planning:** Use typical estimates with 20% headroom

---

## Recommendations

### For Memory-Constrained Environments

1. **Use lossy encoding** - 2-3× less memory than lossless
2. **Use method 0 for lossless** - 30-57% less memory than method 4+
3. **Process in tiles** for very large images
4. **Pre-allocate** based on `peak_memory_bytes_max`

### For Throughput Optimization

1. **Method has minimal impact** on lossy memory, choose based on quality/speed
2. **Batch similar-sized images** to reuse allocator pools
3. **Consider streaming API** for memory-constrained scenarios

---

## Raw Measurement Data

### Lossy Encoding by Method (gradient images)

| Size | M0 | M1 | M2 | M3 | M4 | M5 | M6 |
|------|-----|-----|-----|-----|-----|-----|-----|
| 512×512 | 3.46 | 3.46 | 3.46 | 3.58 | 3.58 | 3.58 | 3.58 |
| 1024×1024 | 13.52 | 13.52 | 13.52 | 14.01 | 14.01 | 14.01 | 14.01 |
| 2048×2048 | 53.73 | 53.73 | 53.73 | 55.06 | 55.06 | 55.06 | 55.06 |

### Lossless Encoding by Method (gradient images)

| Size | M0 | M1 | M2 | M3 | M4 | M5 | M6 |
|------|------|------|------|------|------|------|------|
| 512×512 | 6.98 | 9.51 | 9.51 | 16.09 | 16.09 | 16.22 | 16.09 |
| 1024×1024 | 24.51 | 35.53 | 35.54 | 35.54 | 35.54 | 36.59 | 36.06 |
| 2048×2048 | 97.66 | 137.52 | 137.52 | 137.52 | 137.52 | 139.62 | 137.52 |

---

## References

- [libwebp documentation]https://developers.google.com/speed/webp/docs/api
- [heaptrack]https://github.com/KDE/heaptrack - heap memory profiler
- Measurements: `examples/mem_formula.rs`, `examples/real_image_test.rs`