2016-06-25 15:27:29 -05:00
|
|
|
// 25 june 2016
|
|
|
|
#import "uipriv_darwin.h"
|
|
|
|
|
|
|
|
struct uiImage {
|
|
|
|
NSImage *i;
|
|
|
|
NSSize size;
|
2016-06-27 15:34:57 -05:00
|
|
|
NSMutableArray *swizzled;
|
2016-06-25 15:27:29 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
uiImage *uiNewImage(double width, double height)
|
|
|
|
{
|
|
|
|
uiImage *i;
|
|
|
|
|
2018-04-15 15:05:24 -05:00
|
|
|
i = uiprivNew(uiImage);
|
2016-06-25 15:27:29 -05:00
|
|
|
i->size = NSMakeSize(width, height);
|
|
|
|
i->i = [[NSImage alloc] initWithSize:i->size];
|
2016-06-27 15:34:57 -05:00
|
|
|
i->swizzled = [NSMutableArray new];
|
2016-06-25 15:27:29 -05:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2018-04-15 15:05:24 -05:00
|
|
|
void Image(uiImage *i)
|
2016-06-25 15:27:29 -05:00
|
|
|
{
|
2016-06-27 15:34:57 -05:00
|
|
|
NSValue *v;
|
|
|
|
|
2016-06-25 15:27:29 -05:00
|
|
|
[i->i release];
|
2016-06-27 15:34:57 -05:00
|
|
|
// to be safe, do this after releasing the image
|
|
|
|
for (v in i->swizzled)
|
2018-04-15 15:05:24 -05:00
|
|
|
uiprivFree([v pointerValue]);
|
2016-06-27 15:34:57 -05:00
|
|
|
[i->swizzled release];
|
2018-04-15 15:05:24 -05:00
|
|
|
uiprivFree(i);
|
2016-06-25 15:27:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void uiImageAppend(uiImage *i, void *pixels, int pixelWidth, int pixelHeight, int pixelStride)
|
|
|
|
{
|
|
|
|
NSBitmapImageRep *repCalibrated, *repsRGB;
|
2016-06-27 15:34:57 -05:00
|
|
|
uint8_t *swizzled, *bp, *sp;
|
|
|
|
int x, y;
|
2016-06-25 16:26:50 -05:00
|
|
|
unsigned char *pix[1];
|
2016-06-25 15:27:29 -05:00
|
|
|
|
2016-06-27 15:34:57 -05:00
|
|
|
// OS X demands that R and B are in the opposite order from what we expect
|
|
|
|
// we must swizzle :(
|
|
|
|
// LONGTERM test on a big-endian system
|
2018-04-15 15:05:24 -05:00
|
|
|
swizzled = (uint8_t *) uiprivAlloc((pixelStride * pixelHeight * 4) * sizeof (uint8_t), "uint8_t[]");
|
2016-06-27 15:34:57 -05:00
|
|
|
bp = (uint8_t *) pixels;
|
|
|
|
sp = swizzled;
|
|
|
|
for (y = 0; y < pixelHeight * pixelStride; y += pixelStride)
|
|
|
|
for (x = 0; x < pixelStride; x++) {
|
|
|
|
sp[0] = bp[2];
|
|
|
|
sp[1] = bp[1];
|
|
|
|
sp[2] = bp[0];
|
|
|
|
sp[3] = bp[3];
|
|
|
|
sp += 4;
|
|
|
|
bp += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
pix[0] = (unsigned char *) swizzled;
|
2016-06-25 15:27:29 -05:00
|
|
|
repCalibrated = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:pix
|
|
|
|
pixelsWide:pixelWidth
|
|
|
|
pixelsHigh:pixelHeight
|
|
|
|
bitsPerSample:8
|
|
|
|
samplesPerPixel:4
|
|
|
|
hasAlpha:YES
|
|
|
|
isPlanar:NO
|
|
|
|
colorSpaceName:NSCalibratedRGBColorSpace
|
|
|
|
bitmapFormat:0
|
|
|
|
bytesPerRow:pixelStride
|
|
|
|
bitsPerPixel:32];
|
|
|
|
repsRGB = [repCalibrated bitmapImageRepByRetaggingWithColorSpace:[NSColorSpace sRGBColorSpace]];
|
|
|
|
[repCalibrated release];
|
|
|
|
|
|
|
|
[i->i addRepresentation:repsRGB];
|
|
|
|
[repsRGB setSize:i->size];
|
|
|
|
[repsRGB release];
|
2016-06-27 15:34:57 -05:00
|
|
|
|
|
|
|
// we need to keep swizzled alive for NSBitmapImageRep
|
|
|
|
[i->swizzled addObject:[NSValue valueWithPointer:swizzled]];
|
2016-06-25 15:27:29 -05:00
|
|
|
}
|
2016-06-25 18:18:25 -05:00
|
|
|
|
2018-05-05 20:28:13 -05:00
|
|
|
NSImage *uiprivImageNSImage(uiImage *i)
|
2016-06-25 18:18:25 -05:00
|
|
|
{
|
|
|
|
return i->i;
|
|
|
|
}
|