aexlo 0.2.0

Run After Effects plugins without Adobe dependencies.
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
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
use crate::sys::*;
use rayon::prelude::*;
use std::os::raw::c_void;

/// Emulates `PF_WorldTransformSuite1::copy` function
/// # Safety
/// This function is unsafe because it handles raw pointers.
pub unsafe extern "C" fn Copy_sys(
	_effect_ref: PF_ProgPtr,
	src: *mut PF_EffectWorld,
	dst: *mut PF_EffectWorld,
	src_r: *mut PF_Rect,
	dst_r: *mut PF_Rect,
) -> PF_Err {
	crate::core::ffi::guard("Copy_sys", || {
		// Handle null source/dest pointers gracefully
		if src.is_null() || dst.is_null() {
			return PF_Err_BAD_CALLBACK_PARAM;
		}

		// Check if src and dst point to the same buffer
		if std::ptr::eq(src, dst) {
			// Same buffer - nothing to copy
			return PF_Err_NONE;
		}

		let src_world = &mut unsafe { *src };
		let dst_world = &mut unsafe { *dst };

		// Calculate buffer addresses for overlap detection
		// Buffers overlap if: src_addr <= dst_addr + dst_size && dst_addr <= src_addr + src_size
		let src_addr = src_world.data as usize;
		let dst_addr = dst_world.data as usize;
		let src_size = (src_world.height as usize) * (src_world.rowbytes as usize);
		let dst_size = (dst_world.height as usize) * (dst_world.rowbytes as usize);
		let src_end = src_addr.saturating_add(src_size);
		let dst_end = dst_addr.saturating_add(dst_size);
		let buffers_overlap = !(src_end <= dst_addr || dst_end <= src_addr);

		// Determine source rectangle
		let src_rect = if !src_r.is_null() {
			unsafe { *src_r }
		} else {
			PF_Rect {
				left: 0,
				top: 0,
				right: src_world.width,
				bottom: src_world.height,
			}
		};

		// Determine destination point (top-left)
		let (dst_x, dst_y) = if !dst_r.is_null() {
			((unsafe { *dst_r }).left, (unsafe { *dst_r }).top)
		} else {
			(src_rect.left, src_rect.top)
		};

		// Calculate copy dimensions
		let copy_width = (src_rect.right - src_rect.left).max(0);
		let copy_height = (src_rect.bottom - src_rect.top).max(0);

		if copy_width == 0 || copy_height == 0 {
			return PF_Err_NONE; // Nothing to copy
		}

		// Clipping: Ensure we don't read/write out of bounds
		// Source clipping
		let src_clamped_left = src_rect.left.max(0);
		let src_clamped_top = src_rect.top.max(0);
		// We also need to clip width/height based on src bounds
		let src_avail_width = src_world.width - src_clamped_left;
		let src_avail_height = src_world.height - src_clamped_top;

		// Dest clipping
		let dst_clamped_left = dst_x.max(0); // If negative dst, we must increment src start
		let dst_clamped_top = dst_y.max(0);

		let dst_avail_width = dst_world.width - dst_clamped_left;
		let dst_avail_height = dst_world.height - dst_clamped_top;

		// Adjust for negative destination offsets (if dst_x < 0, we skip pixels in src)
		let skip_x = if dst_x < 0 { -dst_x } else { 0 };
		let skip_y = if dst_y < 0 { -dst_y } else { 0 };

		// Final dimensions to copy
		let final_width = (copy_width - skip_x).min(src_avail_width).min(dst_avail_width);
		let final_height = (copy_height - skip_y).min(src_avail_height).min(dst_avail_height);

		if final_width <= 0 || final_height <= 0 {
			return PF_Err_NONE;
		}

		// Calculate starting offsets
		let actual_src_left = src_clamped_left + skip_x;
		let actual_src_top = src_clamped_top + skip_y;
		let actual_dst_left = dst_x + skip_x;
		let actual_dst_top = dst_y + skip_y;

		// Prepare data for parallel execution
		// We cast to usize to pass across threads safely (assuming buffers are accessible/pinned)
		let src_buffer_addr = src_world.data as usize;
		let src_rowbytes = src_world.rowbytes as isize;
		let dst_buffer_addr = dst_world.data as usize;
		let dst_rowbytes = dst_world.rowbytes as isize;

		// Size of a pixel in bytes. PF_EffectWorld usually PF_Pixel8 (4 bytes).
		// But strictly it depends on deep color.
		// For now assuming PF_Pixel8 (ARGB 8-bit).
		let pixel_size = std::mem::size_of::<PF_Pixel8>();

		// Parallel Copy
		(0..final_height).into_par_iter().for_each(|y| {
			let current_src_y = actual_src_top + y;
			let current_dst_y = actual_dst_top + y;

			// Calculate row start addresses
			// Note: data is *mut c_void, treating as *mut u8 for offset
			let src_row_ptr = (src_buffer_addr as *const u8).wrapping_offset((current_src_y as isize) * src_rowbytes);
			let dst_row_ptr = (dst_buffer_addr as *mut u8).wrapping_offset((current_dst_y as isize) * dst_rowbytes);

			// Calculate signal offsets within the row
			let src_pixel_ptr = src_row_ptr.wrapping_add((actual_src_left as usize) * pixel_size);
			let dst_pixel_ptr = dst_row_ptr.wrapping_add((actual_dst_left as usize) * pixel_size);

			// Use std::ptr::copy if buffers overlap (safe for overlapping regions),
			// otherwise use copy_nonoverlapping for better performance
			unsafe {
				if buffers_overlap {
					std::ptr::copy(src_pixel_ptr, dst_pixel_ptr, (final_width as usize) * pixel_size);
				} else {
					std::ptr::copy_nonoverlapping(src_pixel_ptr, dst_pixel_ptr, (final_width as usize) * pixel_size);
				}
			}
		});

		PF_Err_NONE
	})
}

// ============================================================================
// Shared pixel-access / compositing helpers
//
// Every function below assumes `PF_Pixel8` (straight-alpha 8-bit ARGB), like
// the rest of this crate's suites (see `iterate.rs`, `world.rs`). Deep-color
// (16-bit / float) worlds are not handled.
// ============================================================================

const PIXEL_SIZE: isize = std::mem::size_of::<PF_Pixel8>() as isize;

/// A `Copy`/`Send`/`Sync` view of a `PF_EffectWorld`'s pixel buffer, addressed
/// by raw `usize` rather than a borrowed reference. Mirrors the pattern
/// `Copy_sys`/`iterate_8_sys` already use to move buffer access across rayon's
/// worker threads without fighting Rust's aliasing rules: each pixel touched
/// by the parallel iteration is disjoint, so concurrent raw-pointer access is
/// sound even though a `&mut PF_EffectWorld` could not be shared this way.
#[derive(Clone, Copy)]
struct RawWorld {
	addr: usize,
	rowbytes: isize,
	width: i32,
	height: i32,
}

impl RawWorld {
	fn from(world: &PF_EffectWorld) -> Self {
		Self {
			addr: world.data as usize,
			rowbytes: world.rowbytes as isize,
			width: world.width,
			height: world.height,
		}
	}

	/// Reads the pixel at `(x, y)`, or transparent black if it falls outside
	/// the buffer (this is what gives `convolve`'s "transparent borders" mode
	/// and off-screen mask/transform samples their fall-through behavior).
	#[inline]
	unsafe fn read(&self, x: i32, y: i32) -> PF_Pixel8 {
		if self.addr == 0 || x < 0 || y < 0 || x >= self.width || y >= self.height {
			return PF_Pixel8 {
				alpha: 0,
				red: 0,
				green: 0,
				blue: 0,
			};
		}
		let ptr = (self.addr as *const u8).wrapping_offset(y as isize * self.rowbytes + x as isize * PIXEL_SIZE)
			as *const PF_Pixel8;
		unsafe { *ptr }
	}

	/// Like [`read`](Self::read), but clamps out-of-range coordinates to the
	/// nearest edge pixel instead of returning transparent black, for
	/// `PF_KernelFlag_REPLICATE_BORDERS`.
	#[inline]
	unsafe fn read_edge(&self, x: i32, y: i32, replicate: bool) -> PF_Pixel8 {
		if replicate {
			let cx = x.clamp(0, (self.width - 1).max(0));
			let cy = y.clamp(0, (self.height - 1).max(0));
			unsafe { self.read(cx, cy) }
		} else {
			unsafe { self.read(x, y) }
		}
	}

	#[inline]
	unsafe fn write(&self, x: i32, y: i32, pixel: PF_Pixel8) {
		if self.addr == 0 || x < 0 || y < 0 || x >= self.width || y >= self.height {
			return;
		}
		let ptr = (self.addr as *mut u8).wrapping_offset(y as isize * self.rowbytes + x as isize * PIXEL_SIZE)
			as *mut PF_Pixel8;
		unsafe { *ptr = pixel };
	}

	/// Bilinear sample at continuous coordinate `(x, y)`, where integer
	/// coordinates land exactly on pixel centers. Returns `(a, r, g, b)` on a
	/// 0..255 scale. Taps that fall outside the buffer contribute transparent
	/// black, so sampling near an edge fades out rather than reading garbage.
	unsafe fn sample_bilinear(&self, x: f64, y: f64) -> (f64, f64, f64, f64) {
		let x0 = x.floor();
		let y0 = y.floor();
		let fx = x - x0;
		let fy = y - y0;
		let (x0i, y0i) = (x0 as i32, y0 as i32);
		let p00 = unsafe { self.read(x0i, y0i) };
		let p10 = unsafe { self.read(x0i + 1, y0i) };
		let p01 = unsafe { self.read(x0i, y0i + 1) };
		let p11 = unsafe { self.read(x0i + 1, y0i + 1) };
		let mix = |c00: u8, c10: u8, c01: u8, c11: u8| {
			let top = c00 as f64 + (c10 as f64 - c00 as f64) * fx;
			let bottom = c01 as f64 + (c11 as f64 - c01 as f64) * fx;
			top + (bottom - top) * fy
		};
		(
			mix(p00.alpha, p10.alpha, p01.alpha, p11.alpha),
			mix(p00.red, p10.red, p01.red, p11.red),
			mix(p00.green, p10.green, p01.green, p11.green),
			mix(p00.blue, p10.blue, p01.blue, p11.blue),
		)
	}
}

/// Whether `field` restricts processing to alternating destination scanlines.
/// `UPPER` is treated as the even rows (0, 2, 4, ...) and `LOWER` the odd
/// rows; `FRAME` (and any other value) processes every row.
#[inline]
fn skip_row_for_field(y: i32, field: PF_Field) -> bool {
	if field == PF_Field_UPPER {
		y.rem_euclid(2) != 0
	} else if field == PF_Field_LOWER {
		y.rem_euclid(2) == 0
	} else {
		false
	}
}

/// Unpacks a stored pixel into (alpha, [r, g, b]) on a 0..1 scale. When
/// `premultiplied` is set (`PF_MF_Alpha_PREMUL`), the stored channels are
/// divided back out by alpha so the rest of the compositing math can always
/// work in straight alpha, matching how the rest of this crate stores worlds.
#[inline]
fn to_straight(p: PF_Pixel8, premultiplied: bool) -> (f64, [f64; 3]) {
	let a = p.alpha as f64 / 255.0;
	let raw = [p.red as f64 / 255.0, p.green as f64 / 255.0, p.blue as f64 / 255.0];
	let c = if premultiplied {
		if a > 1e-6 {
			[raw[0] / a, raw[1] / a, raw[2] / a]
		} else {
			[0.0; 3]
		}
	} else {
		raw
	};
	(a, c)
}

#[inline]
fn from_straight(a: f64, c: [f64; 3]) -> PF_Pixel8 {
	let to_u8 = |v: f64| (v.clamp(0.0, 1.0) * 255.0).round() as u8;
	PF_Pixel8 {
		alpha: to_u8(a),
		red: to_u8(c[0]),
		green: to_u8(c[1]),
		blue: to_u8(c[2]),
	}
}

// ---- Blend-mode formulas (operate on 0..1 normalized channel values) ------
// Separable modes follow the W3C compositing/blending spec's per-channel
// formulas; the four non-separable modes (Hue/Saturation/Color/Luminosity)
// use its Lum/Sat/ClipColor helpers. `cb` is the backdrop (destination),
// `cs` the source, matching the spec's naming.

fn hard_light(cb: f64, cs: f64) -> f64 {
	if cs <= 0.5 {
		2.0 * cb * cs
	} else {
		let cs2 = 2.0 * cs - 1.0;
		cb + cs2 - cb * cs2
	}
}

fn soft_light(cb: f64, cs: f64) -> f64 {
	if cs <= 0.5 {
		cb - (1.0 - 2.0 * cs) * cb * (1.0 - cb)
	} else {
		let d = if cb <= 0.25 {
			((16.0 * cb - 12.0) * cb + 4.0) * cb
		} else {
			cb.sqrt()
		};
		cb + (2.0 * cs - 1.0) * (d - cb)
	}
}

fn color_dodge(cb: f64, cs: f64) -> f64 {
	if cb <= 0.0 {
		0.0
	} else if cs >= 1.0 {
		1.0
	} else {
		(cb / (1.0 - cs)).min(1.0)
	}
}

fn color_burn(cb: f64, cs: f64) -> f64 {
	if cb >= 1.0 {
		1.0
	} else if cs <= 0.0 {
		0.0
	} else {
		1.0 - ((1.0 - cb) / cs).min(1.0)
	}
}

fn vivid_light(cb: f64, cs: f64) -> f64 {
	if cs <= 0.5 {
		color_burn(cb, 2.0 * cs)
	} else {
		color_dodge(cb, 2.0 * cs - 1.0)
	}
}

fn pin_light(cb: f64, cs: f64) -> f64 {
	if cs <= 0.5 {
		cb.min(2.0 * cs)
	} else {
		cb.max(2.0 * cs - 1.0)
	}
}

/// Per-channel separable blend. Modes with no simple per-channel formula
/// (Copy/In Front/Dissolve, the alpha-multiply variants, and anything this
/// emulator doesn't recognise) fall back to `cs` -- i.e. Normal/over -- which
/// is also exactly the identity `composite_pixel` needs for those modes.
#[allow(non_upper_case_globals)]
fn blend_channel(mode: A_long, cb: f64, cs: f64) -> f64 {
	match mode {
		PF_Xfer_MULTIPLY
		| PF_Xfer_MULTIPLY_ALPHA
		| PF_Xfer_MULTIPLY_ALPHA_LUMA
		| PF_Xfer_MULTIPLY_NOT_ALPHA
		| PF_Xfer_MULTIPLY_NOT_ALPHA_LUMA => cb * cs,
		PF_Xfer_SCREEN => cb + cs - cb * cs,
		PF_Xfer_OVERLAY => hard_light(cs, cb),
		PF_Xfer_HARD_LIGHT => hard_light(cb, cs),
		PF_Xfer_SOFT_LIGHT => soft_light(cb, cs),
		PF_Xfer_DARKEN => cb.min(cs),
		PF_Xfer_LIGHTEN => cb.max(cs),
		PF_Xfer_DIFFERENCE | PF_Xfer_DIFFERENCE2 => (cb - cs).abs(),
		PF_Xfer_EXCLUSION => cb + cs - 2.0 * cb * cs,
		PF_Xfer_COLOR_DODGE | PF_Xfer_COLOR_DODGE2 => color_dodge(cb, cs),
		PF_Xfer_COLOR_BURN | PF_Xfer_COLOR_BURN2 => color_burn(cb, cs),
		PF_Xfer_ADD | PF_Xfer_LINEAR_DODGE | PF_Xfer_ADDITIVE_PREMUL | PF_Xfer_ALPHA_ADD => (cb + cs).min(1.0),
		PF_Xfer_SUBTRACT => (cb - cs).max(0.0),
		PF_Xfer_LINEAR_BURN => (cb + cs - 1.0).max(0.0),
		PF_Xfer_LINEAR_LIGHT => (cb + 2.0 * cs - 1.0).clamp(0.0, 1.0),
		PF_Xfer_VIVID_LIGHT => vivid_light(cb, cs),
		PF_Xfer_PIN_LIGHT => pin_light(cb, cs),
		PF_Xfer_HARD_MIX => {
			if vivid_light(cb, cs) < 0.5 {
				0.0
			} else {
				1.0
			}
		}
		PF_Xfer_DIVIDE => {
			if cs <= 0.0 {
				1.0
			} else {
				(cb / cs).min(1.0)
			}
		}
		_ => cs,
	}
}

fn lum(c: [f64; 3]) -> f64 {
	0.3 * c[0] + 0.59 * c[1] + 0.11 * c[2]
}

fn clip_color(c: [f64; 3]) -> [f64; 3] {
	let l = lum(c);
	let n = c.iter().cloned().fold(f64::INFINITY, f64::min);
	let x = c.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
	let mut out = c;
	if n < 0.0 && (l - n).abs() > 1e-9 {
		for v in out.iter_mut() {
			*v = l + (*v - l) * l / (l - n);
		}
	}
	if x > 1.0 && (x - l).abs() > 1e-9 {
		for v in out.iter_mut() {
			*v = l + (*v - l) * (1.0 - l) / (x - l);
		}
	}
	out
}

fn set_lum(c: [f64; 3], l: f64) -> [f64; 3] {
	let d = l - lum(c);
	clip_color([c[0] + d, c[1] + d, c[2] + d])
}

fn sat(c: [f64; 3]) -> f64 {
	c.iter().cloned().fold(f64::NEG_INFINITY, f64::max) - c.iter().cloned().fold(f64::INFINITY, f64::min)
}

fn set_sat(c: [f64; 3], s: f64) -> [f64; 3] {
	let mut out = c;
	let mut idx = [0usize, 1, 2];
	idx.sort_by(|&a, &b| out[a].total_cmp(&out[b]));
	let (imin, imid, imax) = (idx[0], idx[1], idx[2]);
	if out[imax] > out[imin] {
		out[imid] = (out[imid] - out[imin]) * s / (out[imax] - out[imin]);
		out[imax] = s;
	} else {
		out[imid] = 0.0;
		out[imax] = 0.0;
	}
	out[imin] = 0.0;
	out
}

/// Full-triple blend, dispatching to the non-separable HSL modes and
/// per-channel [`blend_channel`] for everything else. `cb` is the backdrop
/// (destination), `cs` the source.
#[allow(non_upper_case_globals)]
fn blend_rgb(mode: A_long, cb: [f64; 3], cs: [f64; 3]) -> [f64; 3] {
	match mode {
		PF_Xfer_HUE => set_lum(set_sat(cs, sat(cb)), lum(cb)),
		PF_Xfer_SATURATION => set_lum(set_sat(cb, sat(cs)), lum(cb)),
		PF_Xfer_COLOR => set_lum(cs, lum(cb)),
		PF_Xfer_LUMINOSITY => set_lum(cb, lum(cs)),
		PF_Xfer_LIGHTER_COLOR => {
			if lum(cs) >= lum(cb) {
				cs
			} else {
				cb
			}
		}
		PF_Xfer_DARKER_COLOR => {
			if lum(cs) <= lum(cb) {
				cs
			} else {
				cb
			}
		}
		_ => [
			blend_channel(mode, cb[0], cs[0]),
			blend_channel(mode, cb[1], cs[1]),
			blend_channel(mode, cb[2], cs[2]),
		],
	}
}

/// Standard Porter-Duff "over" composite of `top` onto `bottom`, with `mode`
/// selecting the RGB blend function applied where both are opaque. This is
/// the one compositing primitive `composite_rect`/`transfer_rect`/
/// `transform_world` all reduce to.
fn composite_over(top_a: f64, top_c: [f64; 3], bottom_a: f64, bottom_c: [f64; 3], mode: A_long) -> (f64, [f64; 3]) {
	let out_a = top_a + bottom_a * (1.0 - top_a);
	let blended = blend_rgb(mode, bottom_c, top_c);
	let mut out_c = [0.0; 3];
	for i in 0..3 {
		let premul = top_c[i] * top_a * (1.0 - bottom_a)
			+ bottom_c[i] * bottom_a * (1.0 - top_a)
			+ blended[i] * top_a * bottom_a;
		out_c[i] = if out_a > 1e-6 {
			(premul / out_a).clamp(0.0, 1.0)
		} else {
			0.0
		};
	}
	(out_a, out_c)
}

/// Composites `src` onto `dst` under `mode`. `PF_Xfer_NONE` leaves `dst`
/// untouched; `PF_Xfer_BEHIND` composites `dst` on top of `src` (visible
/// wherever `dst` is transparent) instead of the usual `src`-on-top order.
/// Everything else -- including `COPY`/`IN_FRONT`, which have no distinct
/// blend formula of their own -- is a Normal-mode `over`.
#[allow(non_upper_case_globals)]
fn composite_pixel(src_a: f64, src_c: [f64; 3], dst_a: f64, dst_c: [f64; 3], mode: A_long) -> (f64, [f64; 3]) {
	if mode == PF_Xfer_NONE {
		return (dst_a, dst_c);
	}
	if mode == PF_Xfer_BEHIND {
		return composite_over(dst_a, dst_c, src_a, src_c, PF_Xfer_COPY);
	}
	composite_over(src_a, src_c, dst_a, dst_c, mode)
}

/// Samples a mask world's value at `(x, y)` (in the masked layer's
/// coordinates; `offset` shifts into the mask's own coordinate space),
/// honoring `PF_MaskFlag_LUMINANCE` (use luma instead of alpha) and
/// `PF_MaskFlag_INVERTED`.
#[allow(non_upper_case_globals)]
fn mask_value(mask: RawWorld, offset: PF_Point, what_is_mask: PF_MaskFlags, x: i32, y: i32) -> f64 {
	let p = unsafe { mask.read(x - offset.h, y - offset.v) };
	let v = if what_is_mask & PF_MaskFlag_LUMINANCE != 0 {
		(0.3 * p.red as f64 + 0.59 * p.green as f64 + 0.11 * p.blue as f64) / 255.0
	} else {
		p.alpha as f64 / 255.0
	};
	if what_is_mask & PF_MaskFlag_INVERTED != 0 {
		1.0 - v
	} else {
		v
	}
}

// ---- 3x3 affine matrix helpers (row-vector convention: p' = p * M) --------

type Mat3 = [[f64; 3]; 3];
const IDENTITY3: Mat3 = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];

fn apply_matrix(m: &Mat3, x: f64, y: f64) -> (f64, f64) {
	let xh = x * m[0][0] + y * m[1][0] + m[2][0];
	let yh = x * m[0][1] + y * m[1][1] + m[2][1];
	let wh = x * m[0][2] + y * m[1][2] + m[2][2];
	if (wh - 1.0).abs() > 1e-9 && wh.abs() > 1e-9 {
		(xh / wh, yh / wh)
	} else {
		(xh, yh)
	}
}

fn invert3(m: Mat3) -> Option<Mat3> {
	let [[a, b, c], [d, e, f], [g, h, i]] = m;
	let a1 = e * i - f * h;
	let b1 = f * g - d * i;
	let c1 = d * h - e * g;
	let det = a * a1 + b * b1 + c * c1;
	if det.abs() < 1e-12 {
		return None;
	}
	let inv_det = 1.0 / det;
	let d1 = c * h - b * i;
	let e1 = a * i - c * g;
	let f1 = b * g - a * h;
	let g1 = b * f - c * e;
	let h1 = c * d - a * f;
	let i1 = a * e - b * d;
	Some([
		[a1 * inv_det, d1 * inv_det, g1 * inv_det],
		[b1 * inv_det, e1 * inv_det, h1 * inv_det],
		[c1 * inv_det, f1 * inv_det, i1 * inv_det],
	])
}

// ============================================================================
// Suite entry points
// ============================================================================

/// Whether `world` holds 8 bpc `ARGB32` pixels, the only layout the
/// composite and transform paths here read and write. A 16 or 32 bpc world
/// would be walked four bytes at a time and come back corrupted.
fn is_8bpc(world: &PF_EffectWorld) -> bool {
	world.world_flags & PF_WorldFlag_DEEP == 0
		&& unsafe { super::world::world_pixel_format(world) } == PF_PixelFormat_ARGB32 as u32
}

/// Composites `source_wld`'s `src_rect` onto `dest_wld` at `(dest_x,
/// dest_y)`, scaled by `src_opacity` (0..255) and blended per `xfer_mode`.
pub(crate) unsafe extern "C" fn composite_rect_sys(
	_effect_ref: PF_ProgPtr,
	src_rect: *mut PF_Rect,
	src_opacity: A_long,
	source_wld: *mut PF_EffectWorld,
	dest_x: A_long,
	dest_y: A_long,
	field_rdr: PF_Field,
	xfer_mode: PF_XferMode,
	dest_wld: *mut PF_EffectWorld,
) -> PF_Err {
	crate::core::ffi::guard("composite_rect_sys", || {
		if source_wld.is_null() || dest_wld.is_null() {
			return PF_Err_BAD_CALLBACK_PARAM;
		}
		let src = unsafe { &*source_wld };
		let dst = unsafe { &*dest_wld };
		if src.data.is_null() || dst.data.is_null() {
			return PF_Err_BAD_CALLBACK_PARAM;
		}
		if !is_8bpc(src) || !is_8bpc(dst) {
			log::warn!("composite_rect: only 8 bpc worlds are supported");
			return PF_Err_BAD_CALLBACK_PARAM;
		}

		let rect = if !src_rect.is_null() {
			unsafe { *src_rect }
		} else {
			PF_Rect {
				left: 0,
				top: 0,
				right: src.width,
				bottom: src.height,
			}
		};
		let left = rect.left.max(0);
		let top = rect.top.max(0);
		let width = (rect.right.min(src.width) - left).max(0);
		let height = (rect.bottom.min(src.height) - top).max(0);
		if width == 0 || height == 0 {
			return PF_Err_NONE;
		}

		let opacity = src_opacity.clamp(0, 255) as f64 / 255.0;
		let src_raw = RawWorld::from(src);
		let dst_raw = RawWorld::from(dst);

		(0..height).into_par_iter().for_each(|row| {
			let dy = dest_y + row;
			if skip_row_for_field(dy, field_rdr) {
				return;
			}
			let sy = top + row;
			for col in 0..width {
				let dx = dest_x + col;
				let sx = left + col;
				let sp = unsafe { src_raw.read(sx, sy) };
				let (mut src_a, src_c) = to_straight(sp, false);
				src_a *= opacity;
				let dp = unsafe { dst_raw.read(dx, dy) };
				let (dst_a, dst_c) = to_straight(dp, false);
				let (out_a, out_c) = composite_pixel(src_a, src_c, dst_a, dst_c, xfer_mode);
				unsafe { dst_raw.write(dx, dy, from_straight(out_a, out_c)) };
			}
		});

		PF_Err_NONE
	})
}

/// Cross-fades `src1`/`src2` into `dst` at a constant `ratio` (16.16 fixed,
/// 0.0..1.0), including the alpha channel. Operates over the overlap of all
/// three worlds' dimensions.
unsafe extern "C" fn blend_sys(
	_effect_ref: PF_ProgPtr,
	src1: *const PF_EffectWorld,
	src2: *const PF_EffectWorld,
	ratio: PF_Fixed,
	dst: *mut PF_EffectWorld,
) -> PF_Err {
	crate::core::ffi::guard("blend_sys", || {
		if src1.is_null() || src2.is_null() || dst.is_null() {
			return PF_Err_BAD_CALLBACK_PARAM;
		}
		let s1 = unsafe { &*src1 };
		let s2 = unsafe { &*src2 };
		let d = unsafe { &*dst };
		if s1.data.is_null() || s2.data.is_null() || d.data.is_null() {
			return PF_Err_BAD_CALLBACK_PARAM;
		}

		let t = (ratio as f64 / 65536.0).clamp(0.0, 1.0);
		let width = s1.width.min(s2.width).min(d.width).max(0);
		let height = s1.height.min(s2.height).min(d.height).max(0);
		if width == 0 || height == 0 {
			return PF_Err_NONE;
		}

		let r1 = RawWorld::from(s1);
		let r2 = RawWorld::from(s2);
		let rd = RawWorld::from(d);

		(0..height).into_par_iter().for_each(|y| {
			for x in 0..width {
				let p1 = unsafe { r1.read(x, y) };
				let p2 = unsafe { r2.read(x, y) };
				let lerp8 = |a: u8, b: u8| ((a as f64) + (b as f64 - a as f64) * t).round().clamp(0.0, 255.0) as u8;
				let out = PF_Pixel8 {
					alpha: lerp8(p1.alpha, p2.alpha),
					red: lerp8(p1.red, p2.red),
					green: lerp8(p1.green, p2.green),
					blue: lerp8(p1.blue, p2.blue),
				};
				unsafe { rd.write(x, y, out) };
			}
		});

		PF_Err_NONE
	})
}

fn quantize(v: f64, no_clamp: bool) -> u8 {
	if no_clamp {
		(v.round() as i64).rem_euclid(256) as u8
	} else {
		v.round().clamp(0.0, 255.0) as u8
	}
}

/// Bundles the parameters shared by every tap of a convolution at a given
/// output pixel, so the per-channel helpers below don't need a long, easily
/// mis-ordered argument list.
#[derive(Clone, Copy)]
struct ConvolveCtx<'a> {
	world: RawWorld,
	taps: &'a [(i32, i32)],
	replicate: bool,
	normalized: bool,
}

fn convolve_value(
	ctx: ConvolveCtx,
	x: i32,
	y: i32,
	weights: &[f64],
	sum: f64,
	extract: impl Fn(PF_Pixel8) -> f64,
) -> f64 {
	let mut acc = 0.0;
	for (i, &(ox, oy)) in ctx.taps.iter().enumerate() {
		let w = weights[i];
		if w == 0.0 {
			continue;
		}
		let px = unsafe { ctx.world.read_edge(x + ox, y + oy, ctx.replicate) };
		acc += w * extract(px);
	}
	if ctx.normalized || sum.abs() < 1e-9 {
		acc
	} else {
		acc / sum
	}
}

/// Convolves premultiplied by each tap's own alpha, then unpremultiplies the
/// result -- avoids the dark/bright fringing a straight convolution produces
/// at partially-transparent edges. Self-normalizing regardless of the
/// `NORMALIZED` flag, since scaling every weight by a constant cancels out of
/// the ratio.
fn convolve_value_alpha_weighted(
	ctx: ConvolveCtx,
	x: i32,
	y: i32,
	weights: &[f64],
	extract: impl Fn(PF_Pixel8) -> f64,
) -> f64 {
	let mut num = 0.0;
	let mut den = 0.0;
	for (i, &(ox, oy)) in ctx.taps.iter().enumerate() {
		let w = weights[i];
		if w == 0.0 {
			continue;
		}
		let px = unsafe { ctx.world.read_edge(x + ox, y + oy, ctx.replicate) };
		let a = px.alpha as f64 / 255.0;
		num += w * extract(px) * a;
		den += w * a;
	}
	if den.abs() > 1e-9 { num / den } else { 0.0 }
}

/// Convolves `src`'s `area` with the given per-channel kernels, writing the
/// result to `dst`. A `null` channel kernel leaves that channel unmodified.
/// `src` and `dst` must be different buffers (a sliding-window filter can't
/// safely run in place, and parallelising by row assumes disjoint reads).
#[allow(non_upper_case_globals)]
unsafe extern "C" fn convolve_sys(
	_effect_ref: PF_ProgPtr,
	src: *mut PF_EffectWorld,
	area: *const PF_Rect,
	flags: PF_KernelFlags,
	kernel_size: A_long,
	a_kernel: *mut c_void,
	r_kernel: *mut c_void,
	g_kernel: *mut c_void,
	b_kernel: *mut c_void,
	dst: *mut PF_EffectWorld,
) -> PF_Err {
	crate::core::ffi::guard("convolve_sys", || {
		if src.is_null() || dst.is_null() {
			return PF_Err_BAD_CALLBACK_PARAM;
		}
		if std::ptr::eq(src, dst) {
			log::error!("PF_WorldTransformSuite1::convolve: src and dst must be different buffers");
			return PF_Err_BAD_CALLBACK_PARAM;
		}
		let s = unsafe { &*src };
		let d = unsafe { &*dst };
		if s.data.is_null() || d.data.is_null() || kernel_size <= 0 {
			return PF_Err_BAD_CALLBACK_PARAM;
		}

		let rect = if !area.is_null() {
			unsafe { *area }
		} else {
			PF_Rect {
				left: 0,
				top: 0,
				right: s.width,
				bottom: s.height,
			}
		};
		let left = rect.left.max(0);
		let top = rect.top.max(0);
		let width = (rect.right.min(s.width).min(d.width) - left).max(0);
		let height = (rect.bottom.min(s.height).min(d.height) - top).max(0);
		if width == 0 || height == 0 {
			return PF_Err_NONE;
		}

		let is_1d = flags & PF_KernelFlag_1D != 0;
		let vertical = flags & PF_KernelFlag_VERTICAL != 0;
		let normalized = flags & PF_KernelFlag_NORMALIZED != 0;
		let no_clamp = flags & PF_KernelFlag_NO_CLAMP != 0;
		let replicate = flags & PF_KernelFlag_REPLICATE_BORDERS != 0;
		let alpha_weighted = flags & PF_KernelFlag_ALPHA_WEIGHT_CONVOLVE != 0;
		let kernel_type = flags & (PF_KernelFlag_USE_CHAR | PF_KernelFlag_USE_FIXED);

		let taps: Vec<(i32, i32)> = if is_1d {
			(0..kernel_size)
				.map(|i| {
					let offset = i - kernel_size / 2;
					if vertical { (0, offset) } else { (offset, 0) }
				})
				.collect()
		} else {
			let mut v = Vec::with_capacity((kernel_size * kernel_size).max(0) as usize);
			for ky in 0..kernel_size {
				for kx in 0..kernel_size {
					v.push((kx - kernel_size / 2, ky - kernel_size / 2));
				}
			}
			v
		};

		struct Channel {
			weights: Vec<f64>,
			sum: f64,
		}
		let build_channel = |ptr: *mut c_void| -> Option<Channel> {
			if ptr.is_null() {
				return None;
			}
			let weights: Vec<f64> = (0..taps.len())
				.map(|i| match kernel_type {
					PF_KernelFlag_USE_CHAR => (unsafe { *(ptr as *const i8).add(i) }) as f64,
					PF_KernelFlag_USE_FIXED => (unsafe { *(ptr as *const PF_Fixed).add(i) }) as f64 / 65536.0,
					_ => (unsafe { *(ptr as *const A_long).add(i) }) as f64,
				})
				.collect();
			let sum = weights.iter().sum();
			Some(Channel { weights, sum })
		};

		let ch_a = build_channel(a_kernel);
		let ch_r = build_channel(r_kernel);
		let ch_g = build_channel(g_kernel);
		let ch_b = build_channel(b_kernel);

		let src_raw = RawWorld::from(s);
		let dst_raw = RawWorld::from(d);
		let ctx = ConvolveCtx {
			world: src_raw,
			taps: &taps[..],
			replicate,
			normalized,
		};

		(0..height).into_par_iter().for_each(|row| {
			let y = top + row;
			for col in 0..width {
				let x = left + col;
				let here = unsafe { src_raw.read(x, y) };

				let conv = |ch: &Option<Channel>, extract: fn(PF_Pixel8) -> f64, fallback: u8| -> u8 {
					match ch {
						Some(ch) => {
							let v = if alpha_weighted {
								convolve_value_alpha_weighted(ctx, x, y, &ch.weights, extract)
							} else {
								convolve_value(ctx, x, y, &ch.weights, ch.sum, extract)
							};
							quantize(v, no_clamp)
						}
						None => fallback,
					}
				};

				let out = PF_Pixel8 {
					alpha: conv(&ch_a, |p| p.alpha as f64, here.alpha),
					red: conv(&ch_r, |p| p.red as f64, here.red),
					green: conv(&ch_g, |p| p.green as f64, here.green),
					blue: conv(&ch_b, |p| p.blue as f64, here.blue),
				};
				unsafe { dst_raw.write(x, y, out) };
			}
		});

		PF_Err_NONE
	})
}

/// Like [`Copy_sys`], but resamples with bilinear filtering, so `src_r` and
/// `dst_r` may have different sizes (a scaled blit) rather than requiring an
/// exact 1:1 pixel match.
unsafe extern "C" fn copy_hq_sys(
	_effect_ref: PF_ProgPtr,
	src: *mut PF_EffectWorld,
	dst: *mut PF_EffectWorld,
	src_r: *mut PF_Rect,
	dst_r: *mut PF_Rect,
) -> PF_Err {
	crate::core::ffi::guard("copy_hq_sys", || {
		if src.is_null() || dst.is_null() {
			return PF_Err_BAD_CALLBACK_PARAM;
		}
		let s = unsafe { &*src };
		let d = unsafe { &*dst };
		if s.data.is_null() || d.data.is_null() {
			return PF_Err_BAD_CALLBACK_PARAM;
		}

		let src_rect = if !src_r.is_null() {
			unsafe { *src_r }
		} else {
			PF_Rect {
				left: 0,
				top: 0,
				right: s.width,
				bottom: s.height,
			}
		};
		let dst_rect = if !dst_r.is_null() {
			unsafe { *dst_r }
		} else {
			PF_Rect {
				left: 0,
				top: 0,
				right: d.width,
				bottom: d.height,
			}
		};

		let src_w = (src_rect.right - src_rect.left).max(0);
		let src_h = (src_rect.bottom - src_rect.top).max(0);
		let dst_left = dst_rect.left.max(0);
		let dst_top = dst_rect.top.max(0);
		let dst_w = (dst_rect.right.min(d.width) - dst_left).max(0);
		let dst_h = (dst_rect.bottom.min(d.height) - dst_top).max(0);
		if src_w == 0 || src_h == 0 || dst_w == 0 || dst_h == 0 {
			return PF_Err_NONE;
		}

		let scale_x = src_w as f64 / dst_w as f64;
		let scale_y = src_h as f64 / dst_h as f64;
		let src_raw = RawWorld::from(s);
		let dst_raw = RawWorld::from(d);

		(0..dst_h).into_par_iter().for_each(|row| {
			let dy = dst_top + row;
			let sy = src_rect.top as f64 + (row as f64 + 0.5) * scale_y - 0.5;
			for col in 0..dst_w {
				let dx = dst_left + col;
				let sx = src_rect.left as f64 + (col as f64 + 0.5) * scale_x - 0.5;
				let (a, r, g, b) = unsafe { src_raw.sample_bilinear(sx, sy) };
				let to_u8 = |v: f64| v.round().clamp(0.0, 255.0) as u8;
				let px = PF_Pixel8 {
					alpha: to_u8(a),
					red: to_u8(r),
					green: to_u8(g),
					blue: to_u8(b),
				};
				unsafe { dst_raw.write(dx, dy, px) };
			}
		});

		PF_Err_NONE
	})
}

/// `composite_rect`'s more capable sibling: takes a structured
/// `PF_CompositeMode` (opacity + `rgb_only`) and an optional mask, and
/// respects `m_flags` to unpremultiply `src_world` if needed.
///
/// `comp_mode.rand_seed` and `opacitySu` aren't consulted (dissolve dithering
/// and sub-percent opacity precision aren't modeled); `quality` is accepted
/// but doesn't change behavior, since this emulator has no separate
/// low-quality fast path.
pub(crate) unsafe extern "C" fn transfer_rect_sys(
	_effect_ref: PF_ProgPtr,
	_quality: PF_Quality,
	m_flags: PF_ModeFlags,
	field: PF_Field,
	src_rec: *const PF_Rect,
	src_world: *const PF_EffectWorld,
	comp_mode: *const PF_CompositeMode,
	mask_world0: *const PF_MaskWorld,
	dest_x: A_long,
	dest_y: A_long,
	dst_world: *mut PF_EffectWorld,
) -> PF_Err {
	crate::core::ffi::guard("transfer_rect_sys", || {
		if src_world.is_null() || dst_world.is_null() || comp_mode.is_null() {
			return PF_Err_BAD_CALLBACK_PARAM;
		}
		let src = unsafe { &*src_world };
		let dst = unsafe { &*dst_world };
		if src.data.is_null() || dst.data.is_null() {
			return PF_Err_BAD_CALLBACK_PARAM;
		}
		if !is_8bpc(src) || !is_8bpc(dst) {
			log::warn!("transfer_rect: only 8 bpc worlds are supported");
			return PF_Err_BAD_CALLBACK_PARAM;
		}
		let mode = unsafe { &*comp_mode };

		let rect = if !src_rec.is_null() {
			unsafe { *src_rec }
		} else {
			PF_Rect {
				left: 0,
				top: 0,
				right: src.width,
				bottom: src.height,
			}
		};
		let left = rect.left.max(0);
		let top = rect.top.max(0);
		let width = (rect.right.min(src.width) - left).max(0);
		let height = (rect.bottom.min(src.height) - top).max(0);
		if width == 0 || height == 0 {
			return PF_Err_NONE;
		}

		let opacity = mode.opacity as f64 / 255.0;
		let premultiplied = m_flags & PF_MF_Alpha_STRAIGHT == 0;
		let rgb_only = mode.rgb_only != 0;
		let xfer = mode.xfer;

		let src_raw = RawWorld::from(src);
		let dst_raw = RawWorld::from(dst);
		let mask_raw = unsafe { mask_world0.as_ref() }.map(|m| (RawWorld::from(&m.mask), m.offset, m.what_is_mask));

		(0..height).into_par_iter().for_each(|row| {
			let dy = dest_y + row;
			if skip_row_for_field(dy, field) {
				return;
			}
			let sy = top + row;
			for col in 0..width {
				let dx = dest_x + col;
				let sx = left + col;
				let sp = unsafe { src_raw.read(sx, sy) };
				let (mut src_a, src_c) = to_straight(sp, premultiplied);
				src_a *= opacity;
				if let Some((mraw, offset, what)) = mask_raw {
					src_a *= mask_value(mraw, offset, what, dx, dy);
				}
				let dp = unsafe { dst_raw.read(dx, dy) };
				let (dst_a, dst_c) = to_straight(dp, false);
				let (out_a, out_c) = composite_pixel(src_a, src_c, dst_a, dst_c, xfer);
				let final_a = if rgb_only { dst_a } else { out_a };
				unsafe { dst_raw.write(dx, dy, from_straight(final_a, out_c)) };
			}
		});

		PF_Err_NONE
	})
}

/// Applies one (or, for motion blur, several averaged) affine transform(s)
/// to `src_world`, compositing the result into `dst_world`'s `dest_rect`.
///
/// `matrices` map source space to destination space when `src2dst_matrix` is
/// set (so each destination pixel is resampled through the inverse) and
/// destination-to-source otherwise. Multiple matrices are premultiply-
/// averaged (each tap weighted by its own alpha, then unpremultiplied) to
/// approximate motion blur without fringing at transparent edges.
pub(crate) unsafe extern "C" fn transform_world_sys(
	_effect_ref: PF_ProgPtr,
	_quality: PF_Quality,
	m_flags: PF_ModeFlags,
	field: PF_Field,
	src_world: *const PF_EffectWorld,
	comp_mode: *const PF_CompositeMode,
	mask_world0: *const PF_MaskWorld,
	matrices: *const PF_FloatMatrix,
	num_matrices: A_long,
	src2dst_matrix: PF_Boolean,
	dest_rect: *const PF_Rect,
	dst_world: *mut PF_EffectWorld,
) -> PF_Err {
	crate::core::ffi::guard("transform_world_sys", || {
		if src_world.is_null() || dst_world.is_null() || comp_mode.is_null() || matrices.is_null() || num_matrices <= 0
		{
			return PF_Err_BAD_CALLBACK_PARAM;
		}
		let src = unsafe { &*src_world };
		let dst = unsafe { &*dst_world };
		if src.data.is_null() || dst.data.is_null() {
			return PF_Err_BAD_CALLBACK_PARAM;
		}
		if !is_8bpc(src) || !is_8bpc(dst) {
			log::warn!("transform_world: only 8 bpc worlds are supported");
			return PF_Err_BAD_CALLBACK_PARAM;
		}
		let mode = unsafe { &*comp_mode };

		let rect = if !dest_rect.is_null() {
			unsafe { *dest_rect }
		} else {
			PF_Rect {
				left: 0,
				top: 0,
				right: dst.width,
				bottom: dst.height,
			}
		};
		let left = rect.left.max(0);
		let top = rect.top.max(0);
		let width = (rect.right.min(dst.width) - left).max(0);
		let height = (rect.bottom.min(dst.height) - top).max(0);
		if width == 0 || height == 0 {
			return PF_Err_NONE;
		}

		// Pre-invert into dst->src matrices up front so the per-pixel loop only
		// ever has to forward-map.
		let mats: Vec<Mat3> = unsafe { std::slice::from_raw_parts(matrices, num_matrices as usize) }
			.iter()
			.map(|m| {
				if src2dst_matrix != 0 {
					invert3(m.mat).unwrap_or(IDENTITY3)
				} else {
					m.mat
				}
			})
			.collect();

		let opacity = mode.opacity as f64 / 255.0;
		let premultiplied = m_flags & PF_MF_Alpha_STRAIGHT == 0;
		let rgb_only = mode.rgb_only != 0;
		let xfer = mode.xfer;

		let src_raw = RawWorld::from(src);
		let dst_raw = RawWorld::from(dst);
		let mask_raw = unsafe { mask_world0.as_ref() }.map(|m| (RawWorld::from(&m.mask), m.offset, m.what_is_mask));
		let mats = &mats[..];

		(0..height).into_par_iter().for_each(|row| {
			let dy = top + row;
			if skip_row_for_field(dy, field) {
				return;
			}
			for col in 0..width {
				let dx = left + col;

				let mut acc_a = 0.0;
				let mut acc_c = [0.0; 3];
				for mat in mats {
					let (sx, sy) = apply_matrix(mat, dx as f64 + 0.5, dy as f64 + 0.5);
					let (a, r, g, b) = unsafe { src_raw.sample_bilinear(sx - 0.5, sy - 0.5) };
					let a_n = a / 255.0;
					let (rp, gp, bp) = if premultiplied {
						(r / 255.0, g / 255.0, b / 255.0)
					} else {
						(r / 255.0 * a_n, g / 255.0 * a_n, b / 255.0 * a_n)
					};
					acc_a += a_n;
					acc_c[0] += rp;
					acc_c[1] += gp;
					acc_c[2] += bp;
				}
				let mut src_a = acc_a / mats.len() as f64;
				let src_c = if acc_a > 1e-9 {
					[acc_c[0] / acc_a, acc_c[1] / acc_a, acc_c[2] / acc_a]
				} else {
					[0.0; 3]
				};
				src_a *= opacity;
				if let Some((mraw, offset, what)) = mask_raw {
					src_a *= mask_value(mraw, offset, what, dx, dy);
				}

				let dp = unsafe { dst_raw.read(dx, dy) };
				let (dst_a, dst_c) = to_straight(dp, false);
				let (out_a, out_c) = composite_pixel(src_a, src_c, dst_a, dst_c, xfer);
				let final_a = if rgb_only { dst_a } else { out_a };
				unsafe { dst_raw.write(dx, dy, from_straight(final_a, out_c)) };
			}
		});

		PF_Err_NONE
	})
}

// ============================================================================
// Factory Function
// ============================================================================

/// Builds the `PF_WorldTransformSuite1` vtable.
///
/// `const` so it can initialize the shared [`SUITE_CONTAINER`](crate::suites::SUITE_CONTAINER)
/// static; the suite is a stateless table of function pointers.
pub const fn create_world_transform_suite_1() -> PF_WorldTransformSuite1 {
	PF_WorldTransformSuite1 {
		composite_rect: Some(composite_rect_sys),
		blend: Some(blend_sys),
		convolve: Some(convolve_sys),
		copy: Some(Copy_sys),
		copy_hq: Some(copy_hq_sys),
		transfer_rect: Some(transfer_rect_sys),
		transform_world: Some(transform_world_sys),
	}
}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn composite_rect_rejects_a_deep_world() {
		let mut src = super::super::world::alloc_world(2, 2, 8, PF_PixelFormat_ARGB64 as u32);
		let mut dst = super::super::world::alloc_world(2, 2, 4, PF_PixelFormat_ARGB32 as u32);
		let err = unsafe {
			composite_rect_sys(
				std::ptr::null_mut(),
				std::ptr::null_mut(),
				255,
				&mut src,
				0,
				0,
				PF_Field_FRAME,
				PF_Xfer_IN_FRONT as PF_XferMode,
				&mut dst,
			)
		};
		assert_eq!(err, PF_Err_BAD_CALLBACK_PARAM);
		unsafe {
			super::super::world::free_world(&mut src);
			super::super::world::free_world(&mut dst);
		}
	}

	/// An owned, heap-backed `PF_EffectWorld` for exercising suite entry
	/// points without a real host. `world.data` points into `buf`, which is
	/// why `buf` must outlive every raw-pointer read/write into `world`.
	struct TestWorld {
		buf: Vec<u8>,
		world: PF_EffectWorld,
	}

	impl TestWorld {
		fn new(width: i32, height: i32, fill: PF_Pixel8) -> Self {
			let rowbytes = width * PIXEL_SIZE as i32;
			let mut buf = vec![0u8; (rowbytes * height).max(0) as usize];
			for chunk in buf.as_chunks_mut::<{ PIXEL_SIZE as usize }>().0 {
				chunk[0] = fill.alpha;
				chunk[1] = fill.red;
				chunk[2] = fill.green;
				chunk[3] = fill.blue;
			}
			let world = PF_EffectWorld {
				reserved0: std::ptr::null_mut(),
				reserved1: std::ptr::null_mut(),
				world_flags: 0,
				data: buf.as_mut_ptr() as PF_PixelPtr,
				rowbytes,
				width,
				height,
				extent_hint: PF_UnionableRect {
					left: 0,
					top: 0,
					right: width,
					bottom: height,
				},
				platform_ref: std::ptr::null_mut(),
				reserved_long1: 0,
				reserved_long4: std::ptr::null_mut(),
				pix_aspect_ratio: PF_RationalScale { num: 1, den: 1 },
				reserved_long2: std::ptr::null_mut(),
				origin_x: 0,
				origin_y: 0,
				reserved_long3: 0,
				dephault: 0,
			};
			Self { buf, world }
		}

		fn pixel(&self, x: i32, y: i32) -> PF_Pixel8 {
			let idx = (y * self.world.rowbytes + x * PIXEL_SIZE as i32) as usize;
			PF_Pixel8 {
				alpha: self.buf[idx],
				red: self.buf[idx + 1],
				green: self.buf[idx + 2],
				blue: self.buf[idx + 3],
			}
		}

		fn as_mut_ptr(&mut self) -> *mut PF_EffectWorld {
			&mut self.world
		}
	}

	fn px(a: u8, r: u8, g: u8, b: u8) -> PF_Pixel8 {
		PF_Pixel8 {
			alpha: a,
			red: r,
			green: g,
			blue: b,
		}
	}

	/// `PF_Pixel` (from `after-effects-sys`) doesn't derive `PartialEq`, so
	/// tests compare fields through this helper instead.
	#[track_caller]
	fn assert_px_eq(actual: PF_Pixel8, expected: PF_Pixel8, msg: &str) {
		assert_eq!(
			(actual.alpha, actual.red, actual.green, actual.blue),
			(expected.alpha, expected.red, expected.green, expected.blue),
			"{msg}"
		);
	}

	#[test]
	fn blend_at_endpoints_and_midpoint() {
		let mut s1 = TestWorld::new(2, 2, px(255, 100, 0, 0));
		let mut s2 = TestWorld::new(2, 2, px(255, 0, 200, 0));
		let mut d = TestWorld::new(2, 2, px(0, 0, 0, 0));

		unsafe {
			blend_sys(
				std::ptr::null_mut(),
				s1.as_mut_ptr(),
				s2.as_mut_ptr(),
				0,
				d.as_mut_ptr(),
			)
		};
		assert_px_eq(d.pixel(0, 0), px(255, 100, 0, 0), "ratio=0 should equal src1");

		unsafe {
			blend_sys(
				std::ptr::null_mut(),
				s1.as_mut_ptr(),
				s2.as_mut_ptr(),
				1 << 16,
				d.as_mut_ptr(),
			)
		};
		assert_px_eq(d.pixel(0, 0), px(255, 0, 200, 0), "ratio=1 should equal src2");

		unsafe {
			blend_sys(
				std::ptr::null_mut(),
				s1.as_mut_ptr(),
				s2.as_mut_ptr(),
				1 << 15,
				d.as_mut_ptr(),
			)
		};
		assert_px_eq(d.pixel(0, 0), px(255, 50, 100, 0), "ratio=0.5 should be the midpoint");
	}

	#[test]
	fn composite_rect_copy_opaque_src_replaces_dst() {
		let mut src = TestWorld::new(2, 2, px(255, 10, 20, 30));
		let mut dst = TestWorld::new(2, 2, px(255, 200, 200, 200));

		let err = unsafe {
			composite_rect_sys(
				std::ptr::null_mut(),
				std::ptr::null_mut(),
				255,
				src.as_mut_ptr(),
				0,
				0,
				PF_Field_FRAME,
				PF_Xfer_COPY,
				dst.as_mut_ptr(),
			)
		};
		assert_eq!(err, PF_Err_NONE);
		assert_px_eq(
			dst.pixel(0, 0),
			px(255, 10, 20, 30),
			"opaque COPY should fully replace dst",
		);
	}

	#[test]
	fn composite_rect_zero_opacity_leaves_dst_unchanged() {
		let mut src = TestWorld::new(2, 2, px(255, 10, 20, 30));
		let mut dst = TestWorld::new(2, 2, px(255, 200, 200, 200));

		unsafe {
			composite_rect_sys(
				std::ptr::null_mut(),
				std::ptr::null_mut(),
				0,
				src.as_mut_ptr(),
				0,
				0,
				PF_Field_FRAME,
				PF_Xfer_COPY,
				dst.as_mut_ptr(),
			)
		};
		assert_px_eq(
			dst.pixel(0, 0),
			px(255, 200, 200, 200),
			"zero opacity should leave dst untouched",
		);
	}

	#[test]
	fn copy_hq_same_size_matches_source() {
		let mut src = TestWorld::new(3, 3, px(255, 42, 84, 126));
		let mut dst = TestWorld::new(3, 3, px(0, 0, 0, 0));

		unsafe {
			copy_hq_sys(
				std::ptr::null_mut(),
				src.as_mut_ptr(),
				dst.as_mut_ptr(),
				std::ptr::null_mut(),
				std::ptr::null_mut(),
			)
		};

		for y in 0..3 {
			for x in 0..3 {
				assert_px_eq(dst.pixel(x, y), px(255, 42, 84, 126), &format!("mismatch at ({x},{y})"));
			}
		}
	}

	#[test]
	fn convolve_identity_kernel_is_a_no_op() {
		let mut src = TestWorld::new(3, 3, px(255, 10, 20, 30));
		let mut dst = TestWorld::new(3, 3, px(0, 0, 0, 0));

		// A 3x3 kernel with a single 1 at the center and 0 everywhere else
		// convolves to exactly the source value at every pixel.
		let mut kernel = [0i32; 9];
		kernel[4] = 1;

		let err = unsafe {
			convolve_sys(
				std::ptr::null_mut(),
				src.as_mut_ptr(),
				std::ptr::null(),
				PF_KernelFlag_NORMALIZED,
				3,
				kernel.as_mut_ptr() as *mut c_void,
				kernel.as_mut_ptr() as *mut c_void,
				kernel.as_mut_ptr() as *mut c_void,
				kernel.as_mut_ptr() as *mut c_void,
				dst.as_mut_ptr(),
			)
		};
		assert_eq!(err, PF_Err_NONE);
		for y in 0..3 {
			for x in 0..3 {
				assert_px_eq(dst.pixel(x, y), px(255, 10, 20, 30), &format!("mismatch at ({x},{y})"));
			}
		}
	}

	#[test]
	fn convolve_rejects_aliased_buffers() {
		let mut w = TestWorld::new(2, 2, px(255, 1, 2, 3));
		let ptr = w.as_mut_ptr();
		let kernel = [1i32];
		let err = unsafe {
			convolve_sys(
				std::ptr::null_mut(),
				ptr,
				std::ptr::null(),
				PF_KernelFlag_NORMALIZED,
				1,
				std::ptr::null_mut(),
				std::ptr::null_mut(),
				std::ptr::null_mut(),
				kernel.as_ptr() as *mut c_void,
				ptr,
			)
		};
		assert_eq!(err, PF_Err_BAD_CALLBACK_PARAM);
	}

	#[test]
	fn transform_world_identity_matrix_is_a_no_op() {
		let mut src = TestWorld::new(4, 4, px(255, 5, 6, 7));
		let mut dst = TestWorld::new(4, 4, px(0, 0, 0, 0));
		let comp_mode = PF_CompositeMode {
			xfer: PF_Xfer_COPY,
			rand_seed: 0,
			opacity: 255,
			rgb_only: 0,
			opacitySu: 0,
		};
		let matrix = PF_FloatMatrix { mat: IDENTITY3 };

		let err = unsafe {
			transform_world_sys(
				std::ptr::null_mut(),
				PF_Quality_HI,
				PF_MF_Alpha_STRAIGHT,
				PF_Field_FRAME,
				src.as_mut_ptr(),
				&comp_mode as *const _,
				std::ptr::null(),
				&matrix as *const _,
				1,
				0,
				std::ptr::null(),
				dst.as_mut_ptr(),
			)
		};
		assert_eq!(err, PF_Err_NONE);
		for y in 1..3 {
			for x in 1..3 {
				assert_px_eq(dst.pixel(x, y), px(255, 5, 6, 7), &format!("mismatch at ({x},{y})"));
			}
		}
	}
}