changeset 1567:c776b1310ca6

added an option to write image orientation to the metadata
author nadvornik
date Sun, 26 Apr 2009 09:01:35 +0000
parents 3d9f5c078521
children 678009e94efe
files src/bar.c src/exif.c src/image.c src/metadata.c src/metadata.h src/options.c src/options.h src/preferences.c src/rcfile.c src/thumb.c src/thumb_standard.c
diffstat 11 files changed, 66 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/bar.c	Sat Apr 25 22:08:09 2009 +0000
+++ b/src/bar.c	Sun Apr 26 09:01:35 2009 +0000
@@ -95,7 +95,7 @@
 "                <entry key = 'formatted.ColorProfile' if_set = 'true' editable = 'false' />"
 "                <entry key = 'formatted.SubjectDistance' if_set = 'true' editable = 'false' />"
 "                <entry key = 'formatted.Resolution' if_set = 'true' editable = 'false' />"
-"                <entry key = 'Exif.Image.Orientation' if_set = 'true' editable = 'false' />"
+"                <entry key = '" ORIENTATION_KEY "' if_set = 'true' editable = 'false' />"
 "            </pane_exif>"
 "        </bar>"
 "    </layout>"
--- a/src/exif.c	Sat Apr 25 22:08:09 2009 +0000
+++ b/src/exif.c	Sun Apr 26 09:01:35 2009 +0000
@@ -1605,6 +1605,9 @@
 	
 	if (!key) return NULL;
 	
+	/* convert xmp key to exif key */
+	if (strcmp(key, ORIENTATION_KEY) == 0) key = "Exif.Image.Orientation";
+	
 	if (format == METADATA_FORMATTED)
 		{
 		gchar *text;
--- a/src/image.c	Sat Apr 25 22:08:09 2009 +0000
+++ b/src/image.c	Sun Apr 26 09:01:35 2009 +0000
@@ -401,10 +401,18 @@
 			break;
 		}
 
-	if (type != ALTER_NONE)
+	if (imd->orientation != imd->image_fd->exif_orientation ? imd->image_fd->exif_orientation : 1)
 		{
-		if (imd->image_fd->user_orientation == 0) file_data_ref(imd->image_fd);
-		imd->image_fd->user_orientation = imd->orientation;
+		if (!options->metadata.write_orientation)
+			{
+			/* user_orientation does not work together with options->metadata.write_orientation,
+			   use either one or the other.
+			   we must however handle switching metadata.write_orientation on and off, therefore
+			   we just disable referencing new fd's, not unreferencing the old ones
+			*/
+			if (imd->image_fd->user_orientation == 0) file_data_ref(imd->image_fd);
+			imd->image_fd->user_orientation = imd->orientation;
+			}
 		}
 	else
 		{
@@ -412,6 +420,18 @@
 		imd->image_fd->user_orientation = 0;
 		}
 
+	if (options->metadata.write_orientation)
+		{
+		if (type == ALTER_NONE)
+			{
+			metadata_write_revert(imd->image_fd, ORIENTATION_KEY);
+			}
+		else
+			{
+			metadata_write_int(imd->image_fd, ORIENTATION_KEY, imd->orientation);
+			}
+		}
+
 	pixbuf_renderer_set_orientation((PixbufRenderer *)imd->pr, imd->orientation);
 }
 
@@ -1079,7 +1099,7 @@
 			}
 		else if (options->image.exif_rotate_enable)
 			{
-			imd->orientation = metadata_read_int(imd->image_fd, "Exif.Image.Orientation", EXIF_ORIENTATION_TOP_LEFT);
+			imd->orientation = metadata_read_int(imd->image_fd, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
 			imd->image_fd->exif_orientation = imd->orientation;
 			}
 		}
--- a/src/metadata.c	Sat Apr 25 22:08:09 2009 +0000
+++ b/src/metadata.c	Sun Apr 26 09:01:35 2009 +0000
@@ -189,6 +189,24 @@
 	return FALSE;
 }
 
+gboolean metadata_write_revert(FileData *fd, const gchar *key)
+{
+	if (!fd->modified_xmp) return FALSE;
+	
+	g_hash_table_remove(fd->modified_xmp, key);
+	
+	if (g_hash_table_size(fd->modified_xmp) == 0)
+		{
+		metadata_write_queue_remove(fd);
+		}
+	else
+		{
+		/* reread the metadata to restore the original value */
+		file_data_increment_version(fd);
+		file_data_send_notification(fd, NOTIFY_REREAD);
+		}
+}
+
 gboolean metadata_write_list(FileData *fd, const gchar *key, const GList *values)
 {
 	if (!fd->modified_xmp)
@@ -231,6 +249,13 @@
 	return ret;
 }
 
+gboolean metadata_write_int(FileData *fd, const gchar *key, guint64 value)
+{
+	gchar string[50];
+	
+	g_snprintf(string, sizeof(string), "%ld", value);
+	return metadata_write_string(fd, key, string);
+}
 
 /*
  *-------------------------------------------------------------------
--- a/src/metadata.h	Sat Apr 25 22:08:09 2009 +0000
+++ b/src/metadata.h	Sun Apr 26 09:01:35 2009 +0000
@@ -16,6 +16,7 @@
 
 #define COMMENT_KEY "Xmp.dc.description"
 #define KEYWORD_KEY "Xmp.dc.subject"
+#define ORIENTATION_KEY "Xmp.tiff.Orientation"
 
 gboolean metadata_write_queue_remove(FileData *fd);
 gboolean metadata_write_queue_remove_list(GList *list);
@@ -24,8 +25,10 @@
 
 gint metadata_queue_length(void);
 
+gboolean metadata_write_revert(FileData *fd, const gchar *key);
 gboolean metadata_write_list(FileData *fd, const gchar *key, const GList *values);
 gboolean metadata_write_string(FileData *fd, const gchar *key, const char *value);
+gboolean metadata_write_int(FileData *fd, const gchar *key, guint64 value);
 
 GList *metadata_read_list(FileData *fd, const gchar *key, MetadataFormat format);
 gchar *metadata_read_string(FileData *fd, const gchar *key, MetadataFormat format);
--- a/src/options.c	Sat Apr 25 22:08:09 2009 +0000
+++ b/src/options.c	Sun Apr 26 09:01:35 2009 +0000
@@ -104,6 +104,7 @@
 	options->metadata.confirm_on_image_change = FALSE;
 	options->metadata.confirm_on_dir_change = TRUE;
 	options->metadata.tags_case_sensitive = FALSE;
+	options->metadata.write_orientation = TRUE;
 	
 	options->show_icon_names = TRUE;
 
--- a/src/options.h	Sat Apr 25 22:08:09 2009 +0000
+++ b/src/options.h	Sun Apr 26 09:01:35 2009 +0000
@@ -178,6 +178,7 @@
 		gboolean confirm_on_image_change;
 		gboolean confirm_on_dir_change;
 		gboolean tags_case_sensitive;
+		gboolean write_orientation;
 	} metadata;
 
 };
--- a/src/preferences.c	Sat Apr 25 22:08:09 2009 +0000
+++ b/src/preferences.c	Sun Apr 26 09:01:35 2009 +0000
@@ -325,6 +325,7 @@
 	options->metadata.confirm_on_image_change = c_options->metadata.confirm_on_image_change;
 	options->metadata.confirm_on_dir_change = c_options->metadata.confirm_on_dir_change;
 	options->metadata.tags_case_sensitive = c_options->metadata.tags_case_sensitive;
+	options->metadata.write_orientation = c_options->metadata.write_orientation;
 
 #ifdef DEBUG
 	set_debug_level(debug_c);
@@ -1369,6 +1370,9 @@
 
 	pref_checkbox_new_int(group, _("Allow keywords to differ only in case"),
 			      options->metadata.tags_case_sensitive, &c_options->metadata.tags_case_sensitive);
+
+	pref_checkbox_new_int(group, _("Write altered image orientation to the metadata"),
+			      options->metadata.write_orientation, &c_options->metadata.write_orientation);
 }
 
 /* metadata tab */
--- a/src/rcfile.c	Sat Apr 25 22:08:09 2009 +0000
+++ b/src/rcfile.c	Sun Apr 26 09:01:35 2009 +0000
@@ -438,6 +438,7 @@
 	WRITE_NL(); WRITE_BOOL(*options, metadata.confirm_on_image_change);
 	WRITE_NL(); WRITE_BOOL(*options, metadata.confirm_on_dir_change);
 	WRITE_NL(); WRITE_BOOL(*options, metadata.tags_case_sensitive);
+	WRITE_NL(); WRITE_BOOL(*options, metadata.write_orientation);
 
 }
 
@@ -697,6 +698,7 @@
 		if (READ_BOOL(*options, metadata.confirm_on_image_change)) continue;
 		if (READ_BOOL(*options, metadata.confirm_on_dir_change)) continue;
 		if (READ_BOOL(*options, metadata.tags_case_sensitive)) continue;
+		if (READ_BOOL(*options, metadata.write_orientation)) continue;
 
 		log_printf("unknown attribute %s = %s\n", option, value);
 		}
--- a/src/thumb.c	Sat Apr 25 22:08:09 2009 +0000
+++ b/src/thumb.c	Sun Apr 26 09:01:35 2009 +0000
@@ -142,7 +142,7 @@
 		{
 		if (!tl->fd->exif_orientation)
 			{
-			tl->fd->exif_orientation = metadata_read_int(tl->fd, "Exif.Image.Orientation", EXIF_ORIENTATION_TOP_LEFT);
+			tl->fd->exif_orientation = metadata_read_int(tl->fd, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
 			}
 		
 		if (tl->fd->exif_orientation != EXIF_ORIENTATION_TOP_LEFT)
--- a/src/thumb_standard.c	Sat Apr 25 22:08:09 2009 +0000
+++ b/src/thumb_standard.c	Sun Apr 26 09:01:35 2009 +0000
@@ -388,7 +388,7 @@
 		{
 		if (!tl->fd->exif_orientation)
 			{
-			tl->fd->exif_orientation = metadata_read_int(tl->fd, "Exif.Image.Orientation", EXIF_ORIENTATION_TOP_LEFT);
+			tl->fd->exif_orientation = metadata_read_int(tl->fd, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
 			}
 		
 		if (tl->fd->exif_orientation != EXIF_ORIENTATION_TOP_LEFT)