Index: src/dlg-jpegtran.c
===================================================================
RCS file: /cvs/gnome/gthumb/src/dlg-jpegtran.c,v
retrieving revision 1.39
diff -p -u -r1.39 dlg-jpegtran.c
--- src/dlg-jpegtran.c	12 Oct 2006 19:15:12 -0000	1.39
+++ src/dlg-jpegtran.c	13 Oct 2006 13:02:06 -0000
@@ -239,6 +239,7 @@ apply_transformation (DialogData *data,
 	GtkWindow  	 *window = GTK_WINDOW (data->dialog);
 	gint		  width = 0;
 	gint		  height = 0;
+	ExifShort         orientation = 0;
 	
 	/* Check directory permissions. */
 
@@ -258,36 +259,38 @@ apply_transformation (DialogData *data,
 
         if (image_is_jpeg (path)) {
 
-                if (!get_exif_tag_short(path,EXIF_TAG_ORIENTATION)) {
-                        /* If no exif orientation tag is present, simply
-                           transform the data using the "lossless" jpeg
-                           mathematical transform (only really lossless if
-                           dimensions are multiples of 8 */
-                        apply_transformation_jpeg (window, path, data->transform);
-                }
-                else {
-                        /* Calculate a new orientation tag, based on the
-                           input tag and the requested rotation. This saves
-			   the jpeg, so no further action is required if
-			   "adjust photo orientation" is not selected. */
-                        update_orientation_field (path, data->transform);
-
-                        /* if "adjust photo orientation" is selected mathematically transform
-			   the data so that the viewed image remains the same, but the 
-			   orientation tag can be set to "top left" */
-
-                        if (eel_gconf_get_boolean (PREF_ROTATE_RESET_EXIF_ORIENTATION, TRUE)) {
-
-				gdk_pixbuf_get_file_info(get_file_path_from_uri (path),&width,&height);
-				
-				if (!(height%8) && !(width%8)) {
-					/* Don't do this if the dimensions are not multiples
-					   of the jpeg minimum coding unit (iMCU = 8) */
-	             			data->transform = read_orientation_field (path);
-					apply_transformation_jpeg (window, path, data->transform);
+		/* read the existing exif orientation tag, if present */
+		orientation = get_exif_tag_short(path,EXIF_TAG_ORIENTATION);
+
+                if (orientation) {
+
+			/* determine image dimensions from file header - do not trust the
+			   exif dimension data, which although mandatory may be untrue */
+			gdk_pixbuf_get_file_info(get_file_path_from_uri (path),&width,&height);
+
+			/* do a physical transform if requested and if the dimensions are
+			   multiples of the jpeg mcu (8) */
+                        if (eel_gconf_get_boolean (PREF_ROTATE_RESET_EXIF_ORIENTATION, TRUE)
+			    && !(height%8) && !(width%8)) {
+				/* calculate the actual transform required, taking into account
+				   the starting exif orientation tag */
+				data->transform = get_next_transformation(orientation, data->transform);
+				apply_transformation_jpeg (window, path, data->transform);
 				}
-                        }
+                        
+			else {
+				/* Calculate a new orientation tag, based on the
+				input tag and the requested rotation. This saves
+                        	the jpeg, so no further action is required if
+                           	"adjust photo orientation" is not selected. */
+	                        update_orientation_field (path, data->transform);
+			}
+
                 }
+		else {
+			/* if no exif orientation tag is present, do a physical transform */
+			apply_transformation_jpeg (window, path, data->transform);
+		}
 
         }
 	else 
