diff --git a/mobile/lib/features/agents/agent_avatar_picker.dart b/mobile/lib/features/agents/agent_avatar_picker.dart index 7ecbd29eb3..24d157a942 100644 --- a/mobile/lib/features/agents/agent_avatar_picker.dart +++ b/mobile/lib/features/agents/agent_avatar_picker.dart @@ -171,7 +171,7 @@ class AgentAvatarPicker extends HookConsumerWidget { } Future applyImage(Uint8List bytes) async { - // Crop exports JPEG; convert to PNG so all local drafts share one MIME. + // Crop exports JPEG or PNG; convert to PNG so all local drafts share one MIME. final codec = await ui.instantiateImageCodec(bytes); final frame = await codec.getNextFrame(); final data = await frame.image.toByteData(format: ui.ImageByteFormat.png); diff --git a/mobile/lib/features/profile/profile_avatar_draft.dart b/mobile/lib/features/profile/profile_avatar_draft.dart index 2b40b2cf6b..edf20b37de 100644 --- a/mobile/lib/features/profile/profile_avatar_draft.dart +++ b/mobile/lib/features/profile/profile_avatar_draft.dart @@ -31,10 +31,10 @@ final class ProfileUrlAvatarDraft extends ProfileAvatarDraft { /// A locally prepared still image awaiting upload on Save. final class ProfileImageAvatarDraft extends ProfileAvatarDraft { - /// Creates a still-image draft from JPEG [bytes]. + /// Creates a still-image draft from JPEG or PNG [bytes]. ProfileImageAvatarDraft(this.bytes); - /// The prepared JPEG payload. + /// The prepared JPEG or PNG payload. final Uint8List bytes; MediaUploadService? _uploadService; Future? _uploadedUrl; @@ -48,7 +48,7 @@ final class ProfileImageAvatarDraft extends ProfileAvatarDraft { final existing = _uploadedUrl; if (existing != null) return existing; final upload = service - .uploadBytes(bytes, mimeType: 'image/jpeg') + .uploadBytes(bytes, mimeType: detectImageMimeType(bytes)) .then((descriptor) => descriptor.url); _uploadedUrl = upload; try { diff --git a/mobile/lib/shared/relay/media_upload.dart b/mobile/lib/shared/relay/media_upload.dart index e183b8d3f5..806c73958f 100644 --- a/mobile/lib/shared/relay/media_upload.dart +++ b/mobile/lib/shared/relay/media_upload.dart @@ -713,7 +713,7 @@ class MediaUploadService { _PreparedUploadImage _buildPreparedUploadImage(Uint8List bytes) { return _PreparedUploadImage( bytes: bytes, - mimeType: _detectImageMimeType(bytes), + mimeType: detectImageMimeType(bytes), ); } @@ -804,13 +804,14 @@ String _sha256Hex(Uint8List bytes) => crypto.sha256.convert(bytes).toString(); String? _tryDetectImageMimeType(Uint8List bytes) { try { - return _detectImageMimeType(bytes); + return detectImageMimeType(bytes); } on Exception { return null; } } -String _detectImageMimeType(Uint8List bytes) { +/// Returns the image MIME type of [bytes] from their signature. +String detectImageMimeType(Uint8List bytes) { if (_startsWith(bytes, const [0xff, 0xd8, 0xff])) { return 'image/jpeg'; } diff --git a/mobile/lib/shared/widgets/profile_avatar_crop_page.dart b/mobile/lib/shared/widgets/profile_avatar_crop_page.dart index 73bc3a994e..bd699cf105 100644 --- a/mobile/lib/shared/widgets/profile_avatar_crop_page.dart +++ b/mobile/lib/shared/widgets/profile_avatar_crop_page.dart @@ -545,5 +545,8 @@ Uint8List _cropAvatar(_CropRequest request) { width: _cropOutputSize, height: _cropOutputSize, ); - return image.encodeJpg(resized, quality: 90); + return resized.hasAlpha && + resized.any((pixel) => pixel.a < pixel.maxChannelValue) + ? image.encodePng(resized, singleFrame: true) + : image.encodeJpg(resized, quality: 90); } diff --git a/mobile/test/features/profile/profile_edit_page_test.dart b/mobile/test/features/profile/profile_edit_page_test.dart index bf179f9775..02867efe70 100644 --- a/mobile/test/features/profile/profile_edit_page_test.dart +++ b/mobile/test/features/profile/profile_edit_page_test.dart @@ -647,6 +647,70 @@ void main() { expect(notifier.savedAvatarUrls, ['https://relay.example/profile.png']); expect(uploadService.uploadCount, 1); + expect(uploadService.uploadedMimeType, 'image/jpeg'); + }); + + Future<_FakeMediaUploadService> cropAndUpload( + WidgetTester tester, + image.Image photo, + ) async { + final notifier = _FakeProfileNotifier(); + final uploadService = _FakeMediaUploadService( + galleryBytes: image.encodePng(photo), + ); + addTearDown(uploadService.dispose); + await tester.pumpWidget( + WidgetHelpers.testable( + overrides: [ + profileProvider.overrideWith(() => notifier), + mediaUploadServiceProvider.overrideWithValue(uploadService), + ], + child: const ProfileEditPage(), + ), + ); + await tester.pumpAndSettle(); + await tester.tap(find.text('Edit Photo')); + await tester.pumpAndSettle(); + await tester.tap(find.text('Photo Library')); + await _waitForAvatarCropToLoad(tester); + await tester.tap(find.byKey(const ValueKey('avatar-crop-use-photo'))); + await _waitForAvatarCropToClose(tester); + await tester.tap(find.byKey(const ValueKey('avatar-save'))); + await tester.pumpAndSettle(); + return uploadService; + } + + testWidgets('uploads a transparent photo crop as PNG', (tester) async { + final uploadService = await cropAndUpload( + tester, + image.Image(width: 8, height: 8, numChannels: 4), + ); + + expect(uploadService.uploadedMimeType, 'image/png'); + final uploaded = image.decodePng(uploadService.uploadedBytes!)!; + expect(uploaded.getPixel(0, 0).a, 0); + }); + + testWidgets('uploads only the first frame of an animated transparent crop', ( + tester, + ) async { + final photo = image.Image(width: 8, height: 8, numChannels: 4) + ..addFrame(image.Image(width: 8, height: 8, numChannels: 4)); + final uploadService = await cropAndUpload(tester, photo); + + expect(uploadService.uploadedMimeType, 'image/png'); + final uploaded = image.decodePng(uploadService.uploadedBytes!)!; + expect(uploaded.numFrames, 1); + expect(uploaded.getPixel(0, 0).a, 0); + }); + + testWidgets('uploads an opaque 16-bit photo crop as JPEG', (tester) async { + final uploadService = await cropAndUpload( + tester, + image.Image(width: 8, height: 8, format: image.Format.uint16), + ); + + expect(uploadService.uploadedMimeType, 'image/jpeg'); }); testWidgets('keeps a failed avatar draft visible and retries publish', ( @@ -1233,6 +1297,7 @@ class _FakeMediaUploadService extends MediaUploadService { _FakeMediaUploadService({ this.delayGallery = false, this.failImagePreparation = false, + this.galleryBytes, }) : super( baseUrl: 'https://relay.example', nsec: null, @@ -1242,11 +1307,14 @@ class _FakeMediaUploadService extends MediaUploadService { final bool delayGallery; final bool failImagePreparation; + final Uint8List? galleryBytes; final _gallerySelection = Completer(); int uploadCount = 0; + Uint8List? uploadedBytes; + String? uploadedMimeType; XFile _image() => XFile.fromData( - image.encodePng(image.Image(width: 8, height: 8)), + galleryBytes ?? image.encodePng(image.Image(width: 8, height: 8)), mimeType: 'image/png', name: 'avatar.png', ); @@ -1273,6 +1341,8 @@ class _FakeMediaUploadService extends MediaUploadService { UploadCancellationToken? cancellationToken, }) async { uploadCount++; + uploadedBytes = bytes; + uploadedMimeType = mimeType; return BlobDescriptor( url: 'https://relay.example/profile.png', sha256: 'hash', diff --git a/mobile/test/features/profile/profile_edit_retry_test.dart b/mobile/test/features/profile/profile_edit_retry_test.dart index 0c157c6930..27003c2ff7 100644 --- a/mobile/test/features/profile/profile_edit_retry_test.dart +++ b/mobile/test/features/profile/profile_edit_retry_test.dart @@ -332,7 +332,7 @@ void main() { onPrepareChanged(() async { prepareCalls++; return ProfileImageAvatarDraft( - Uint8List.fromList([1, 2, 3]), + Uint8List.fromList([0xff, 0xd8, 0xff]), ); }); }); @@ -402,7 +402,7 @@ void main() { WidgetsBinding.instance.addPostFrameCallback((_) { onPrepareChanged( () async => ProfileImageAvatarDraft( - Uint8List.fromList([1, 2, 3]), + Uint8List.fromList([0xff, 0xd8, 0xff]), ), ); }); @@ -467,7 +467,7 @@ void main() { WidgetsBinding.instance.addPostFrameCallback((_) { onPrepareChanged( () async => ProfileImageAvatarDraft( - Uint8List.fromList([1, 2, 3]), + Uint8List.fromList([0xff, 0xd8, 0xff]), ), ); }); @@ -534,7 +534,7 @@ void main() { WidgetsBinding.instance.addPostFrameCallback((_) { onPrepareChanged( () async => ProfileImageAvatarDraft( - Uint8List.fromList([1, 2, 3]), + Uint8List.fromList([0xff, 0xd8, 0xff]), ), ); });