39 lines
1.9 KiB
Dart
39 lines
1.9 KiB
Dart
// toLandscape 转置变换的像素级验证:纵向四象限色块 → 转置后对角象限互换。
|
||
// 转置语义:P(x, y) -> out(y, x),即左上/右下不动,右上↔左下互换。
|
||
import 'dart:convert';
|
||
|
||
import 'package:desktop_plugin/image/card_image.dart';
|
||
import 'package:image/image.dart';
|
||
import 'package:flutter_test/flutter_test.dart';
|
||
|
||
void main() {
|
||
test('toLandscape 把纵向卡面转置为横向(对角象限互换)', () {
|
||
// 构造 642×1014 纵向源图:四象限填不同颜色(fromDataUrl 尺寸相同不触发缩放)
|
||
final src = Image(width: 642, height: 1014);
|
||
fillRect(src, x1: 0, y1: 0, x2: 641, y2: 506, color: ColorRgb8(255, 0, 0)); // 上:红
|
||
fillRect(src, x1: 0, y1: 507, x2: 320, y2: 1013, color: ColorRgb8(0, 255, 0)); // 左下:绿
|
||
fillRect(src, x1: 321, y1: 507, x2: 641, y2: 1013, color: ColorRgb8(0, 0, 255)); // 右下:蓝
|
||
final dataUrl = 'data:image/png;base64,${base64Encode(encodePng(src))}';
|
||
|
||
final card = CardImage.fromDataUrl(dataUrl);
|
||
expect(card.isLandscape, isFalse, reason: '源图应为纵向');
|
||
|
||
final out = card.toLandscape();
|
||
expect(out.isLandscape, isTrue, reason: '转置后应为横向 1014×642');
|
||
expect(out.width, 1014);
|
||
expect(out.height, 642);
|
||
|
||
// 像素断言:转置后 out(x', y') = src(y', x')(以中心附近取样避免象限边界)
|
||
final bgr = out.toBgr24();
|
||
// 转置后左上(原上半红区)≈ 红
|
||
(int, int, int) px(int x, int y) {
|
||
final o = (y * out.width + x) * 3;
|
||
return (bgr[o + 2], bgr[o + 1], bgr[o]); // BGR 转回 RGB
|
||
}
|
||
expect(px(300, 100), (255, 0, 0), reason: '左上应保持红(源上半)');
|
||
// 转置把源的"右上"(x大,y小)映到"左下":源上半仍红,源左下绿映到右半
|
||
expect(px(800, 300), (0, 255, 0), reason: '右半上应为绿(源左下转置)');
|
||
expect(px(800, 500), (0, 0, 255), reason: '右半下应为蓝(源右下转置)');
|
||
});
|
||
}
|