mirror of
https://github.com/immich-app/immich.git
synced 2026-02-28 09:38:43 +03:00
feat: mobile editing
This commit is contained in:
70
mobile/lib/utils/editor.utils.dart
Normal file
70
mobile/lib/utils/editor.utils.dart
Normal file
@@ -0,0 +1,70 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:immich_mobile/domain/models/asset_edit.model.dart';
|
||||
import 'package:immich_mobile/utils/matrix.utils.dart';
|
||||
import 'package:openapi/api.dart' hide AssetEditAction;
|
||||
|
||||
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),
|
||||
);
|
||||
}
|
||||
|
||||
AffineMatrix buildAffineFromEdits(List<AssetEdit> edits) {
|
||||
return AffineMatrix.compose(
|
||||
edits.map<AffineMatrix>((edit) {
|
||||
switch (edit.action) {
|
||||
case AssetEditAction.rotate:
|
||||
final angleInDegrees = edit.parameters["angle"] as num;
|
||||
final angleInRadians = angleInDegrees * pi / 180;
|
||||
return AffineMatrix.rotate(angleInRadians);
|
||||
case AssetEditAction.mirror:
|
||||
final axis = edit.parameters["axis"] as String;
|
||||
return axis == "horizontal" ? AffineMatrix.flipY() : AffineMatrix.flipX();
|
||||
default:
|
||||
return AffineMatrix.identity();
|
||||
}
|
||||
}).toList(),
|
||||
);
|
||||
}
|
||||
|
||||
bool isCloseToZero(double value, [double epsilon = 1e-15]) {
|
||||
return value.abs() < epsilon;
|
||||
}
|
||||
|
||||
typedef NormalizedTransform = ({double rotation, bool mirrorHorizontal, bool mirrorVertical});
|
||||
|
||||
NormalizedTransform normalizeTransformEdits(List<AssetEdit> edits) {
|
||||
final matrix = buildAffineFromEdits(edits);
|
||||
|
||||
double a = matrix.a;
|
||||
double b = matrix.b;
|
||||
double c = matrix.c;
|
||||
double d = matrix.d;
|
||||
|
||||
final rotation = ((isCloseToZero(a) ? asin(c) : acos(a)) * 180) / pi;
|
||||
|
||||
return (
|
||||
rotation: rotation < 0 ? 360 + rotation : rotation,
|
||||
mirrorHorizontal: false,
|
||||
mirrorVertical: isCloseToZero(a) ? b == c : a == -d,
|
||||
);
|
||||
}
|
||||
@@ -1,8 +1,14 @@
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:crop_image/crop_image.dart';
|
||||
import 'dart:ui'; // Import the dart:ui library for Rect
|
||||
|
||||
import 'package:crop_image/crop_image.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
|
||||
/// A hook that provides a [CropController] instance.
|
||||
CropController useCropController() {
|
||||
return useMemoized(() => CropController(defaultCrop: const Rect.fromLTRB(0, 0, 1, 1)));
|
||||
CropController useCropController({Rect? initialCrop, CropRotation? initialRotation}) {
|
||||
return useMemoized(
|
||||
() => CropController(
|
||||
defaultCrop: initialCrop ?? const Rect.fromLTRB(0, 0, 1, 1),
|
||||
rotation: initialRotation ?? CropRotation.up,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
50
mobile/lib/utils/matrix.utils.dart
Normal file
50
mobile/lib/utils/matrix.utils.dart
Normal file
@@ -0,0 +1,50 @@
|
||||
import 'dart:math';
|
||||
|
||||
class AffineMatrix {
|
||||
final double a;
|
||||
final double b;
|
||||
final double c;
|
||||
final double d;
|
||||
final double e;
|
||||
final double f;
|
||||
|
||||
const AffineMatrix(this.a, this.b, this.c, this.d, this.e, this.f);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'AffineMatrix(a: $a, b: $b, c: $c, d: $d, e: $e, f: $f)';
|
||||
}
|
||||
|
||||
factory AffineMatrix.identity() {
|
||||
return const AffineMatrix(1, 0, 0, 1, 0, 0);
|
||||
}
|
||||
|
||||
AffineMatrix multiply(AffineMatrix other) {
|
||||
return AffineMatrix(
|
||||
a * other.a + c * other.b,
|
||||
b * other.a + d * other.b,
|
||||
a * other.c + c * other.d,
|
||||
b * other.c + d * other.d,
|
||||
a * other.e + c * other.f + e,
|
||||
b * other.e + d * other.f + f,
|
||||
);
|
||||
}
|
||||
|
||||
factory AffineMatrix.compose([List<AffineMatrix> transformations = const []]) {
|
||||
return transformations.fold<AffineMatrix>(AffineMatrix.identity(), (acc, matrix) => acc.multiply(matrix));
|
||||
}
|
||||
|
||||
factory AffineMatrix.rotate(double angle) {
|
||||
final cosAngle = cos(angle);
|
||||
final sinAngle = sin(angle);
|
||||
return AffineMatrix(cosAngle, -sinAngle, sinAngle, cosAngle, 0, 0);
|
||||
}
|
||||
|
||||
factory AffineMatrix.flipY() {
|
||||
return const AffineMatrix(-1, 0, 0, 1, 0, 0);
|
||||
}
|
||||
|
||||
factory AffineMatrix.flipX() {
|
||||
return const AffineMatrix(1, 0, 0, -1, 0, 0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user