otf-pixels-ops 0.2.0

Operation kernels for otf-pixels.
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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
//! [`Resize`] — separable resampling to a target size.
//!
//! # Two passes, one intermediate
//!
//! A separable filter applied in two one-dimensional passes costs
//! `O(w · h · (support_x + support_y))` instead of the product, which for
//! Lanczos3 is the difference between 6 taps per pixel and 36. The cost is one
//! intermediate buffer, `output_width × input_height`, held only for the
//! duration of a tile.
//!
//! Horizontal runs first, deliberately: when downscaling it narrows the rows
//! before the vertical pass ever touches them, so the intermediate is the
//! smaller of the two possible orderings.
//!
//! # Demand
//!
//! Resize is the first op whose `input_regions` is not a translation. An
//! output tile needs the union of the filter footprints of its rows and
//! columns, which [`Weights::footprint`] computes from the same tables the
//! kernel uses — so what is requested and what is read cannot drift apart.

use std::sync::OnceLock;

use otf_pixels_core::{
    AccessPattern, ChannelLayout, ImageDescriptor, Op, PixelFormat, PixelsError, Region, Result,
    SampleKind, Tile, TileBuf, TileMut,
};

use crate::filter::{Filter, Weights};
use crate::resample::{column_f32, column_u8, column_u16, row_f32, row_u8, row_u16};

/// How a resize reconciles the requested box with the source aspect ratio,
/// with sharp's meanings.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum Fit {
    /// Stretch to exactly the box, ignoring aspect ratio.
    #[default]
    Fill,
    /// Preserve aspect, scaled so the image fits inside the box; the output
    /// is the scaled image, at most the box.
    Inside,
    /// Preserve aspect, scaled so the image covers the box; the output is
    /// the scaled image, at least the box.
    Outside,
    /// Preserve aspect and fill the box exactly: scale to cover it, then
    /// crop the overflow, centred.
    Cover,
    /// Preserve aspect and fill the box exactly: scale to fit inside it,
    /// then pad the rest with [`ResizeOptions::background`], centred.
    Contain,
}

/// Options for [`Resize`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct ResizeOptions {
    /// The resampling filter. Defaults to [`Filter::Lanczos3`].
    pub filter: Filter,
    /// How the target box is interpreted.
    pub fit: Fit,
    /// Never scale *up*: an image already smaller than the box is left alone.
    pub without_enlargement: bool,
    /// The RGBA colour [`Fit::Contain`] pads with: opaque black by default,
    /// as in sharp. Grey formats take its luma; formats without alpha
    /// ignore its alpha.
    pub background: [u8; 4],
}

impl Default for ResizeOptions {
    fn default() -> Self {
        Self {
            filter: Filter::default(),
            fit: Fit::default(),
            without_enlargement: false,
            background: [0, 0, 0, 255],
        }
    }
}

impl ResizeOptions {
    /// Options with an explicit filter.
    #[must_use]
    pub const fn with_filter(mut self, filter: Filter) -> Self {
        self.filter = filter;
        self
    }

    /// Options with an explicit fit mode.
    #[must_use]
    pub const fn with_fit(mut self, fit: Fit) -> Self {
        self.fit = fit;
        self
    }

    /// Options with an explicit padding colour for [`Fit::Contain`].
    #[must_use]
    pub const fn with_background(mut self, rgba: [u8; 4]) -> Self {
        self.background = rgba;
        self
    }

    /// Options that refuse to enlarge.
    #[must_use]
    pub const fn without_enlargement(mut self, refuse: bool) -> Self {
        self.without_enlargement = refuse;
        self
    }
}

/// The weight tables this op was bound to when it was chained onto an image.
///
/// Tables are built **per image, not per tile**. Building them per tile would
/// make an output pixel depend on which tile it landed in, because a tile's
/// local scale is not the image's scale — and SPEC §Guarantees 2 requires the
/// output to be independent of tiling. So they are resolved once, at
/// graph-build time, and indexed by the tile's position within the image.
#[derive(Debug)]
struct Binding {
    input: ImageDescriptor,
    /// Tables for the visible part of the scaled image only.
    horizontal: Weights,
    vertical: Weights,
    layout: Layout,
}

/// Where the scaled image lands in the output.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Layout {
    /// The whole output.
    output: (u32, u32),
    /// The image's part of it, and where that part sits: everything outside
    /// is padding ([`Fit::Contain`]).
    placed: Region,
}

/// Resample an image to a new size.
#[derive(Debug)]
pub struct Resize {
    width: u32,
    height: u32,
    options: ResizeOptions,
    /// Filled by `output_descriptor` at graph-build time.
    bound: OnceLock<Binding>,
}

impl Clone for Resize {
    fn clone(&self) -> Self {
        // The binding is deliberately not cloned: a clone may be chained onto
        // a differently shaped image, and inheriting the original's tables
        // would silently resample against the wrong scale.
        Self {
            width: self.width,
            height: self.height,
            options: self.options,
            bound: OnceLock::new(),
        }
    }
}

impl Resize {
    /// Resize to `width` by `height` with `options`.
    ///
    /// # Errors
    ///
    /// Returns [`PixelsError::InvalidArgument`] if either dimension is zero.
    pub fn new(width: u32, height: u32, options: ResizeOptions) -> Result<Self> {
        if width == 0 || height == 0 {
            return Err(PixelsError::invalid_argument(
                "size",
                format!("resize target {width}x{height} has no pixels"),
            ));
        }
        Ok(Self {
            width,
            height,
            options,
            bound: OnceLock::new(),
        })
    }

    /// Resize to `width` by `height` with the default filter and fit.
    ///
    /// # Errors
    ///
    /// As [`Resize::new`].
    pub fn to(width: u32, height: u32) -> Result<Self> {
        Self::new(width, height, ResizeOptions::default())
    }

    /// The options this op was built with.
    #[must_use]
    pub const fn options(&self) -> ResizeOptions {
        self.options
    }

    /// The output size for a given input, after fit and enlargement rules.
    ///
    /// Computed rather than stored, because the requested box is a *constraint*
    /// and the answer depends on the input the op is eventually chained onto.
    #[must_use]
    pub fn target(&self, input: &ImageDescriptor) -> (u32, u32) {
        self.layout(input).1.output
    }

    /// The scaled size, the crop window within it, and the output layout.
    fn layout(&self, input: &ImageDescriptor) -> ((u32, u32), Layout, (u32, u32)) {
        // Scale by whichever axis binds, in f64 so a 30000-pixel input does
        // not lose precision on the ratio.
        let by_width = f64::from(self.width) / f64::from(input.width);
        let by_height = f64::from(self.height) / f64::from(input.height);
        let preserving = |scale: f64| {
            (
                ((f64::from(input.width) * scale).round() as u32).max(1),
                ((f64::from(input.height) * scale).round() as u32).max(1),
            )
        };
        let (mut width, mut height) = match self.options.fit {
            Fit::Fill => (self.width, self.height),
            Fit::Inside | Fit::Contain => preserving(by_width.min(by_height)),
            Fit::Outside | Fit::Cover => preserving(by_width.max(by_height)),
        };
        if self.options.without_enlargement {
            width = width.min(input.width);
            height = height.min(input.height);
        }
        let scaled = (width.max(1), height.max(1));
        let (sw, sh) = scaled;
        match self.options.fit {
            Fit::Cover => {
                let (ow, oh) = (sw.min(self.width), sh.min(self.height));
                let window = ((sw - ow) / 2, (sh - oh) / 2);
                (
                    scaled,
                    Layout {
                        output: (ow, oh),
                        placed: Region::new(0, 0, ow, oh),
                    },
                    window,
                )
            }
            Fit::Contain => {
                let (ow, oh) = (self.width.max(sw), self.height.max(sh));
                let placed = Region::new((ow - sw) / 2, (oh - sh) / 2, sw, sh);
                (
                    scaled,
                    Layout {
                        output: (ow, oh),
                        placed,
                    },
                    (0, 0),
                )
            }
            _ => (
                scaled,
                Layout {
                    output: scaled,
                    placed: Region::new(0, 0, sw, sh),
                },
                (0, 0),
            ),
        }
    }

    /// The weight tables for resampling `input`, resolved once and reused.
    ///
    /// # Errors
    ///
    /// Returns [`PixelsError::graph`] if this op was already bound to a
    /// differently shaped input. An op instance belongs to one graph edge;
    /// reusing it across two shapes would resample the second against the
    /// first one's scale, which is a silent wrong answer rather than a loud
    /// one.
    fn binding(&self, input: &ImageDescriptor) -> Result<&Binding> {
        if let Some(bound) = self.bound.get() {
            return check_binding(bound, input);
        }
        let ((width, height), layout, (wx, wy)) = self.layout(input);
        let (visible_w, visible_h) = (layout.placed.width, layout.placed.height);
        let candidate = Binding {
            input: *input,
            horizontal: Weights::build(self.options.filter, input.width, width)?
                .window(wx, visible_w),
            vertical: Weights::build(self.options.filter, input.height, height)?
                .window(wy, visible_h),
            layout,
        };
        let bound = self.bound.get_or_init(|| candidate);
        check_binding(bound, input)
    }

    /// The tables resolved at graph-build time, for use during `compute`.
    fn bound(&self) -> Result<&Binding> {
        self.bound.get().ok_or_else(|| {
            PixelsError::graph("`resize` computed before its output shape was resolved")
        })
    }
}

/// Confirm a cached binding matches the input it is about to be used for.
fn check_binding<'a>(bound: &'a Binding, input: &ImageDescriptor) -> Result<&'a Binding> {
    if bound.input.width != input.width || bound.input.height != input.height {
        return Err(PixelsError::graph(format!(
            "`resize` is bound to a {}x{} input but was given {}x{}; \
             build a new Resize per image",
            bound.input.width, bound.input.height, input.width, input.height
        )));
    }
    Ok(bound)
}

impl Op for Resize {
    /// Resize pins its own output size, so a smaller input reaches the same
    /// answer with less work. This is the op shrink-on-load exists for.
    ///
    /// The clone drops the filter tables, which are bound to the input shape
    /// they were first built for — reusing them against a reduced source is
    /// exactly the resample-at-the-wrong-scale this guards against.
    fn rescaled(&self) -> Option<std::sync::Arc<dyn Op>> {
        Some(std::sync::Arc::new(self.clone()))
    }
    fn name(&self) -> &'static str {
        "resize"
    }

    fn output_descriptor(&self, inputs: &[ImageDescriptor]) -> Result<ImageDescriptor> {
        let input = inputs
            .first()
            .ok_or_else(|| PixelsError::graph("`resize` takes one input, got none"))?;
        // Resolving the binding here is what makes the tables per-image:
        // `output_descriptor` runs exactly once per graph edge, at build time.
        self.binding(input)?;
        let (width, height) = self.target(input);
        input.resized(width, height)
    }

    fn input_regions(&self, output: Region, inputs: &[ImageDescriptor]) -> Result<Vec<Region>> {
        let input = inputs
            .first()
            .ok_or_else(|| PixelsError::graph("`resize` takes one input, got none"))?;
        let bound = self.binding(input)?;
        // Only the part of the tile the image covers reads anything; a tile
        // of pure padding still names a valid (1x1) input region, as
        // `composite` does for an overlay it misses.
        let Some(image) = bound.image_part(output) else {
            return Ok(vec![Region::new(0, 0, 1, 1)]);
        };
        // The footprint of exactly the output rows and columns requested,
        // taken from the same tables the kernel will use — so what is asked
        // for and what is read cannot drift apart.
        let (x, width) = bound.horizontal.footprint(image.x, image.width);
        let (y, height) = bound.vertical.footprint(image.y, image.height);
        Ok(vec![Region::new(x, y, width, height)])
    }

    fn access_pattern(&self) -> AccessPattern {
        // Resize reads a two-dimensional neighbourhood: every output row draws
        // on a band of input rows, so square tiles keep that band small.
        AccessPattern::Spatial
    }

    fn compute(&self, inputs: &[Tile<'_>], output: &mut TileMut<'_>) -> Result<()> {
        let input = inputs
            .first()
            .ok_or_else(|| PixelsError::graph("`resize` takes one input tile, got none"))?;

        let format = output.pixel();
        if input.pixel() != format {
            return Err(PixelsError::graph(format!(
                "`resize` input is {} but output is {format}",
                input.pixel()
            )));
        }
        let channels = format.channels();
        let bound = self.bound()?;
        let region = output.region();
        if bound.layout.placed.contains(region)
            && bound.layout.placed.x == 0
            && bound.layout.placed.y == 0
        {
            return resample_tile(bound, input, output, channels);
        }
        // Padding: background everywhere, then the image's part resampled in
        // the scaled image's own coordinates and copied into place.
        let fill = background_pixel(format, self.options.background);
        for row in output.rows_mut() {
            for (slot, &byte) in row.iter_mut().zip(fill.iter().cycle()) {
                *slot = byte;
            }
        }
        let Some(image) = bound.image_part(region) else {
            return Ok(());
        };
        let mut part = TileBuf::zeroed(image, format)?;
        resample_tile(bound, input, &mut part.as_tile_mut()?, channels)?;
        let (px, py) = (bound.layout.placed.x, bound.layout.placed.y);
        let bytes = format.bytes_per_pixel();
        let row_len = image.width as usize * bytes;
        for (i, from) in part.bytes().chunks_exact(row_len.max(1)).enumerate() {
            let Some(row) = output.row_mut(image.y + py + i as u32) else {
                continue;
            };
            let at = (image.x + px - region.x) as usize * bytes;
            if let Some(to) = row.get_mut(at..at + row_len) {
                to.copy_from_slice(from);
            }
        }
        Ok(())
    }
}

impl Binding {
    /// The part of output `region` the image covers, in the scaled image's
    /// coordinates; `None` for a tile of pure padding.
    fn image_part(&self, region: Region) -> Option<Region> {
        let placed = self.layout.placed;
        let overlap = region.intersect(placed);
        (overlap.width > 0 && overlap.height > 0).then(|| {
            Region::new(
                overlap.x - placed.x,
                overlap.y - placed.y,
                overlap.width,
                overlap.height,
            )
        })
    }
}

/// One pixel of `rgba` in `format`'s bytes.
fn background_pixel(format: PixelFormat, rgba: [u8; 4]) -> Vec<u8> {
    let [r, g, b, a] = rgba;
    let luma = ((u32::from(r) * 299 + u32::from(g) * 587 + u32::from(b) * 114 + 500) / 1000) as u8;
    let samples: Vec<u8> = match format.layout() {
        ChannelLayout::Gray => vec![luma],
        ChannelLayout::GrayAlpha => vec![luma, a],
        ChannelLayout::Rgb => vec![r, g, b],
        ChannelLayout::Rgba => vec![r, g, b, a],
    };
    match format.sample_kind() {
        SampleKind::U8 => samples,
        SampleKind::U16 => samples
            .iter()
            .flat_map(|&v| (u16::from(v) * 257).to_ne_bytes())
            .collect(),
        SampleKind::F32 => samples
            .iter()
            .flat_map(|&v| (f32::from(v) / 255.0).to_ne_bytes())
            .collect(),
    }
}

/// Run both passes for one output tile.
///
/// The tables are the image's, not the tile's. Each output row and column of
/// the tile looks up its own whole-image run, and reads it out of the input
/// tile at an offset — which is why the answer is the same however the image
/// is cut up.
fn resample_tile(
    bound: &Binding,
    input: &Tile<'_>,
    output: &mut TileMut<'_>,
    channels: usize,
) -> Result<()> {
    let region = output.region();
    let source = input.region();
    let horizontal = bound
        .horizontal
        .for_tile(region.x, region.width, source.x)?;
    let vertical = bound.vertical.for_tile(region.y, region.height, source.y)?;

    let format = output.pixel();
    let intermediate_row = region.width as usize * channels;

    match format.sample_kind() {
        SampleKind::U8 => {
            // Horizontal pass into an intermediate of output width by input
            // height, then vertical down each column of that.
            let mut intermediate = vec![0_u8; intermediate_row * source.height as usize];
            for y in 0..source.height {
                let Some(row) = input.row(source.y + y) else {
                    continue;
                };
                let at = y as usize * intermediate_row;
                let Some(target) = intermediate.get_mut(at..at + intermediate_row) else {
                    continue;
                };
                row_u8(row, target, channels, &horizontal);
            }

            // The vertical pass accumulates a whole output row at a time,
            // reading input rows sequentially. See the note in `resample`.
            let mut accumulator = vec![0_i32; intermediate_row];
            let mut resampled = vec![0_u8; intermediate_row];
            for (index, run) in vertical.runs().iter().enumerate() {
                column_u8(
                    &intermediate,
                    intermediate_row,
                    run.start as usize,
                    vertical.quantized(run),
                    &mut accumulator,
                    &mut resampled,
                );
                write_row(output, region.y + index as u32, &resampled);
            }
        }
        SampleKind::U16 => {
            let mut intermediate = vec![0_u16; intermediate_row * source.height as usize];
            for y in 0..source.height {
                let Some(row) = input.row(source.y + y) else {
                    continue;
                };
                let wide = to_u16(row);
                let at = y as usize * intermediate_row;
                let Some(target) = intermediate.get_mut(at..at + intermediate_row) else {
                    continue;
                };
                row_u16(&wide, target, channels, &horizontal);
            }
            let mut accumulator = vec![0.0_f32; intermediate_row];
            let mut resampled = vec![0_u16; intermediate_row];
            for (index, run) in vertical.runs().iter().enumerate() {
                column_u16(
                    &intermediate,
                    intermediate_row,
                    run.start as usize,
                    vertical.exact(run),
                    &mut accumulator,
                    &mut resampled,
                );
                let bytes: Vec<u8> = resampled.iter().flat_map(|v| v.to_ne_bytes()).collect();
                write_row(output, region.y + index as u32, &bytes);
            }
        }
        SampleKind::F32 => {
            let mut intermediate = vec![0.0_f32; intermediate_row * source.height as usize];
            for y in 0..source.height {
                let Some(row) = input.row(source.y + y) else {
                    continue;
                };
                let floats = to_f32(row);
                let at = y as usize * intermediate_row;
                let Some(target) = intermediate.get_mut(at..at + intermediate_row) else {
                    continue;
                };
                row_f32(&floats, target, channels, &horizontal);
            }
            let mut accumulator = vec![0.0_f32; intermediate_row];
            let mut resampled = vec![0.0_f32; intermediate_row];
            for (index, run) in vertical.runs().iter().enumerate() {
                column_f32(
                    &intermediate,
                    intermediate_row,
                    run.start as usize,
                    vertical.exact(run),
                    &mut accumulator,
                    &mut resampled,
                );
                let bytes: Vec<u8> = resampled.iter().flat_map(|v| v.to_ne_bytes()).collect();
                write_row(output, region.y + index as u32, &bytes);
            }
        }
    }
    Ok(())
}

/// Reinterpret a native-endian byte row as 16-bit samples.
fn to_u16(row: &[u8]) -> Vec<u16> {
    row.chunks_exact(2)
        .map(|pair| {
            u16::from_ne_bytes([
                pair.first().copied().unwrap_or(0),
                pair.get(1).copied().unwrap_or(0),
            ])
        })
        .collect()
}

/// Reinterpret a native-endian byte row as float samples.
fn to_f32(row: &[u8]) -> Vec<f32> {
    row.chunks_exact(4)
        .map(|quad| {
            let mut bytes = [0_u8; 4];
            for (slot, &byte) in bytes.iter_mut().zip(quad) {
                *slot = byte;
            }
            f32::from_ne_bytes(bytes)
        })
        .collect()
}

/// Write a resampled row into the output tile.
fn write_row(output: &mut TileMut<'_>, y: u32, bytes: &[u8]) {
    let Some(row) = output.row_mut(y) else { return };
    let len = row.len().min(bytes.len());
    if let (Some(to), Some(from)) = (row.get_mut(..len), bytes.get(..len)) {
        to.copy_from_slice(from);
    }
}

#[cfg(test)]
#[allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::indexing_slicing,
    clippy::panic,
    reason = "tests operate on known-good values and assert shapes directly"
)]
mod tests {
    use super::*;
    use otf_pixels_core::{PixelFormat, TileBuf};

    /// Resize a whole image in one tile, the simple case.
    fn resize_whole(
        op: &Resize,
        input: &ImageDescriptor,
        bytes: &[u8],
    ) -> Result<(ImageDescriptor, Vec<u8>)> {
        let out_desc = op.output_descriptor(std::slice::from_ref(input))?;
        let source = TileBuf::from_vec(input.region(), input.pixel, bytes.to_vec())?;
        let mut target = TileBuf::for_image(&out_desc)?;
        let regions = op.input_regions(out_desc.region(), std::slice::from_ref(input))?;
        assert_eq!(
            regions[0],
            input.region(),
            "whole-image demand should be the whole input"
        );
        op.compute(&[source.as_tile()?], &mut target.as_tile_mut()?)?;
        Ok((out_desc, target.into_bytes()))
    }

    fn ramp(width: u32, height: u32, format: PixelFormat) -> (ImageDescriptor, Vec<u8>) {
        let descriptor = ImageDescriptor::new(width, height, format).unwrap();
        let len = descriptor.byte_len().unwrap();
        let bytes = (0..len).map(|i| ((i * 37) % 251) as u8).collect();
        (descriptor, bytes)
    }

    #[test]
    fn the_output_is_independent_of_how_the_image_is_tiled() {
        // The property that makes per-image weight tables necessary rather
        // than merely tidy. Resizing in one tile and in many must give the
        // same bytes, or SPEC §Guarantees 2 is false.
        let (input, bytes) = ramp(97, 71, PixelFormat::Rgb8);
        let op = Resize::new(41, 33, ResizeOptions::default()).unwrap();
        let (out_desc, whole) = resize_whole(&op, &input, &bytes).unwrap();

        for (tile_w, tile_h) in [(8_u32, 8_u32), (16, 4), (41, 1), (1, 33), (7, 13)] {
            // A fresh op per run: a binding belongs to one graph edge.
            let op = Resize::new(41, 33, ResizeOptions::default()).unwrap();
            op.output_descriptor(std::slice::from_ref(&input)).unwrap();
            let source = TileBuf::from_vec(input.region(), input.pixel, bytes.clone()).unwrap();
            let mut target = TileBuf::for_image(&out_desc).unwrap();

            let mut y = 0;
            while y < out_desc.height {
                let h = tile_h.min(out_desc.height - y);
                let mut x = 0;
                while x < out_desc.width {
                    let w = tile_w.min(out_desc.width - x);
                    let region = Region::new(x, y, w, h);
                    let demand = op
                        .input_regions(region, std::slice::from_ref(&input))
                        .unwrap();
                    let window = source.as_tile().unwrap();
                    let mut sub = TileBuf::zeroed(region, out_desc.pixel).unwrap();

                    // Hand the op exactly the footprint it asked for.
                    let mut cut = TileBuf::zeroed(demand[0], input.pixel).unwrap();
                    otf_pixels_core::copy_region(
                        &window,
                        &mut cut.as_tile_mut().unwrap(),
                        demand[0],
                    )
                    .unwrap();

                    op.compute(&[cut.as_tile().unwrap()], &mut sub.as_tile_mut().unwrap())
                        .unwrap();
                    otf_pixels_core::copy_region(
                        &sub.as_tile().unwrap(),
                        &mut target.as_tile_mut().unwrap(),
                        region,
                    )
                    .unwrap();
                    x += w;
                }
                y += h;
            }
            assert_eq!(
                target.into_bytes(),
                whole,
                "tiling at {tile_w}x{tile_h} changed the pixels"
            );
        }
    }

    /// Run `op` over the whole output in `tile`-sized pieces, each handed
    /// exactly the input it demands.
    fn resize_tiled(
        op: &Resize,
        input: &ImageDescriptor,
        bytes: &[u8],
        tile: (u32, u32),
    ) -> (ImageDescriptor, Vec<u8>) {
        let out_desc = op.output_descriptor(std::slice::from_ref(input)).unwrap();
        let source = TileBuf::from_vec(input.region(), input.pixel, bytes.to_vec()).unwrap();
        let mut target = TileBuf::for_image(&out_desc).unwrap();
        let mut y = 0;
        while y < out_desc.height {
            let h = tile.1.min(out_desc.height - y);
            let mut x = 0;
            while x < out_desc.width {
                let w = tile.0.min(out_desc.width - x);
                let region = Region::new(x, y, w, h);
                let demand = op
                    .input_regions(region, std::slice::from_ref(input))
                    .unwrap();
                let mut cut = TileBuf::zeroed(demand[0], input.pixel).unwrap();
                otf_pixels_core::copy_region(
                    &source.as_tile().unwrap(),
                    &mut cut.as_tile_mut().unwrap(),
                    demand[0],
                )
                .unwrap();
                let mut sub = TileBuf::zeroed(region, out_desc.pixel).unwrap();
                op.compute(&[cut.as_tile().unwrap()], &mut sub.as_tile_mut().unwrap())
                    .unwrap();
                otf_pixels_core::copy_region(
                    &sub.as_tile().unwrap(),
                    &mut target.as_tile_mut().unwrap(),
                    region,
                )
                .unwrap();
                x += w;
            }
            y += h;
        }
        (out_desc, target.into_bytes())
    }

    fn with_fit(width: u32, height: u32, fit: Fit) -> Resize {
        Resize::new(
            width,
            height,
            ResizeOptions::default()
                .with_fit(fit)
                .with_background([10, 20, 30, 255]),
        )
        .unwrap()
    }

    #[test]
    fn outside_and_inside_scale_by_the_other_axis() {
        let (input, _) = ramp(200, 100, PixelFormat::Rgb8);
        assert_eq!(with_fit(50, 50, Fit::Inside).target(&input), (50, 25));
        assert_eq!(with_fit(50, 50, Fit::Outside).target(&input), (100, 50));
        assert_eq!(with_fit(50, 50, Fit::Cover).target(&input), (50, 50));
        assert_eq!(with_fit(50, 50, Fit::Contain).target(&input), (50, 50));
        assert_eq!(with_fit(50, 50, Fit::Fill).target(&input), (50, 50));
    }

    #[test]
    fn cover_is_outside_then_a_centred_crop() {
        for (w, h, format) in [
            (200, 100, PixelFormat::Rgb8),
            (61, 97, PixelFormat::Rgba16),
            (90, 90, PixelFormat::Gray8),
        ] {
            let (input, bytes) = ramp(w, h, format);
            let (outside_desc, outside) = resize_tiled(
                &with_fit(40, 30, Fit::Outside),
                &input,
                &bytes,
                (1000, 1000),
            );
            let bpp = format.bytes_per_pixel();
            for tile in [(1000, 1000), (7, 5), (40, 1)] {
                let (desc, cover) =
                    resize_tiled(&with_fit(40, 30, Fit::Cover), &input, &bytes, tile);
                assert_eq!(
                    (desc.width, desc.height),
                    (40.min(outside_desc.width), 30.min(outside_desc.height))
                );
                let (dx, dy) = (
                    (outside_desc.width - desc.width) / 2,
                    (outside_desc.height - desc.height) / 2,
                );
                for y in 0..desc.height as usize {
                    let from =
                        ((y + dy as usize) * outside_desc.width as usize + dx as usize) * bpp;
                    let row = desc.width as usize * bpp;
                    assert_eq!(
                        &cover[y * row..(y + 1) * row],
                        &outside[from..from + row],
                        "{w}x{h} {format} row {y} tile {tile:?}"
                    );
                }
            }
        }
    }

    #[test]
    fn contain_is_inside_centred_on_the_background() {
        for (w, h, format) in [
            (200, 100, PixelFormat::Rgb8),
            (61, 97, PixelFormat::Rgba8),
            (30, 90, PixelFormat::Gray16),
        ] {
            let (input, bytes) = ramp(w, h, format);
            let (inside_desc, inside) =
                resize_tiled(&with_fit(40, 30, Fit::Inside), &input, &bytes, (1000, 1000));
            let bpp = format.bytes_per_pixel();
            let fill = background_pixel(format, [10, 20, 30, 255]);
            for tile in [(1000, 1000), (7, 5), (3, 30), (40, 2)] {
                let (desc, contain) =
                    resize_tiled(&with_fit(40, 30, Fit::Contain), &input, &bytes, tile);
                assert_eq!((desc.width, desc.height), (40, 30));
                let (px, py) = ((40 - inside_desc.width) / 2, (30 - inside_desc.height) / 2);
                for y in 0..30_u32 {
                    for x in 0..40_u32 {
                        let at = ((y * 40 + x) as usize) * bpp;
                        let got = &contain[at..at + bpp];
                        let inside_x = x.checked_sub(px).filter(|&v| v < inside_desc.width);
                        let inside_y = y.checked_sub(py).filter(|&v| v < inside_desc.height);
                        let want = match (inside_x, inside_y) {
                            (Some(ix), Some(iy)) => {
                                let from = ((iy * inside_desc.width + ix) as usize) * bpp;
                                &inside[from..from + bpp]
                            }
                            _ => &fill[..],
                        };
                        assert_eq!(got, want, "{w}x{h} {format} at ({x}, {y}) tile {tile:?}");
                    }
                }
            }
        }
    }

    #[test]
    fn the_background_follows_the_pixel_format() {
        assert_eq!(
            background_pixel(PixelFormat::Rgba8, [1, 2, 3, 4]),
            vec![1, 2, 3, 4]
        );
        assert_eq!(
            background_pixel(PixelFormat::Rgb8, [1, 2, 3, 4]),
            vec![1, 2, 3]
        );
        assert_eq!(
            background_pixel(PixelFormat::Gray8, [255, 255, 255, 0]),
            vec![255]
        );
        assert_eq!(
            background_pixel(PixelFormat::GrayA8, [0, 0, 0, 9]),
            vec![0, 9]
        );
        assert_eq!(
            background_pixel(PixelFormat::Gray16, [255, 0, 0, 255]),
            (76_u16 * 257).to_ne_bytes().to_vec()
        );
    }

    #[test]
    fn a_one_to_one_resize_returns_the_image_unchanged() {
        for format in [
            PixelFormat::Gray8,
            PixelFormat::Rgb8,
            PixelFormat::Rgba8,
            PixelFormat::Gray16,
            PixelFormat::Rgb16,
        ] {
            let (input, bytes) = ramp(31, 23, format);
            let op = Resize::new(31, 23, ResizeOptions::default()).unwrap();
            let (out_desc, out) = resize_whole(&op, &input, &bytes).unwrap();
            assert_eq!(out_desc.width, 31);
            assert_eq!(out, bytes, "{format} changed at 1:1");
        }
    }

    #[test]
    fn a_flat_image_stays_flat_at_every_scale_and_filter() {
        for filter in [
            Filter::Nearest,
            Filter::Box,
            Filter::Bilinear,
            Filter::CatmullRom,
            Filter::Mitchell,
            Filter::Lanczos2,
            Filter::Lanczos3,
        ] {
            for (w, h) in [(10_u32, 10_u32), (200, 150), (37, 91)] {
                let input = ImageDescriptor::new(64, 64, PixelFormat::Rgb8).unwrap();
                let bytes = vec![137_u8; input.byte_len().unwrap()];
                let op = Resize::new(w, h, ResizeOptions::default().with_filter(filter)).unwrap();
                let (_, out) = resize_whole(&op, &input, &bytes).unwrap();
                assert!(
                    out.iter().all(|&v| v == 137),
                    "{} to {w}x{h} did not stay flat",
                    filter.as_str()
                );
            }
        }
    }

    #[test]
    fn fit_inside_preserves_the_aspect_ratio() {
        let input = ImageDescriptor::new(1000, 500, PixelFormat::Rgb8).unwrap();
        let op = Resize::new(100, 100, ResizeOptions::default().with_fit(Fit::Inside)).unwrap();
        assert_eq!(
            op.target(&input),
            (100, 50),
            "wide image should bind on width"
        );

        let tall = ImageDescriptor::new(500, 1000, PixelFormat::Rgb8).unwrap();
        assert_eq!(
            op.target(&tall),
            (50, 100),
            "tall image should bind on height"
        );
    }

    #[test]
    fn fit_fill_ignores_the_aspect_ratio() {
        let input = ImageDescriptor::new(1000, 500, PixelFormat::Rgb8).unwrap();
        let op = Resize::new(100, 100, ResizeOptions::default()).unwrap();
        assert_eq!(op.target(&input), (100, 100));
    }

    #[test]
    fn without_enlargement_leaves_a_small_image_alone() {
        let input = ImageDescriptor::new(40, 30, PixelFormat::Rgb8).unwrap();
        let options = ResizeOptions::default()
            .with_fit(Fit::Inside)
            .without_enlargement(true);
        let op = Resize::new(1000, 1000, options).unwrap();
        assert_eq!(op.target(&input), (40, 30));

        // The same options must still shrink something larger than the box.
        let big = ImageDescriptor::new(4000, 3000, PixelFormat::Rgb8).unwrap();
        assert_eq!(op.target(&big), (1000, 750));
    }

    #[test]
    fn a_zero_target_is_an_error() {
        assert!(Resize::new(0, 10, ResizeOptions::default()).is_err());
        assert!(Resize::new(10, 0, ResizeOptions::default()).is_err());
    }

    #[test]
    fn a_target_never_collapses_to_zero() {
        // Fit::Inside on an extreme aspect ratio would round a dimension to
        // zero, which is not a representable image.
        let input = ImageDescriptor::new(10_000, 3, PixelFormat::Gray8).unwrap();
        let op = Resize::new(50, 50, ResizeOptions::default().with_fit(Fit::Inside)).unwrap();
        let (w, h) = op.target(&input);
        assert!(w >= 1 && h >= 1, "target collapsed to {w}x{h}");
    }

    #[test]
    fn reusing_an_op_across_two_shapes_is_an_error_not_a_wrong_answer() {
        // A binding belongs to one graph edge. Silently resampling the second
        // image against the first one's scale would be a wrong answer with no
        // symptom, so it is refused instead.
        let first = ImageDescriptor::new(100, 100, PixelFormat::Gray8).unwrap();
        let second = ImageDescriptor::new(200, 200, PixelFormat::Gray8).unwrap();
        let op = Resize::to(50, 50).unwrap();
        op.output_descriptor(std::slice::from_ref(&first)).unwrap();
        let error = op
            .output_descriptor(std::slice::from_ref(&second))
            .unwrap_err();
        assert!(error.to_string().contains("bound to"), "{error}");
    }

    #[test]
    fn a_clone_can_be_bound_to_a_different_shape() {
        // The counterpart: cloning is how a caller reuses a configured op, so
        // a clone must start unbound rather than inherit the original tables.
        let first = ImageDescriptor::new(100, 100, PixelFormat::Gray8).unwrap();
        let second = ImageDescriptor::new(200, 200, PixelFormat::Gray8).unwrap();
        let op = Resize::to(50, 50).unwrap();
        op.output_descriptor(std::slice::from_ref(&first)).unwrap();
        let fresh = op.clone();
        assert!(
            fresh
                .output_descriptor(std::slice::from_ref(&second))
                .is_ok()
        );
    }

    #[test]
    fn demand_never_reaches_outside_the_input() {
        // Op::input_regions requires clamped regions; an op reading past its
        // input is a defect, and Lanczos3 near an edge is where it would.
        let input = ImageDescriptor::new(50, 40, PixelFormat::Rgb8).unwrap();
        let op = Resize::new(200, 160, ResizeOptions::default()).unwrap();
        let out = op.output_descriptor(std::slice::from_ref(&input)).unwrap();
        for y in 0..out.height {
            for x in 0..out.width {
                let region = Region::new(x, y, 1, 1);
                let demand = op
                    .input_regions(region, std::slice::from_ref(&input))
                    .unwrap();
                let r = demand[0];
                assert!(
                    r.x + r.width <= input.width && r.y + r.height <= input.height,
                    "demand {r} for output {region} leaves a {}x{} input",
                    input.width,
                    input.height
                );
            }
        }
    }

    #[test]
    fn resize_is_deterministic() {
        let (input, bytes) = ramp(123, 87, PixelFormat::Rgba8);
        let op = Resize::to(61, 43).unwrap();
        let (_, first) = resize_whole(&op, &input, &bytes).unwrap();
        for _ in 0..5 {
            let op = Resize::to(61, 43).unwrap();
            let (_, again) = resize_whole(&op, &input, &bytes).unwrap();
            assert_eq!(again, first, "resize is not deterministic");
        }
    }
}