Skip to content

Commit db38189

Browse files
committed
FIX: invalid data in loaded images with an alpha channel on macOS
resolves: Oldes/Rebol-issues#2534
1 parent ff5e131 commit db38189

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/os/osx/sys-codecs.m

+5-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ int DecodeImageFromFile(const char *uri, unsigned int frame, REBCDI *codi)
5757
CGContextRef ctx;
5858
CFDataRef binSrc;
5959

60-
NSUInteger w, h;
60+
NSUInteger w, h, bytes;
6161
NSUInteger bytesPerPixel = 4;
6262
NSUInteger bitsPerComponent = 8;
6363

@@ -77,8 +77,10 @@ int DecodeImageFromFile(const char *uri, unsigned int frame, REBCDI *codi)
7777
ASSERT_NOT_NULL(img, 3, "create an image");
7878
w = CGImageGetWidth(img);
7979
h = CGImageGetHeight(img);
80-
pixels = (UInt32*)malloc(w * h * 4); // Rebol's library side must free it!
80+
bytes = w * h * bytesPerPixel;
81+
pixels = (UInt32*)malloc(bytes); // Rebol's library side must free it!
8182
ASSERT_NOT_NULL(pixels, 4, "allocate pixels buffer");
83+
memset(pixels, 0, bytes);
8284
space = CGColorSpaceCreateDeviceRGB();
8385
ctx = CGBitmapContextCreate(pixels, w, h, bitsPerComponent, bytesPerPixel * w, space, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);
8486
ASSERT_NOT_NULL(ctx, 5, "create a bitmap context");
@@ -88,7 +90,7 @@ int DecodeImageFromFile(const char *uri, unsigned int frame, REBCDI *codi)
8890

8991
codi->w = (UInt32)w;
9092
codi->h = (UInt32)h;
91-
codi->len = w * h * 4;
93+
codi->len = (UInt32)bytes;
9294
codi->data = (unsigned char*)pixels;
9395
} while(FALSE);
9496
SAFE_CF_RELEASE(url);

src/tests/units/image-test.r3

+10-3
Original file line numberDiff line numberDiff line change
@@ -429,19 +429,26 @@ if value? 'blur [
429429

430430

431431
===start-group=== "Save/load image"
432+
;@@ https://github.com/Oldes/Rebol-issues/issues/2534
432433
if find codecs 'png [
433434
--test-- "save/load PNG"
434-
img1: make image! [2x2 255.0.0.10]
435+
img1: make image! [2x2 255.0.0.210]
435436
save %units/files/test.png img1
436437
img2: load %units/files/test.png
437-
--assert #{FF00000AFF00000AFF00000AFF00000A} = to binary! img2
438+
--assert find [
439+
#{FF00000AFF00000AFF00000AFF00000A}
440+
#{D20000D2D20000D2D20000D2D20000D2} ;; premultiplied on macOS :/
441+
] to binary! img2
438442
]
439443
if find codecs 'bmp [
440444
--test-- "save/load BMP"
441445
img1: make image! [2x2 255.0.0.10]
442446
save %units/files/test.bmp img1
443447
img2: load %units/files/test.bmp
444-
--assert #{FF00000AFF00000AFF00000AFF00000A} = to binary! img2
448+
--assert find [
449+
#{FF00000AFF00000AFF00000AFF00000A}
450+
#{D20000D2D20000D2D20000D2D20000D2} ;; premultiplied on macOS :/
451+
] to binary! img2
445452
]
446453
===end-group===
447454

0 commit comments

Comments
 (0)