Skip to content

Fix #5711: Incorrect rotation logic and re-edit bug#6642

Merged
nicolas-raoul merged 32 commits intocommons-app:mainfrom
Kota-Jagadeesh:fix/5711-incorrect-rotation-edit-image
Mar 1, 2026
Merged

Fix #5711: Incorrect rotation logic and re-edit bug#6642
nicolas-raoul merged 32 commits intocommons-app:mainfrom
Kota-Jagadeesh:fix/5711-incorrect-rotation-edit-image

Conversation

@Kota-Jagadeesh
Copy link
Collaborator

Description (required)

Fixes #5711

This PR addresses the "Incorrect rotation" where the images were being rotated 90 degrees upon opening the "Edit Image" screen or saving without edits.

Changes made:

  1. fixed "save without edit" bug: Updated TransformImageImpl.kt to stop defaulting to a 90-degree rotation when the requested rotation is 0. It now correctly applies no rotation (OPT_DEFAULTS).

  2. fixed the re-edit workflow: Updated UploadMediaDetailFragment.kt to check if an edited file path exists (mediaUri) before launching the editor. This ensures that clicking "Edit" a second time loads the current state of the image instead of reverting to the original file.

Tests performed (required)

Tested ProdDebug on Redmi Note 13 Pro with API level 35 (Android 15).

Screenshots (for UI changes only)

COMMONS.REC1.mp4

@Kota-Jagadeesh Kota-Jagadeesh changed the title Fix/5711 incorrect rotation edit image Fix #5711: Incorrect rotation logic and re-edit bug Feb 3, 2026
@Kota-Jagadeesh Kota-Jagadeesh self-assigned this Feb 3, 2026
@mnalis
Copy link
Contributor

mnalis commented Feb 4, 2026

I've tried that beta 6.2.1-debug-HEAD~bfd341e that bot autobuilt here, but it still behaves strange -- image orientation that originally opens in Commons app is different then the one shown immediately when Edit Image is pressed, and it should be the same.

see video:

small_Screen_Recording_20260204_014959_Commons.mp4

can you reproduce it @Kota-Jagadeesh ?

(also if I rotate and save, it also displays differently in Edit Image window then main Commons app window when I return to it). The example image is included in .zip file in that #5711 (comment) so you can try yourself)

@mnalis
Copy link
Contributor

mnalis commented Feb 4, 2026

Additionally, I noticed it seems that on every rotate/save the app saves a new image in my Internal Storage/Download folder, which is not desirable. If files really must be saved to disk instead of just kept as memory objects (which would be preferable IMHO) for some technical reason, then a care should be taken that:

  • before saving new version, older versions of the rotation of that specific picture1 be deleted from the disk, lest the junk build up
  • on finished upload, even that last temporary version of the file should be deleted from the disk as it is not needed anymore
  • such temporary files should be kept in internal app cache dir, not in public & permanent Download folder
    small_Screenshot_20260204_021031_My Files

Footnotes

  1. but not rotated versions of other pictures; which might exist e.g. if there are multiple concurrent processes running - and that can happen, e.g. if I share multiple times to the Commons App without finishing with previous upload

@nicolas-raoul
Copy link
Member

Sorry for extending scope, but would you mind creating unit tests for various rotations with all JPGs at https://github.com/recurser/exif-orientation-examples ?

EXIF rotation is a tricky topic, so getting a clear+testable definition of how it is supposed to work would help a lot now and in the future.
Thanks a lot for your consideration! 🙂

@Kota-Jagadeesh
Copy link
Collaborator Author

Screenrecorder-2026-02-04-21-01-35-136.mp4

@mnalis I tested with this branch from android studio, but couldn't reproduce the issue. It works fine for me

and i also dowwnloaded the ProDebugApk from #6642 (comment) and couldn't reproduce the issue even in this.

Could you please some more information regarding hwo you reproduced the issue ?

@Kota-Jagadeesh
Copy link
Collaborator Author

Kota-Jagadeesh commented Feb 4, 2026

Additionally, I noticed it seems that on every rotate/save the app saves a new image in my Internal Storage/Download

even i thought of this issue& even though the current main branch behaves this way, we definitely shouldnn't be polluting the user's downloads folder like this wwith the temporary edits. i will update the pr to save these intermediate files to the app's internal cacheDir instead.

@Kota-Jagadeesh
Copy link
Collaborator Author

Sorry for extending scope, but would you mind creating unit tests for various rotations with all JPGs at https://github.com/recurser/exif-orientation-examples ?

@nicolas-raoul yeah sure, i'll add the unit testt class that iterates through the standard exif orientation examples to verify that the rotation logic holds up against all of the edge cases.

@Kota-Jagadeesh
Copy link
Collaborator Author

Kota-Jagadeesh commented Feb 4, 2026

@mnalis @nicolas-raoul I have updated the code with the fix for the images to store in the cache rather than the downloads. I tested the changes and everyhtinng worked fine for me and here is the video clip of that :

Screenrecorder-2026-02-04-23-33-55-689.mp4

Here, i uploaded a picture #5711 (comment) and saved with rotations and without rotations, before this fix: all the pics are been saved in the downloads dir, after the fix: now the images are not being saved in the downloads anymore :)

@nicolas-raoul
Copy link
Member

Sorry for extending scope, but would you mind creating unit tests for various rotations with all JPGs at https://github.com/recurser/exif-orientation-examples ?

@nicolas-raoul yeah sure, i'll add the unit testt class that iterates through the standard exif orientation examples to verify that the rotation logic holds up against all of the edge cases.

Looking forward to it! Would you mind doing it in this PR? Thanks! 🙂

@Kota-Jagadeesh
Copy link
Collaborator Author

@nicolas-raoul Updated the PR with all the changes, PLease review it :)

@mnalis
Copy link
Contributor

mnalis commented Feb 9, 2026

@mnalis I tested with this branch from android studio, but couldn't reproduce the issue. It works fine for me
Could you please some more information regarding hwo you reproduced the issue ?

It is still incorrect for me, even with latest changes. Here is a detailed procedure for reproducing the issue:

  1. download demo_IMG_20250208_130121.zip file from [Bug]: Incorrect rotation shown in "Edit Image" #5711 (comment) to my phone
  2. uninstall any old beta Commons app you might have, and delete all existing copies of that image
  3. install new beta Commons app from latest bot build in this PR (i.e. Fix #5711: Incorrect rotation logic and re-edit bug #6642 (comment)) - it identifies as 6.3.0-debug-HEAD~5ca7de9 in About for me.
  4. unpack IMG_20250208_130121.jpg that .zip file from step (1) to your phone in Downloads or other accessible folder
  5. open that IMG_20250208_130121.jpg image in your phone gallery app (I use Fossify Gallery)
  6. notice that the building roof points to the right (which is OK)
  7. share it to beta Commons app
  8. notice that the building roof still points to the right (which is OK)
  9. click Edit image
  10. notice that the building roof now immediately points up, even if user haven't yet done any rotating yet (which is a BUG -- at start, it should look exactly like in steps (6) and (8), i.e. it should be pointing to the right )

Here is the video how it looks for me:

small_SVID_20260209_030103_1.mp4

Can you reproduce the issue there @Kota-Jagadeesh? Or does the step (10) differ for you, if you use exactly that image I provided ? Perhaps you had an old copy of the image which was saved so it was modified and behaves differently? And in which environment (phone/android version) that happens for you?

Because I've tried on 2 quite different devices (Samsung Galaxy S23+ on Android 15, and Huawei P30Pro on Android 10) and the bug is exactly the same on both with that specific picture freshly extracted from .zip file.

@mnalis
Copy link
Contributor

mnalis commented Feb 9, 2026

(BTW here is a copy of that zip with image file, if GitHub is acting up with following links):
demo_IMG_20250208_130121.zip

before this fix: all the pics are been saved in the downloads dir, after the fix: now the images are not being saved in the downloads anymore

Thanks, I can confirm that multiple junk copies in Download folder seems to be fixed!

@Kota-Jagadeesh
Copy link
Collaborator Author

Can you reproduce the issue there @Kota-Jagadeesh?

Sorry for my oversight. I was able to reproduce the issue. I thought I was using the same image you provided, but I ended up using a duplicate image which was saved while editing the image, and that made the bug seem to be solved. I understand where the problem is and will fix it and update the PR shortly. @mnalis, thank you for testing! 🙂

@github-actions
Copy link

✅ Generated APK variants!

@Kota-Jagadeesh
Copy link
Collaborator Author

I think the description in the other PR said "Fixes ..."

Yeah, that PR fixes one of the issue mentioned in the comments of this PR, so I had to use the fixes keyword. Sorry, for that :)

@github-actions
Copy link

✅ Generated APK variants!

@Kota-Jagadeesh
Copy link
Collaborator Author

Kota-Jagadeesh commented Feb 24, 2026

@nicolas-raoul Should i proceed with this ? 🙂

@nicolas-raoul
Copy link
Member

Can you leave the unit test in place and just disable it? And mention it in that unit test in the new issue.

Thanks!

@github-actions
Copy link

✅ Generated APK variants!

@Kota-Jagadeesh
Copy link
Collaborator Author

just disable it?

@nicolas-raoul i've added the @Ignore("... see issue #6659") annotation to the test so that it won't block the build. let me know if everything looks good to merge! 🙂

@github-actions
Copy link

✅ Generated APK variants!

Copy link
Member

@nicolas-raoul nicolas-raoul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then I think it will be good to merge. Thanks for your patience! :-)

val gDiff = abs(gExp - gNorm)
val bDiff = abs(bExp - bNorm)

if (rDiff > tolerance || gDiff > tolerance || bDiff > tolerance) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please remove the concept of tolerance from this pull request?
It complexifies the code and is not necessary since the editor must be lossless.
Thanks!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolas-raoul Made the required changes, once you are free, can you please look into it and let me me know if there are any changes needed 🙂

@github-actions
Copy link

✅ Generated APK variants!

@github-actions
Copy link

✅ Generated APK variants!

@Kota-Jagadeesh
Copy link
Collaborator Author

@nicolas-raoul Any update on this PR? 🙂

@github-actions
Copy link

github-actions bot commented Mar 1, 2026

✅ Generated APK variants!

Copy link
Member

@nicolas-raoul nicolas-raoul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rotation seems to work well in my tests, including one test where I actually uploaded the picture:
https://commons.wikimedia.org/wiki/File:Orix_Nagoya_Nishiki_Building.jpg

As mentioned the next step will be #6659 but the code here is using jpegtran so I am optimistic.

Thanks for the great work!

@nicolas-raoul nicolas-raoul merged commit 05e5944 into commons-app:main Mar 1, 2026
2 checks passed
@Kota-Jagadeesh
Copy link
Collaborator Author

Thanks for the great work!

@nicolas-raoul @mnalis Thank you for the in-depth review and for correcting me whenever I’m wrong ❤️

@nicolas-raoul
Copy link
Member

Thank you for your patience!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Incorrect rotation shown in "Edit Image"

4 participants