zenjxl-decoder 0.3.8

High performance Rust implementation of a JPEG XL decoder
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

use crate::{
    error::Result,
    frame::modular::{
        ModularChannel, Predictor,
        predict::{PredictionData, WeightedPredictorState},
    },
    headers::modular::WeightedHeader,
    image::Image,
};

const RGB_CHANNELS: usize = 3;

// 5x5x5 color cube for the larger cube.
const LARGE_CUBE: usize = 5;

// Smaller interleaved color cube to fill the holes of the larger cube.
const SMALL_CUBE: usize = 4;
const SMALL_CUBE_BITS: usize = 2;
// SMALL_CUBE ** 3
const LARGE_CUBE_OFFSET: usize = SMALL_CUBE * SMALL_CUBE * SMALL_CUBE;

fn scale<const DENOM: usize>(value: usize, bit_depth: usize) -> i32 {
    // return (value * ((1 << bit_depth) - 1)) / DENOM;
    // We only call this function with SMALL_CUBE or LARGE_CUBE - 1 as DENOM,
    // allowing us to avoid a division here.
    const {
        assert!(DENOM == 4, "denom must be 4");
    }
    ((value * ((1 << bit_depth) - 1)) >> 2) as i32
}

// The purpose of this function is solely to extend the interpretation of
// palette indices to implicit values. If index < nb_deltas, indicating that the
// result is a delta palette entry, it is the responsibility of the caller to
// treat it as such.
fn get_palette_value(
    palette: &Image<i32>,
    index: isize,
    c: usize,
    palette_size: usize,
    bit_depth: usize,
) -> i32 {
    if index < 0 {
        const DELTA_PALETTE: [[i32; 3]; 72] = [
            [0, 0, 0],
            [4, 4, 4],
            [11, 0, 0],
            [0, 0, -13],
            [0, -12, 0],
            [-10, -10, -10],
            [-18, -18, -18],
            [-27, -27, -27],
            [-18, -18, 0],
            [0, 0, -32],
            [-32, 0, 0],
            [-37, -37, -37],
            [0, -32, -32],
            [24, 24, 45],
            [50, 50, 50],
            [-45, -24, -24],
            [-24, -45, -45],
            [0, -24, -24],
            [-34, -34, 0],
            [-24, 0, -24],
            [-45, -45, -24],
            [64, 64, 64],
            [-32, 0, -32],
            [0, -32, 0],
            [-32, 0, 32],
            [-24, -45, -24],
            [45, 24, 45],
            [24, -24, -45],
            [-45, -24, 24],
            [80, 80, 80],
            [64, 0, 0],
            [0, 0, -64],
            [0, -64, -64],
            [-24, -24, 45],
            [96, 96, 96],
            [64, 64, 0],
            [45, -24, -24],
            [34, -34, 0],
            [112, 112, 112],
            [24, -45, -45],
            [45, 45, -24],
            [0, -32, 32],
            [24, -24, 45],
            [0, 96, 96],
            [45, -24, 24],
            [24, -45, -24],
            [-24, -45, 24],
            [0, -64, 0],
            [96, 0, 0],
            [128, 128, 128],
            [64, 0, 64],
            [144, 144, 144],
            [96, 96, 0],
            [-36, -36, 36],
            [45, -24, -45],
            [45, -45, -24],
            [0, 0, -96],
            [0, 128, 128],
            [0, 96, 0],
            [45, 24, -45],
            [-128, 0, 0],
            [24, -45, 24],
            [-45, 24, -45],
            [64, 0, -64],
            [64, -64, -64],
            [96, 0, 96],
            [45, -45, 24],
            [24, 45, -45],
            [64, 64, -64],
            [128, 128, 0],
            [0, 0, -128],
            [-24, 45, -45],
        ];
        if c >= RGB_CHANNELS {
            return 0;
        }
        // Do not open the brackets, otherwise INT32_MIN negation could overflow.
        let mut index = -(index + 1) as usize;
        index %= 1 + 2 * (DELTA_PALETTE.len() - 1);
        const MULTIPLIER: [i32; 2] = [-1, 1];
        let mut result = DELTA_PALETTE[(index + 1) >> 1][c] * MULTIPLIER[index & 1];
        if bit_depth > 8 {
            result *= 1 << (bit_depth - 8);
        }
        result
    } else {
        let mut index = index as usize;
        if palette_size <= index && index < palette_size + LARGE_CUBE_OFFSET {
            if c >= RGB_CHANNELS {
                return 0;
            }
            index -= palette_size;
            index >>= c * SMALL_CUBE_BITS;
            scale::<SMALL_CUBE>(index % SMALL_CUBE, bit_depth)
                + (1 << (0.max(bit_depth as isize - 3)))
        } else if palette_size + LARGE_CUBE_OFFSET <= index {
            if c >= RGB_CHANNELS {
                return 0;
            }
            index -= palette_size + LARGE_CUBE_OFFSET;
            // TODO(eustas): should we take care of ambiguity created by
            //               index >= LARGE_CUBE ** 3 ?
            match c {
                0 => (),
                1 => {
                    index /= LARGE_CUBE;
                }
                2 => {
                    index /= LARGE_CUBE * LARGE_CUBE;
                }
                _ => (),
            }
            scale::<{ LARGE_CUBE - 1 }>(index % LARGE_CUBE, bit_depth)
        } else {
            palette.row(c)[index]
        }
    }
}

pub fn do_palette_step_general(
    buf_in: &ModularChannel,
    buf_pal: &ModularChannel,
    buf_out: &mut [&mut ModularChannel],
    num_colors: usize,
    num_deltas: usize,
    predictor: Predictor,
    wp_header: &WeightedHeader,
) -> Result<()> {
    let (w, h) = buf_in.data.size();
    let palette = &buf_pal.data;
    let bit_depth = buf_in.bit_depth.bits_per_sample().min(24) as usize;

    if w == 0 {
        // Nothing to do.
        // Avoid touching "empty" channels with non-zero height.
    } else if num_deltas == 0 && predictor == Predictor::Zero {
        for (chan_index, out) in buf_out.iter_mut().enumerate() {
            for y in 0..h {
                let row_index = buf_in.data.row(y);
                let row_out = out.data.row_mut(y);
                for x in 0..w {
                    let index = row_index[x];
                    let palette_value = get_palette_value(
                        palette,
                        index as isize,
                        /*c=*/ chan_index,
                        /*palette_size=*/ num_colors,
                        /*bit_depth=*/ bit_depth,
                    );
                    row_out[x] = palette_value;
                }
            }
        }
    } else if predictor == Predictor::Weighted {
        let w = buf_in.data.size().0;
        let mut row_prev_buf: Vec<i32> = Vec::new();
        let mut row_prev2_buf: Vec<i32> = Vec::new();
        for (chan_index, out) in buf_out.iter_mut().enumerate() {
            let mut wp_state = WeightedPredictorState::new(wp_header, w)?;
            for y in 0..h {
                let idx = buf_in.data.row(y);

                // Pre-fetch completed top rows (avoids per-pixel .row() calls)
                std::mem::swap(&mut row_prev2_buf, &mut row_prev_buf);
                if y > 0 {
                    row_prev_buf.clear();
                    row_prev_buf.extend_from_slice(out.data.row(y - 1));
                }
                let row_prev = if y > 0 {
                    Some(row_prev_buf.as_slice())
                } else {
                    None
                };
                let row_prev2 = if y > 1 {
                    Some(row_prev2_buf.as_slice())
                } else {
                    None
                };

                let mut last = if y > 0 { row_prev.unwrap()[0] } else { 0 };
                let mut last_last = last;

                for (x, &index) in idx.iter().enumerate() {
                    let palette_entry = get_palette_value(
                        palette,
                        index as isize,
                        /*c=*/ chan_index,
                        /*palette_size=*/ num_colors + num_deltas,
                        /*bit_depth=*/ bit_depth,
                    );
                    let left = last;
                    let leftleft = if x > 1 { last_last } else { left };
                    let val = if index < num_deltas as i32 {
                        let prediction_data = PredictionData::from_hoisted_neighbors(
                            left, leftleft, row_prev, row_prev2, w, None, None, None, None, None,
                            x, y, w, h,
                        );
                        let (wp_pred, _) =
                            wp_state.predict_and_property((x, y), w, &prediction_data);
                        let pred = predictor.predict_one(prediction_data, wp_pred);
                        (pred + palette_entry as i64) as i32
                    } else {
                        palette_entry
                    };
                    out.data.row_mut(y)[x] = val;
                    wp_state.update_errors(val, (x, y), w);
                    last_last = last;
                    last = val;
                }
            }
        }
    } else {
        let mut row_prev_buf: Vec<i32> = Vec::new();
        let mut row_prev2_buf: Vec<i32> = Vec::new();
        for (chan_index, out) in buf_out.iter_mut().enumerate() {
            for y in 0..h {
                let idx = buf_in.data.row(y);

                // Pre-fetch completed top rows (avoids per-pixel .row() calls)
                std::mem::swap(&mut row_prev2_buf, &mut row_prev_buf);
                if y > 0 {
                    row_prev_buf.clear();
                    row_prev_buf.extend_from_slice(out.data.row(y - 1));
                }
                let row_prev = if y > 0 {
                    Some(row_prev_buf.as_slice())
                } else {
                    None
                };
                let row_prev2 = if y > 1 {
                    Some(row_prev2_buf.as_slice())
                } else {
                    None
                };

                let mut last = if y > 0 { row_prev.unwrap()[0] } else { 0 };
                let mut last_last = last;

                for (x, &index) in idx.iter().enumerate() {
                    let palette_entry = get_palette_value(
                        palette,
                        index as isize,
                        /*c=*/ chan_index,
                        /*palette_size=*/ num_colors + num_deltas,
                        /*bit_depth=*/ bit_depth,
                    );
                    let left = last;
                    let leftleft = if x > 1 { last_last } else { left };
                    let val = if index < num_deltas as i32 {
                        let prediction_data = PredictionData::from_hoisted_neighbors(
                            left, leftleft, row_prev, row_prev2, w, None, None, None, None, None,
                            x, y, w, h,
                        );
                        let pred = predictor.predict_one(prediction_data, /*wp_pred=*/ 0);
                        (pred + palette_entry as i64) as i32
                    } else {
                        palette_entry
                    };
                    out.data.row_mut(y)[x] = val;
                    last_last = last;
                    last = val;
                }
            }
        }
    }
    Ok(())
}

/// Like `get_prediction_data` but takes pre-fetched/tracked values for the
/// main rect, avoiding per-pixel `Image::row()` overhead.
///
/// - `left`, `leftleft`: tracked as running variables by the caller
/// - `row_prev`, `row_prev2`: pre-fetched (copied) once per y-iteration
/// - Neighbor images: only accessed at grid boundaries
#[allow(clippy::too_many_arguments)]
#[inline(always)]
fn get_prediction_data_hoisted(
    left: i32,
    leftleft: i32,
    row_prev: Option<&[i32]>,
    row_prev2: Option<&[i32]>,
    rect_width: usize,
    buf: &[&mut ModularChannel],
    idx: usize,
    grid_x: usize,
    grid_y: usize,
    grid_xsize: usize,
    x: usize,
    y: usize,
    xsize: usize,
    ysize: usize,
) -> PredictionData {
    PredictionData::from_hoisted_neighbors(
        left,
        leftleft,
        row_prev,
        row_prev2,
        rect_width,
        if grid_x > 0 {
            Some(&buf[idx - 1].data)
        } else {
            None
        },
        if grid_y > 0 {
            Some(&buf[idx - grid_xsize].data)
        } else {
            None
        },
        if grid_x > 0 && grid_y > 0 {
            Some(&buf[idx - grid_xsize - 1].data)
        } else {
            None
        },
        if grid_x + 1 < grid_xsize {
            Some(&buf[idx + 1].data)
        } else {
            None
        },
        if grid_x + 1 < grid_xsize && grid_y > 0 {
            Some(&buf[idx - grid_xsize + 1].data)
        } else {
            None
        },
        x,
        y,
        xsize,
        ysize,
    )
}

#[allow(clippy::too_many_arguments)]
pub fn do_palette_step_one_group(
    buf_in: &ModularChannel,
    buf_pal: &ModularChannel,
    buf_out: &mut [&mut ModularChannel],
    grid_x: usize,
    grid_y: usize,
    grid_xsize: usize,
    grid_ysize: usize,
    num_colors: usize,
    num_deltas: usize,
    predictor: Predictor,
) {
    let h = buf_in.data.size().1;
    let palette = &buf_pal.data;
    let bit_depth = buf_in.bit_depth.bits_per_sample().min(24) as usize;
    let num_c = buf_out.len() / (grid_xsize * grid_ysize);
    let (xsize, ysize) = buf_out[0].data.size();

    for c in 0..num_c {
        let out_idx = c * grid_ysize * grid_xsize + grid_y * grid_xsize + grid_x;
        let rect_width = buf_out[out_idx].data.size().0;
        // Reusable buffers for pre-fetched completed rows (avoids per-pixel .row() calls)
        let mut row_prev_buf: Vec<i32> = Vec::new();
        let mut row_prev2_buf: Vec<i32> = Vec::new();

        for y in 0..h {
            let index_img = buf_in.data.row(y);

            // Copy completed top rows — these don't change during the x-loop.
            // row_prev2 = previous row_prev (rotate), row_prev = row just completed.
            std::mem::swap(&mut row_prev2_buf, &mut row_prev_buf);
            if y > 0 {
                row_prev_buf.clear();
                row_prev_buf.extend_from_slice(buf_out[out_idx].data.row(y - 1));
            }
            let row_prev = if y > 0 {
                Some(row_prev_buf.as_slice())
            } else {
                None
            };
            let row_prev2 = if y > 1 {
                Some(row_prev2_buf.as_slice())
            } else {
                None
            };

            // Track left/leftleft as running variables
            let initial_left = if y > 0 {
                row_prev.unwrap()[0]
            } else if grid_y > 0 {
                buf_out[out_idx - grid_xsize].data.row(ysize - 1)[0]
            } else {
                0
            };
            let mut last = initial_left;
            let mut last_last = initial_left;

            for (x, &index) in index_img.iter().enumerate() {
                let palette_entry = get_palette_value(
                    palette,
                    index as isize,
                    c,
                    /*palette_size=*/ num_colors + num_deltas,
                    /*bit_depth=*/ bit_depth,
                );
                // Compute left/leftleft for this pixel
                let left = if x > 0 {
                    last
                } else if grid_x > 0 {
                    buf_out[out_idx - 1].data.row(y)[xsize - 1]
                } else {
                    initial_left
                };
                let leftleft = if x > 1 {
                    last_last
                } else if let Some(l_img) = if grid_x > 0 {
                    Some(&buf_out[out_idx - 1].data)
                } else {
                    None
                } {
                    l_img.row(y)[xsize + x - 2]
                } else {
                    left
                };

                let val = if index < num_deltas as i32 {
                    let pred = predictor.predict_one(
                        get_prediction_data_hoisted(
                            left, leftleft, row_prev, row_prev2, rect_width, buf_out, out_idx,
                            grid_x, grid_y, grid_xsize, x, y, xsize, ysize,
                        ),
                        /*wp_pred=*/ 0,
                    );
                    (pred + palette_entry as i64) as i32
                } else {
                    palette_entry
                };
                buf_out[out_idx].data.row_mut(y)[x] = val;
                last_last = last;
                last = val;
            }
        }
    }
}

#[allow(clippy::too_many_arguments)]
pub fn do_palette_step_group_row(
    buf_in: &[&ModularChannel],
    buf_pal: &ModularChannel,
    buf_out: &mut [&mut ModularChannel],
    grid_y: usize,
    grid_xsize: usize,
    num_colors: usize,
    num_deltas: usize,
    predictor: Predictor,
    wp_header: &WeightedHeader,
) -> Result<()> {
    let palette = &buf_pal.data;
    let h = buf_in[0].data.size().1;
    let bit_depth = buf_in[0].bit_depth.bits_per_sample().min(24) as usize;
    let grid_ysize = grid_y + 1;
    let num_c = buf_out.len() / (grid_xsize * grid_ysize);
    let total_w = buf_out[0..grid_xsize]
        .iter()
        .map(|buf| buf.data.size().0)
        .sum();
    let (xsize, ysize) = buf_out[0].data.size();

    // Helper closure: compute initial "left" value for x=0 of a grid block
    let initial_left_for = |buf_out: &[&mut ModularChannel],
                            out_idx: usize,
                            grid_y: usize,
                            grid_xsize: usize,
                            row_prev: Option<&[i32]>,
                            ysize: usize|
     -> i32 {
        if let Some(rp) = row_prev {
            rp[0]
        } else if grid_y > 0 {
            buf_out[out_idx - grid_xsize].data.row(ysize - 1)[0]
        } else {
            0
        }
    };

    // Per-grid-x row buffers: each grid block needs its own prev/prev2 tracking
    // because blocks may have different widths and the swap pattern must rotate
    // per-block across y iterations, not across grid_x iterations.
    let mut row_prev_bufs: Vec<Vec<i32>> = (0..grid_xsize).map(|_| Vec::new()).collect();
    let mut row_prev2_bufs: Vec<Vec<i32>> = (0..grid_xsize).map(|_| Vec::new()).collect();

    if predictor == Predictor::Weighted {
        for c in 0..num_c {
            let mut wp_state = WeightedPredictorState::new(wp_header, total_w)?;
            let out_row_idx = c * grid_ysize * grid_xsize + grid_y * grid_xsize;
            if grid_y > 0 {
                let prev_row_idx = out_row_idx - grid_y * grid_xsize;
                wp_state.restore_state(
                    buf_out[prev_row_idx].auxiliary_data.as_ref().unwrap(),
                    total_w,
                );
            }
            for y in 0..h {
                for (grid_x, index_buf) in buf_in.iter().enumerate().take(grid_xsize) {
                    let index_img = index_buf.data.row(y);
                    let out_idx = out_row_idx + grid_x;
                    let rect_width = buf_out[out_idx].data.size().0;

                    // Pre-fetch completed top rows for THIS grid block
                    std::mem::swap(&mut row_prev2_bufs[grid_x], &mut row_prev_bufs[grid_x]);
                    if y > 0 {
                        row_prev_bufs[grid_x].clear();
                        row_prev_bufs[grid_x].extend_from_slice(buf_out[out_idx].data.row(y - 1));
                    }
                    let row_prev = if y > 0 {
                        Some(row_prev_bufs[grid_x].as_slice())
                    } else {
                        None
                    };
                    let row_prev2 = if y > 1 {
                        Some(row_prev2_bufs[grid_x].as_slice())
                    } else {
                        None
                    };

                    let initial_left =
                        initial_left_for(buf_out, out_idx, grid_y, grid_xsize, row_prev, ysize);
                    let mut last = initial_left;
                    let mut last_last = initial_left;

                    for (x, &index) in index_img.iter().enumerate() {
                        let palette_entry = get_palette_value(
                            palette,
                            index as isize,
                            c,
                            /*palette_size=*/ num_colors + num_deltas,
                            /*bit_depth=*/ bit_depth,
                        );
                        let left = if x > 0 {
                            last
                        } else if grid_x > 0 {
                            buf_out[out_idx - 1].data.row(y)[xsize - 1]
                        } else {
                            initial_left
                        };
                        let leftleft = if x > 1 {
                            last_last
                        } else if let Some(l_img) = if grid_x > 0 {
                            Some(&buf_out[out_idx - 1].data)
                        } else {
                            None
                        } {
                            l_img.row(y)[xsize + x - 2]
                        } else {
                            left
                        };

                        let val = if index < num_deltas as i32 {
                            let prediction_data = get_prediction_data_hoisted(
                                left, leftleft, row_prev, row_prev2, rect_width, buf_out, out_idx,
                                grid_x, grid_y, grid_xsize, x, y, xsize, ysize,
                            );
                            let (pred, _) = wp_state.predict_and_property(
                                (grid_x * xsize + x, y & 1),
                                total_w,
                                &prediction_data,
                            );
                            (pred + palette_entry as i64) as i32
                        } else {
                            palette_entry
                        };
                        buf_out[out_idx].data.row_mut(y)[x] = val;
                        wp_state.update_errors(val, (grid_x * xsize + x, y & 1), total_w);
                        last_last = last;
                        last = val;
                    }
                }
            }
            let mut wp_image = Image::<i32>::new((total_w + 2, 1))?;
            wp_state.save_state(&mut wp_image, total_w);
            buf_out[out_row_idx].auxiliary_data = Some(wp_image);
        }
    } else {
        for c in 0..num_c {
            for y in 0..h {
                for (grid_x, index_buf) in buf_in.iter().enumerate().take(grid_xsize) {
                    let index_img = index_buf.data.row(y);
                    let out_idx = c * grid_ysize * grid_xsize + grid_y * grid_xsize + grid_x;
                    let rect_width = buf_out[out_idx].data.size().0;

                    // Pre-fetch completed top rows for THIS grid block
                    std::mem::swap(&mut row_prev2_bufs[grid_x], &mut row_prev_bufs[grid_x]);
                    if y > 0 {
                        row_prev_bufs[grid_x].clear();
                        row_prev_bufs[grid_x].extend_from_slice(buf_out[out_idx].data.row(y - 1));
                    }
                    let row_prev = if y > 0 {
                        Some(row_prev_bufs[grid_x].as_slice())
                    } else {
                        None
                    };
                    let row_prev2 = if y > 1 {
                        Some(row_prev2_bufs[grid_x].as_slice())
                    } else {
                        None
                    };

                    let initial_left =
                        initial_left_for(buf_out, out_idx, grid_y, grid_xsize, row_prev, ysize);
                    let mut last = initial_left;
                    let mut last_last = initial_left;

                    for (x, &index) in index_img.iter().enumerate() {
                        let palette_entry = get_palette_value(
                            palette,
                            index as isize,
                            c,
                            /*palette_size=*/ num_colors + num_deltas,
                            /*bit_depth=*/ bit_depth,
                        );
                        let left = if x > 0 {
                            last
                        } else if grid_x > 0 {
                            buf_out[out_idx - 1].data.row(y)[xsize - 1]
                        } else {
                            initial_left
                        };
                        let leftleft = if x > 1 {
                            last_last
                        } else if let Some(l_img) = if grid_x > 0 {
                            Some(&buf_out[out_idx - 1].data)
                        } else {
                            None
                        } {
                            l_img.row(y)[xsize + x - 2]
                        } else {
                            left
                        };

                        let val = if index < num_deltas as i32 {
                            let pred = predictor.predict_one(
                                get_prediction_data_hoisted(
                                    left, leftleft, row_prev, row_prev2, rect_width, buf_out,
                                    out_idx, grid_x, grid_y, grid_xsize, x, y, xsize, ysize,
                                ),
                                /*wp_pred=*/ 0,
                            );
                            (pred + palette_entry as i64) as i32
                        } else {
                            palette_entry
                        };
                        buf_out[out_idx].data.row_mut(y)[x] = val;
                        last_last = last;
                        last = val;
                    }
                }
            }
        }
    }
    Ok(())
}