Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Move image dimension decoding to image utility

Neeraj Guptac13a3740beac
diff --git a/mobile/apps/photos/lib/ui/viewer/file_details/file_properties_item_widget.dart b/mobile/apps/photos/lib/ui/viewer/file_details/file_properties_item_widget.dart
index ea38612d5f..e729702db6 100644
--- a/mobile/apps/photos/lib/ui/viewer/file_details/file_properties_item_widget.dart
+++ b/mobile/apps/photos/lib/ui/viewer/file_details/file_properties_item_widget.dart
@@ -7,8 +7,8 @@ import 'package:photos/models/file/file.dart';
import 'package:photos/models/file/file_type.dart';
import "package:photos/theme/ente_theme.dart";
import "package:photos/ui/components/info_item_widget.dart";
-import "package:photos/utils/file_uploader_util.dart";
import "package:photos/utils/file_util.dart";
+import "package:photos/utils/image_util.dart";
import "package:photos/utils/magic_util.dart";
class FilePropertiesItemWidget extends StatefulWidget {
@@ -70,11 +70,11 @@ class _FilePropertiesItemWidgetState extends State<FilePropertiesItemWidget> {
try {
final localFile = await getFile(widget.file);
final decoded = localFile != null
- ? await getImageHeightAndWith(imagePath: localFile.path)
+ ? await getImageDimensions(imagePath: localFile.path)
: null;
if (decoded != null) {
- width = decoded["width"];
- height = decoded["height"];
+ width = decoded.width;
+ height = decoded.height;
}
} catch (_) {}
}
diff --git a/mobile/apps/photos/lib/utils/file_uploader_util.dart b/mobile/apps/photos/lib/utils/file_uploader_util.dart
index 8784f86b80..17cc1668f0 100644
--- a/mobile/apps/photos/lib/utils/file_uploader_util.dart
+++ b/mobile/apps/photos/lib/utils/file_uploader_util.dart
@@ -1,7 +1,6 @@
import 'dart:async';
import "dart:io";
import 'dart:typed_data';
-import 'dart:ui' as ui;
import "package:archive/archive_io.dart";
import "package:computer/computer.dart";
@@ -29,6 +28,7 @@ import "package:photos/utils/apple_photos_errors.dart";
import "package:photos/utils/embedded_media_location.dart";
import "package:photos/utils/exif_util.dart";
import 'package:photos/utils/file_util.dart';
+import "package:photos/utils/image_util.dart";
import 'package:photos/utils/thumbnail_util.dart';
import "package:uuid/uuid.dart";
import 'package:video_thumbnail/video_thumbnail.dart';
@@ -344,9 +344,9 @@ Future<MediaUploadData> _getMediaUploadDataFromAppCache(
final fileHash = CryptoUtil.bin2base64(
await CryptoUtil.getHash(sourceFile),
);
- Map<String, int>? dimensions;
+ ({int width, int height})? dimensions;
if (file.fileType == FileType.image) {
- dimensions = await getImageHeightAndWith(imagePath: localPath);
+ dimensions = await getImageDimensions(imagePath: localPath);
exifData = await tryExifFromFile(sourceFile);
if (exifData != null) {
cameraMake = extractPrintableExifValue(exifData['Image Make']);
@@ -367,7 +367,7 @@ Future<MediaUploadData> _getMediaUploadDataFromAppCache(
thumbnailPath: (await getTemporaryDirectory()).path,
quality: 10,
);
- dimensions = await getImageHeightAndWith(imagePath: thumbnailFilePath);
+ dimensions = await getImageDimensions(imagePath: thumbnailFilePath);
}
if (!file.hasLocation && file.isVideo && Platform.isAndroid) {
@@ -381,8 +381,8 @@ Future<MediaUploadData> _getMediaUploadDataFromAppCache(
thumbnailData,
isDeleted,
FileHashData(fileHash),
- height: dimensions?['height'],
- width: dimensions?['width'],
+ height: dimensions?.height,
+ width: dimensions?.width,
cameraMake: cameraMake,
cameraModel: cameraModel,
exifData: exifData,
@@ -395,31 +395,3 @@ Future<MediaUploadData> _getMediaUploadDataFromAppCache(
);
}
}
-
-Future<Map<String, int>?> getImageHeightAndWith({
- String? imagePath,
- Uint8List? imageBytes,
-}) async {
- if (imagePath == null && imageBytes == null) {
- throw ArgumentError("imagePath and imageBytes cannot be null");
- }
- try {
- late Uint8List bytes;
- if (imagePath != null) {
- final File imageFile = File(imagePath);
- bytes = await imageFile.readAsBytes();
- } else {
- bytes = imageBytes!;
- }
- final ui.Codec codec = await ui.instantiateImageCodec(bytes);
- final ui.FrameInfo frameInfo = await codec.getNextFrame();
- if (frameInfo.image.width == 0 || frameInfo.image.height == 0) {
- return null;
- } else {
- return {"width": frameInfo.image.width, "height": frameInfo.image.height};
- }
- } catch (e) {
- _logger.severe("Failed to get image size", e);
- return null;
- }
-}
diff --git a/mobile/apps/photos/lib/utils/image_util.dart b/mobile/apps/photos/lib/utils/image_util.dart
index dc4994fc2a..5938698454 100644
--- a/mobile/apps/photos/lib/utils/image_util.dart
+++ b/mobile/apps/photos/lib/utils/image_util.dart
@@ -1,14 +1,19 @@
import 'dart:async';
+import 'dart:io';
import 'dart:typed_data';
+import 'dart:ui' as ui;
import 'package:exif_reader/exif_reader.dart' as exif;
import 'package:flutter/widgets.dart';
import 'package:image/image.dart' as img;
// ignore: implementation_imports
import 'package:image/src/util/rational.dart' as img_util;
+import 'package:logging/logging.dart';
import "package:photos/models/file/file.dart";
import "package:photos/utils/exif_util.dart";
+final _logger = Logger("ImageUtil");
+
Future<ImageInfo> getImageInfo(ImageProvider imageProvider) {
final completer = Completer<ImageInfo>();
final imageStream = imageProvider.resolve(const ImageConfiguration());
@@ -20,6 +25,32 @@ Future<ImageInfo> getImageInfo(ImageProvider imageProvider) {
return completer.future;
}
+Future<({int width, int height})?> getImageDimensions({
+ String? imagePath,
+ Uint8List? imageBytes,
+}) async {
+ if (imagePath == null && imageBytes == null) {
+ throw ArgumentError("imagePath and imageBytes cannot be null");
+ }
+ try {
+ late Uint8List bytes;
+ if (imagePath != null) {
+ bytes = await File(imagePath).readAsBytes();
+ } else {
+ bytes = imageBytes!;
+ }
+ final ui.Codec codec = await ui.instantiateImageCodec(bytes);
+ final ui.FrameInfo frameInfo = await codec.getNextFrame();
+ if (frameInfo.image.width == 0 || frameInfo.image.height == 0) {
+ return null;
+ }
+ return (width: frameInfo.image.width, height: frameInfo.image.height);
+ } catch (e) {
+ _logger.severe("Failed to get image size", e);
+ return null;
+ }
+}
+
// dart format off
const _copiedExifFields = [
(ifd: "Image", src: "Make", dst: "Make", render: false),