How are the gallery application edits applied to images? I noticed today that if you apply filters or modify an image using the gallery application, it applies the changes to the image, rather than creating a new image with the changes. My first thought was that it was not a good idea. Sometimes you want to keep the original, untouched image, as well as the modified image, maybe for comparison purposes, or to try different filters or settings. In my case I was playing with the curves, and then I saved the image. I wanted to compare the new image with the original, but the original was gone. I decided to go back to the curves settings, and saw that the curves were still as I had set them, which means I can just restore the curve for each channel and that will give me back the original image.

I decided to do a test with another image. I opened a new image, applied a filter from the preset filters, and saved the image. Then I went back to the image, and edited it again, but this time I removed the filter, and the image was back to normal.

My first thought was that the gallery image applied non-destructive edits. Something similar to what some editing software does. The one that comes to mind is Shotwell, although this is more of a viewer than and editor, but it has some limited functionality to play around with the saturation, levels, and other settings. It didn't seem likely that the gallery application in adroid was doing that because other image viewing applications showed the image with the filter applied to it.

I decided to connect the device to the computer using a USB cable. I navigated to the camera folder, and opened the image in the computer, and it showed the modified image. This means that the image is being modified. While the devices was still connected to the computer, I decided to remove the filter, and open the image again in the computer. The image was back to original.

How is android able to modify the image file directly, and then restore it to original. Is it saving the original file somewhere else?


  How to reproduce:
  
  
  Open the Gallery application (the default one)
  Chose an image to edit, and touch the little pencil on the lower
  left corner of the screen.
  Edit your image, touch the save button, or just press the back
  button. It will ask if you want to save the image. Save it.
  Touch the pencil again, remove the filter.
  Your image is back to original.
  


Device: Nexus 7

OS: Android 4.4.2
You can find the source for this app in the Gallery2 package in AOSP. All the code for the photo editor is in src/com/android/gallery3d/filtershow. The saving is handled by tools/ImageSave.java, which has this comment explaining the behaviour (which I've formatted):-


  In order to support the new edit-save behavior such that user won't see the edited image together with the original image, we are adding a new auxiliary directory for the edited image. Basically, the original image will be hidden in that directory after edit and user will see the edited image only.
  
  Note that deletion on the edited image will also cause the deletion of the original image under auxiliary directory.
  
  There are several situations we need to consider:
  
  
  User edit local image local01.jpg. A local02.jpg will be created in the same directory, and original image will be moved to auxiliary directory as ./.aux/local02.jpg. 
  If user edit the local02.jpg, local03.jpg will be created in the local directory and ./.aux/local02.jpg will be renamed to ./.aux/local03.jpg
  User edit remote image remote01.jpg from picassa or other server. remoteSavedLocal01.jpg will be saved under proper local directory. In remoteSavedLocal01.jpg, there will be a reference pointing to the remote01.jpg. There will be no local copy of remote01.jpg. If user edit remoteSavedLocal01.jpg, then a new remoteSavedLocal02.jpg will be generated and still pointing to the remote01.jpg
  User delete any local image local.jpg. Since the filenames are kept consistent in auxiliary directory, every time a local.jpg get deleted, the files in auxiliary directory whose names starting with "local." will be deleted. This pattern will facilitate the multiple images deletion in the auxiliary directory.
  


In short: yes, it saves edited images in a .aux directory, inside the directory the image came from.

It also stores the filter chain you used to create the image in XMP metadata inside the new image (the filtered one), in the namespace http://ns.google.com/photos/1.0/filter/. This information consists of:


SourceFileUri: the URI where it has saved the unmodified file
filterstack: a serialized representation of the stack of filters it used (in the same format it saves presets to its private database)