Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mobile][photos] copy exif using only getExif implementation, only limited fields

Priyanshu Dangare739e4e661ba3
diff --git a/mobile/apps/photos/lib/ui/tools/editor/image_editor/image_editor_page.dart b/mobile/apps/photos/lib/ui/tools/editor/image_editor/image_editor_page.dart
index 5b1f9ca769..072fa68c04 100644
--- a/mobile/apps/photos/lib/ui/tools/editor/image_editor/image_editor_page.dart
+++ b/mobile/apps/photos/lib/ui/tools/editor/image_editor/image_editor_page.dart
@@ -5,7 +5,6 @@ import 'dart:ui' as ui show Image, ImageByteFormat;
import "package:ente_components/ente_components.dart";
import "package:ente_pure_utils/ente_pure_utils.dart";
-import "package:exif_reader/exif_reader.dart";
import 'package:flutter/material.dart';
import "package:flutter/services.dart";
import "package:flutter_image_compress/flutter_image_compress.dart";
@@ -36,8 +35,7 @@ import "package:photos/ui/tools/editor/image_editor/image_editor_text_bar.dart";
import "package:photos/ui/tools/editor/image_editor/image_editor_tune_bar.dart";
import "package:photos/ui/viewer/file/detail_page.dart";
import "package:photos/utils/dialog_util.dart";
-import "package:photos/utils/exif_util.dart";
-import 'package:photos/utils/file_util.dart';
+import "package:photos/utils/image_util.dart";
import 'package:pro_image_editor/pro_image_editor.dart';
class ImageEditorPage extends StatefulWidget {
@@ -84,20 +82,14 @@ class _ImageEditorPageState extends State<ImageEditorPage> {
format: CompressFormat.jpeg,
);
if (flagService.internalUser) {
- final image = img.decodePng(bytes);
- if (image != null) {
- final originalExifTags = await _getOriginalExifTags(
- widget.originalFile,
- );
- final originalImageExif = await _readOriginalImageExif(
- widget.originalFile,
- );
- if (originalImageExif != null || originalExifTags.isNotEmpty) {
- image.exif = originalImageExif ?? img.ExifData();
- _clearEditorExifTags(image.exif);
- _copyCameraExif(originalExifTags, image.exif);
+ try {
+ final image = img.decodePng(bytes);
+ if (image != null) {
+ await copyEXIF(widget.originalFile, image);
result = img.encodeJpg(image, quality: 95);
}
+ } catch (e, s) {
+ _logger.warning("Image Editor: copyEXIF failed", e, s);
}
}
_logger.info('Size after compression = ${result.length}');
@@ -173,120 +165,6 @@ class _ImageEditorPageState extends State<ImageEditorPage> {
}
}
- Future<Map<String, IfdTag>> _getOriginalExifTags(
- ente.EnteFile enteFile,
- ) async {
- try {
- return await getExif(enteFile);
- } catch (e, s) {
- _logger.warning("Image Editor: getExif failed", e, s);
- return const {};
- }
- }
-
- Future<img.ExifData?> _readOriginalImageExif(ente.EnteFile enteFile) async {
- final file = await getFile(enteFile, isOrigin: true);
- if (file == null) {
- return null;
- }
- try {
- final bytes = await file.readAsBytes();
- final image = img.decodeImage(bytes);
- return image?.exif;
- } catch (e, s) {
- _logger.warning("Image Editor: _readOriginalImageExif failed: ", e, s);
- return null;
- } finally {
- if (!enteFile.isRemoteOnlyFile && Platform.isIOS) {
- await file.delete();
- }
- }
- }
-
- void _clearEditorExifTags(img.ExifData exif) {
- final imageIfdKeys = [
- "Orientation",
- "WhitePoint",
- "PrimaryChromaticities",
- "PhotometricInterpretation",
- "BitsPerSample",
- "SamplesPerPixel",
- "Compression",
- "ImageWidth",
- "ImageHeight",
- "YCbCrPositioning",
- "YCbCrSubSampling",
- "YCbCrCoefficients",
- "ReferenceBlackWhite",
- "XResolution",
- "YResolution",
- "ResolutionUnit",
- ];
- final thumbnailIfdKeys = [
- "JPEGInterchangeFormat",
- "JPEGInterchangeFormatLength",
- ];
- final exifIfdKeys = [
- "ColorSpace",
- "Gamma",
- "ExifImageWidth",
- "ExifImageHeight",
- "ExifImageLength",
- "ComponentsConfiguration",
- "CustomRendered",
- "SceneCaptureType",
- "WhiteBalance",
- "LightSource",
- "Flash",
- "GainControl",
- "Contrast",
- "Saturation",
- "Sharpness",
- "SubjectDistanceRange",
- ];
-
- void clear(img.IfdDirectory ifd, List<String> keys) {
- for (final key in keys) {
- ifd[key] = null;
- }
- }
-
- clear(exif.imageIfd, imageIfdKeys);
- clear(exif.thumbnailIfd, thumbnailIfdKeys);
- clear(exif.exifIfd, exifIfdKeys);
- }
-
- void _copyCameraExif(Map<String, IfdTag> tags, img.ExifData exif) {
- for (final entry in {
- "Image Make": "Make",
- "Image Model": "Model",
- }.entries) {
- final value = tags[entry.key]?.toString();
- if (value != null) {
- exif.imageIfd[entry.value] = value;
- }
- }
-
- for (final entry in {
- "EXIF FocalLength": "FocalLength",
- "EXIF FNumber": "FNumber",
- "EXIF ExposureTime": "ExposureTime",
- }.entries) {
- final value = tags[entry.key]?.values.toList().firstOrNull;
- if (value is Ratio) {
- exif.exifIfd[entry.value] = img.IfdValueRational(
- value.numerator,
- value.denominator,
- );
- }
- }
-
- final iso = tags["EXIF ISOSpeedRatings"]?.values;
- if (iso != null && iso.length > 0) {
- exif.exifIfd["ISOSpeed"] = iso.firstAsInt();
- }
- }
-
Future<void> _showExitConfirmationDialog(BuildContext context) async {
final actionResult = await showActionSheet(
context: context,
diff --git a/mobile/apps/photos/lib/utils/image_util.dart b/mobile/apps/photos/lib/utils/image_util.dart
index a1255a5b1e..231f6ace7b 100644
--- a/mobile/apps/photos/lib/utils/image_util.dart
+++ b/mobile/apps/photos/lib/utils/image_util.dart
@@ -1,8 +1,14 @@
import 'dart:async';
+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:photos/models/file/file.dart";
+import "package:photos/utils/exif_util.dart";
Future<ImageInfo> getImageInfo(ImageProvider imageProvider) {
final completer = Completer<ImageInfo>();
@@ -47,3 +53,76 @@ Future<ui.Image> convertImageToFlutterUi(img.Image image) async {
return uiImage;
}
+
+const _copiedExifFields = [
+ (ifd: "Image", source: "Make", dest: "Make"),
+ (ifd: "Image", source: "Model", dest: "Model"),
+ (ifd: "Image", source: "Orientation", dest: "Orientation"),
+ (ifd: "Image", source: "Artist", dest: "Artist"),
+ (ifd: "Image", source: "Copyright", dest: "Copyright"),
+ (ifd: "EXIF", source: "DateTimeOriginal", dest: "DateTimeOriginal"),
+ (ifd: "EXIF", source: "DateTimeDigitized", dest: "DateTimeDigitized"),
+ (ifd: "EXIF", source: "OffsetTimeOriginal", dest: "OffsetTimeOriginal"),
+ (ifd: "EXIF", source: "LensModel", dest: "LensModel"),
+ (ifd: "EXIF", source: "ExposureTime", dest: "ExposureTime"),
+ (ifd: "EXIF", source: "FNumber", dest: "FNumber"),
+ (ifd: "EXIF", source: "ISOSpeedRatings", dest: "ISOSpeed"), //ishowspeed
+ (ifd: "EXIF", source: "FocalLength", dest: "FocalLength"),
+ (ifd: "EXIF", source: "FocalLengthIn35mmFilm", dest: "FocalLengthIn35mmFilm"),
+ (ifd: "EXIF", source: "ColorSpace", dest: "ColorSpace"),
+];
+
+img.IfdValue? convertExifReaderValueToImageValue(exif.IfdTag? tag) {
+ final values = tag?.values;
+ if (tag == null || values == null || values is exif.IfdNone) {
+ return null;
+ }
+
+ List<int> ints() => values.toList().cast<int>();
+ List<double> doubles() =>
+ values.toList().map((value) => (value as num).toDouble()).toList();
+ List<img_util.Rational> ratios() {
+ return values
+ .toList()
+ .cast<exif.Ratio>()
+ .map((value) => img_util.Rational(value.numerator, value.denominator))
+ .toList();
+ }
+
+ return switch (tag.tagType) {
+ "Byte" => img.IfdByteValue.list(Uint8List.fromList(ints())),
+ "ASCII" => img.IfdValueAscii(
+ String.fromCharCodes(ints().takeWhile((byte) => byte != 0)),
+ ),
+ "Short" => img.IfdValueShort.list(ints()),
+ "Long" || "IFD" => img.IfdValueLong.list(ints()),
+ "Signed Byte" => img.IfdValueSByte.list(ints()),
+ "Undefined" => img.IfdValueUndefined.list(ints()),
+ "Signed Short" => img.IfdValueSShort.list(ints()),
+ "Signed Long" => img.IfdValueSLong.list(ints()),
+ "Ratio" => img.IfdValueRational.list(ratios()),
+ "Signed Ratio" => img.IfdValueSRational.list(ratios()),
+ "Single-Precision Floating Point (32-bit)" => img.IfdValueSingle.list(
+ doubles(),
+ ),
+ "Double-Precision Floating Point (64-bit)" => img.IfdValueDouble.list(
+ doubles(),
+ ),
+ _ => null,
+ };
+}
+
+Future<void> copyEXIF(EnteFile src, img.Image dest) async {
+ final srcExif = await getExif(src);
+ for (final field in _copiedExifFields) {
+ final destIfd = field.ifd == "Image"
+ ? dest.exif.imageIfd
+ : dest.exif.exifIfd;
+ final value = convertExifReaderValueToImageValue(
+ srcExif["${field.ifd} ${field.source}"],
+ );
+ if (value != null) {
+ destIfd[field.dest] = value;
+ }
+ }
+}