Fixed colorspace for ImageIO loading
authorSam Lantinga <slouken@libsdl.org>
Tue, 03 Jan 2012 20:04:02 -0500
changeset 2925953114c0d27
parent 291 0a42199fd73f
child 293 b51a5e14a456
Fixed colorspace for ImageIO loading

albert

Test case: https://gist.github.com/1551241

It should output:

Code:
$ ./sdlimagetest
PixelFormat: BitsPerPixel: 32, BytesPerPixel: 4
R/G/B/A mask: ff0000/ff00/ff/ff000000
R/G/B/A loss: 0/0/0/0
Colorkey: 0, Alpha: 255
FF0000FF
FF0000FF
FF0000FF
FF0000FF
FF0000FF
FF0000FF
FF0000FF
FF0000FF
FF0000FF
FF0000FF


Output with bad SDL_image:

Code:

$ ./sdlimagetest
PixelFormat:
BitsPerPixel: 32, BytesPerPixel: 4
R/G/B/A mask: ff0000/ff00/ff/ff000000
R/G/B/A loss: 0/0/0/0
Colorkey: 0, Alpha: 255
FF1514F4
FF1514F4
FF1514F4
FF1514F4
FF1514F4
FF1514F4
FF1514F4
FF1514F4
FF1514F4
FF1514F4


And I searched a bit around and I found a fix in the Allegro code (http://codesearch.google.com/#w3aXj_apqFs/allegro/tags/5.0.0/addons/image/macosx.m&ct=rc&cd=3&q=CGColorSpaceCreateCalibratedRGB).

So I wrote a patch for SDL_image: https://gist.github.com/1551404
IMG_ImageIO.m
     1.1 --- a/IMG_ImageIO.m	Tue Jan 03 01:38:26 2012 -0500
     1.2 +++ b/IMG_ImageIO.m	Tue Jan 03 20:04:02 2012 -0500
     1.3 @@ -212,8 +212,25 @@
     1.4      
     1.5      CGContextRef bitmap_context;
     1.6      CGBitmapInfo bitmap_info;
     1.7 -    CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB();
     1.8 -    
     1.9 +
    1.10 +	/* This sets up a color space that results in identical values
    1.11 +	 * as the image data itself, which is the same as the standalone
    1.12 +	 * libpng loader.
    1.13 +	 * Thanks to Allegro. :)
    1.14 +	 */
    1.15 +	CGFloat whitePoint[3] = { 1, 1, 1 };
    1.16 +	CGFloat blackPoint[3] = { 0, 0, 0 };
    1.17 +	CGFloat gamma[3] = { 2.2, 2.2, 2.2 };
    1.18 +	CGFloat matrix[9] = {
    1.19 +		1, 1, 1,
    1.20 +		1, 1, 1,
    1.21 +		1, 1, 1
    1.22 +	};
    1.23 +	CGColorSpaceRef color_space =
    1.24 +		CGColorSpaceCreateCalibratedRGB(
    1.25 +									whitePoint, blackPoint, gamma, matrix
    1.26 +									);   
    1.27 +	
    1.28      if (alpha == kCGImageAlphaNone ||
    1.29          alpha == kCGImageAlphaNoneSkipFirst ||
    1.30          alpha == kCGImageAlphaNoneSkipLast) {