forked from andlabs/ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitmap_darwin.m
61 lines (47 loc) · 1.57 KB
/
bitmap_darwin.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#import <Foundation/Foundation.h>
typedef struct uiDrawContext {
CGContextRef c;
CGFloat height;
} uiDrawContext;
typedef struct {
CGContextRef ctx;
CGImageRef img;
} uiBitmap;
uiBitmap *uiNewBitmap(uiDrawContext *ctx, int width, int height, int stride,
const void *rgba) {
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
if (space == NULL) {
return NULL;
}
CGDataProviderRef data =
CGDataProviderCreateWithData(NULL, rgba, stride * height, NULL);
const int kBitsPerComponent = 8;
const int kNumChannels = 4;
CGBitmapInfo info =
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little;
CGImageRef img = CGImageCreate(
width, height, kBitsPerComponent, kBitsPerComponent * kNumChannels,
stride, space, info, data, NULL, NO, kCGRenderingIntentDefault);
CGDataProviderRelease(data);
CGColorSpaceRelease(space);
CGContextRetain(ctx->c);
uiBitmap *bmp = malloc(sizeof(uiBitmap));
bmp->ctx = ctx->c;
bmp->img = img;
return bmp;
}
void uiFreeBitmap(uiBitmap *bmp) {
CGImageRelease(bmp->img);
CGContextRelease(bmp->ctx);
free(bmp);
}
void uiDrawBitmap(uiBitmap *bmp, double x, double y, double height,
double width) {
width = width > 0 ? width : CGImageGetWidth(bmp->img);
height = height > 0 ? height : CGImageGetHeight(bmp->img);
CGContextSaveGState(bmp->ctx);
CGContextTranslateCTM(bmp->ctx, 0, height + 2 * y);
CGContextScaleCTM(bmp->ctx, 1, -1);
CGContextDrawImage(bmp->ctx, CGRectMake(x, y, width, height), bmp->img);
CGContextRestoreGState(bmp->ctx);
}