SDL_image: jpg: Fix channel order of saved JPEGs on big-endian platforms

From 8f1d23ca9c6a77bc6496315f8fa9a78529da0d7b Mon Sep 17 00:00:00 2001
From: Simon McVittie <[EMAIL REDACTED]>
Date: Wed, 25 May 2022 13:33:10 +0100
Subject: [PATCH] jpg: Fix channel order of saved JPEGs on big-endian platforms

Both of the JPEG libraries we use (jpeglib or tinyjpeg) expect to see
the red channel in the first byte of the buffer, regardless of whether
the first byte is considered to be the most-significant or
least-significant in native endianness.

Signed-off-by: Simon McVittie <smcv@collabora.com>
---
 IMG_jpg.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/IMG_jpg.c b/IMG_jpg.c
index 46d9c59d..341b85da 100644
--- a/IMG_jpg.c
+++ b/IMG_jpg.c
@@ -492,11 +492,9 @@ static void jpeg_SDL_RW_dest(j_compress_ptr cinfo, SDL_RWops *ctx)
 
 static int IMG_SaveJPG_RW_jpeglib(SDL_Surface *surface, SDL_RWops *dst, int freedst, int quality)
 {
-#if SDL_BYTEORDER == SDL_LIL_ENDIAN
+    /* The JPEG library reads bytes in R,G,B order, so this is the right
+     * encoding for either endianness */
     static const Uint32 jpg_format = SDL_PIXELFORMAT_RGB24;
-#else
-    static const Uint32 jpg_format = SDL_PIXELFORMAT_BGR24;
-#endif
     struct jpeg_compress_struct cinfo;
     struct my_error_mgr jerr;
     JSAMPROW row_pointer[1];
@@ -703,11 +701,9 @@ static void IMG_SaveJPG_RW_tinyjpeg_callback(void* context, void* data, int size
 
 static int IMG_SaveJPG_RW_tinyjpeg(SDL_Surface *surface, SDL_RWops *dst, int freedst, int quality)
 {
-#if SDL_BYTEORDER == SDL_LIL_ENDIAN
+    /* The JPEG library reads bytes in R,G,B order, so this is the right
+     * encoding for either endianness */
     static const Uint32 jpg_format = SDL_PIXELFORMAT_RGB24;
-#else
-    static const Uint32 jpg_format = SDL_PIXELFORMAT_BGR24;
-#endif
     SDL_Surface* jpeg_surface = surface;
     int result = -1;