Fixed bug #868
authorSam Lantinga <slouken@libsdl.org>
Tue, 10 Nov 2009 08:26:52 +0000
changeset 2402fdb3e90ede0
parent 239 950b63dc92d6
child 241 14718af046dc
Fixed bug #868

Premultiplied alpha is not entirely lossy, as it turns out.

Also fixed building with SDL_IMAGE_USE_COMMON_BACKEND
IMG_ImageIO.c
IMG_UIImage.m
     1.1 --- a/IMG_ImageIO.c	Tue Nov 10 07:39:07 2009 +0000
     1.2 +++ b/IMG_ImageIO.c	Tue Nov 10 08:26:52 2009 +0000
     1.3 @@ -6,6 +6,9 @@
     1.4   *  Copyright 2009 __MyCompanyName__. All rights reserved.
     1.5   *
     1.6   */
     1.7 +
     1.8 +#if defined(__APPLE__) && !defined(SDL_IMAGE_USE_COMMON_BACKEND)
     1.9 +
    1.10  #include "SDL_image.h"
    1.11  
    1.12  // For ImageIO framework and also LaunchServices framework (for UTIs)
    1.13 @@ -282,7 +285,26 @@
    1.14  
    1.15  		// FIXME: Reverse the premultiplied alpha
    1.16  		if ((bitmap_info & kCGBitmapAlphaInfoMask) == kCGImageAlphaPremultipliedFirst) {
    1.17 -			// Errr, alpha premultiplication is lossy...
    1.18 +			int i, j;
    1.19 +			Uint8 *p = (Uint8 *)surface->pixels;
    1.20 +			for (i = surface->h * surface->pitch/4; i--; ) {
    1.21 +#if __LITTLE_ENDIAN__
    1.22 +				Uint8 A = p[3];
    1.23 +				if (A) {
    1.24 +					for (j = 0; j < 3; ++j) {
    1.25 +						p[j] = (p[j] * 255) / A;
    1.26 +					}
    1.27 +				}
    1.28 +#else
    1.29 +				Uint8 A = p[0];
    1.30 +				if (A) {
    1.31 +					for (j = 1; i < 4; ++j) {
    1.32 +						p[j] = (p[j] * 255) / A;
    1.33 +					}
    1.34 +				}
    1.35 +#endif /* ENDIAN */
    1.36 +				p += 4;
    1.37 +			}
    1.38  		}
    1.39  	}
    1.40  
    1.41 @@ -487,3 +509,4 @@
    1.42  	return sdl_surface;
    1.43  }
    1.44  
    1.45 +#endif /* defined(__APPLE__) && !defined(SDL_IMAGE_USE_COMMON_BACKEND) */
     2.1 --- a/IMG_UIImage.m	Tue Nov 10 07:39:07 2009 +0000
     2.2 +++ b/IMG_UIImage.m	Tue Nov 10 08:26:52 2009 +0000
     2.3 @@ -77,7 +77,26 @@
     2.4  
     2.5  		// FIXME: Reverse the premultiplied alpha
     2.6  		if ((bitmap_info & kCGBitmapAlphaInfoMask) == kCGImageAlphaPremultipliedFirst) {
     2.7 -			// Errr, alpha premultiplication is lossy...
     2.8 +			int i, j;
     2.9 +			Uint8 *p = (Uint8 *)surface->pixels;
    2.10 +			for (i = surface->h * surface->pitch/4; i--; ) {
    2.11 +#if __LITTLE_ENDIAN__
    2.12 +				Uint8 A = p[3];
    2.13 +				if (A) {
    2.14 +					for (j = 0; j < 3; ++j) {
    2.15 +						p[j] = (p[j] * 255) / A;
    2.16 +					}
    2.17 +				}
    2.18 +#else
    2.19 +				Uint8 A = p[0];
    2.20 +				if (A) {
    2.21 +					for (j = 1; i < 4; ++j) {
    2.22 +						p[j] = (p[j] * 255) / A;
    2.23 +					}
    2.24 +				}
    2.25 +#endif /* ENDIAN */
    2.26 +				p += 4;
    2.27 +			}
    2.28  		}
    2.29  	}
    2.30