mirror of
https://github.com/immich-app/immich.git
synced 2026-01-25 02:44:46 -08:00
47 lines
1.9 KiB
Dart
47 lines
1.9 KiB
Dart
import 'dart:math';
|
|
|
|
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) {
|
|
final x = (rect.left * originalWidth).round();
|
|
final y = (rect.top * originalHeight).round();
|
|
final width = (rect.width * originalWidth).round();
|
|
final height = (rect.height * originalHeight).round();
|
|
|
|
return CropParameters(
|
|
x: max(x, 0).clamp(0, originalWidth),
|
|
y: max(y, 0).clamp(0, originalHeight),
|
|
width: max(width, 0).clamp(0, originalWidth - x),
|
|
height: max(height, 0).clamp(0, originalHeight - y),
|
|
);
|
|
}
|
|
|
|
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),
|
|
};
|
|
}
|