SDL_image: More consistent RGB PNG saving with LIBPNG

From f2e8453fc7ce80d9bed4b69b2df1ad569032f9d8 Mon Sep 17 00:00:00 2001
From: Starbuck5 <[EMAIL REDACTED]>
Date: Sun, 19 Jun 2022 00:48:11 -0700
Subject: [PATCH] More consistent RGB PNG saving with LIBPNG

---
 IMG_png.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/IMG_png.c b/IMG_png.c
index 6ab0aa77..e9db1fbc 100644
--- a/IMG_png.c
+++ b/IMG_png.c
@@ -629,9 +629,18 @@ static int IMG_SavePNG_RW_libpng(SDL_Surface *surface, SDL_RWops *dst, int freed
             }
         }
         else if (surface->format->format == SDL_PIXELFORMAT_RGB24) {
+            /* If the surface is exactly the right RGB format it is just passed through */
             png_color_type = PNG_COLOR_TYPE_RGB;
         }
+        else if (!SDL_ISPIXELFORMAT_ALPHA(surface->format->format)) {
+            /* If the surface is not exactly the right RGB format but does not have alpha
+               information, it should be converted to RGB24 before being passed through */
+            png_color_type = PNG_COLOR_TYPE_RGB;
+            source = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_RGB24, 0);
+        }
         else if (surface->format->format != png_format) {
+            /* Otherwise, (surface has alpha data), and it is not in the exact right
+               format , so it should be converted to that */
             source = SDL_ConvertSurfaceFormat(surface, png_format, 0);
         }