SDL_image: Don't link tinyjpeg if we have a hard dependency on libjpeg

From d218f7fecbd9414c1f32596a9709cad5b78d4f3f Mon Sep 17 00:00:00 2001
From: Simon McVittie <[EMAIL REDACTED]>
Date: Thu, 12 May 2022 14:46:10 +0100
Subject: [PATCH] Don't link tinyjpeg if we have a hard dependency on libjpeg

Linux distributions like to control the number of image loading/saving
implementations they are compiling, because each one comes with a risk
of security vulnerabilities.

If we've built with --disable-stb-image and --disable-jpg-shared
(ordinary linking with -ljpeg, as opposed to using dlopen()) then
initializing libjpeg can't fail: either it's present and works, or
loading SDL_image would have failed in the runtime linker. As a result,
there's no point in having the code path that can fall back to tiny_jpeg
in this configuration.

Signed-off-by: Simon McVittie <>
 IMG_jpg.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/IMG_jpg.c b/IMG_jpg.c
index c93518e..3c5cba6 100644
--- a/IMG_jpg.c
+++ b/IMG_jpg.c
@@ -679,7 +679,8 @@ SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src)
 #endif /* LOAD_JPG */
+/* Use tinyjpeg as a fallback if we don't have a hard dependency on libjpeg */
+#if SAVE_JPG && (defined(LOAD_JPG_DYNAMIC) || !defined(WANT_JPEGLIB))
 #ifdef __WATCOMC__ /* watcom has issues.. */
 #define ceilf ceil
@@ -759,7 +760,7 @@ static int IMG_SaveJPG_RW_tinyjpeg(SDL_Surface *surface, SDL_RWops *dst, int fre
     return result;
-#endif /* SAVE_JPG */
+#endif /* SAVE_JPG && (defined(LOAD_JPG_DYNAMIC) || !defined(WANT_JPEGLIB)) */
 int IMG_SaveJPG(SDL_Surface *surface, const char *file, int quality)
@@ -781,7 +782,12 @@ int IMG_SaveJPG_RW(SDL_Surface *surface, SDL_RWops *dst, int freedst, int qualit
+#if defined(LOAD_JPG_DYNAMIC) || !defined(WANT_JPEGLIB)
     return IMG_SaveJPG_RW_tinyjpeg(surface, dst, freedst, quality);
+    return -1;
     return IMG_SetError("SDL_image built without JPEG save support");