mlxrs 0.1.0

Safe Rust bindings for Apple's MLX array framework, with LM, VLM, audio, and embeddings support
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
use super::*;
use ::image::{
  DynamicImage, GrayAlphaImage, GrayImage, ImageBuffer, Luma, LumaA, Rgb, RgbImage, Rgba,
  RgbaImage, metadata::Orientation,
};

/// Build a `width × height` Rgb8 image whose pixel values encode
/// `(x, y)` so any rotation/flip is checkable byte-for-byte against
/// the upstream `image::imageops` reference. Default test sizes use
/// 4x3 (non-square) so 90° rotates flip dimensions visibly.
fn xy_encoded_rgb8(width: u32, height: u32) -> DynamicImage {
  let mut buf = RgbImage::new(width, height);
  for y in 0..height {
    for x in 0..width {
      // (10*x, 10*y, 200) is unique-per-pixel for x,y < 25 and stays
      // well below 256, so rotate/flip orderings are visible at the
      // byte level.
      buf.put_pixel(x, y, Rgb([(x * 10) as u8, (y * 10) as u8, 200]));
    }
  }
  DynamicImage::ImageRgb8(buf)
}

/// Luma8 (single-channel u8) variant of [`xy_encoded_rgb8`].
/// Encodes the row-major pixel index so rotation invariants are
/// visible at the byte level — for a 4x3 image, source pixel
/// `(x, y)` carries value `y * 4 + x` (0..12).
fn xy_encoded_luma8(width: u32, height: u32) -> DynamicImage {
  let mut buf = GrayImage::new(width, height);
  for y in 0..height {
    for x in 0..width {
      buf.put_pixel(x, y, Luma([(y * width + x) as u8]));
    }
  }
  DynamicImage::ImageLuma8(buf)
}

/// LumaA8 (grayscale + alpha) variant: luma encodes `y*w + x`,
/// alpha encodes `255 - (y*w + x)` so each byte position is
/// individually traceable through the rotation.
fn xy_encoded_luma_alpha8(width: u32, height: u32) -> DynamicImage {
  let mut buf = GrayAlphaImage::new(width, height);
  for y in 0..height {
    for x in 0..width {
      let v = (y * width + x) as u8;
      buf.put_pixel(x, y, LumaA([v, 255 - v]));
    }
  }
  DynamicImage::ImageLumaA8(buf)
}

/// Rgba8 variant: each channel is independently traceable
/// (R = 10*x, G = 10*y, B = 200, A = 255 - 10*y) so the
/// 4-byte stride is visible end-to-end.
fn xy_encoded_rgba8(width: u32, height: u32) -> DynamicImage {
  let mut buf = RgbaImage::new(width, height);
  for y in 0..height {
    for x in 0..width {
      buf.put_pixel(
        x,
        y,
        Rgba([(x * 10) as u8, (y * 10) as u8, 200, 255 - (y * 10) as u8]),
      );
    }
  }
  DynamicImage::ImageRgba8(buf)
}

#[test]
fn no_transforms_passes_through_unchanged() {
  // Identity-orientation path: no rotation overhead, no clone. The
  // returned image's raw bytes must match the source exactly
  // (rules out an accidental rotate dispatch).
  let img = xy_encoded_rgb8(4, 3);
  let original_bytes = img.as_rgb8().expect("rgb8 source").as_raw().clone();
  let out = apply_orientation_fallible(img, Orientation::NoTransforms).expect("infallible path");
  assert_eq!(out.width(), 4);
  assert_eq!(out.height(), 3);
  assert_eq!(
    out.as_rgb8().expect("rgb8 output").as_raw(),
    &original_bytes
  );
}

#[test]
fn rotate180_in_place_path_matches_reference() {
  // Rotate180 goes through `rotate180_in_place` upstream (no source-
  // sized alloc). Verify the dispatcher routes it through the
  // no-allocation arm AND that the pixel transform matches
  // `image::imageops::rotate180`.
  let img = xy_encoded_rgb8(4, 3);
  let reference: ImageBuffer<Rgb<u8>, Vec<u8>> =
    ::image::imageops::rotate180(img.as_rgb8().expect("rgb8 source"));
  let out =
    apply_orientation_fallible(img, Orientation::Rotate180).expect("in-place path infallible");
  assert_eq!(out.width(), 4, "Rotate180 preserves width");
  assert_eq!(out.height(), 3, "Rotate180 preserves height");
  assert_eq!(
    out.as_rgb8().expect("rgb8 output").as_raw(),
    reference.as_raw(),
    "Rotate180 pixel bytes must match image::imageops::rotate180"
  );
}

#[test]
fn flip_horizontal_in_place_path_matches_reference() {
  // FlipHorizontal goes through `fliph_in_place` upstream — verify
  // pixel-level parity with `image::imageops::flip_horizontal`.
  let img = xy_encoded_rgb8(4, 3);
  let reference = ::image::imageops::flip_horizontal(img.as_rgb8().expect("rgb8 source"));
  let out =
    apply_orientation_fallible(img, Orientation::FlipHorizontal).expect("in-place path infallible");
  assert_eq!(
    out.as_rgb8().expect("rgb8 output").as_raw(),
    reference.as_raw(),
    "FlipHorizontal pixel bytes must match image::imageops::flip_horizontal"
  );
}

// -- Rgb8 manual rotation parity -------------------------------------

#[test]
fn rotate90_rgb8_manual_matches_reference() {
  // Rotate90 on Rgb8 now goes through the truly-fallible
  // manual `rotate_buf` path. Must:
  //   (1) succeed for a small input under `MAX_DECODED_IMAGE_BYTES`,
  //   (2) swap dimensions (4x3 → 3x4),
  //   (3) produce byte-identical pixels to `image::imageops::rotate90`.
  let img = xy_encoded_rgb8(4, 3);
  let reference = ::image::imageops::rotate90(img.as_rgb8().expect("rgb8 source"));
  let out = apply_orientation_fallible(img, Orientation::Rotate90)
    .expect("manual rotate90 should succeed for a 4x3 image well under the 512 MiB ceiling");
  assert_eq!(out.width(), 3, "Rotate90 swaps width <- height");
  assert_eq!(out.height(), 4, "Rotate90 swaps height <- width");
  assert_eq!(
    out.as_rgb8().expect("rgb8 output").as_raw(),
    reference.as_raw(),
    "manual rotate must yield byte-identical pixels to image::imageops::rotate90"
  );
}

#[test]
fn rotate270_rgb8_manual_matches_reference() {
  let img = xy_encoded_rgb8(4, 3);
  let reference = ::image::imageops::rotate270(img.as_rgb8().expect("rgb8 source"));
  let out = apply_orientation_fallible(img, Orientation::Rotate270).expect("rotate270 ok");
  assert_eq!(out.width(), 3);
  assert_eq!(out.height(), 4);
  assert_eq!(
    out.as_rgb8().expect("rgb8 output").as_raw(),
    reference.as_raw(),
  );
}

#[test]
fn rotate90_fliph_rgb8_composite_matches_reference() {
  // Rotate90FlipH = rotate90 + fliph (the composite collapses to
  // `dst.put_pixel(y, x, src.get_pixel(x, y))` in `rotate_buf`).
  // Verify the composite output is byte-identical to the
  // image-rs dispatch.
  let img = xy_encoded_rgb8(4, 3);
  let rotated = ::image::imageops::rotate90(img.as_rgb8().expect("rgb8 source"));
  let reference = ::image::imageops::flip_horizontal(&rotated);
  let out = apply_orientation_fallible(img, Orientation::Rotate90FlipH)
    .expect("Rotate90FlipH composite ok");
  assert_eq!(out.width(), reference.width());
  assert_eq!(out.height(), reference.height());
  assert_eq!(
    out.as_rgb8().expect("rgb8 output").as_raw(),
    reference.as_raw(),
  );
}

#[test]
fn rotate270_fliph_rgb8_composite_matches_reference() {
  let img = xy_encoded_rgb8(4, 3);
  let rotated = ::image::imageops::rotate270(img.as_rgb8().expect("rgb8 source"));
  let reference = ::image::imageops::flip_horizontal(&rotated);
  let out = apply_orientation_fallible(img, Orientation::Rotate270FlipH)
    .expect("Rotate270FlipH composite ok");
  assert_eq!(out.width(), 3);
  assert_eq!(out.height(), 4);
  assert_eq!(
    out.as_rgb8().expect("rgb8 output").as_raw(),
    reference.as_raw(),
  );
}

// -- Rgba8 manual rotation parity ------------------------------------

#[test]
fn rotate90_rgba8_manual_matches_reference() {
  // Rgba8 has 4 channels (R, G, B, A) — verify the per-pixel
  // 4-byte memcpy in `rotate_buf` preserves the alpha channel
  // alongside RGB.
  let img = xy_encoded_rgba8(4, 3);
  let reference = ::image::imageops::rotate90(img.as_rgba8().expect("rgba8 source"));
  let out = apply_orientation_fallible(img, Orientation::Rotate90).expect("rgba8 rotate90 ok");
  assert_eq!(out.width(), 3);
  assert_eq!(out.height(), 4);
  assert_eq!(
    out.as_rgba8().expect("rgba8 output").as_raw(),
    reference.as_raw(),
    "manual Rgba8 rotate90 must preserve all 4 channels byte-identical to image::imageops::rotate90"
  );
}

#[test]
fn rotate270_fliph_rgba8_composite_matches_reference() {
  // Composite path × alpha channel: verify the
  // collapsed `dst[h-1-y, w-1-x] = src[x, y]` index math handles
  // the 4-byte stride correctly.
  let img = xy_encoded_rgba8(4, 3);
  let rotated = ::image::imageops::rotate270(img.as_rgba8().expect("rgba8 source"));
  let reference = ::image::imageops::flip_horizontal(&rotated);
  let out =
    apply_orientation_fallible(img, Orientation::Rotate270FlipH).expect("rgba8 Rotate270FlipH ok");
  assert_eq!(out.width(), 3);
  assert_eq!(out.height(), 4);
  assert_eq!(
    out.as_rgba8().expect("rgba8 output").as_raw(),
    reference.as_raw(),
  );
}

// -- Luma8 manual rotation parity ------------------------------------

#[test]
fn rotate90_luma8_manual_matches_reference() {
  // Luma8 has 1 channel — verify the per-pixel 1-byte memcpy in
  // `rotate_buf` produces the same byte order as image-rs.
  let img = xy_encoded_luma8(4, 3);
  let reference = ::image::imageops::rotate90(img.as_luma8().expect("luma8 source"));
  let out = apply_orientation_fallible(img, Orientation::Rotate90).expect("luma8 rotate90 ok");
  assert_eq!(out.width(), 3);
  assert_eq!(out.height(), 4);
  assert_eq!(
    out.as_luma8().expect("luma8 output").as_raw(),
    reference.as_raw(),
    "manual Luma8 rotate90 must match image::imageops::rotate90 byte-identical"
  );
}

#[test]
fn rotate270_luma8_manual_matches_reference() {
  let img = xy_encoded_luma8(4, 3);
  let reference = ::image::imageops::rotate270(img.as_luma8().expect("luma8 source"));
  let out = apply_orientation_fallible(img, Orientation::Rotate270).expect("luma8 rotate270 ok");
  assert_eq!(out.width(), 3);
  assert_eq!(out.height(), 4);
  assert_eq!(
    out.as_luma8().expect("luma8 output").as_raw(),
    reference.as_raw(),
  );
}

// -- LumaA8 manual rotation parity -----------------------------------

#[test]
fn rotate90_luma_alpha8_manual_matches_reference() {
  // LumaA8 has 2 channels — verify the 2-byte stride.
  let img = xy_encoded_luma_alpha8(4, 3);
  let reference = ::image::imageops::rotate90(img.as_luma_alpha8().expect("luma_alpha8 source"));
  let out =
    apply_orientation_fallible(img, Orientation::Rotate90).expect("luma_alpha8 rotate90 ok");
  assert_eq!(out.width(), 3);
  assert_eq!(out.height(), 4);
  assert_eq!(
    out.as_luma_alpha8().expect("luma_alpha8 output").as_raw(),
    reference.as_raw(),
    "manual LumaA8 rotate90 must match image::imageops::rotate90 byte-identical"
  );
}

// -- 16-bit-PNG / float-pixel rotate parity ------------------
//
// image-rs 0.25's PNG decoder emits `Luma16`/`LumaA16`/`Rgb16`/
// `Rgba16` for `BitDepth::Sixteen` PNG inputs (`codecs/png.rs:64-71`),
// and caller-supplied `DynamicImage::ImageRgb32F`/`ImageRgba32F`
// round-trip through the same fallible rotate path. An infallible
// image-rs delegate could otherwise abort on allocator pressure for
// a 16-bit PNG with EXIF Rotate90/270; the manual rotate is generic
// over `T: Copy` so the same `try_reserve_exact` gate covers u16 and
// f32 — the tests below verify byte-identical parity with
// `image::imageops` for the new element widths.

/// Build a non-square `Rgb16` image whose subpixels encode pixel
/// position so any rotation is checkable byte-for-byte against
/// `image::imageops::rotate*`. Uses the high byte of each u16 so
/// the values survive any accidental u8-truncation regression and
/// remain unique-per-pixel for small sizes.
fn xy_encoded_rgb16(width: u32, height: u32) -> DynamicImage {
  let mut buf: ImageBuffer<Rgb<u16>, Vec<u16>> = ImageBuffer::new(width, height);
  for y in 0..height {
    for x in 0..width {
      // (256 * (10*x + 1), 256 * (10*y + 1), 0xBEEF) — three
      // independent u16 channels with non-zero high bytes so a
      // hypothetical truncation to u8 would zero out the channel
      // distinctly.
      buf.put_pixel(
        x,
        y,
        Rgb([
          (((x * 10) as u16) + 1) << 8,
          (((y * 10) as u16) + 1) << 8,
          0xBEEF,
        ]),
      );
    }
  }
  DynamicImage::ImageRgb16(buf)
}

/// `Luma16` (single-channel u16) test image: encodes the
/// row-major pixel index in the high byte plus a fixed low-byte
/// pattern so any rotation is byte-traceable.
fn xy_encoded_luma16(width: u32, height: u32) -> DynamicImage {
  let mut buf: ImageBuffer<Luma<u16>, Vec<u16>> = ImageBuffer::new(width, height);
  for y in 0..height {
    for x in 0..width {
      let idx = (y * width + x) as u16;
      // (idx << 8) | 0x5A — high byte = row-major index, low byte
      // = constant 0x5A so a stride-1 u8-truncation would lose
      // identity.
      buf.put_pixel(x, y, Luma([(idx << 8) | 0x005A]));
    }
  }
  DynamicImage::ImageLuma16(buf)
}

/// `Rgb32F` test image: encodes pixel position in three f32
/// channels with deliberately fractional values so any 4-byte
/// stride bug surfaces as a visible bit-pattern shift.
fn xy_encoded_rgb32f(width: u32, height: u32) -> DynamicImage {
  let mut buf: ImageBuffer<Rgb<f32>, Vec<f32>> = ImageBuffer::new(width, height);
  for y in 0..height {
    for x in 0..width {
      // (x + 0.25, y + 0.5, 0.875) — distinct fractions in each
      // channel; 0.875 is exactly representable so equality
      // comparison against the rotated buffer is exact.
      buf.put_pixel(x, y, Rgb([(x as f32) + 0.25, (y as f32) + 0.5, 0.875]));
    }
  }
  DynamicImage::ImageRgb32F(buf)
}

#[test]
fn rotate90_rgb16_manual_matches_reference() {
  // Rotate90 on Rgb16 routes through the same
  // truly-fallible `rotate_buf<u16>` path the u8 variants use.
  // A non-square 4x3 image must rotate to 3x4 with byte-identical
  // pixels to `image::imageops::rotate90`. This is the regression
  // test for the 16-bit-PNG-EXIF-rotate `load_image` gap.
  let img = xy_encoded_rgb16(4, 3);
  let reference = ::image::imageops::rotate90(img.as_rgb16().expect("rgb16 source"));
  let out = apply_orientation_fallible(img, Orientation::Rotate90)
    .expect("manual Rgb16 rotate90 should succeed under MAX_DECODED_IMAGE_BYTES");
  assert_eq!(out.width(), 3, "Rotate90 swaps width <- height");
  assert_eq!(out.height(), 4, "Rotate90 swaps height <- width");
  assert_eq!(
    out.as_rgb16().expect("rgb16 output").as_raw(),
    reference.as_raw(),
    "manual Rgb16 rotate90 must yield byte-identical u16 subpixels to image::imageops::rotate90"
  );
}

#[test]
fn rotate270_luma16_manual_matches_reference() {
  // Rotate270 on Luma16 — single-channel u16 path. Verifies the
  // 1-subpixel-per-pixel stride is honored for the u16 element
  // type. Non-square 4x3 → 3x4.
  let img = xy_encoded_luma16(4, 3);
  let reference = ::image::imageops::rotate270(img.as_luma16().expect("luma16 source"));
  let out =
    apply_orientation_fallible(img, Orientation::Rotate270).expect("manual Luma16 rotate270 ok");
  assert_eq!(out.width(), 3);
  assert_eq!(out.height(), 4);
  assert_eq!(
    out.as_luma16().expect("luma16 output").as_raw(),
    reference.as_raw(),
    "manual Luma16 rotate270 must yield byte-identical u16 subpixels to image::imageops::rotate270"
  );
}

#[test]
fn rotate90_rgb32f_manual_matches_reference() {
  // Rotate90 on Rgb32F — verifies the `rotate_buf<f32>` arm.
  // Comparison is bit-exact via `Vec<f32>` equality because the
  // operation is a pure permutation (no arithmetic, no FP drift).
  let img = xy_encoded_rgb32f(4, 3);
  let reference = ::image::imageops::rotate90(img.as_rgb32f().expect("rgb32f source"));
  let out =
    apply_orientation_fallible(img, Orientation::Rotate90).expect("manual Rgb32F rotate90 ok");
  assert_eq!(out.width(), 3);
  assert_eq!(out.height(), 4);
  assert_eq!(
    out.as_rgb32f().expect("rgb32f output").as_raw(),
    reference.as_raw(),
    "manual Rgb32F rotate90 must yield bit-identical f32 subpixels to image::imageops::rotate90 \
       (permutation only — no FP arithmetic)"
  );
}

#[test]
fn synthetic_16bit_rotate_succeeds_via_orientation_entry_point() {
  // End-to-end check that mimics the 16-bit `load_image`
  // closure: build a 16-bit `DynamicImage` directly (no on-disk
  // PNG needed; a unit test does not need a real decoder to
  // exercise the post-decode rotate path) and hand it to
  // `apply_orientation_fallible` with EXIF Rotate90 / Rotate270 /
  // composite orientations. All four allocating-rotate
  // orientations must succeed via the generic `rotate_buf`
  // path rather than the infallible fallback that would abort on
  // allocator pressure. (The "real 16-bit PNG decode" path is exercised
  // implicitly through `load_image` once a 16-bit PNG with EXIF
  // orientation is supplied; we test the rotate-shaped subgoal
  // here without coupling to disk I/O.)
  for rotation in [
    Orientation::Rotate90,
    Orientation::Rotate270,
    Orientation::Rotate90FlipH,
    Orientation::Rotate270FlipH,
  ] {
    let img = xy_encoded_rgb16(4, 3);
    let out = apply_orientation_fallible(img, rotation)
      .expect("16-bit Rgb16 rotate must succeed via the fallible u16 path");
    assert_eq!(out.width(), 3, "all four rotate orientations swap w<->h");
    assert_eq!(out.height(), 4);
    assert!(
      matches!(out, DynamicImage::ImageRgb16(_)),
      "output must preserve the source DynamicImage variant (Rgb16 in, Rgb16 out)"
    );
  }
}

// -- Byte-budget gate -----------------------------------------------

#[test]
fn rotate90_accepts_small_image_within_budget() {
  // The byte-budget gate against `MAX_DECODED_IMAGE_BYTES` must
  // accept inputs that fit. Sanity check: a 1x1 Rgb8 image is
  // 3 bytes, well under the 512 MiB cap. The negative
  // (overflow → ArithmeticOverflow) path uses the identical
  // `checked_mul` chain that `pad_to_square`'s overflow tests
  // already cover; we can't construct an image at hostile
  // dimensions without OOM-ing the test process itself.
  let img = xy_encoded_rgb8(1, 1);
  let bytes_per_pixel = u64::from(img.color().bytes_per_pixel());
  let bytes = u64::from(img.width()) * u64::from(img.height()) * bytes_per_pixel;
  assert!(
    bytes <= MAX_DECODED_IMAGE_BYTES,
    "1x1 Rgb8 = {bytes} bytes must be well under MAX_DECODED_IMAGE_BYTES={MAX_DECODED_IMAGE_BYTES}"
  );
  let out = apply_orientation_fallible(img, Orientation::Rotate90).expect("1x1 ok");
  assert_eq!(out.width(), 1);
  assert_eq!(out.height(), 1);
}

// -- LumaA16 / Rgba16 / Rgba32F manual rotation parity ---------------
//
// The u8 (Luma8/LumaA8/Rgb8/Rgba8), Luma16/Rgb16, and Rgb32F per-variant
// arms of `apply_rotate_fallible` are already exercised above. These
// three close the remaining `DynamicImage` arms: `ImageLumaA16`
// (2-channel u16), `ImageRgba16` (4-channel u16), and `ImageRgba32F`
// (4-channel f32). Each compares byte-for-byte against the matching
// `image::imageops::rotate*` reference so the per-variant `from_raw`
// reconstruction + the generic `rotate_buf<T>` index math is verified
// for these element widths.

/// `LumaA16` (grayscale + alpha, both u16) test image: luma encodes the
/// row-major index in the high byte, alpha its complement, so each
/// subpixel position is individually traceable through a rotation.
fn xy_encoded_luma_alpha16(width: u32, height: u32) -> DynamicImage {
  let mut buf: ImageBuffer<LumaA<u16>, Vec<u16>> = ImageBuffer::new(width, height);
  for y in 0..height {
    for x in 0..width {
      let idx = (y * width + x) as u16;
      // luma = (idx << 8) | 0x33, alpha = 0xFF00 - luma-high so the two
      // u16 channels carry distinct, non-zero high bytes.
      buf.put_pixel(x, y, LumaA([(idx << 8) | 0x0033, 0xFF00 - (idx << 8)]));
    }
  }
  DynamicImage::ImageLumaA16(buf)
}

/// `Rgba16` (four u16 channels) test image: each channel carries a
/// distinct non-zero high byte so a stride bug surfaces as a visible
/// channel swap.
fn xy_encoded_rgba16(width: u32, height: u32) -> DynamicImage {
  let mut buf: ImageBuffer<Rgba<u16>, Vec<u16>> = ImageBuffer::new(width, height);
  for y in 0..height {
    for x in 0..width {
      buf.put_pixel(
        x,
        y,
        Rgba([
          (((x * 10) as u16) + 1) << 8,
          (((y * 10) as u16) + 1) << 8,
          0xBEEF,
          0xA55A,
        ]),
      );
    }
  }
  DynamicImage::ImageRgba16(buf)
}

/// `Rgba32F` (four f32 channels) test image with exactly-representable
/// fractional values so equality against the rotated buffer is exact.
fn xy_encoded_rgba32f(width: u32, height: u32) -> DynamicImage {
  let mut buf: ImageBuffer<Rgba<f32>, Vec<f32>> = ImageBuffer::new(width, height);
  for y in 0..height {
    for x in 0..width {
      // (x + 0.25, y + 0.5, 0.875, 0.125) — all exactly representable.
      buf.put_pixel(
        x,
        y,
        Rgba([(x as f32) + 0.25, (y as f32) + 0.5, 0.875, 0.125]),
      );
    }
  }
  DynamicImage::ImageRgba32F(buf)
}

#[test]
fn rotate90_luma_alpha16_manual_matches_reference() {
  // `ImageLumaA16` arm: 2 u16 subpixels per pixel. Non-square 4x3 -> 3x4.
  let img = xy_encoded_luma_alpha16(4, 3);
  let reference = ::image::imageops::rotate90(img.as_luma_alpha16().expect("luma_alpha16 source"));
  let out = apply_orientation_fallible(img, Orientation::Rotate90)
    .expect("manual LumaA16 rotate90 should succeed under MAX_DECODED_IMAGE_BYTES");
  assert_eq!(out.width(), 3, "Rotate90 swaps width <- height");
  assert_eq!(out.height(), 4, "Rotate90 swaps height <- width");
  assert_eq!(
    out.as_luma_alpha16().expect("luma_alpha16 output").as_raw(),
    reference.as_raw(),
    "manual LumaA16 rotate90 must yield byte-identical u16 subpixels to image::imageops::rotate90"
  );
}

#[test]
fn rotate270_rgba16_manual_matches_reference() {
  // `ImageRgba16` arm: 4 u16 subpixels per pixel. Verifies the 4-wide
  // u16 stride under Rotate270. Non-square 4x3 -> 3x4.
  let img = xy_encoded_rgba16(4, 3);
  let reference = ::image::imageops::rotate270(img.as_rgba16().expect("rgba16 source"));
  let out =
    apply_orientation_fallible(img, Orientation::Rotate270).expect("manual Rgba16 rotate270 ok");
  assert_eq!(out.width(), 3);
  assert_eq!(out.height(), 4);
  assert_eq!(
    out.as_rgba16().expect("rgba16 output").as_raw(),
    reference.as_raw(),
    "manual Rgba16 rotate270 must yield byte-identical u16 subpixels to image::imageops::rotate270"
  );
}

#[test]
fn rotate90_fliph_rgba32f_composite_matches_reference() {
  // `ImageRgba32F` arm via the composite Rotate90FlipH orientation
  // (collapses to `dst[y, x] = src[x, y]` in `rotate_buf`). Comparison
  // is bit-exact (pure permutation, no FP arithmetic). 4x3 -> 3x4.
  let img = xy_encoded_rgba32f(4, 3);
  let rotated = ::image::imageops::rotate90(img.as_rgba32f().expect("rgba32f source"));
  let reference = ::image::imageops::flip_horizontal(&rotated);
  let out = apply_orientation_fallible(img, Orientation::Rotate90FlipH)
    .expect("manual Rgba32F Rotate90FlipH ok");
  assert_eq!(out.width(), 3);
  assert_eq!(out.height(), 4);
  assert_eq!(
    out.as_rgba32f().expect("rgba32f output").as_raw(),
    reference.as_raw(),
    "manual Rgba32F Rotate90FlipH must yield bit-identical f32 subpixels to the \
       image::imageops rotate90 + flip_horizontal composite (permutation only)"
  );
}

// -- Rgba8 SIMD-dispatcher arm coverage (rotate_buf_u8) --------------
//
// Rgba8 (u8, channels=4) is the hot path that routes through
// `rotate_buf_u8` -> the SIMD `rotate_buf` dispatcher, whose
// `RotateKind`-to-internal mapping has one arm per orientation. The
// Rotate90 and Rotate270FlipH arms are covered by sibling tests above;
// these two close the Rotate270 and Rotate90FlipH dispatcher arms.

#[test]
fn rotate270_rgba8_manual_matches_reference() {
  // Rgba8 Rotate270 -> `rotate_buf_u8` Rotate270 dispatcher arm.
  let img = xy_encoded_rgba8(4, 3);
  let reference = ::image::imageops::rotate270(img.as_rgba8().expect("rgba8 source"));
  let out = apply_orientation_fallible(img, Orientation::Rotate270).expect("rgba8 rotate270 ok");
  assert_eq!(out.width(), 3);
  assert_eq!(out.height(), 4);
  assert_eq!(
    out.as_rgba8().expect("rgba8 output").as_raw(),
    reference.as_raw(),
    "manual Rgba8 rotate270 must match image::imageops::rotate270 byte-identical"
  );
}

#[test]
fn rotate90_fliph_rgba8_composite_matches_reference() {
  // Rgba8 Rotate90FlipH -> `rotate_buf_u8` Rotate90FlipH dispatcher arm.
  let img = xy_encoded_rgba8(4, 3);
  let rotated = ::image::imageops::rotate90(img.as_rgba8().expect("rgba8 source"));
  let reference = ::image::imageops::flip_horizontal(&rotated);
  let out =
    apply_orientation_fallible(img, Orientation::Rotate90FlipH).expect("rgba8 Rotate90FlipH ok");
  assert_eq!(out.width(), 3);
  assert_eq!(out.height(), 4);
  assert_eq!(
    out.as_rgba8().expect("rgba8 output").as_raw(),
    reference.as_raw(),
    "manual Rgba8 Rotate90FlipH must match the image::imageops rotate90 + flip_horizontal composite"
  );
}