Fixed bug 2788 - color space fix for ImageIO backend (OS X, iOS)
authorSam Lantinga
Sat, 29 Nov 2014 12:06:05 -0800
changeset 46655801d955e3a
parent 465 e8573815cc1f
child 467 2b0ada991468
Fixed bug 2788 - color space fix for ImageIO backend (OS X, iOS)

Jeffrey Carpenter

Test case resources (should be able to be replaced with your own as long as the pixel format and file encoding matches):
test.bmp RGB8888 (24-bit, BMP)
test.png ARGB8888 (32-bit, PNG)

This patch reverts [revision 292](https://hg.libsdl.org/SDL_image/log?rev=292) back to the original author's implementation (Eric Wing). The comments made in this revision seem to contradict what is intended; see CGColorSpaceCreateCalibratedRGB [1] and CGColorSpaceCreateDeviceRGB [2] for Apple's API notes. Also note that the revision comments by "Albert" use a test case that is for use with SDL1 -- how this may change things is unknown to me.

1. https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGColorSpace/index.html#//apple_ref/c/func/CGColorSpaceCreateCalibratedRGB
2. https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGColorSpace/index.html#//apple_ref/c/func/CGColorSpaceCreateDeviceRGB

Additional notes
* Tested on Mac OS X 10.10 "Yosemite".
* Has not been tested against iOS, SDL1 or images that have been processed with pre-multiplied alpha.

Build test case:
clang++ sdl2_image-colorspace-fix.cpp -o sdl2_image-colorspace-fix -framework SDL2 -framework SDL2_image

Run test case to generate my existing data (my baseline / reference):
(output in diag_main.txt, using test.bmp and test.png)

bitmap(0, 0) = ff6e8fbd
png(0, 0) = ff6e8fbd
sdlbmp(0, 0) = 6e90be

bitmap(31, 0) = ff6e8fbd
png(31, 0) = ff6e8fbd
sdlbmp(31, 0) = 6e90be

<snip remaining debug info>

NOTE: ff6e8fbd = R(110) G(143) B(189)
The actual pixel value is 110, 144, 190. SDL_LoadBMP gets it right in both
cases, whereas IMG_Load (ImageIO backend) gets it wrong on both counts.

Apply revert-rev-292.patch:
hg import revert-rev-292.patch

Rebuild test case, ensuring that we compile and **run** with the proper runtime
search paths for our patched SDL2_image library (I built mine using Xcode project files):

```sh
# man dyld (1)
DYLD_FRAMEWORK_PATH=<my_local_prefix_lib_path> ./sdl2_image-colorspace-fix
```

Comparison output of reference data with patched library:
(output in diag_main.txt, using test.bmp and test.png)

bitmap(0, 0) = ff6e90be
png(0, 0) = ff6e90be
sdlbmp(0, 0) = 6e90be

bitmap(31, 0) = ff6e90be
png(31, 0) = ff6e90be
sdlbmp(31, 0) = 6e90be

<snip remaining debug info>

NOTE: ff6e90be = R(110) G(144) B(190)
The actual pixel values match using both SDL_LoadBMP and IMG_Load. This is the
expected behavior.

References
1. [Introduction of ImageIO backend](https://forums.libsdl.org/viewtopic.php?p=14777)
2. [Pixel bug in Mac OS X](https://forums.libsdl.org/viewtopic.php?p=30317)
3. [Perhaps again? Pixel bug in Mac OS X](https://forums.libsdl.org/viewtopic.php?t=10013&sid=34e8ef7cf74074039495037c1a077025)
IMG_ImageIO.m
     1.1 --- a/IMG_ImageIO.m	Thu Aug 07 09:03:28 2014 -0400
     1.2 +++ b/IMG_ImageIO.m	Sat Nov 29 12:06:05 2014 -0800
     1.3 @@ -211,24 +211,7 @@
     1.4  
     1.5      CGContextRef bitmap_context;
     1.6      CGBitmapInfo bitmap_info;
     1.7 -
     1.8 -    /* This sets up a color space that results in identical values
     1.9 -     * as the image data itself, which is the same as the standalone
    1.10 -     * libpng loader.
    1.11 -     * Thanks to Allegro. :)
    1.12 -     */
    1.13 -    CGFloat whitePoint[3] = { 0.950, 1.000, 1.089 };
    1.14 -    CGFloat blackPoint[3] = { 0.000, 0.000, 0.000 };
    1.15 -    CGFloat gamma[3] = { 2.2, 2.2, 2.2 };
    1.16 -    CGFloat matrix[9] = {
    1.17 -        0.412, 0.213, 0.019,
    1.18 -        0.358, 0.715, 0.119,
    1.19 -        0.180, 0.072, 0.950
    1.20 -    };
    1.21 -    CGColorSpaceRef color_space =
    1.22 -        CGColorSpaceCreateCalibratedRGB(
    1.23 -                                    whitePoint, blackPoint, gamma, matrix
    1.24 -                                    );
    1.25 +    CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB();
    1.26  
    1.27      if (alpha == kCGImageAlphaNone ||
    1.28          alpha == kCGImageAlphaNoneSkipFirst ||