Files
immich/mobile/lib/utils/editor.utils.dart
2026-01-24 00:18:51 -06:00

40 lines
1.7 KiB
Dart

import 'package:crop_image/crop_image.dart';
import 'package:flutter/widgets.dart';
import 'package:openapi/api.dart';
Rect convertCropParametersToRect(CropParameters parameters, int originalWidth, int originalHeight) {
return Rect.fromLTWH(
parameters.x.toDouble() / originalWidth,
parameters.y.toDouble() / originalHeight,
parameters.width.toDouble() / originalWidth,
parameters.height.toDouble() / originalHeight,
);
}
CropParameters convertRectToCropParameters(Rect rect, int originalWidth, int originalHeight) {
return CropParameters(
x: (rect.left * originalWidth).round(),
y: (rect.top * originalHeight).round(),
width: (rect.width * originalWidth).round(),
height: (rect.height * originalHeight).round(),
);
}
Rect convertCropRectToRotated(Rect cropRect, CropRotation rotation) {
return switch (rotation) {
CropRotation.up => cropRect,
CropRotation.right => Rect.fromLTWH(1 - cropRect.bottom, cropRect.left, cropRect.height, cropRect.width),
CropRotation.down => Rect.fromLTWH(1 - cropRect.right, 1 - cropRect.bottom, cropRect.width, cropRect.height),
CropRotation.left => Rect.fromLTWH(cropRect.top, 1 - cropRect.right, cropRect.height, cropRect.width),
};
}
Rect convertCropRectFromRotated(Rect cropRect, CropRotation rotation) {
return switch (rotation) {
CropRotation.up => cropRect,
CropRotation.right => Rect.fromLTWH(cropRect.top, 1 - cropRect.right, cropRect.height, cropRect.width),
CropRotation.down => Rect.fromLTWH(1 - cropRect.right, 1 - cropRect.bottom, cropRect.width, cropRect.height),
CropRotation.left => Rect.fromLTWH(1 - cropRect.bottom, cropRect.left, cropRect.height, cropRect.width),
};
}