linear-srgb 0.6.12

Fast linear↔sRGB color space conversion with FMA acceleration and LUT 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
//! Brute-force accuracy sweep of every f32 in [0, 1] across all code paths.
//!
//! Tests against both IEC f64 reference and moxcms C0 f64 reference.
//! Run with: cargo run --release --example brute_force_accuracy --features "std alt"

fn main() {
    println!("═══════════════════════════════════════════════════════════════════════");
    println!("  Brute-force accuracy: every f32 in [0, 1]");
    println!("═══════════════════════════════════════════════════════════════════════\n");

    // =========================================================================
    // sRGB → Linear sweep
    // =========================================================================

    println!("───────────────────────────────────────────────────────────────────────");
    println!("  sRGB → Linear: every f32 in [0.0, 1.0]");
    println!("───────────────────────────────────────────────────────────────────────\n");

    let mut s2l_stats = SweepStats::new("default (rat.poly + IEC thresh)");
    let mut s2l_precise_stats = SweepStats::new("precise (powf + moxcms thresh)");
    let mut s2l_default_vs_iec = SweepStats::new("default vs IEC f64 ref");
    let mut s2l_default_vs_mox = SweepStats::new("default vs moxcms f64 ref");
    let mut s2l_precise_vs_iec = SweepStats::new("precise vs IEC f64 ref");
    let mut s2l_precise_vs_mox = SweepStats::new("precise vs moxcms f64 ref");

    // Also: what happens at the moxcms threshold for the default path?
    let mut s2l_default_disc_at_iec = DiscontinuityTracker::new("default at IEC threshold");
    let mut s2l_default_disc_at_mox = DiscontinuityTracker::new("default at moxcms threshold");
    let mut s2l_precise_disc_at_iec = DiscontinuityTracker::new("precise at IEC threshold");
    let mut s2l_precise_disc_at_mox = DiscontinuityTracker::new("precise at moxcms threshold");

    let iec_gamma_thresh: f32 = 0.04045;
    let mox_gamma_thresh: f32 = (12.92 * 0.003_041_282_560_127_521_f64) as f32;

    let mut v = 0.0_f32;
    let mut count = 0u64;
    while v <= 1.0 {
        let vf64 = v as f64;

        // Reference values
        let iec_ref = s2l_iec_f64(vf64);
        let mox_ref = s2l_mox_f64(vf64);

        // Code path values
        let default_val = s2l_default(v);
        let precise_val = s2l_precise(v);

        // Track accuracy vs both references
        s2l_default_vs_iec.update(v, default_val, iec_ref);
        s2l_default_vs_mox.update(v, default_val, mox_ref);
        s2l_precise_vs_iec.update(v, precise_val, iec_ref);
        s2l_precise_vs_mox.update(v, precise_val, mox_ref);

        // Track discontinuity at thresholds
        s2l_default_disc_at_iec.update(v, default_val, iec_gamma_thresh);
        s2l_default_disc_at_mox.update(v, default_val, mox_gamma_thresh);
        s2l_precise_disc_at_iec.update(v, precise_val, iec_gamma_thresh);
        s2l_precise_disc_at_mox.update(v, precise_val, mox_gamma_thresh);

        // Self-consistency
        s2l_stats.update(v, default_val, precise_val as f64);
        s2l_precise_stats.update(v, precise_val, iec_ref);

        count += 1;
        v = next_f32(v);
    }

    println!("  Swept {} f32 values.\n", count);

    println!("  Accuracy vs f64 reference (full [0,1] range):");
    println!(
        "  {:50} {:>12} {:>12} {:>14}",
        "", "max ULP", "avg ULP", "max abs err"
    );
    println!(
        "  {:50} {:>12} {:>12} {:>14}",
        "".repeat(50),
        "".repeat(12),
        "".repeat(12),
        "".repeat(14)
    );
    s2l_default_vs_iec.print_summary();
    s2l_default_vs_mox.print_summary();
    s2l_precise_vs_iec.print_summary();
    s2l_precise_vs_mox.print_summary();

    println!("\n  Where max errors occur:");
    s2l_default_vs_iec.print_worst();
    s2l_default_vs_mox.print_worst();
    s2l_precise_vs_iec.print_worst();
    s2l_precise_vs_mox.print_worst();

    println!("\n  Discontinuity at thresholds (jump between adjacent f32 values):");
    s2l_default_disc_at_iec.print();
    s2l_default_disc_at_mox.print();
    s2l_precise_disc_at_iec.print();
    s2l_precise_disc_at_mox.print();

    // =========================================================================
    // Linear → sRGB sweep
    // =========================================================================

    println!("\n───────────────────────────────────────────────────────────────────────");
    println!("  Linear → sRGB: every f32 in [0.0, 1.0]");
    println!("───────────────────────────────────────────────────────────────────────\n");

    let mut l2s_default_vs_iec = SweepStats::new("default vs IEC f64 ref");
    let mut l2s_default_vs_mox = SweepStats::new("default vs moxcms f64 ref");
    let mut l2s_precise_vs_iec = SweepStats::new("precise vs IEC f64 ref");
    let mut l2s_precise_vs_mox = SweepStats::new("precise vs moxcms f64 ref");

    let iec_linear_thresh: f32 = 0.003_130_8;
    let mox_linear_thresh: f32 = 0.003_041_282_560_127_521_f64 as f32;

    let mut l2s_default_disc_at_iec = DiscontinuityTracker::new("default at IEC threshold");
    let mut l2s_default_disc_at_mox = DiscontinuityTracker::new("default at moxcms threshold");
    let mut l2s_precise_disc_at_iec = DiscontinuityTracker::new("precise at IEC threshold");
    let mut l2s_precise_disc_at_mox = DiscontinuityTracker::new("precise at moxcms threshold");

    let mut v = 0.0_f32;
    count = 0;
    while v <= 1.0 {
        let vf64 = v as f64;

        let iec_ref = l2s_iec_f64(vf64);
        let mox_ref = l2s_mox_f64(vf64);

        let default_val = l2s_default(v);
        let precise_val = l2s_precise(v);

        l2s_default_vs_iec.update(v, default_val, iec_ref);
        l2s_default_vs_mox.update(v, default_val, mox_ref);
        l2s_precise_vs_iec.update(v, precise_val, iec_ref);
        l2s_precise_vs_mox.update(v, precise_val, mox_ref);

        l2s_default_disc_at_iec.update(v, default_val, iec_linear_thresh);
        l2s_default_disc_at_mox.update(v, default_val, mox_linear_thresh);
        l2s_precise_disc_at_iec.update(v, precise_val, iec_linear_thresh);
        l2s_precise_disc_at_mox.update(v, precise_val, mox_linear_thresh);

        count += 1;
        v = next_f32(v);
    }

    println!("  Swept {} f32 values.\n", count);

    println!("  Accuracy vs f64 reference (full [0,1] range):");
    println!(
        "  {:50} {:>12} {:>12} {:>14}",
        "", "max ULP", "avg ULP", "max abs err"
    );
    println!(
        "  {:50} {:>12} {:>12} {:>14}",
        "".repeat(50),
        "".repeat(12),
        "".repeat(12),
        "".repeat(14)
    );
    l2s_default_vs_iec.print_summary();
    l2s_default_vs_mox.print_summary();
    l2s_precise_vs_iec.print_summary();
    l2s_precise_vs_mox.print_summary();

    println!("\n  Where max errors occur:");
    l2s_default_vs_iec.print_worst();
    l2s_default_vs_mox.print_worst();
    l2s_precise_vs_iec.print_worst();
    l2s_precise_vs_mox.print_worst();

    println!("\n  Discontinuity at thresholds (jump between adjacent f32 values):");
    l2s_default_disc_at_iec.print();
    l2s_default_disc_at_mox.print();
    l2s_precise_disc_at_iec.print();
    l2s_precise_disc_at_mox.print();

    // =========================================================================
    // Verdict
    // =========================================================================

    println!("\n═══════════════════════════════════════════════════════════════════════");
    println!("  Verdict");
    println!("═══════════════════════════════════════════════════════════════════════\n");

    println!("  The rational polynomial was fitted to the IEC curve, so it should be");
    println!("  measured against the IEC f64 reference. The precise:: powf path uses");
    println!("  moxcms C0 constants, so it should be measured against the moxcms ref.");
    println!();
    println!("  Key numbers:");
    println!(
        "    s2l default vs IEC:    max {} ULP  (correct reference)",
        s2l_default_vs_iec.max_ulp
    );
    println!(
        "    s2l precise vs moxcms: max {} ULP  (correct reference)",
        s2l_precise_vs_mox.max_ulp
    );
    println!(
        "    l2s default vs IEC:    max {} ULP  (correct reference)",
        l2s_default_vs_iec.max_ulp
    );
    println!(
        "    l2s precise vs moxcms: max {} ULP  (correct reference)",
        l2s_precise_vs_mox.max_ulp
    );
    println!();
    println!("  Cross-reference (mismatched constants — not bugs, just different curves):");
    println!(
        "    s2l default vs moxcms: max {} ULP",
        s2l_default_vs_mox.max_ulp
    );
    println!(
        "    s2l precise vs IEC:    max {} ULP",
        s2l_precise_vs_iec.max_ulp
    );
    println!(
        "    l2s default vs moxcms: max {} ULP",
        l2s_default_vs_mox.max_ulp
    );
    println!(
        "    l2s precise vs IEC:    max {} ULP",
        l2s_precise_vs_iec.max_ulp
    );
    println!();
}

// =============================================================================
// f64 reference implementations
// =============================================================================

fn s2l_iec_f64(gamma: f64) -> f64 {
    if gamma <= 0.0 {
        0.0
    } else if gamma < 0.04045 {
        gamma / 12.92
    } else if gamma < 1.0 {
        ((gamma + 0.055) / 1.055).powf(2.4)
    } else {
        1.0
    }
}

fn s2l_mox_f64(gamma: f64) -> f64 {
    const A: f64 = 0.055_010_718_947_586_6;
    const SCALE: f64 = 1.055_010_718_947_586_6;
    const THRESH: f64 = 12.92 * 0.003_041_282_560_127_521;
    if gamma <= 0.0 {
        0.0
    } else if gamma < THRESH {
        gamma / 12.92
    } else if gamma < 1.0 {
        ((gamma + A) / SCALE).powf(2.4)
    } else {
        1.0
    }
}

fn l2s_iec_f64(linear: f64) -> f64 {
    if linear <= 0.0 {
        0.0
    } else if linear < 0.003_130_8 {
        linear * 12.92
    } else if linear < 1.0 {
        1.055 * linear.powf(1.0 / 2.4) - 0.055
    } else {
        1.0
    }
}

fn l2s_mox_f64(linear: f64) -> f64 {
    const A: f64 = 0.055_010_718_947_586_6;
    const SCALE: f64 = 1.055_010_718_947_586_6;
    const THRESH: f64 = 0.003_041_282_560_127_521;
    if linear <= 0.0 {
        0.0
    } else if linear < THRESH {
        linear * 12.92
    } else if linear < 1.0 {
        SCALE * linear.powf(1.0 / 2.4) - A
    } else {
        1.0
    }
}

// =============================================================================
// f32 code path implementations
// =============================================================================

fn s2l_default(gamma: f32) -> f32 {
    if gamma <= 0.0 {
        0.0
    } else if gamma < 0.04045 {
        gamma / 12.92
    } else if gamma < 1.0 {
        s2l_rational_poly(gamma)
    } else {
        1.0
    }
}

fn s2l_precise(gamma: f32) -> f32 {
    const THRESH: f32 = (12.92 * 0.003_041_282_560_127_521_f64) as f32;
    const A: f32 = 0.055_010_718_947_586_6_f64 as f32;
    const SCALE: f32 = 1.055_010_718_947_586_6_f64 as f32;
    if gamma <= 0.0 {
        0.0
    } else if gamma < THRESH {
        gamma / 12.92
    } else if gamma < 1.0 {
        ((gamma + A) / SCALE).powf(2.4)
    } else {
        1.0
    }
}

fn l2s_default(linear: f32) -> f32 {
    if linear <= 0.0 {
        0.0
    } else if linear < 0.003_130_8 {
        linear * 12.92
    } else if linear < 1.0 {
        l2s_rational_poly(linear)
    } else {
        1.0
    }
}

fn l2s_precise(linear: f32) -> f32 {
    const THRESH: f32 = 0.003_041_282_560_127_521_f64 as f32;
    const A: f32 = 0.055_010_718_947_586_6_f64 as f32;
    const SCALE: f32 = 1.055_010_718_947_586_6_f64 as f32;
    if linear <= 0.0 {
        0.0
    } else if linear < THRESH {
        linear * 12.92
    } else if linear < 1.0 {
        SCALE * linear.powf(1.0 / 2.4) - A
    } else {
        1.0
    }
}

// =============================================================================
// Rational polynomial (from libjxl)
// =============================================================================

fn s2l_rational_poly(gamma: f32) -> f32 {
    const P: [f32; 5] = [
        2.200_248_3e-4,
        1.043_637_6e-2,
        1.624_820_4e-1,
        7.961_565e-1,
        8.210_153e-1,
    ];
    const Q: [f32; 5] = [
        2.631_847e-1,
        1.076_976_5,
        4.987_528_3e-1,
        -5.512_498_3e-2,
        6.521_209e-3,
    ];
    let x = gamma;
    let yp = P[4]
        .mul_add(x, P[3])
        .mul_add(x, P[2])
        .mul_add(x, P[1])
        .mul_add(x, P[0]);
    let yq = Q[4]
        .mul_add(x, Q[3])
        .mul_add(x, Q[2])
        .mul_add(x, Q[1])
        .mul_add(x, Q[0]);
    yp / yq
}

fn l2s_rational_poly(linear: f32) -> f32 {
    const P: [f32; 5] = [
        -5.135_152_6e-4,
        5.287_254_7e-3,
        3.903_843e-1,
        1.474_205_3,
        7.352_63e-1,
    ];
    const Q: [f32; 5] = [
        1.004_519_6e-2,
        3.036_675_5e-1,
        1.340_817,
        9.258_482e-1,
        2.424_867_8e-2,
    ];
    let x = linear.sqrt();
    let yp = P[4]
        .mul_add(x, P[3])
        .mul_add(x, P[2])
        .mul_add(x, P[1])
        .mul_add(x, P[0]);
    let yq = Q[4]
        .mul_add(x, Q[3])
        .mul_add(x, Q[2])
        .mul_add(x, Q[1])
        .mul_add(x, Q[0]);
    yp / yq
}

// =============================================================================
// Stats tracking
// =============================================================================

struct SweepStats {
    name: &'static str,
    max_ulp: u32,
    max_ulp_at: f32,
    max_abs_err: f64,
    max_abs_at: f32,
    total_ulp: u64,
    count: u64,
    actual_at_worst: f32,
    ref_at_worst: f64,
}

impl SweepStats {
    fn new(name: &'static str) -> Self {
        Self {
            name,
            max_ulp: 0,
            max_ulp_at: 0.0,
            max_abs_err: 0.0,
            max_abs_at: 0.0,
            total_ulp: 0,
            count: 0,
            actual_at_worst: 0.0,
            ref_at_worst: 0.0,
        }
    }

    fn update(&mut self, input: f32, actual: f32, reference: f64) {
        let expected = reference as f32;
        let ulp = ulp_distance(actual, expected);
        let abs_err = (actual as f64 - reference).abs();

        self.total_ulp += ulp as u64;
        self.count += 1;

        if ulp > self.max_ulp {
            self.max_ulp = ulp;
            self.max_ulp_at = input;
            self.actual_at_worst = actual;
            self.ref_at_worst = reference;
        }
        if abs_err > self.max_abs_err {
            self.max_abs_err = abs_err;
            self.max_abs_at = input;
        }
    }

    fn print_summary(&self) {
        let avg_ulp = if self.count > 0 {
            self.total_ulp as f64 / self.count as f64
        } else {
            0.0
        };
        println!(
            "  {:50} {:>12} {:>12.2} {:>14.6e}",
            self.name, self.max_ulp, avg_ulp, self.max_abs_err,
        );
    }

    fn print_worst(&self) {
        println!(
            "    {:48} at input={:.10}: actual={:.10e}, ref={:.15e}",
            self.name, self.max_ulp_at, self.actual_at_worst, self.ref_at_worst,
        );
    }
}

/// Track the jump across a threshold to detect discontinuity
struct DiscontinuityTracker {
    name: &'static str,
    threshold: f32,
    // Value just below threshold
    val_below: f32,
    input_below: f32,
    // Value at or just above threshold
    val_above: f32,
    input_above: f32,
    found_below: bool,
    found_above: bool,
}

impl DiscontinuityTracker {
    fn new(name: &'static str) -> Self {
        Self {
            name,
            threshold: 0.0,
            val_below: 0.0,
            input_below: 0.0,
            val_above: 0.0,
            input_above: 0.0,
            found_below: false,
            found_above: false,
        }
    }

    fn update(&mut self, input: f32, output: f32, threshold: f32) {
        self.threshold = threshold;
        if input < threshold {
            // Keep updating - we want the last value below
            self.val_below = output;
            self.input_below = input;
            self.found_below = true;
        } else if !self.found_above {
            // First value at or above threshold
            self.val_above = output;
            self.input_above = input;
            self.found_above = true;
        }
    }

    fn print(&self) {
        if self.found_below && self.found_above {
            let jump = (self.val_above - self.val_below).abs();
            let input_step = (self.input_above - self.input_below).abs();
            // Expected step: if continuous, output should change by roughly
            // derivative * input_step. A jump >> expected indicates discontinuity.
            let ulps = ulp_distance(self.val_below, self.val_above);
            println!(
                "    {:48} below={:.10e} above={:.10e} jump={:.6e} ({} ULP) step={:.6e}",
                self.name, self.val_below, self.val_above, jump, ulps, input_step,
            );
        }
    }
}

fn next_f32(v: f32) -> f32 {
    f32::from_bits(v.to_bits() + 1)
}

fn ulp_distance(a: f32, b: f32) -> u32 {
    if a.is_nan() || b.is_nan() {
        return u32::MAX;
    }
    let a_bits = a.to_bits() as i32;
    let b_bits = b.to_bits() as i32;
    (a_bits - b_bits).unsigned_abs()
}