changeset 757:dd59a650a318

Fix 24-bit surface conversions in image loader.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 08 May 2013 02:02:01 +0300
parents 4dda14ff8e1a
children 49e96e581ac8
files dmimage.c
diffstat 1 files changed, 50 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/dmimage.c	Sun May 05 06:38:34 2013 +0300
+++ b/dmimage.c	Wed May 08 02:02:01 2013 +0300
@@ -15,13 +15,33 @@
 
 SDL_Surface *dmCreateRGBSurfaceFrom(void *data, const int width, const int height, const int depth, const int pitch, const int rmask, const int gmask, const int bmask, const int amask)
 {
-    SDL_Surface *tmp, *pixtmp, *result = NULL;
-    
-    tmp = SDL_CreateRGBSurfaceFrom(data, width, height, depth, pitch, bmask, gmask, rmask, amask);
+    // Create source surface from given data
+    SDL_Surface *tmp = SDL_CreateRGBSurfaceFrom(data, width, height, depth, pitch, bmask, gmask, rmask, amask);
 
-    pixtmp = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCALPHA, 10, 10, 32, rmask, gmask, bmask, amask);
+    // Result surface component orders as masks
+#if SDL_BYTEORDER == SDL_BIG_ENDIAN
+    Uint32
+        Xrmask = 0xff000000,
+        Xgmask = 0x00ff0000,
+        Xbmask = 0x0000ff00,
+        Xamask = 0x000000ff;
+#else
+    Uint32
+        Xrmask = 0x000000ff,
+        Xgmask = 0x0000ff00,
+        Xbmask = 0x00ff0000,
+        Xamask = 0xff000000;
+#endif
+
+    // Create result conversion surface
+    SDL_Surface
+        *result = NULL,
+        *pixtmp = SDL_CreateRGBSurface(
+        SDL_SWSURFACE | SDL_SRCALPHA, 16, 16, 32, Xrmask, Xgmask, Xbmask, Xamask);
+
     if (tmp != NULL && pixtmp != NULL)
     {
+        // Convert surface
         result = SDL_ConvertSurface(tmp, pixtmp->format, SDL_SWSURFACE | SDL_SRCALPHA);
         SDL_FreeSurface(tmp);
     }
@@ -97,22 +117,37 @@
         return NULL;
     }
 
-#if SDL_BYTEORDER == SDL_BIG_ENDIAN
-    rmask = 0xff000000;
-    gmask = 0x00ff0000;
-    bmask = 0x0000ff00;
-    amask = 0x000000ff;
-#else
-    rmask = 0x000000ff;
-    gmask = 0x0000ff00;
-    bmask = 0x00ff0000;
-    amask = 0xff000000;
-#endif
 
     switch (comp)
     {
         case 4:
+#if SDL_BYTEORDER == SDL_BIG_ENDIAN
+            rmask = 0xff000000;
+            gmask = 0x00ff0000;
+            bmask = 0x0000ff00;
+            amask = 0x000000ff;
+#else
+            rmask = 0x000000ff;
+            gmask = 0x0000ff00;
+            bmask = 0x00ff0000;
+            amask = 0xff000000;
+#endif
+            result = dmCreateRGBSurfaceFrom(data, width, height, comp * 8,
+                width * comp, rmask, gmask, bmask, amask);
+            break;
+
         case 3:
+#if SDL_BYTEORDER == SDL_BIG_ENDIAN
+            rmask = 0x00ff0000;
+            gmask = 0x0000ff00;
+            bmask = 0x000000ff;
+            amask = 0x00000000;
+#else
+            rmask = 0x000000ff;
+            gmask = 0x0000ff00;
+            bmask = 0x00ff0000;
+            amask = 0x00000000;
+#endif
             result = dmCreateRGBSurfaceFrom(data, width, height, comp * 8,
                 width * comp, rmask, gmask, bmask, amask);
             break;