Index: src/rotation-utils.c
===================================================================
--- src/rotation-utils.c	(revision 1403)
+++ src/rotation-utils.c	(working copy)
@@ -54,7 +54,7 @@
 		return GTH_TRANSFORM_NONE;
 }
 
-
+/*
 void
 write_orientation_field (const char   *path,
 			 GthTransform  transform)
@@ -82,8 +82,196 @@
 	exif_data_unref (edata);
 	jpeg_data_unref (jdata);
 }
+*/
 
+#include <stdio.h>
+#include <stdlib.h>
 
+/* Error exit handler */
+#define ERREXIT(msg)  (exit(0))
+
+/* Read one byte, testing for EOF */
+static int
+read_1_byte (FILE* myfile)
+{
+  int c;
+
+  c = getc(myfile);
+  if (c == EOF)
+    ERREXIT("Premature EOF in JPEG file");
+  return c;
+}
+
+/* Read 2 bytes, convert to unsigned int */
+/* All 2-byte quantities in JPEG markers are MSB first */
+static unsigned int
+read_2_bytes (FILE* myfile)
+{
+  int c1, c2;
+
+  c1 = getc(myfile);
+  if (c1 == EOF)
+    ERREXIT("Premature EOF in JPEG file");
+  c2 = getc(myfile);
+  if (c2 == EOF)
+    ERREXIT("Premature EOF in JPEG file");
+  return (((unsigned int) c1) << 8) + ((unsigned int) c2);
+}
+
+int
+jpegexiforient (const char *path, int orientation)
+{
+	FILE* myfile;		/* My JPEG file */
+	unsigned char exif_data[65536L];
+	unsigned int length, i;
+	int is_motorola; /* Flag for byte order */
+	unsigned int offset, number_of_tags, tagnum;
+
+	if ((myfile = fopen(path, "rb+")) == NULL) {
+		fprintf(stderr, "jpegexiforient: can't open %s\n", path);
+		return 1;
+	}
+
+	/* Read File head, check for JPEG SOI + Exif APP1 */
+	for (i = 0; i < 4; i++)
+		exif_data[i] = (unsigned char) read_1_byte(myfile);
+	if (exif_data[0] != 0xFF ||
+		exif_data[1] != 0xD8 ||
+		exif_data[2] != 0xFF ||
+		exif_data[3] != 0xE1)
+		return 1;
+
+	/* Get the marker parameter length count */
+	length = read_2_bytes(myfile);
+	/* Length includes itself, so must be at least 2 */
+	/* Following Exif data length must be at least 6 */
+	if (length < 8)
+		return 1;
+	length -= 8;
+	/* Read Exif head, check for "Exif" */
+	for (i = 0; i < 6; i++)
+		exif_data[i] = (unsigned char) read_1_byte(myfile);
+	if (exif_data[0] != 0x45 ||
+		exif_data[1] != 0x78 ||
+		exif_data[2] != 0x69 ||
+		exif_data[3] != 0x66 ||
+		exif_data[4] != 0 ||
+		exif_data[5] != 0)
+		return 1;
+	/* Read Exif body */
+	for (i = 0; i < length; i++)
+		exif_data[i] = (unsigned char) read_1_byte(myfile);
+
+	if (length < 12) return 1; /* Length of an IFD entry */
+
+	/* Discover byte order */
+	if (exif_data[0] == 0x49 && exif_data[1] == 0x49)
+		is_motorola = 0;
+	else if (exif_data[0] == 0x4D && exif_data[1] == 0x4D)
+		is_motorola = 1;
+	else
+		return 1;
+
+	/* Check Tag Mark */
+	if (is_motorola) {
+		if (exif_data[2] != 0) return 1;
+		if (exif_data[3] != 0x2A) return 1;
+	} else {
+		if (exif_data[3] != 0) return 1;
+		if (exif_data[2] != 0x2A) return 1;
+	}
+
+	/* Get first IFD offset (offset to IFD0) */
+	if (is_motorola) {
+		if (exif_data[4] != 0) return 1;
+		if (exif_data[5] != 0) return 1;
+		offset = exif_data[6];
+		offset <<= 8;
+		offset += exif_data[7];
+	} else {
+		if (exif_data[7] != 0) return 1;
+		if (exif_data[6] != 0) return 1;
+		offset = exif_data[5];
+		offset <<= 8;
+		offset += exif_data[4];
+	}
+	if (offset > length - 2) return 1; /* check end of data segment */
+
+	/* Get the number of directory entries contained in this IFD */
+	if (is_motorola) {
+		number_of_tags = exif_data[offset];
+		number_of_tags <<= 8;
+		number_of_tags += exif_data[offset+1];
+	} else {
+		number_of_tags = exif_data[offset+1];
+		number_of_tags <<= 8;
+		number_of_tags += exif_data[offset];
+	}
+	if (number_of_tags == 0) return 1;
+	offset += 2;
+
+	/* Search for Orientation Tag in IFD0 */
+	for (;;) {
+		if (offset > length - 12) return 1; /* check end of data segment */
+		/* Get Tag number */
+		if (is_motorola) {
+			tagnum = exif_data[offset];
+			tagnum <<= 8;
+			tagnum += exif_data[offset+1];
+		} else {
+			tagnum = exif_data[offset+1];
+			tagnum <<= 8;
+			tagnum += exif_data[offset];
+		}
+		if (tagnum == 0x0112) break; /* found Orientation Tag */
+		if (--number_of_tags == 0) return 1;
+		offset += 12;
+	}
+
+	/* Set the Orientation value */
+	if (is_motorola) {
+		exif_data[offset+2] = 0; /* Format = unsigned short (2 octets) */
+		exif_data[offset+3] = 3;
+		exif_data[offset+4] = 0; /* Number Of Components = 1 */
+		exif_data[offset+5] = 0;
+		exif_data[offset+6] = 0;
+		exif_data[offset+7] = 1;
+		exif_data[offset+8] = 0;
+		exif_data[offset+9] = (unsigned char)orientation;
+		exif_data[offset+10] = 0;
+		exif_data[offset+11] = 0;
+	} else {
+		exif_data[offset+2] = 3; /* Format = unsigned short (2 octets) */
+		exif_data[offset+3] = 0;
+		exif_data[offset+4] = 1; /* Number Of Components = 1 */
+		exif_data[offset+5] = 0;
+		exif_data[offset+6] = 0;
+		exif_data[offset+7] = 0;
+		exif_data[offset+8] = (unsigned char)orientation;
+		exif_data[offset+9] = 0;
+		exif_data[offset+10] = 0;
+		exif_data[offset+11] = 0;
+	}
+	fseek(myfile, (4 + 2 + 6 + 2) + offset, SEEK_SET);
+	fwrite(exif_data + 2 + offset, 1, 10, myfile);
+
+	/* All done. */
+	return 0;
+}
+
+void
+write_orientation_field (const char   *path,
+			 GthTransform  transform)
+{
+	path = get_file_path_from_uri (path);
+	if (path == NULL)
+		return;
+
+	if (jpegexiforient(path, transform) != 0)
+		printf("ERROR: jpegexiforient failed\n");
+}
+
+
 static boolean
 jpeg_mcu_dialog (const char  *path,
 		 JXFORM_CODE *transform,
