import 'dart:math'; import 'dart:typed_data'; import 'package:flutter_test/flutter_test.dart'; import 'package:image/image.dart' as img; import 'package:sd_kovinar/screens/yolo_detector.dart'; /// The model input as it was built before the speed-up: decode, crop the /// centre square, resize (nearest), then pixel by pixel. Float32List oldInput(Uint8List jpeg, int width, int height) { final decoded = img.decodeImage(jpeg)!; final size = min(decoded.width, decoded.height); final square = img.copyCrop(decoded, x: (decoded.width - size) ~/ 2, y: (decoded.height - size) ~/ 2, width: size, height: size); final resized = img.copyResize(square, width: width, height: height); final input = Float32List(width * height * 3); int i = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { final p = resized.getPixel(x, y); input[i++] = p.r / 255.0; input[i++] = p.g / 255.0; input[i++] = p.b / 255.0; } } return input; } /// A photo-like JPEG: smooth shapes plus sensor noise, so it compresses like /// a camera picture rather than pure noise. Uint8List cameraLikeJpeg(int width, int height, {int seed = 1, int? orientation}) { final rng = Random(seed); final image = img.Image(width: width, height: height); for (final p in image) { final d = sqrt(pow(p.x - width / 2, 2) + pow(p.y - height / 2, 2)); final ring = (d ~/ 40).isEven ? 230 : 40; p.setRgb((ring + rng.nextInt(20)).clamp(0, 255), (ring * 0.9 + rng.nextInt(20)).clamp(0, 255), (p.x * 255 ~/ width)); } img.fillRect(image, x1: 0, y1: 0, x2: 80, y2: 40, color: img.ColorRgb8(255, 0, 0)); if (orientation != null) image.exif.imageIfd.orientation = orientation; return img.encodeJpg(image, quality: 92); } void main() { group('model input with the Dart decoder is bit-identical to before', () { for (final (w, h) in [(1280, 720), (720, 1280), (720, 720), (640, 640), (1000, 700)]) { test('${w}x$h photo', () { final jpeg = cameraLikeJpeg(w, h); final now = inputFromPhoto(decodePhotoDart(jpeg), 640, 640); expect(now, orderedEquals(oldInput(jpeg, 640, 640))); }); } }); test('saved square is the same as cropping the decoded photo', () { final jpeg = cameraLikeJpeg(1280, 720); final decoded = img.decodeImage(jpeg)!; final expected = img.copyCrop(decoded, x: 280, y: 0, width: 720, height: 720); final square = squareOf(decodePhotoDart(jpeg)); expect(square.width, 720); expect(square.height, 720); for (final (x, y) in [(0, 0), (359, 359), (719, 719), (100, 600)]) { final a = square.getPixel(x, y), b = expected.getPixel(x, y); expect([a.r, a.g, a.b], [b.r, b.g, b.b], reason: 'pixel $x,$y'); } }); group('platform decoder', () { testWidgets('applies the EXIF rotation like the Dart decoder', (tester) async { // Stored 1280x720 with "rotate 90°": both decoders must give 720x1280 // with the red corner in the same place final jpeg = cameraLikeJpeg(1280, 720, orientation: 6); final dart = decodePhotoDart(jpeg); final native = (await tester.runAsync(() => decodePhotoNative(jpeg)))!; expect([native.width, native.height], [dart.width, dart.height]); expect([native.width, native.height], [720, 1280]); int red(Photo p, int x, int y) => p.pixels[(y * p.width + x) * p.channels]; // Red block was top-left before rotating 90° clockwise: now top-right expect(red(native, native.width - 10, 10), greaterThan(200)); expect(red(dart, dart.width - 10, 10), greaterThan(200)); }); testWidgets('gives practically the same model input', (tester) async { final jpeg = cameraLikeJpeg(1280, 720, seed: 7); final dart = inputFromPhoto(decodePhotoDart(jpeg), 640, 640); final decoded = await tester.runAsync(() => decodePhotoNative(jpeg)); final native = inputFromPhoto(decoded!, 640, 640); var maxDiff = 0.0, sum = 0.0, differing = 0; for (int i = 0; i < dart.length; i++) { final d = (dart[i] - native[i]).abs(); if (d > 0) differing++; sum += d; maxDiff = max(maxDiff, d); } // ignore: avoid_print print('platform vs Dart decoder: ${(differing / dart.length * 100).toStringAsFixed(1)}% ' 'of values differ, mean ${(sum / dart.length * 255).toStringAsFixed(3)} / 255, ' 'max ${(maxDiff * 255).round()} / 255'); // JPEG decoders may round a few values differently; nothing visible expect(sum / dart.length * 255, lessThan(1.0)); }); }); }