rawshift-image 0.1.1

Still-image decoding, RAW processing, and encoding for rawshift
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
use super::{Demosaic, DemosaicError};
use crate::core::image::{CfaPattern, RawImage};
use rayon::prelude::*;

/// Bilinear interpolation demosaicing algorithm.
///
/// A fast, simple demosaicing algorithm that interpolates missing color
/// values using the average of neighboring pixels. This produces acceptable
/// results for most images but may show color fringing on high-contrast edges.
///
/// This implementation uses rayon for parallel row processing on multi-core systems.
pub struct Bilinear;

impl Demosaic for Bilinear {
    fn demosaic_into(&self, raw: &RawImage, output: &mut [u16]) -> Result<(), DemosaicError> {
        let width = raw.active_area().size.width;
        let height = raw.active_area().size.height;
        let x_offset = raw.active_area().origin.x;
        let y_offset = raw.active_area().origin.y;

        let expected_size = (width as usize) * (height as usize) * 3;
        if output.len() != expected_size {
            return Err(DemosaicError::BufferSizeMismatch {
                expected: expected_size,
                actual: output.len(),
            });
        }

        let raw_width = raw.width();
        let raw_height = raw.height();
        let raw_data = &raw.data;
        let cfa_pattern = raw.cfa_pattern();
        let row_stride = (width as usize) * 3;

        // Get raw pixel with bounds checking (closure captures raw data)
        let get_raw = |x: u32, y: u32| -> u16 {
            if x < raw_width && y < raw_height {
                raw_data[(y as usize) * (raw_width as usize) + (x as usize)]
            } else {
                0
            }
        };

        // Process rows in parallel
        output
            .par_chunks_mut(row_stride)
            .enumerate()
            .for_each(|(y, row_output)| {
                let abs_y = (y as u32) + y_offset;

                for x in 0..width {
                    let abs_x = x + x_offset;
                    let (r, g, b) = demosaic_pixel_bilinear(abs_x, abs_y, cfa_pattern, &get_raw);

                    let idx = (x as usize) * 3;
                    row_output[idx] = r;
                    row_output[idx + 1] = g;
                    row_output[idx + 2] = b;
                }
            });

        Ok(())
    }
}

/// Interpolates a single pixel using bilinear interpolation.
/// `get_raw`: (x: u32, y: u32) -> <raw pixel value>: u16
fn demosaic_pixel_bilinear<F>(x: u32, y: u32, pattern: CfaPattern, get_raw: &F) -> (u16, u16, u16)
where
    F: Fn(u32, u32) -> u16,
{
    // Determine the color of the current pixel location
    let (is_red, is_blue) = match pattern {
        CfaPattern::Rggb => (
            x.is_multiple_of(2) && y.is_multiple_of(2),
            (x % 2 == 1) && (y % 2 == 1),
        ),
        CfaPattern::Grbg => (
            (x % 2 == 1) && y.is_multiple_of(2),
            x.is_multiple_of(2) && (y % 2 == 1),
        ),
        CfaPattern::Gbrg => (
            x.is_multiple_of(2) && (y % 2 == 1),
            (x % 2 == 1) && y.is_multiple_of(2),
        ),
        CfaPattern::Bggr => (
            (x % 2 == 1) && (y % 2 == 1),
            x.is_multiple_of(2) && y.is_multiple_of(2),
        ),
    };

    // Green is true if it's neither red nor blue
    let is_green = !is_red && !is_blue;

    if is_green {
        // Current pixel is Green.
        // We need to interpolate Red and Blue.
        // One of them will be vertical neighbors, the other horizontal neighbors.

        let g = get_raw(x, y);

        let (r, b) = match pattern {
            // For RGGB:
            // R G R G
            // G B G B
            // If we are at (0,1) [Green], Left/Right is B, Up/Down is R
            // If we are at (1,0) [Green], Left/Right is R, Up/Down is B
            CfaPattern::Rggb | CfaPattern::Bggr => {
                if y.is_multiple_of(2) {
                    // Even row
                    // R G R G (RGGB case) -> (x%2==1) -> Horizontal is R, Vertical is B
                    // B G B G (BGGR case) -> (x%2==1) -> Horizontal is B, Vertical is R
                    // checking pattern specifically:
                    if matches!(pattern, CfaPattern::Rggb) {
                        // Row 0: R G R G. We are at G. Neighbors (left/right) are R. Vertical are B.
                        (
                            avg2(get_raw(x.saturating_sub(1), y), get_raw(x + 1, y)),
                            avg2(get_raw(x, y.saturating_sub(1)), get_raw(x, y + 1)),
                        )
                    } else {
                        // BGGR
                        // Row 0: B G B G. We are at G. Neighbors (left/right) are B. Vertical are R.
                        (
                            avg2(get_raw(x, y.saturating_sub(1)), get_raw(x, y + 1)),
                            avg2(get_raw(x.saturating_sub(1), y), get_raw(x + 1, y)),
                        )
                    }
                } else {
                    // Odd row
                    // G B G B (RGGB case). We are at G. Left/Right B, Vert R.
                    // G R G R (BGGR case). We are at G. Left/Right R, Vert B.
                    if matches!(pattern, CfaPattern::Rggb) {
                        (
                            avg2(get_raw(x, y.saturating_sub(1)), get_raw(x, y + 1)),
                            avg2(get_raw(x.saturating_sub(1), y), get_raw(x + 1, y)),
                        )
                    } else {
                        (
                            avg2(get_raw(x.saturating_sub(1), y), get_raw(x + 1, y)),
                            avg2(get_raw(x, y.saturating_sub(1)), get_raw(x, y + 1)),
                        )
                    }
                }
            }
            CfaPattern::Grbg | CfaPattern::Gbrg => {
                // Similar logic...
                // GRBG:
                // G R G R
                // B G B G
                if y.is_multiple_of(2) {
                    // Even row G R G R. At G. Horizontal R, Vert B.
                    if matches!(pattern, CfaPattern::Grbg) {
                        (
                            avg2(get_raw(x.saturating_sub(1), y), get_raw(x + 1, y)),
                            avg2(get_raw(x, y.saturating_sub(1)), get_raw(x, y + 1)),
                        )
                    } else {
                        // GBRG: G B G B. At G. Horizontal B, Vert R.
                        (
                            avg2(get_raw(x, y.saturating_sub(1)), get_raw(x, y + 1)),
                            avg2(get_raw(x.saturating_sub(1), y), get_raw(x + 1, y)),
                        )
                    }
                } else {
                    // Odd row
                    // GRBG: B G B G. At G. Horizontal B, Vert R.
                    if matches!(pattern, CfaPattern::Grbg) {
                        (
                            avg2(get_raw(x, y.saturating_sub(1)), get_raw(x, y + 1)),
                            avg2(get_raw(x.saturating_sub(1), y), get_raw(x + 1, y)),
                        )
                    } else {
                        // GBRG: R G R G. At G. Horizontal R, Vert B.
                        (
                            avg2(get_raw(x.saturating_sub(1), y), get_raw(x + 1, y)),
                            avg2(get_raw(x, y.saturating_sub(1)), get_raw(x, y + 1)),
                        )
                    }
                }
            }
        };
        (r, g, b)
    } else if is_red {
        // Current is Red.
        let r = get_raw(x, y);
        // Green is average of 4 cross neighbors (up, down, left, right)
        let g = avg4(
            get_raw(x, y.saturating_sub(1)),
            get_raw(x, y + 1),
            get_raw(x.saturating_sub(1), y),
            get_raw(x + 1, y),
        );
        // Blue is average of 4 diagonal neighbors
        let b = avg4(
            get_raw(x.saturating_sub(1), y.saturating_sub(1)),
            get_raw(x + 1, y.saturating_sub(1)),
            get_raw(x.saturating_sub(1), y + 1),
            get_raw(x + 1, y + 1),
        );
        (r, g, b)
    } else {
        // Current is Blue.
        let b = get_raw(x, y);
        // Green is average of 4 cross neighbors
        let g = avg4(
            get_raw(x, y.saturating_sub(1)),
            get_raw(x, y + 1),
            get_raw(x.saturating_sub(1), y),
            get_raw(x + 1, y),
        );
        // Red is average of 4 diagonal neighbors
        let r = avg4(
            get_raw(x.saturating_sub(1), y.saturating_sub(1)),
            get_raw(x + 1, y.saturating_sub(1)),
            get_raw(x.saturating_sub(1), y + 1),
            get_raw(x + 1, y + 1),
        );
        (r, g, b)
    }
}

#[inline(always)]
fn avg2(a: u16, b: u16) -> u16 {
    ((a as u32 + b as u32) / 2) as u16
}

#[inline(always)]
fn avg4(a: u16, b: u16, c: u16, d: u16) -> u16 {
    ((a as u32 + b as u32 + c as u32 + d as u32) / 4) as u16
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::image::{Point, Rect, Size};

    /// Create a test raw image with given dimensions and CFA pattern.
    fn create_test_raw(width: u32, height: u32, pattern: CfaPattern, value: u16) -> RawImage {
        let size = Size::new(width, height);
        let active_area = Rect::new(Point::ORIGIN, size);
        let pixel_count = (width * height) as usize;
        RawImage::builder(size, active_area, 14, pattern)
            .white_level(16383)
            .data(vec![value; pixel_count])
            .build()
    }

    /// Create a raw image with a 2x2 Bayer pattern.
    fn create_bayer_2x2(pattern: CfaPattern) -> RawImage {
        // Create a 2x2 pattern with distinct values for each cell
        let mut raw = create_test_raw(2, 2, pattern, 0);
        // Set values for each pixel position
        raw.data[0] = 1000; // (0, 0)
        raw.data[1] = 2000; // (1, 0)
        raw.data[2] = 3000; // (0, 1)
        raw.data[3] = 4000; // (1, 1)
        raw
    }

    #[test]
    fn test_demosaic_into_correct_size() {
        let raw = create_test_raw(10, 10, CfaPattern::Rggb, 1000);
        let mut output = vec![0u16; 10 * 10 * 3]; // Correct size

        let result = Bilinear.demosaic_into(&raw, &mut output);
        assert!(result.is_ok());
    }

    #[test]
    fn test_demosaic_into_wrong_size() {
        let raw = create_test_raw(10, 10, CfaPattern::Rggb, 1000);
        let mut output = vec![0u16; 50]; // Too small

        let result = Bilinear.demosaic_into(&raw, &mut output);
        assert!(matches!(
            result,
            Err(DemosaicError::BufferSizeMismatch { .. })
        ));
    }

    #[test]
    fn test_demosaic_solid_color() {
        // A uniform input should produce roughly uniform output
        let raw = create_test_raw(10, 10, CfaPattern::Rggb, 5000);
        let demosaic = Bilinear;
        let rgb = demosaic.demosaic(&raw);

        assert_eq!(rgb.width(), 10);
        assert_eq!(rgb.height(), 10);
        assert_eq!(rgb.data.len(), 10 * 10 * 3);

        // Interior pixels (not on edge) should be close to input value
        // Edge pixels may have lower values due to boundary handling
        for y in 1..9 {
            for x in 1..9 {
                let idx = ((y * 10 + x) * 3) as usize;
                for c in 0..3 {
                    let pixel = rgb.data[idx + c];
                    assert!(
                        (4000..=5500).contains(&pixel),
                        "Interior pixel at ({},{}) channel {} value {} out of range",
                        x,
                        y,
                        c,
                        pixel
                    );
                }
            }
        }
    }

    #[test]
    fn test_demosaic_all_cfa_patterns() {
        let patterns = [
            CfaPattern::Rggb,
            CfaPattern::Grbg,
            CfaPattern::Gbrg,
            CfaPattern::Bggr,
        ];

        for pattern in patterns {
            let raw = create_test_raw(8, 8, pattern, 2000);
            let rgb = Bilinear.demosaic(&raw);

            assert_eq!(rgb.width(), 8);
            assert_eq!(rgb.height(), 8);
            assert_eq!(rgb.data.len(), 8 * 8 * 3);

            // Verify all pixels have reasonable values
            for pixel in &rgb.data {
                assert!(
                    *pixel <= 3000,
                    "Pattern {:?}: pixel value {} too high",
                    pattern,
                    pixel
                );
            }
        }
    }

    #[test]
    fn test_demosaic_rggb_pattern() {
        // Test that RGGB pattern is correctly interpreted
        let raw = create_bayer_2x2(CfaPattern::Rggb);
        let rgb = Bilinear.demosaic(&raw);

        // For RGGB, position (0,0) is Red
        // The output should have interpolated values
        assert_eq!(rgb.width(), 2);
        assert_eq!(rgb.height(), 2);

        // First pixel (0,0) - this is a Red position in RGGB
        let r = rgb.data[0];
        let g = rgb.data[1];
        let _b = rgb.data[2];

        // Red should be the original value (1000)
        assert_eq!(r, 1000, "Red at RGGB position (0,0) should be 1000");

        // Green should be interpolated from neighbors
        // At (0,0), only (1,0) and (0,1) are in bounds, both at edges
        assert!(g > 0, "Green should be interpolated");
    }

    #[test]
    fn test_demosaic_with_active_area() {
        // Test that active_area is respected
        let raw = {
            let size = Size::new(10, 10);
            let active_area = Rect::from_coords(3, 3, 4, 4);
            RawImage::builder(size, active_area, 14, CfaPattern::Rggb)
                .white_level(16383)
                .data(vec![1000u16; 100])
                .build()
        };

        let rgb = Bilinear.demosaic(&raw);

        // Output dimensions should match active area
        assert_eq!(rgb.width(), 4);
        assert_eq!(rgb.height(), 4);
        assert_eq!(rgb.data.len(), 4 * 4 * 3);
    }

    #[test]
    fn test_avg2() {
        assert_eq!(avg2(0, 0), 0);
        assert_eq!(avg2(100, 100), 100);
        assert_eq!(avg2(100, 200), 150);
        assert_eq!(avg2(0, 65535), 32767);
    }

    #[test]
    fn test_avg4() {
        assert_eq!(avg4(0, 0, 0, 0), 0);
        assert_eq!(avg4(100, 100, 100, 100), 100);
        assert_eq!(avg4(0, 100, 200, 300), 150);
        assert_eq!(avg4(0, 0, 0, 65535), 16383);
    }

    #[test]
    fn test_bilinear_all_cfa_patterns() {
        // All 4 CFA patterns should produce valid RGB output with correct dimensions
        let patterns = [
            CfaPattern::Rggb,
            CfaPattern::Grbg,
            CfaPattern::Gbrg,
            CfaPattern::Bggr,
        ];

        for pattern in patterns {
            let raw = create_test_raw(6, 6, pattern, 8000);
            let rgb = Bilinear.demosaic(&raw);

            assert_eq!(rgb.width(), 6, "width for {:?}", pattern);
            assert_eq!(rgb.height(), 6, "height for {:?}", pattern);
            assert_eq!(rgb.data.len(), 6 * 6 * 3, "data length for {:?}", pattern);

            // All output pixels must be in valid u16 range (which they always are,
            // but also check that at least some pixels are non-zero for a non-zero input)
            let non_zero = rgb.data.iter().any(|&v| v > 0);
            assert!(
                non_zero,
                "Output for {:?} should have non-zero pixels",
                pattern
            );
        }
    }

    #[test]
    fn test_bilinear_with_active_area() {
        // Test various active area offsets
        let raw = {
            let size = Size::new(12, 12);
            let active_area = Rect::from_coords(2, 4, 6, 6);
            RawImage::builder(size, active_area, 14, CfaPattern::Rggb)
                .white_level(16383)
                .data(vec![5000u16; 144])
                .build()
        };

        let rgb = Bilinear.demosaic(&raw);

        assert_eq!(
            rgb.width(),
            6,
            "output width should match active area width"
        );
        assert_eq!(
            rgb.height(),
            6,
            "output height should match active area height"
        );
        assert_eq!(
            rgb.data.len(),
            6 * 6 * 3,
            "output should have correct data length"
        );

        // Output values are u16, so always in [0, 65535] by definition
        assert!(!rgb.data.is_empty(), "output should have pixel data");
    }

    #[test]
    fn test_bilinear_gradient_smooth() {
        // A horizontally-varying input should produce a smooth (monotonically varying)
        // green channel in the output, since bilinear averages neighbors.
        let width = 8u32;
        let height = 4u32;
        let size = Size::new(width, height);
        let active_area = Rect::new(Point::ORIGIN, size);

        // Fill with a horizontal gradient: pixel value increases with x
        let mut data = vec![0u16; (width * height) as usize];
        for y in 0..height as usize {
            for x in 0..width as usize {
                data[y * width as usize + x] = (x as u16) * 1000;
            }
        }

        let raw = RawImage::builder(size, active_area, 14, CfaPattern::Rggb)
            .white_level(16383)
            .data(data)
            .build();

        let rgb = Bilinear.demosaic(&raw);

        // Check that the green channel (index 1) generally increases left to right
        // for a middle row. Allow for some non-monotonicity at edges.
        let mid_row = 2usize;
        let row_start = mid_row * width as usize * 3;

        // Compare interior pixels: pixel at x+1 should have green >= pixel at x
        // (with tolerance for boundary effects)
        for x in 1..(width as usize - 2) {
            let g_left = rgb.data[row_start + (x - 1) * 3 + 1];
            let g_right = rgb.data[row_start + (x + 1) * 3 + 1];
            assert!(
                g_right >= g_left || (g_right as i32 - g_left as i32).abs() < 2000,
                "gradient smoothness: g[{}]={} should not greatly exceed g[{}]={} in row {}",
                x - 1,
                g_left,
                x + 1,
                g_right,
                mid_row
            );
        }
    }
}