thumbnails: the Exif orientation of a JPEG is read along with its frame header, so photos also come out upright where the image loader does not report it (as GDI+ under Wine)

This commit is contained in:
Daemonratte 2026-10-17 13:36:11 +02:00
commit 7e44fbde30
No known key found for this signature in database

View file

@ -62,6 +62,7 @@ typedef struct
goffset size;
goffset max_remote_size; /* 0: none of network shares */
gint thumbnail_size; /* 128 or 256 */
gint orientation; /* of a JPEG, from its Exif data; 0: none */
GCancellable *cancellable;
GtkFileThumbnailDone done;
gpointer data;
@ -319,29 +320,95 @@ size_prepared (GdkPixbufLoader *loader,
}
}
/* The orientation (1-8) in the Exif data of an APP1 segment, 0 if none */
static gint
exif_orientation (const guchar *d,
gsize len)
{
gboolean le;
guint32 ifd;
guint n, i;
#define U16(p) (le ? (guint) ((p)[0] | (p)[1] << 8) : (guint) ((p)[0] << 8 | (p)[1]))
#define U32(p) (le ? ((guint32) (p)[0] | (guint32) (p)[1] << 8 | (guint32) (p)[2] << 16 | (guint32) (p)[3] << 24) \
: ((guint32) (p)[0] << 24 | (guint32) (p)[1] << 16 | (guint32) (p)[2] << 8 | (guint32) (p)[3]))
if (len < 6 + 8 || memcmp (d, "Exif\0\0", 6) != 0)
return 0;
d += 6;
len -= 6;
if (d[0] == 'I' && d[1] == 'I')
le = TRUE;
else if (d[0] == 'M' && d[1] == 'M')
le = FALSE;
else
return 0;
if (U16 (d + 2) != 42)
return 0;
ifd = U32 (d + 4);
if (ifd > len - 2)
return 0;
n = U16 (d + ifd);
for (i = 0; i < n && ifd + 2 + (i + 1) * 12 <= len; i++)
{
const guchar *e = d + ifd + 2 + i * 12;
/* Orientation, a SHORT */
if (U16 (e) == 0x0112 && U16 (e + 2) == 3)
{
guint o = U16 (e + 8);
return o >= 1 && o <= 8 ? (gint) o : 0;
}
}
#undef U16
#undef U32
return 0;
}
/* A progressive JPEG needs memory for the whole picture, however small
* it is decoded; its frame header (SOFn) tells */
* it is decoded; its frame header (SOFn) tells. On the way there, the
* Exif orientation is read: not every image loader reports it (the one
* of GDI+ under Wine, gdk-pixbuf before 2.12), and without it photos
* taken upright show lying on their side */
static gboolean
jpeg_is_progressive (const gchar *path)
jpeg_scan (const gchar *path,
gint *orientation)
{
FILE *f = g_fopen (path, "rb");
gboolean progressive = TRUE; /* not found: the careful answer */
guchar m[4];
gint i;
*orientation = 0;
if (f == NULL)
return TRUE;
if (fread (m, 1, 2, f) == 2 && m[0] == 0xff && m[1] == 0xd8)
for (i = 0; i < 64 && fread (m, 1, 4, f) == 4 && m[0] == 0xff; i++)
{
gsize len = (m[2] << 8) | m[3];
/* SOF0 to SOF15, but for DHT, JPG and DAC */
if (m[1] >= 0xc0 && m[1] <= 0xcf && m[1] != 0xc4 && m[1] != 0xc8 && m[1] != 0xcc)
{
progressive = m[1] == 0xc2 || m[1] == 0xc6 || m[1] == 0xca || m[1] == 0xce;
break;
}
if (fseek (f, ((m[2] << 8) | m[3]) - 2, SEEK_CUR) != 0)
if (len < 2)
break;
if (m[1] == 0xe1 && *orientation == 0)
{
guchar *app1 = g_malloc (len - 2);
gboolean read = fread (app1, 1, len - 2, f) == len - 2;
if (read)
*orientation = exif_orientation (app1, len - 2);
g_free (app1);
if (!read)
break;
}
else if (fseek (f, len - 2, SEEK_CUR) != 0)
break;
}
fclose (f);
@ -377,6 +444,13 @@ decode (Job *job)
if (gdk_pixbuf_loader_close (loader, NULL) && ok)
{
pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
if (pixbuf && job->orientation > 1 &&
gdk_pixbuf_get_option (pixbuf, "orientation") == NULL)
{
gchar o[2] = { '0' + job->orientation, 0 };
gdk_pixbuf_set_option (pixbuf, "orientation", o);
}
if (pixbuf)
pixbuf = gdk_pixbuf_apply_embedded_orientation (pixbuf);
}
@ -774,7 +848,7 @@ make_thumbnail (Job *job)
gboolean fits;
if (g_strcmp0 (name, "jpeg") == 0)
fits = pixels <= (jpeg_is_progressive (job->path) ? MAX_PIXELS : MAX_JPEG_PIXELS);
fits = pixels <= (jpeg_scan (job->path, &job->orientation) ? MAX_PIXELS : MAX_JPEG_PIXELS);
else if (g_strcmp0 (name, "gif") == 0)
fits = pixels <= MAX_PIXELS && job->size <= MAX_GIF_SIZE;
else