zenwebp 0.4.2

High-performance WebP encoding and decoding in pure Rust
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
//! Content type classification for auto-preset selection.
//!
//! Analyzes the Y plane and alpha histogram to detect content type
//! (photo, drawing, text, icon) and select appropriate encoding parameters.
//!
//! ## SIMD Optimizations
//!
//! - `compute_edge_density`: SIMD horizontal abs_diff scan

#![allow(dead_code)]

use archmage::prelude::*;

#[cfg(target_arch = "aarch64")]
use archmage::intrinsics::aarch64 as simd_mem;
#[cfg(target_arch = "x86_64")]
use archmage::intrinsics::x86_64 as simd_mem;

/// Detected content type for auto-preset selection.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum ImageContentType {
    /// Natural photograph or complex texture.
    Photo,
    /// Hand or line drawing, screenshot, UI.
    Drawing,
    /// Text-heavy content.
    Text,
    /// Small icon or sprite.
    Icon,
}

/// Diagnostic info from the classifier.
#[derive(Debug, Clone, Copy)]
pub struct ClassifierDiag {
    /// Detected content type.
    pub content_type: ImageContentType,
    /// Fraction of alpha histogram in low quarter (0-63).
    pub low_frac: f32,
    /// Fraction of alpha histogram in high quarter (192-255).
    pub high_frac: f32,
    /// Whether the alpha histogram is bimodal.
    pub is_bimodal: bool,
    /// Fraction of sampled pixels with sharp horizontal transitions.
    pub edge_density: f32,
    /// Fraction of sampled blocks with few distinct Y values.
    pub uniformity: f32,
}

/// Classify image content type from Y plane and alpha histogram.
///
/// This runs after `analyze_image()` and uses the alpha histogram (nearly free)
/// plus a lightweight scan of the Y plane to determine content type.
///
/// Heuristics:
/// 1. Small images (≤128x128) → Icon
/// 2. Bimodal alpha histogram + high edge density + uniform blocks → Text
/// 3. Bimodal alpha histogram + uniform blocks → Drawing (screenshots, UI)
/// 4. Otherwise → Photo
pub fn classify_image_type(
    y_src: &[u8],
    width: usize,
    height: usize,
    y_stride: usize,
    alpha_histogram: &[u32; 256],
) -> ImageContentType {
    classify_image_type_diag(y_src, width, height, y_stride, alpha_histogram).content_type
}

/// Classify with full diagnostic output.
pub fn classify_image_type_diag(
    y_src: &[u8],
    width: usize,
    height: usize,
    y_stride: usize,
    alpha_histogram: &[u32; 256],
) -> ClassifierDiag {
    // 1. Small images → Icon
    if width <= 128 && height <= 128 {
        return ClassifierDiag {
            content_type: ImageContentType::Icon,
            low_frac: 0.0,
            high_frac: 0.0,
            is_bimodal: false,
            edge_density: 0.0,
            uniformity: 0.0,
        };
    }

    // Compute alpha histogram shape
    let total: u32 = alpha_histogram.iter().sum();
    if total == 0 {
        return ClassifierDiag {
            content_type: ImageContentType::Photo,
            low_frac: 0.0,
            high_frac: 0.0,
            is_bimodal: false,
            edge_density: 0.0,
            uniformity: 0.0,
        };
    }

    // Check if histogram is bimodal: significant mass at both ends
    // Low alpha = flat/simple regions, high alpha = textured regions
    let low_quarter: u32 = alpha_histogram[..64].iter().sum();
    let high_quarter: u32 = alpha_histogram[192..].iter().sum();
    let low_frac = low_quarter as f32 / total as f32;
    let high_frac = high_quarter as f32 / total as f32;
    let is_bimodal = low_frac > 0.15 && high_frac > 0.15;

    // 2. Compute edge density from Y plane
    // Sample every 16th row, count sharp horizontal transitions
    let edge_density = compute_edge_density(y_src, width, height, y_stride);

    // 3. Compute color uniformity: count distinct Y values in sampled blocks
    let uniformity = compute_color_uniformity(y_src, width, height, y_stride);

    // Classification logic: uniformity-based approach.
    // High uniformity (many flat blocks) → Photo tuning (SNS=80, lighter filter)
    // Low uniformity (complex textures) → Default tuning (SNS=50, stronger filter)
    //
    // Empirically, Drawing/Text presets produce larger files than Default on all
    // tested corpora (CID22, gb82-sc screenshots). Photo preset benefits images
    // with large uniform regions (screenshots, graphics, and clean photos).
    let content_type = if uniformity >= 0.45 {
        ImageContentType::Photo
    } else {
        ImageContentType::Drawing // "complex content" — uses Default tuning values
    };

    ClassifierDiag {
        content_type,
        low_frac,
        high_frac,
        is_bimodal,
        edge_density,
        uniformity,
    }
}

/// Compute edge density by scanning the Y plane for sharp horizontal transitions.
/// Returns fraction of sampled pixels that are sharp edges (0.0 to 1.0).
fn compute_edge_density(y_src: &[u8], width: usize, height: usize, y_stride: usize) -> f32 {
    incant!(
        compute_edge_density_impl(y_src, width, height, y_stride),
        [v3, neon, scalar]
    )
}

#[cfg(target_arch = "x86_64")]
#[cfg(target_arch = "x86_64")]
#[inline(always)]
fn compute_edge_density_impl_v3(
    token: X64V3Token,
    y_src: &[u8],
    width: usize,
    height: usize,
    y_stride: usize,
) -> f32 {
    compute_edge_density_sse2(token, y_src, width, height, y_stride)
}

#[cfg(target_arch = "aarch64")]
#[inline(always)]
fn compute_edge_density_impl_neon(
    token: NeonToken,
    y_src: &[u8],
    width: usize,
    height: usize,
    y_stride: usize,
) -> f32 {
    compute_edge_density_neon(token, y_src, width, height, y_stride)
}

#[inline(always)]
fn compute_edge_density_impl_scalar(
    _token: ScalarToken,
    y_src: &[u8],
    width: usize,
    height: usize,
    y_stride: usize,
) -> f32 {
    compute_edge_density_scalar(y_src, width, height, y_stride)
}

/// Scalar implementation of edge density computation.
fn compute_edge_density_scalar(y_src: &[u8], width: usize, height: usize, y_stride: usize) -> f32 {
    if width < 2 || height < 16 {
        return 0.0;
    }

    let mut edge_count = 0u32;
    let mut sample_count = 0u32;
    let threshold = 32u8;

    let mut y = 0;
    while y < height {
        let row = &y_src[y * y_stride..][..width];
        for x in 1..width {
            let diff = row[x].abs_diff(row[x - 1]);
            if diff > threshold {
                edge_count += 1;
            }
            sample_count += 1;
        }
        y += 16;
    }

    if sample_count == 0 {
        return 0.0;
    }
    edge_count as f32 / sample_count as f32
}

// compute_edge_density_dispatch removed — replaced by incant! in compute_edge_density

/// SSE2 edge density: Process 16 pixels at a time.
#[cfg(target_arch = "x86_64")]
#[arcane]
fn compute_edge_density_sse2(
    _token: X64V3Token,
    y_src: &[u8],
    width: usize,
    height: usize,
    y_stride: usize,
) -> f32 {
    if width < 2 || height < 16 {
        return 0.0;
    }

    let mut edge_count = 0u32;
    let mut sample_count = 0u32;
    let threshold_vec = _mm_set1_epi8(32i8);

    let mut y = 0;
    while y < height {
        let row = &y_src[y * y_stride..];

        // Process 16 pixels at a time (comparing pixels x and x-1)
        let mut x = 1usize;
        while x + 15 < width {
            // Load pixels at positions [x, x+1, ..., x+15] and [x-1, x, ..., x+14]
            let curr_arr = <&[u8; 16]>::try_from(&row[x..x + 16]).unwrap();
            let prev_arr = <&[u8; 16]>::try_from(&row[x - 1..x + 15]).unwrap();
            let curr = simd_mem::_mm_loadu_si128(curr_arr);
            let prev = simd_mem::_mm_loadu_si128(prev_arr);

            // Compute |curr - prev| using saturating sub both ways
            let diff1 = _mm_subs_epu8(curr, prev);
            let diff2 = _mm_subs_epu8(prev, curr);
            let abs_diff = _mm_or_si128(diff1, diff2);

            // Compare: abs_diff > threshold
            // Subtract (threshold+1) and check for non-zero (if >= 33, result is non-zero)
            let above_thresh = _mm_subs_epu8(abs_diff, threshold_vec);
            // Convert to 0xFF where above threshold
            let zero = _mm_setzero_si128();
            let mask = _mm_cmpeq_epi8(above_thresh, zero);
            // Invert: we want 0xFF where above threshold (mask is 0xFF where NOT above)
            let edges = _mm_andnot_si128(mask, _mm_set1_epi8(-1i8));

            // Count set bytes (each edge pixel has 0xFF)
            let mask_bits = _mm_movemask_epi8(edges) as u32;
            edge_count += mask_bits.count_ones();
            sample_count += 16;

            x += 16;
        }

        // Handle remaining pixels with scalar
        while x < width {
            let diff = row[x].abs_diff(row[x - 1]);
            if diff > 32 {
                edge_count += 1;
            }
            sample_count += 1;
            x += 1;
        }

        y += 16;
    }

    if sample_count == 0 {
        return 0.0;
    }
    edge_count as f32 / sample_count as f32
}

// =============================================================================
// NEON (aarch64) edge density
// =============================================================================

// compute_edge_density_neon_dispatch removed — replaced by incant! in compute_edge_density

/// NEON edge density: Process 16 pixels at a time.
#[cfg(target_arch = "aarch64")]
#[arcane]
fn compute_edge_density_neon(
    _token: NeonToken,
    y_src: &[u8],
    width: usize,
    height: usize,
    y_stride: usize,
) -> f32 {
    if width < 2 || height < 16 {
        return 0.0;
    }

    let mut edge_count = 0u32;
    let mut sample_count = 0u32;
    let threshold_vec = vdupq_n_u8(32);

    let mut y = 0;
    while y < height {
        let row = &y_src[y * y_stride..];

        // Process 16 pixels at a time (comparing pixels x and x-1)
        let mut x = 1usize;
        while x + 15 < width {
            // Load pixels at positions [x, x+15] and [x-1, x+14]
            let curr = simd_mem::vld1q_u8(<&[u8; 16]>::try_from(&row[x..x + 16]).unwrap());
            let prev = simd_mem::vld1q_u8(<&[u8; 16]>::try_from(&row[x - 1..x + 15]).unwrap());

            // Compute |curr - prev| using absolute difference
            let abs_diff = vabdq_u8(curr, prev);

            // Compare: abs_diff > threshold
            // vcgtq_u8 returns 0xFF for lanes where abs_diff > threshold
            let above_thresh = vcgtq_u8(abs_diff, threshold_vec);

            // Count set bytes: each edge pixel has 0xFF, AND with 1 gives 0 or 1
            // Use horizontal add after masking with 1
            let ones = vandq_u8(above_thresh, vdupq_n_u8(1));

            // Sum all the 1s: vaddlvq_u8 sums all u8 lanes into a u16
            edge_count += vaddlvq_u8(ones) as u32;
            sample_count += 16;

            x += 16;
        }

        // Handle remaining pixels with scalar
        while x < width {
            let diff = row[x].abs_diff(row[x - 1]);
            if diff > 32 {
                edge_count += 1;
            }
            sample_count += 1;
            x += 1;
        }

        y += 16;
    }

    if sample_count == 0 {
        return 0.0;
    }
    edge_count as f32 / sample_count as f32
}

/// Compute color uniformity by sampling 16x16 blocks and measuring Y value spread.
/// Returns fraction of blocks that are "uniform" (low Y variance), 0.0 to 1.0.
fn compute_color_uniformity(y_src: &[u8], width: usize, height: usize, y_stride: usize) -> f32 {
    let mb_w = width / 16;
    let mb_h = height / 16;
    if mb_w == 0 || mb_h == 0 {
        return 0.0;
    }

    let mut uniform_count = 0u32;
    let mut total_blocks = 0u32;

    // Sample every 4th macroblock in both dimensions
    let mut mby = 0;
    while mby < mb_h {
        let mut mbx = 0;
        while mbx < mb_w {
            // Count distinct Y values in this 16x16 block
            let mut seen = [false; 256];
            let mut distinct = 0u32;
            for dy in 0..16 {
                let row_y = mby * 16 + dy;
                if row_y >= height {
                    break;
                }
                let row = &y_src[row_y * y_stride..];
                for dx in 0..16 {
                    let col_x = mbx * 16 + dx;
                    if col_x >= width {
                        break;
                    }
                    let val = row[col_x] as usize;
                    if !seen[val] {
                        seen[val] = true;
                        distinct += 1;
                    }
                }
            }

            // A block with few distinct values is "uniform"
            // Screenshots/drawings typically have <32 distinct values per block
            if distinct <= 32 {
                uniform_count += 1;
            }
            total_blocks += 1;

            mbx += 4;
        }
        mby += 4;
    }

    if total_blocks == 0 {
        return 0.0;
    }
    uniform_count as f32 / total_blocks as f32
}

/// Get tuning parameters for a detected content type.
/// Returns (sns_strength, filter_strength, filter_sharpness, num_segments).
pub fn content_type_to_tuning(content_type: ImageContentType) -> (u8, u8, u8, u8) {
    match content_type {
        ImageContentType::Photo => (80, 30, 3, 4), // Photo preset: high SNS for uniform regions
        ImageContentType::Drawing => (50, 60, 0, 4), // Default tuning: moderate SNS, strong filter
        ImageContentType::Text => (50, 60, 0, 4), // Default tuning (Text preset was counterproductive)
        ImageContentType::Icon => (0, 0, 0, 4),   // Icon preset: no SNS, no filter
    }
}