Skip to content

Commit 19f638e

Browse files
committed
FIX: Loaded/saved image data have reversed RGB colour order on macOS
resolves: Oldes/Rebol-issues#2495
1 parent dd3f3e0 commit 19f638e

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

src/os/osx/sys-codecs.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ int DecodeImageFromFile(const char *uri, unsigned int frame, REBCDI *codi)
8080
pixels = (UInt32*)malloc(w * h * 4); // Rebol's library side must free it!
8181
ASSERT_NOT_NULL(pixels, 4, "allocate pixels buffer");
8282
space = CGColorSpaceCreateDeviceRGB();
83-
ctx = CGBitmapContextCreate(pixels, w, h, bitsPerComponent, bytesPerPixel * w, space, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
83+
ctx = CGBitmapContextCreate(pixels, w, h, bitsPerComponent, bytesPerPixel * w, space, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);
8484
ASSERT_NOT_NULL(ctx, 5, "create a bitmap context");
8585
CGContextDrawImage(ctx, CGRectMake(0, 0, w, h), img);
8686
CGColorSpaceRelease(space);
@@ -126,7 +126,7 @@ int EncodeImageToFile(const char *uri, REBCDI *codi)
126126
data = CGDataProviderCreateWithData(NULL, codi->bits, codi->w * codi->h * 4, NULL);
127127
ASSERT_NOT_NULL(data, 1, "prepare input data");
128128
colorSpace = CGColorSpaceCreateDeviceRGB();
129-
img = CGImageCreate(codi->w, codi->h, 8, 32, codi->w * 4, colorSpace, (kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big), data, NULL, TRUE, 0);
129+
img = CGImageCreate(codi->w, codi->h, 8, 32, codi->w * 4, colorSpace, (kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little), data, NULL, TRUE, 0);
130130
CGDataProviderRelease(data);
131131
CGColorSpaceRelease(colorSpace);
132132
ASSERT_NOT_NULL(img, 2, "create an image");

src/tests/units/codecs-test.r3

+30
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,21 @@ if find codecs 'PNG [
447447
3015 = size? %units/files/png-from-photoshop.png
448448
]
449449
try [delete %new.png]
450+
451+
--test-- "png rgb image"
452+
;@@ https://github.com/Oldes/Rebol-issues/issues/2495
453+
--assert all [
454+
image? img: try [load %units/files/rbgw.png]
455+
img/1 = 200.0.0.255
456+
img/2 = 0.0.200.255
457+
img/3 = 0.200.0.255
458+
img/rgb = #{C800000000C800C800FFFFFF}
459+
]
460+
--assert all [
461+
image? img: try [load save %new.png make image! [2x2 #{C800000000C800C800FFFFFF}]]
462+
img/rgb = #{C800000000C800C800FFFFFF}
463+
]
464+
try [delete %new.png]
450465
===end-group===
451466
]
452467

@@ -484,6 +499,21 @@ if find codecs 'GIF [
484499
--test-- "gif/size?"
485500
--assert 256x256 = codecs/gif/size? %units/files/flower.gif
486501
--assert none? codecs/gif/size? %units/files/test.aar
502+
503+
--test-- "gif rgb image"
504+
;@@ https://github.com/Oldes/Rebol-issues/issues/2495
505+
--assert all [
506+
image? img: try [load %units/files/rbgw.gif]
507+
img/1 = 200.0.0.255
508+
img/2 = 0.0.200.255
509+
img/3 = 0.200.0.255
510+
img/rgb = #{C800000000C800C800FFFFFF}
511+
]
512+
--assert all [
513+
image? img: try [load save %new.gif make image! [2x2 #{C800000000C800C800FFFFFF}]]
514+
img/rgb = #{C800000000C800C800FFFFFF}
515+
]
516+
try [delete %new.gif]
487517
===end-group===
488518
]
489519

src/tests/units/files/rbgw.gif

62 Bytes
Loading

src/tests/units/files/rbgw.png

1.82 KB
Loading

0 commit comments

Comments
 (0)