From 97e7217e06e8424304e668e4bfd2563d89c4a3d6 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Tue, 3 Jan 2012 20:04:02 -0500 Subject: [PATCH] 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 | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/IMG_ImageIO.m b/IMG_ImageIO.m index ac615a67..d2f002c8 100644 --- a/IMG_ImageIO.m +++ b/IMG_ImageIO.m @@ -212,8 +212,25 @@ static CFDictionaryRef CreateHintDictionary(CFStringRef uti_string_hint) CGContextRef bitmap_context; CGBitmapInfo bitmap_info; - CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB(); - + + /* This sets up a color space that results in identical values + * as the image data itself, which is the same as the standalone + * libpng loader. + * Thanks to Allegro. :) + */ + CGFloat whitePoint[3] = { 1, 1, 1 }; + CGFloat blackPoint[3] = { 0, 0, 0 }; + CGFloat gamma[3] = { 2.2, 2.2, 2.2 }; + CGFloat matrix[9] = { + 1, 1, 1, + 1, 1, 1, + 1, 1, 1 + }; + CGColorSpaceRef color_space = + CGColorSpaceCreateCalibratedRGB( + whitePoint, blackPoint, gamma, matrix + ); + if (alpha == kCGImageAlphaNone || alpha == kCGImageAlphaNoneSkipFirst || alpha == kCGImageAlphaNoneSkipLast) {